OS X build fix.
[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,
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 (2, 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, _("Annotation"), true);
112                 _annotation_text = new wxStaticText (overall_panel, wxID_ANY, wxT(""));
113                 dkdm->Add (_annotation_text, 1, wxEXPAND);
114                 add_label_to_sizer (dkdm, overall_panel, _("Content title"), true);
115                 _content_title_text = new wxStaticText (overall_panel, wxID_ANY, wxT(""));
116                 dkdm->Add (_content_title_text, 1, wxEXPAND);
117                 add_label_to_sizer (dkdm, overall_panel, _("Issue date"), true);
118                 _issue_date = new wxStaticText (overall_panel, wxID_ANY, wxT(""));
119                 dkdm->Add (_issue_date, 1, wxEXPAND);
120                 vertical->Add (dkdm, 0, wxALL, DCPOMATIC_SIZER_X_GAP);
121
122                 h = new wxStaticText (overall_panel, wxID_ANY, _("Output"));
123                 h->SetFont (subheading_font);
124                 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
125                 /* XXX: hard-coded non-interop here */
126                 _output = new KDMOutputPanel (overall_panel, false);
127                 vertical->Add (_output, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
128
129                 _create = new wxButton (overall_panel, wxID_ANY, _("Create KDMs"));
130                 vertical->Add (_create, 0, wxALL, DCPOMATIC_SIZER_GAP);
131
132                 main_sizer->Add (vertical, 1, wxALL, DCPOMATIC_DIALOG_BORDER);
133                 overall_panel->SetSizer (main_sizer);
134
135                 /* Instantly save any config changes when using a DCP-o-matic GUI */
136                 Config::instance()->Changed.connect (boost::bind (&Config::write, Config::instance ()));
137
138                 _screens->ScreensChanged.connect (boost::bind (&DOMFrame::setup_sensitivity, this));
139                 _dkdm->Bind (wxEVT_COMMAND_FILEPICKER_CHANGED, bind (&DOMFrame::dkdm_changed, this));
140                 _create->Bind (wxEVT_COMMAND_BUTTON_CLICKED, bind (&DOMFrame::create_kdms, this));
141
142                 setup_sensitivity ();
143         }
144
145 private:
146         void file_exit ()
147         {
148                 /* false here allows the close handler to veto the close request */
149                 Close (false);
150         }
151
152         void edit_preferences ()
153         {
154                 if (!_config_dialog) {
155                         _config_dialog = create_config_dialog ();
156                 }
157                 _config_dialog->Show (this);
158         }
159
160         void help_about ()
161         {
162                 AboutDialog* d = new AboutDialog (this);
163                 d->ShowModal ();
164                 d->Destroy ();
165         }
166
167         void help_report_a_problem ()
168         {
169                 ReportProblemDialog* d = new ReportProblemDialog (this, shared_ptr<Film> ());
170                 if (d->ShowModal () == wxID_OK) {
171                         d->report ();
172                 }
173                 d->Destroy ();
174         }
175
176         void setup_menu (wxMenuBar* m)
177         {
178                 wxMenu* file = new wxMenu;
179
180 #ifdef __WXOSX__
181                 file->Append (wxID_EXIT, _("&Exit"));
182 #else
183                 file->Append (wxID_EXIT, _("&Quit"));
184 #endif
185
186 #ifdef __WXOSX__
187                 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
188 #else
189                 wxMenu* edit = new wxMenu;
190                 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
191 #endif
192
193                 wxMenu* help = new wxMenu;
194 #ifdef __WXOSX__
195                 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
196 #else
197                 help->Append (wxID_ABOUT, _("About"));
198 #endif
199                 help->Append (ID_help_report_a_problem, _("Report a problem..."));
200
201                 m->Append (file, _("&File"));
202 #ifndef __WXOSX__
203                 m->Append (edit, _("&Edit"));
204 #endif
205                 m->Append (help, _("&Help"));
206         }
207
208         void dkdm_changed ()
209         {
210                 if (_dkdm->GetPath().IsEmpty()) {
211                         return;
212                 }
213
214                 try {
215                         dcp::EncryptedKDM encrypted (dcp::file_to_string (wx_to_std (_dkdm->GetPath())));
216                         dcp::DecryptedKDM decrypted (encrypted, Config::instance()->decryption_chain()->key().get());
217                         _annotation_text->Enable (true);
218                         _annotation_text->SetLabel (std_to_wx (decrypted.annotation_text ()));
219                         _content_title_text->Enable (true);
220                         _content_title_text->SetLabel (std_to_wx (decrypted.content_title_text ()));
221                         _issue_date->Enable (true);
222                         _issue_date->SetLabel (std_to_wx (decrypted.issue_date ()));
223                 } catch (exception& e) {
224                         error_dialog (this, wxString::Format (_("Could not load DKDM (%s)"), std_to_wx (e.what()).data()));
225                         _dkdm->SetPath (wxT(""));
226                         _annotation_text->SetLabel (wxT(""));
227                         _annotation_text->Enable (false);
228                         _content_title_text->SetLabel (wxT(""));
229                         _content_title_text->Enable (false);
230                         _issue_date->SetLabel (wxT(""));
231                         _issue_date->Enable (false);
232                 }
233
234                 setup_sensitivity ();
235         }
236
237         void create_kdms ()
238         {
239                 try {
240                         /* Decrypt the DKDM */
241                         dcp::EncryptedKDM encrypted (dcp::file_to_string (wx_to_std (_dkdm->GetPath())));
242                         dcp::DecryptedKDM decrypted (encrypted, Config::instance()->decryption_chain()->key().get());
243
244                         /* This is the signer for our new KDMs */
245                         shared_ptr<const dcp::CertificateChain> signer = Config::instance()->signer_chain ();
246                         if (!signer->valid ()) {
247                                 throw InvalidSignerError ();
248                         }
249
250                         list<ScreenKDM> screen_kdms;
251                         BOOST_FOREACH (shared_ptr<Screen> i, _screens->screens()) {
252
253                                 if (!i->certificate) {
254                                         continue;
255                                 }
256
257                                 /* Make an empty KDM */
258                                 dcp::DecryptedKDM kdm (
259                                         _timing->from(), _timing->until(), decrypted.annotation_text(), decrypted.content_title_text(), dcp::LocalTime().as_string()
260                                         );
261
262                                 /* Add keys from the DKDM */
263                                 BOOST_FOREACH (dcp::DecryptedKDMKey const & j, decrypted.keys()) {
264                                         kdm.add_key (j);
265                                 }
266
267                                 /* Encrypt */
268                                 screen_kdms.push_back (ScreenKDM (i, kdm.encrypt (signer, i->certificate.get(), _output->formulation())));
269                         }
270
271                         if (_output->write_to()) {
272                                 ScreenKDM::write_files (decrypted.content_title_text(), screen_kdms, _output->directory());
273                                 /* XXX: proper plural form support in wxWidgets? */
274                                 wxString s = screen_kdms.size() == 1 ? _("%d KDM written to %s") : _("%d KDMs written to %s");
275                                 message_dialog (
276                                         this,
277                                         wxString::Format (s, int(screen_kdms.size()), std_to_wx(_output->directory().string()).data())
278                                         );
279                         } else {
280                                 string film_name = decrypted.annotation_text ();
281                                 if (film_name.empty ()) {
282                                         film_name = decrypted.content_title_text ();
283                                 }
284                                 shared_ptr<Job> job (new SendKDMEmailJob (
285                                                              film_name,
286                                                              decrypted.content_title_text(),
287                                                              _timing->from(), _timing->until(),
288                                                              CinemaKDMs::collect (screen_kdms)
289                                                              ));
290
291                                 JobManager::instance()->add (job);
292                                 if (_job_view) {
293                                         _job_view->Destroy ();
294                                         _job_view = 0;
295                                 }
296                                 _job_view = new JobViewDialog (this, _("Send KDM emails"), job);
297                                 _job_view->ShowModal ();
298                         }
299                 } catch (dcp::NotEncryptedError& e) {
300                         error_dialog (this, _("CPL's content is not encrypted."));
301                 } catch (exception& e) {
302                         error_dialog (this, e.what ());
303                 } catch (...) {
304                         error_dialog (this, _("An unknown exception occurred."));
305                 }
306         }
307
308         void setup_sensitivity ()
309         {
310                 _screens->setup_sensitivity ();
311                 _output->setup_sensitivity ();
312                 _create->Enable (!_screens->screens().empty() && !_dkdm->GetPath().IsEmpty());
313         }
314
315         wxPreferencesEditor* _config_dialog;
316         ScreensPanel* _screens;
317         KDMTimingPanel* _timing;
318         /* I can't seem to clear the value in a wxFilePickerCtrl, so use our own */
319         FilePickerCtrl* _dkdm;
320         wxStaticText* _annotation_text;
321         wxStaticText* _content_title_text;
322         wxStaticText* _issue_date;
323         wxButton* _create;
324         KDMOutputPanel* _output;
325         JobViewDialog* _job_view;
326 };
327
328 /** @class App
329  *  @brief The magic App class for wxWidgets.
330  */
331 class App : public wxApp
332 {
333 public:
334         App ()
335                 : wxApp ()
336                 , _frame (0)
337         {}
338
339 private:
340
341         bool OnInit ()
342         try
343         {
344                 wxInitAllImageHandlers ();
345
346                 SetAppName (_("DCP-o-matic KDM creator"));
347
348                 if (!wxApp::OnInit()) {
349                         return false;
350                 }
351
352 #ifdef DCPOMATIC_LINUX
353                 unsetenv ("UBUNTU_MENUPROXY");
354 #endif
355
356 #ifdef __WXOSX__
357                 ProcessSerialNumber serial;
358                 GetCurrentProcess (&serial);
359                 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
360 #endif
361
362                 dcpomatic_setup_path_encoding ();
363
364                 /* Enable i18n; this will create a Config object
365                    to look for a force-configured language.  This Config
366                    object will be wrong, however, because dcpomatic_setup
367                    hasn't yet been called and there aren't any filters etc.
368                    set up yet.
369                 */
370                 dcpomatic_setup_i18n ();
371
372                 /* Set things up, including filters etc.
373                    which will now be internationalised correctly.
374                 */
375                 dcpomatic_setup ();
376
377                 /* Force the configuration to be re-loaded correctly next
378                    time it is needed.
379                 */
380                 Config::drop ();
381
382                 _frame = new DOMFrame (_("DCP-o-matic KDM creator"));
383                 SetTopWindow (_frame);
384                 _frame->Maximize ();
385                 _frame->Show ();
386
387                 signal_manager = new wxSignalManager (this);
388                 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
389
390                 return true;
391         }
392         catch (exception& e)
393         {
394                 error_dialog (0, wxString::Format ("DCP-o-matic could not start: %s", e.what ()));
395                 return true;
396         }
397
398         /* An unhandled exception has occurred inside the main event loop */
399         bool OnExceptionInMainLoop ()
400         {
401                 try {
402                         throw;
403                 } catch (FileError& e) {
404                         error_dialog (
405                                 0,
406                                 wxString::Format (
407                                         _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
408                                         std_to_wx (e.what()),
409                                         std_to_wx (e.file().string().c_str ())
410                                         )
411                                 );
412                 } catch (exception& e) {
413                         error_dialog (
414                                 0,
415                                 wxString::Format (
416                                         _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
417                                         std_to_wx (e.what ())
418                                         )
419                                 );
420                 } catch (...) {
421                         error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
422                 }
423
424                 /* This will terminate the program */
425                 return false;
426         }
427
428         void OnUnhandledException ()
429         {
430                 error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
431         }
432
433         void idle ()
434         {
435                 signal_manager->ui_idle ();
436         }
437
438         DOMFrame* _frame;
439 };
440
441 IMPLEMENT_APP (App)