Include tidying.
[dcpomatic.git] / src / wx / kdm_dialog.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "kdm_dialog.h"
21 #include "cinema_dialog.h"
22 #include "screen_dialog.h"
23 #include "wx_util.h"
24 #include "lib/cinema.h"
25 #include "lib/config.h"
26 #include "lib/film.h"
27 #include "lib/screen.h"
28 #include <libcxml/cxml.h>
29 #ifdef DCPOMATIC_USE_OWN_DIR_PICKER
30 #include "dir_picker_ctrl.h"
31 #else
32 #include <wx/filepicker.h>
33 #endif
34 #include <wx/treectrl.h>
35 #include <wx/datectrl.h>
36 #include <wx/timectrl.h>
37 #include <wx/stdpaths.h>
38 #include <wx/listctrl.h>
39
40 using std::string;
41 using std::map;
42 using std::list;
43 using std::pair;
44 using std::cout;
45 using std::vector;
46 using std::make_pair;
47 using boost::shared_ptr;
48
49 KDMDialog::KDMDialog (wxWindow* parent, boost::shared_ptr<const Film> film)
50         : wxDialog (parent, wxID_ANY, _("Make KDMs"))
51 {
52         /* Main sizer */
53         wxBoxSizer* vertical = new wxBoxSizer (wxVERTICAL);
54
55         /* Font for sub-headings */
56         wxFont subheading_font (*wxNORMAL_FONT);
57         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
58
59
60         /* Sub-heading: Screens */
61         wxStaticText* h = new wxStaticText (this, wxID_ANY, _("Screens"));
62         h->SetFont (subheading_font);
63         vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL);
64
65         wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
66         _targets = new wxTreeCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_MULTIPLE | wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT);
67         targets->Add (_targets, 1, wxEXPAND | wxTOP | wxRIGHT, DCPOMATIC_SIZER_GAP);
68
69         _root = _targets->AddRoot ("Foo");
70
71         list<shared_ptr<Cinema> > c = Config::instance()->cinemas ();
72         for (list<shared_ptr<Cinema> >::iterator i = c.begin(); i != c.end(); ++i) {
73                 add_cinema (*i);
74         }
75
76         _targets->ExpandAll ();
77
78         wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
79
80         _add_cinema = new wxButton (this, wxID_ANY, _("Add Cinema..."));
81         target_buttons->Add (_add_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
82         _edit_cinema = new wxButton (this, wxID_ANY, _("Edit Cinema..."));
83         target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
84         _remove_cinema = new wxButton (this, wxID_ANY, _("Remove Cinema"));
85         target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
86
87         _add_screen = new wxButton (this, wxID_ANY, _("Add Screen..."));
88         target_buttons->Add (_add_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
89         _edit_screen = new wxButton (this, wxID_ANY, _("Edit Screen..."));
90         target_buttons->Add (_edit_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
91         _remove_screen = new wxButton (this, wxID_ANY, _("Remove Screen"));
92         target_buttons->Add (_remove_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
93
94         targets->Add (target_buttons, 0, 0);
95
96         vertical->Add (targets, 1, wxEXPAND);
97
98
99         /* Sub-heading: Timing */
100         h = new wxStaticText (this, wxID_ANY, S_("KDM|Timing"));
101         h->SetFont (subheading_font);
102         vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
103
104         wxFlexGridSizer* table = new wxFlexGridSizer (3, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
105         add_label_to_sizer (table, this, _("From"), true);
106         wxDateTime from;
107         from.SetToCurrent ();
108         _from_date = new wxDatePickerCtrl (this, wxID_ANY, from);
109         table->Add (_from_date, 1, wxEXPAND);
110         _from_time = new wxTimePickerCtrl (this, wxID_ANY, from);
111         table->Add (_from_time, 1, wxEXPAND);
112
113         add_label_to_sizer (table, this, _("Until"), true);
114         wxDateTime to = from;
115         /* 1 week from now */
116         to.Add (wxDateSpan (0, 0, 1, 0));
117         _until_date = new wxDatePickerCtrl (this, wxID_ANY, to);
118         table->Add (_until_date, 1, wxEXPAND);
119         _until_time = new wxTimePickerCtrl (this, wxID_ANY, to);
120         table->Add (_until_time, 1, wxEXPAND);
121
122         vertical->Add (table, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP);
123
124
125         /* Sub-heading: CPL */
126         h = new wxStaticText (this, wxID_ANY, _("CPL"));
127         h->SetFont (subheading_font);
128         vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
129
130         /* CPL choice */
131         wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
132         add_label_to_sizer (s, this, _("CPL"), true);
133         _cpl = new wxChoice (this, wxID_ANY);
134         s->Add (_cpl, 1, wxEXPAND);
135         _cpl_browse = new wxButton (this, wxID_ANY, _("Browse..."));
136         s->Add (_cpl_browse, 0);
137         vertical->Add (s, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP + 2);
138
139         /* CPL details */
140         table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
141         add_label_to_sizer (table, this, _("DCP directory"), true);
142         _dcp_directory = new wxStaticText (this, wxID_ANY, "");
143         table->Add (_dcp_directory);
144         add_label_to_sizer (table, this, _("CPL ID"), true);
145         _cpl_id = new wxStaticText (this, wxID_ANY, "");
146         table->Add (_cpl_id);
147         add_label_to_sizer (table, this, _("CPL annotation text"), true);
148         _cpl_annotation_text = new wxStaticText (this, wxID_ANY, "");
149         table->Add (_cpl_annotation_text);
150         vertical->Add (table, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP + 2);
151
152         _cpls = film->cpls ();
153         update_cpl_choice ();
154
155
156         /* Sub-heading: Output */
157         h = new wxStaticText (this, wxID_ANY, _("Output"));
158         h->SetFont (subheading_font);
159         vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
160
161         table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, 0);
162
163         add_label_to_sizer (table, this, _("KDM type"), true);
164         _type = new wxChoice (this, wxID_ANY);
165         _type->Append ("Modified Transitional 1", ((void *) dcp::MODIFIED_TRANSITIONAL_1));
166         if (!film->interop ()) {
167                 _type->Append ("DCI Any", ((void *) dcp::DCI_ANY));
168                 _type->Append ("DCI Specific", ((void *) dcp::DCI_SPECIFIC));
169         }
170         table->Add (_type, 1, wxEXPAND);
171         _type->SetSelection (0);
172
173         _write_to = new wxRadioButton (this, wxID_ANY, _("Write to"));
174         table->Add (_write_to, 1, wxEXPAND);
175
176 #ifdef DCPOMATIC_USE_OWN_DIR_PICKER
177         _folder = new DirPickerCtrl (this);
178 #else
179         _folder = new wxDirPickerCtrl (this, wxID_ANY, wxEmptyString, wxDirSelectorPromptStr, wxDefaultPosition, wxSize (300, -1));
180 #endif
181
182         _folder->SetPath (wxStandardPaths::Get().GetDocumentsDir());
183
184         table->Add (_folder, 1, wxEXPAND);
185
186         _email = new wxRadioButton (this, wxID_ANY, _("Send by email"));
187         table->Add (_email, 1, wxEXPAND);
188         table->AddSpacer (0);
189
190         vertical->Add (table, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP);
191
192         /* Make an overall sizer to get a nice border, and put some buttons in */
193
194         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
195         overall_sizer->Add (vertical, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, DCPOMATIC_DIALOG_BORDER);
196
197         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
198         if (buttons) {
199                 overall_sizer->Add (buttons, 0, wxEXPAND | wxALL, DCPOMATIC_SIZER_Y_GAP);
200         }
201
202         _write_to->SetValue (true);
203
204         /* Bind */
205
206         _targets->Bind       (wxEVT_COMMAND_TREE_SEL_CHANGED, boost::bind (&KDMDialog::setup_sensitivity, this));
207
208         _add_cinema->Bind    (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::add_cinema_clicked, this));
209         _edit_cinema->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::edit_cinema_clicked, this));
210         _remove_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::remove_cinema_clicked, this));
211
212         _add_screen->Bind    (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::add_screen_clicked, this));
213         _edit_screen->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::edit_screen_clicked, this));
214         _remove_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::remove_screen_clicked, this));
215
216         _cpl->Bind           (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&KDMDialog::update_cpl_summary, this));
217         _cpl_browse->Bind    (wxEVT_COMMAND_BUTTON_CLICKED,  boost::bind (&KDMDialog::cpl_browse_clicked, this));
218
219         _write_to->Bind      (wxEVT_COMMAND_RADIOBUTTON_SELECTED, boost::bind (&KDMDialog::setup_sensitivity, this));
220         _email->Bind         (wxEVT_COMMAND_RADIOBUTTON_SELECTED, boost::bind (&KDMDialog::setup_sensitivity, this));
221
222         setup_sensitivity ();
223
224         SetSizer (overall_sizer);
225         overall_sizer->Layout ();
226         overall_sizer->SetSizeHints (this);
227 }
228
229 list<pair<wxTreeItemId, shared_ptr<Cinema> > >
230 KDMDialog::selected_cinemas () const
231 {
232         wxArrayTreeItemIds s;
233         _targets->GetSelections (s);
234
235         list<pair<wxTreeItemId, shared_ptr<Cinema> > > c;
236         for (size_t i = 0; i < s.GetCount(); ++i) {
237                 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator j = _cinemas.find (s[i]);
238                 if (j != _cinemas.end ()) {
239                         c.push_back (make_pair (j->first, j->second));
240                 }
241         }
242
243         return c;
244 }
245
246 list<pair<wxTreeItemId, shared_ptr<Screen> > >
247 KDMDialog::selected_screens () const
248 {
249         wxArrayTreeItemIds s;
250         _targets->GetSelections (s);
251
252         list<pair<wxTreeItemId, shared_ptr<Screen> > > c;
253         for (size_t i = 0; i < s.GetCount(); ++i) {
254                 map<wxTreeItemId, shared_ptr<Screen> >::const_iterator j = _screens.find (s[i]);
255                 if (j != _screens.end ()) {
256                         c.push_back (make_pair (j->first, j->second));
257                 }
258         }
259
260         return c;
261 }
262
263 void
264 KDMDialog::setup_sensitivity ()
265 {
266         bool const sc = selected_cinemas().size() == 1;
267         bool const ss = selected_screens().size() == 1;
268         bool const sd = _cpl->GetSelection() != -1;
269
270         _edit_cinema->Enable (sc);
271         _remove_cinema->Enable (sc);
272
273         _add_screen->Enable (sc);
274         _edit_screen->Enable (ss);
275         _remove_screen->Enable (ss);
276
277         wxButton* ok = dynamic_cast<wxButton *> (FindWindowById (wxID_OK, this));
278         if (ok) {
279                 ok->Enable ((selected_cinemas().size() > 0 || selected_screens().size() > 0) && sd);
280         }
281
282         _folder->Enable (_write_to->GetValue ());
283 }
284
285 void
286 KDMDialog::add_cinema (shared_ptr<Cinema> c)
287 {
288         _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
289
290         list<shared_ptr<Screen> > sc = c->screens ();
291         for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
292                 add_screen (c, *i);
293         }
294 }
295
296 void
297 KDMDialog::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
298 {
299         map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator i = _cinemas.begin();
300         while (i != _cinemas.end() && i->second != c) {
301                 ++i;
302         }
303
304         if (i == _cinemas.end()) {
305                 return;
306         }
307
308         _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
309         _targets->Expand (i->first);
310 }
311
312 void
313 KDMDialog::add_cinema_clicked ()
314 {
315         CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
316         if (d->ShowModal () == wxID_OK) {
317                 shared_ptr<Cinema> c (new Cinema (d->name(), d->email()));
318                 Config::instance()->add_cinema (c);
319                 add_cinema (c);
320         }
321
322         d->Destroy ();
323 }
324
325 void
326 KDMDialog::edit_cinema_clicked ()
327 {
328         if (selected_cinemas().size() != 1) {
329                 return;
330         }
331
332         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
333
334         CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->email);
335         if (d->ShowModal () == wxID_OK) {
336                 c.second->name = d->name ();
337                 c.second->email = d->email ();
338                 _targets->SetItemText (c.first, std_to_wx (d->name()));
339                 Config::instance()->changed ();
340         }
341
342         d->Destroy ();
343 }
344
345 void
346 KDMDialog::remove_cinema_clicked ()
347 {
348         if (selected_cinemas().size() != 1) {
349                 return;
350         }
351
352         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
353
354         Config::instance()->remove_cinema (c.second);
355         _targets->Delete (c.first);
356 }
357
358 void
359 KDMDialog::add_screen_clicked ()
360 {
361         if (selected_cinemas().size() != 1) {
362                 return;
363         }
364
365         shared_ptr<Cinema> c = selected_cinemas().front().second;
366
367         ScreenDialog* d = new ScreenDialog (this, "Add Screen");
368         if (d->ShowModal () != wxID_OK) {
369                 return;
370         }
371
372         shared_ptr<Screen> s (new Screen (d->name(), d->certificate()));
373         c->add_screen (s);
374         add_screen (c, s);
375
376         Config::instance()->changed ();
377
378         d->Destroy ();
379 }
380
381 void
382 KDMDialog::edit_screen_clicked ()
383 {
384         if (selected_screens().size() != 1) {
385                 return;
386         }
387
388         pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
389
390         ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->certificate);
391         if (d->ShowModal () == wxID_OK) {
392                 s.second->name = d->name ();
393                 s.second->certificate = d->certificate ();
394                 _targets->SetItemText (s.first, std_to_wx (d->name()));
395                 Config::instance()->changed ();
396         }
397
398         d->Destroy ();
399 }
400
401 void
402 KDMDialog::remove_screen_clicked ()
403 {
404         if (selected_screens().size() != 1) {
405                 return;
406         }
407
408         pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
409
410         map<wxTreeItemId, shared_ptr<Cinema> >::iterator i = _cinemas.begin ();
411         while (i != _cinemas.end ()) {
412                 list<shared_ptr<Screen> > sc = i->second->screens ();
413                 if (find (sc.begin(), sc.end(), s.second) != sc.end ()) {
414                         break;
415                 }
416         }
417
418         if (i == _cinemas.end()) {
419                 return;
420         }
421
422         i->second->remove_screen (s.second);
423         _targets->Delete (s.first);
424
425         Config::instance()->changed ();
426 }
427
428 list<shared_ptr<Screen> >
429 KDMDialog::screens () const
430 {
431         list<shared_ptr<Screen> > s;
432
433         list<pair<wxTreeItemId, shared_ptr<Cinema> > > cinemas = selected_cinemas ();
434         for (list<pair<wxTreeItemId, shared_ptr<Cinema> > >::iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
435                 list<shared_ptr<Screen> > sc = i->second->screens ();
436                 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
437                         s.push_back (*j);
438                 }
439         }
440
441         list<pair<wxTreeItemId, shared_ptr<Screen> > > screens = selected_screens ();
442         for (list<pair<wxTreeItemId, shared_ptr<Screen> > >::iterator i = screens.begin(); i != screens.end(); ++i) {
443                 s.push_back (i->second);
444         }
445
446         s.sort ();
447         s.unique ();
448
449         return s;
450 }
451
452 boost::posix_time::ptime
453 KDMDialog::from () const
454 {
455         return posix_time (_from_date, _from_time);
456 }
457
458 boost::posix_time::ptime
459 KDMDialog::posix_time (wxDatePickerCtrl* date_picker, wxTimePickerCtrl* time_picker)
460 {
461         wxDateTime const date = date_picker->GetValue ();
462         wxDateTime const time = time_picker->GetValue ();
463         return boost::posix_time::ptime (
464                 boost::gregorian::date (date.GetYear(), date.GetMonth() + 1, date.GetDay()),
465                 boost::posix_time::time_duration (time.GetHour(), time.GetMinute(), time.GetSecond())
466                 );
467 }
468
469 boost::posix_time::ptime
470 KDMDialog::until () const
471 {
472         return posix_time (_until_date, _until_time);
473 }
474
475 boost::filesystem::path
476 KDMDialog::cpl () const
477 {
478         int const item = _cpl->GetSelection ();
479         DCPOMATIC_ASSERT (item >= 0);
480         return _cpls[item].cpl_file;
481 }
482
483 boost::filesystem::path
484 KDMDialog::directory () const
485 {
486         return wx_to_std (_folder->GetPath ());
487 }
488
489 bool
490 KDMDialog::write_to () const
491 {
492         return _write_to->GetValue ();
493 }
494
495 dcp::Formulation
496 KDMDialog::formulation () const
497 {
498         return (dcp::Formulation) reinterpret_cast<intptr_t> (_type->GetClientData (_type->GetSelection()));
499 }
500
501 void
502 KDMDialog::update_cpl_choice ()
503 {
504         _cpl->Clear ();
505
506         for (vector<CPLSummary>::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
507                 _cpl->Append (std_to_wx (i->cpl_id));
508
509                 if (_cpls.size() > 0) {
510                         _cpl->SetSelection (0);
511                 }
512         }
513
514         update_cpl_summary ();
515 }
516
517 void
518 KDMDialog::update_cpl_summary ()
519 {
520         int const n = _cpl->GetSelection();
521         if (n == wxNOT_FOUND) {
522                 return;
523         }
524
525         _dcp_directory->SetLabel (std_to_wx (_cpls[n].dcp_directory));
526         _cpl_id->SetLabel (std_to_wx (_cpls[n].cpl_id));
527         _cpl_annotation_text->SetLabel (std_to_wx (_cpls[n].cpl_annotation_text));
528 }
529
530 void
531 KDMDialog::cpl_browse_clicked ()
532 {
533         wxFileDialog* d = new wxFileDialog (this, _("Select CPL XML file"), wxEmptyString, wxEmptyString, "*.xml");
534         if (d->ShowModal() == wxID_CANCEL) {
535                 d->Destroy ();
536                 return;
537         }
538
539         boost::filesystem::path cpl_file (wx_to_std (d->GetPath ()));
540         boost::filesystem::path dcp_dir = cpl_file.parent_path ();
541
542         d->Destroy ();
543
544         /* XXX: hack alert */
545         cxml::Document cpl_document ("CompositionPlaylist");
546         cpl_document.read_file (cpl_file);
547
548         try {
549                 _cpls.push_back (
550                         CPLSummary (
551                                 dcp_dir.filename().string(),
552                                 cpl_document.string_child("Id").substr (9),
553                                 cpl_document.string_child ("ContentTitleText"),
554                                 cpl_file
555                                 )
556                         );
557         } catch (cxml::Error) {
558                 error_dialog (this, _("This is not a valid CPL file"));
559                 return;
560         }
561
562         update_cpl_choice ();
563         _cpl->SetSelection (_cpls.size() - 1);
564         update_cpl_summary ();
565 }