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