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