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