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