Shrink height of KDM creator a bit.
[dcpomatic.git] / src / tools / dcpomatic_kdm.cc
1 /*
2     Copyright (C) 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 "wx/config_dialog.h"
21 #include "wx/about_dialog.h"
22 #include "wx/report_problem_dialog.h"
23 #include "wx/file_picker_ctrl.h"
24 #include "wx/wx_util.h"
25 #include "wx/wx_signal_manager.h"
26 #include "wx/screens_panel.h"
27 #include "wx/kdm_timing_panel.h"
28 #include "wx/kdm_output_panel.h"
29 #include "wx/job_view_dialog.h"
30 #include "lib/config.h"
31 #include "lib/util.h"
32 #include "lib/screen.h"
33 #include "lib/job_manager.h"
34 #include "lib/screen_kdm.h"
35 #include "lib/exceptions.h"
36 #include "lib/cinema_kdms.h"
37 #include "lib/send_kdm_email_job.h"
38 #include <dcp/encrypted_kdm.h>
39 #include <dcp/decrypted_kdm.h>
40 #include <dcp/exceptions.h>
41 #include <wx/wx.h>
42 #include <wx/preferences.h>
43 #include <wx/filepicker.h>
44 #ifdef __WXOSX__
45 #include <ApplicationServices/ApplicationServices.h>
46 #endif
47 #include <boost/bind.hpp>
48 #include <boost/foreach.hpp>
49
50 #ifdef check
51 #undef check
52 #endif
53
54 using std::exception;
55 using std::list;
56 using std::string;
57 using boost::shared_ptr;
58 using boost::bind;
59
60 enum {
61         ID_help_report_a_problem = 1,
62 };
63
64 class DOMFrame : public wxFrame
65 {
66 public:
67         DOMFrame (wxString const & title)
68                 : wxFrame (NULL, -1, title)
69                 , _config_dialog (0)
70                 , _job_view (0)
71         {
72                 wxMenuBar* bar = new wxMenuBar;
73                 setup_menu (bar);
74                 SetMenuBar (bar);
75
76                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::file_exit, this),             wxID_EXIT);
77                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::edit_preferences, this),      wxID_PREFERENCES);
78                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::help_about, this),            wxID_ABOUT);
79                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
80
81                 /* Use a panel as the only child of the Frame so that we avoid
82                    the dark-grey background on Windows.
83                 */
84                 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
85                 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
86
87                 wxBoxSizer* vertical = new wxBoxSizer (wxVERTICAL);
88
89                 wxFont subheading_font (*wxNORMAL_FONT);
90                 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
91
92                 wxStaticText* h = new wxStaticText (overall_panel, wxID_ANY, _("Screens"));
93                 h->SetFont (subheading_font);
94                 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL);
95                 _screens = new ScreensPanel (overall_panel);
96                 vertical->Add (_screens, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_Y_GAP);
97
98                 h = new wxStaticText (overall_panel, wxID_ANY, S_("KDM|Timing"));
99                 h->SetFont (subheading_font);
100                 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
101                 _timing = new KDMTimingPanel (overall_panel);
102                 vertical->Add (_timing, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
103
104                 h = new wxStaticText (overall_panel, wxID_ANY, _("DKDM"));
105                 h->SetFont (subheading_font);
106                 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
107                 wxSizer* dkdm = new wxFlexGridSizer (4, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
108                 add_label_to_sizer (dkdm, overall_panel, _("DKDM file"), true);
109                 _dkdm = new FilePickerCtrl (overall_panel, _("Select a DKDM XML file..."), "*.xml");
110                 dkdm->Add (_dkdm, 1, wxEXPAND);
111                 add_label_to_sizer (dkdm, overall_panel, _("Content title"), true);
112                 _content_title_text = new wxStaticText (overall_panel, wxID_ANY, wxT(""));
113                 dkdm->Add (_content_title_text, 1, wxEXPAND);
114                 dkdm->AddSpacer (0);
115                 dkdm->AddSpacer (0);
116                 add_label_to_sizer (dkdm, overall_panel, _("Annotation"), true);
117                 _annotation_text = new wxStaticText (overall_panel, wxID_ANY, wxT(""));
118                 dkdm->Add (_annotation_text, 1, wxEXPAND);
119                 dkdm->AddSpacer (0);
120                 dkdm->AddSpacer (0);
121                 add_label_to_sizer (dkdm, overall_panel, _("Issue date"), true);
122                 _issue_date = new wxStaticText (overall_panel, wxID_ANY, wxT(""));
123                 dkdm->Add (_issue_date, 1, wxEXPAND);
124                 vertical->Add (dkdm, 0, wxALL, DCPOMATIC_SIZER_X_GAP);
125
126                 h = new wxStaticText (overall_panel, wxID_ANY, _("Output"));
127                 h->SetFont (subheading_font);
128                 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
129                 /* XXX: hard-coded non-interop here */
130                 _output = new KDMOutputPanel (overall_panel, false);
131                 vertical->Add (_output, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
132
133                 _create = new wxButton (overall_panel, wxID_ANY, _("Create KDMs"));
134                 vertical->Add (_create, 0, wxALL, DCPOMATIC_SIZER_GAP);
135
136                 main_sizer->Add (vertical, 1, wxALL, DCPOMATIC_DIALOG_BORDER);
137                 overall_panel->SetSizer (main_sizer);
138
139                 /* Instantly save any config changes when using a DCP-o-matic GUI */
140                 Config::instance()->Changed.connect (boost::bind (&Config::write, Config::instance ()));
141
142                 _screens->ScreensChanged.connect (boost::bind (&DOMFrame::setup_sensitivity, this));
143                 _dkdm->Bind (wxEVT_COMMAND_FILEPICKER_CHANGED, bind (&DOMFrame::dkdm_changed, this));
144                 _create->Bind (wxEVT_COMMAND_BUTTON_CLICKED, bind (&DOMFrame::create_kdms, this));
145
146                 setup_sensitivity ();
147         }
148
149 private:
150         void file_exit ()
151         {
152                 /* false here allows the close handler to veto the close request */
153                 Close (false);
154         }
155
156         void edit_preferences ()
157         {
158                 if (!_config_dialog) {
159                         _config_dialog = create_config_dialog ();
160                 }
161                 _config_dialog->Show (this);
162         }
163
164         void help_about ()
165         {
166                 AboutDialog* d = new AboutDialog (this);
167                 d->ShowModal ();
168                 d->Destroy ();
169         }
170
171         void help_report_a_problem ()
172         {
173                 ReportProblemDialog* d = new ReportProblemDialog (this, shared_ptr<Film> ());
174                 if (d->ShowModal () == wxID_OK) {
175                         d->report ();
176                 }
177                 d->Destroy ();
178         }
179
180         void setup_menu (wxMenuBar* m)
181         {
182                 wxMenu* file = new wxMenu;
183
184 #ifdef __WXOSX__
185                 file->Append (wxID_EXIT, _("&Exit"));
186 #else
187                 file->Append (wxID_EXIT, _("&Quit"));
188 #endif
189
190 #ifdef __WXOSX__
191                 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
192 #else
193                 wxMenu* edit = new wxMenu;
194                 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
195 #endif
196
197                 wxMenu* help = new wxMenu;
198 #ifdef __WXOSX__
199                 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
200 #else
201                 help->Append (wxID_ABOUT, _("About"));
202 #endif
203                 help->Append (ID_help_report_a_problem, _("Report a problem..."));
204
205                 m->Append (file, _("&File"));
206 #ifndef __WXOSX__
207                 m->Append (edit, _("&Edit"));
208 #endif
209                 m->Append (help, _("&Help"));
210         }
211
212         void dkdm_changed ()
213         {
214                 if (_dkdm->GetPath().IsEmpty()) {
215                         return;
216                 }
217
218                 try {
219                         dcp::EncryptedKDM encrypted (dcp::file_to_string (wx_to_std (_dkdm->GetPath())));
220                         dcp::DecryptedKDM decrypted (encrypted, Config::instance()->decryption_chain()->key().get());
221                         _annotation_text->Enable (true);
222                         _annotation_text->SetLabel (std_to_wx (decrypted.annotation_text ()));
223                         _content_title_text->Enable (true);
224                         _content_title_text->SetLabel (std_to_wx (decrypted.content_title_text ()));
225                         _issue_date->Enable (true);
226                         _issue_date->SetLabel (std_to_wx (decrypted.issue_date ()));
227                 } catch (exception& e) {
228                         error_dialog (this, wxString::Format (_("Could not load DKDM (%s)"), std_to_wx (e.what()).data()));
229                         _dkdm->SetPath (wxT(""));
230                         _annotation_text->SetLabel (wxT(""));
231                         _annotation_text->Enable (false);
232                         _content_title_text->SetLabel (wxT(""));
233                         _content_title_text->Enable (false);
234                         _issue_date->SetLabel (wxT(""));
235                         _issue_date->Enable (false);
236                 }
237
238                 setup_sensitivity ();
239         }
240
241         void create_kdms ()
242         {
243                 try {
244                         /* Decrypt the DKDM */
245                         dcp::EncryptedKDM encrypted (dcp::file_to_string (wx_to_std (_dkdm->GetPath())));
246                         dcp::DecryptedKDM decrypted (encrypted, Config::instance()->decryption_chain()->key().get());
247
248                         /* This is the signer for our new KDMs */
249                         shared_ptr<const dcp::CertificateChain> signer = Config::instance()->signer_chain ();
250                         if (!signer->valid ()) {
251                                 throw InvalidSignerError ();
252                         }
253
254                         list<ScreenKDM> screen_kdms;
255                         BOOST_FOREACH (shared_ptr<Screen> i, _screens->screens()) {
256
257                                 if (!i->certificate) {
258                                         continue;
259                                 }
260
261                                 /* Make an empty KDM */
262                                 dcp::DecryptedKDM kdm (
263                                         _timing->from(), _timing->until(), decrypted.annotation_text(), decrypted.content_title_text(), dcp::LocalTime().as_string()
264                                         );
265
266                                 /* Add keys from the DKDM */
267                                 BOOST_FOREACH (dcp::DecryptedKDMKey const & j, decrypted.keys()) {
268                                         kdm.add_key (j);
269                                 }
270
271                                 /* Encrypt */
272                                 screen_kdms.push_back (ScreenKDM (i, kdm.encrypt (signer, i->certificate.get(), _output->formulation())));
273                         }
274
275                         if (_output->write_to()) {
276                                 ScreenKDM::write_files (decrypted.content_title_text(), screen_kdms, _output->directory());
277                                 /* XXX: proper plural form support in wxWidgets? */
278                                 wxString s = screen_kdms.size() == 1 ? _("%d KDM written to %s") : _("%d KDMs written to %s");
279                                 message_dialog (
280                                         this,
281                                         wxString::Format (s, int(screen_kdms.size()), std_to_wx(_output->directory().string()).data())
282                                         );
283                         } else {
284                                 string film_name = decrypted.annotation_text ();
285                                 if (film_name.empty ()) {
286                                         film_name = decrypted.content_title_text ();
287                                 }
288                                 shared_ptr<Job> job (new SendKDMEmailJob (
289                                                              film_name,
290                                                              decrypted.content_title_text(),
291                                                              _timing->from(), _timing->until(),
292                                                              CinemaKDMs::collect (screen_kdms),
293                                                              shared_ptr<Log> ()
294                                                              ));
295
296                                 JobManager::instance()->add (job);
297                                 if (_job_view) {
298                                         _job_view->Destroy ();
299                                         _job_view = 0;
300                                 }
301                                 _job_view = new JobViewDialog (this, _("Send KDM emails"), job);
302                                 _job_view->ShowModal ();
303                         }
304                 } catch (dcp::NotEncryptedError& e) {
305                         error_dialog (this, _("CPL's content is not encrypted."));
306                 } catch (exception& e) {
307                         error_dialog (this, e.what ());
308                 } catch (...) {
309                         error_dialog (this, _("An unknown exception occurred."));
310                 }
311         }
312
313         void setup_sensitivity ()
314         {
315                 _screens->setup_sensitivity ();
316                 _output->setup_sensitivity ();
317                 _create->Enable (!_screens->screens().empty() && !_dkdm->GetPath().IsEmpty());
318         }
319
320         wxPreferencesEditor* _config_dialog;
321         ScreensPanel* _screens;
322         KDMTimingPanel* _timing;
323         /* I can't seem to clear the value in a wxFilePickerCtrl, so use our own */
324         FilePickerCtrl* _dkdm;
325         wxStaticText* _annotation_text;
326         wxStaticText* _content_title_text;
327         wxStaticText* _issue_date;
328         wxButton* _create;
329         KDMOutputPanel* _output;
330         JobViewDialog* _job_view;
331 };
332
333 /** @class App
334  *  @brief The magic App class for wxWidgets.
335  */
336 class App : public wxApp
337 {
338 public:
339         App ()
340                 : wxApp ()
341                 , _frame (0)
342         {}
343
344 private:
345
346         bool OnInit ()
347         try
348         {
349                 wxInitAllImageHandlers ();
350
351                 SetAppName (_("DCP-o-matic KDM creator"));
352
353                 if (!wxApp::OnInit()) {
354                         return false;
355                 }
356
357 #ifdef DCPOMATIC_LINUX
358                 unsetenv ("UBUNTU_MENUPROXY");
359 #endif
360
361 #ifdef __WXOSX__
362                 ProcessSerialNumber serial;
363                 GetCurrentProcess (&serial);
364                 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
365 #endif
366
367                 dcpomatic_setup_path_encoding ();
368
369                 /* Enable i18n; this will create a Config object
370                    to look for a force-configured language.  This Config
371                    object will be wrong, however, because dcpomatic_setup
372                    hasn't yet been called and there aren't any filters etc.
373                    set up yet.
374                 */
375                 dcpomatic_setup_i18n ();
376
377                 /* Set things up, including filters etc.
378                    which will now be internationalised correctly.
379                 */
380                 dcpomatic_setup ();
381
382                 /* Force the configuration to be re-loaded correctly next
383                    time it is needed.
384                 */
385                 Config::drop ();
386
387                 _frame = new DOMFrame (_("DCP-o-matic KDM creator"));
388                 SetTopWindow (_frame);
389                 _frame->Maximize ();
390                 _frame->Show ();
391
392                 signal_manager = new wxSignalManager (this);
393                 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
394
395                 return true;
396         }
397         catch (exception& e)
398         {
399                 error_dialog (0, wxString::Format ("DCP-o-matic could not start: %s", e.what ()));
400                 return true;
401         }
402
403         /* An unhandled exception has occurred inside the main event loop */
404         bool OnExceptionInMainLoop ()
405         {
406                 try {
407                         throw;
408                 } catch (FileError& e) {
409                         error_dialog (
410                                 0,
411                                 wxString::Format (
412                                         _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
413                                         std_to_wx (e.what()),
414                                         std_to_wx (e.file().string().c_str ())
415                                         )
416                                 );
417                 } catch (exception& e) {
418                         error_dialog (
419                                 0,
420                                 wxString::Format (
421                                         _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
422                                         std_to_wx (e.what ())
423                                         )
424                                 );
425                 } catch (...) {
426                         error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
427                 }
428
429                 /* This will terminate the program */
430                 return false;
431         }
432
433         void OnUnhandledException ()
434         {
435                 error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
436         }
437
438         void idle ()
439         {
440                 signal_manager->ui_idle ();
441         }
442
443         DOMFrame* _frame;
444 };
445
446 IMPLEMENT_APP (App)