ee5e454579bfda8812f6b2e324437dd5a4c59401
[dcpomatic.git] / src / tools / dcpomatic_kdm.cc
1 /*
2     Copyright (C) 2015-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "wx/full_config_dialog.h"
23 #include "wx/about_dialog.h"
24 #include "wx/report_problem_dialog.h"
25 #include "wx/file_picker_ctrl.h"
26 #include "wx/wx_util.h"
27 #include "wx/wx_signal_manager.h"
28 #include "wx/screens_panel.h"
29 #include "wx/kdm_timing_panel.h"
30 #include "wx/kdm_output_panel.h"
31 #include "wx/job_view_dialog.h"
32 #include "wx/file_dialog_wrapper.h"
33 #include "wx/new_dkdm_folder_dialog.h"
34 #include "wx/editable_list.h"
35 #include "wx/static_text.h"
36 #include "wx/dcpomatic_button.h"
37 #include "wx/nag_dialog.h"
38 #include "lib/config.h"
39 #include "lib/util.h"
40 #include "lib/screen.h"
41 #include "lib/job_manager.h"
42 #include "lib/kdm_with_metadata.h"
43 #include "lib/exceptions.h"
44 #include "lib/send_kdm_email_job.h"
45 #include "lib/compose.hpp"
46 #include "lib/cinema.h"
47 #include "lib/dkdm_wrapper.h"
48 #include "lib/cross.h"
49 #include <dcp/encrypted_kdm.h>
50 #include <dcp/decrypted_kdm.h>
51 #include <dcp/exceptions.h>
52 #include "lib/warnings.h"
53 DCPOMATIC_DISABLE_WARNINGS
54 #include <wx/wx.h>
55 #include <wx/preferences.h>
56 #include <wx/splash.h>
57 #include <wx/filepicker.h>
58 DCPOMATIC_ENABLE_WARNINGS
59 #ifdef __WXOSX__
60 #include <ApplicationServices/ApplicationServices.h>
61 #endif
62 #include <boost/bind/bind.hpp>
63
64 #ifdef check
65 #undef check
66 #endif
67
68
69 using std::exception;
70 using std::list;
71 using std::string;
72 using std::vector;
73 using std::pair;
74 using std::map;
75 using std::shared_ptr;
76 using boost::bind;
77 using boost::optional;
78 using boost::ref;
79 using std::dynamic_pointer_cast;
80 #if BOOST_VERSION >= 106100
81 using namespace boost::placeholders;
82 #endif
83 using namespace dcpomatic;
84
85
86 enum {
87         ID_help_report_a_problem = 1,
88 };
89
90
91 class DOMFrame : public wxFrame
92 {
93 public:
94         explicit DOMFrame (wxString const & title)
95                 : wxFrame (0, -1, title)
96                 , _config_dialog (nullptr)
97                 , _job_view (nullptr)
98         {
99 #if defined(DCPOMATIC_WINDOWS)
100                 if (Config::instance()->win32_console ()) {
101                         AllocConsole();
102
103                         HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
104                         int hCrt = _open_osfhandle((intptr_t) handle_out, _O_TEXT);
105                         FILE* hf_out = _fdopen(hCrt, "w");
106                         setvbuf(hf_out, NULL, _IONBF, 1);
107                         *stdout = *hf_out;
108
109                         HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
110                         hCrt = _open_osfhandle((intptr_t) handle_in, _O_TEXT);
111                         FILE* hf_in = _fdopen(hCrt, "r");
112                         setvbuf(hf_in, NULL, _IONBF, 128);
113                         *stdin = *hf_in;
114
115                         std::cout << "DCP-o-matic KDM creator is starting." << "\n";
116                 }
117 #endif
118
119                 auto bar = new wxMenuBar;
120                 setup_menu (bar);
121                 SetMenuBar (bar);
122
123                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this),             wxID_EXIT);
124                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this),      wxID_PREFERENCES);
125                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this),            wxID_ABOUT);
126                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
127
128                 /* Use a panel as the only child of the Frame so that we avoid
129                    the dark-grey background on Windows.
130                 */
131                 auto overall_panel = new wxPanel (this, wxID_ANY);
132                 auto main_sizer = new wxBoxSizer (wxHORIZONTAL);
133
134                 auto horizontal = new wxBoxSizer (wxHORIZONTAL);
135                 auto left = new wxBoxSizer (wxVERTICAL);
136                 auto right = new wxBoxSizer (wxVERTICAL);
137
138                 horizontal->Add (left, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_X_GAP * 2);
139                 horizontal->Add (right, 1, wxEXPAND);
140
141                 wxFont subheading_font (*wxNORMAL_FONT);
142                 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
143
144                 auto h = new StaticText (overall_panel, _("Screens"));
145                 h->SetFont (subheading_font);
146                 left->Add (h, 0, wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
147                 _screens = new ScreensPanel (overall_panel);
148                 left->Add (_screens, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
149
150                 /// TRANSLATORS: translate the word "Timing" here; do not include the "KDM|" prefix
151                 h = new StaticText (overall_panel, S_("KDM|Timing"));
152                 h->SetFont (subheading_font);
153                 right->Add (h);
154                 _timing = new KDMTimingPanel (overall_panel);
155                 right->Add (_timing, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
156
157                 h = new StaticText (overall_panel, _("DKDM"));
158                 h->SetFont (subheading_font);
159                 right->Add (h, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
160                 auto dkdm_sizer = new wxBoxSizer (wxHORIZONTAL);
161                 _dkdm = new wxTreeCtrl (
162                         overall_panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT
163                 );
164                 dkdm_sizer->Add (_dkdm, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_Y_GAP);
165                 wxBoxSizer* dkdm_buttons = new wxBoxSizer(wxVERTICAL);
166                 _add_dkdm = new Button (overall_panel, _("Add..."));
167                 dkdm_buttons->Add (_add_dkdm, 0, wxALL | wxEXPAND, DCPOMATIC_BUTTON_STACK_GAP);
168                 _add_dkdm_folder = new Button (overall_panel, _("Add folder..."));
169                 dkdm_buttons->Add (_add_dkdm_folder, 0, wxALL | wxEXPAND, DCPOMATIC_BUTTON_STACK_GAP);
170                 _remove_dkdm = new Button (overall_panel, _("Remove"));
171                 dkdm_buttons->Add (_remove_dkdm, 0, wxALL | wxEXPAND, DCPOMATIC_BUTTON_STACK_GAP);
172                 _export_dkdm = new Button (overall_panel, _("Export..."));
173                 dkdm_buttons->Add (_export_dkdm, 0, wxALL | wxEXPAND, DCPOMATIC_BUTTON_STACK_GAP);
174                 dkdm_sizer->Add (dkdm_buttons, 0, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
175                 right->Add (dkdm_sizer, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_Y_GAP);
176
177                 add_dkdm_view (Config::instance()->dkdms());
178
179                 h = new StaticText (overall_panel, _("Output"));
180                 h->SetFont (subheading_font);
181                 right->Add (h, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
182                 /* XXX: hard-coded non-interop here */
183                 _output = new KDMOutputPanel (overall_panel, false);
184                 right->Add (_output, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
185
186                 _create = new Button (overall_panel, _("Create KDMs"));
187                 right->Add (_create, 0, wxALL, DCPOMATIC_SIZER_GAP);
188
189                 main_sizer->Add (horizontal, 1, wxALL | wxEXPAND, DCPOMATIC_DIALOG_BORDER);
190                 overall_panel->SetSizer (main_sizer);
191
192                 /* Instantly save any config changes when using a DCP-o-matic GUI */
193                 Config::instance()->Changed.connect (boost::bind (&Config::write, Config::instance ()));
194
195                 _screens->ScreensChanged.connect (boost::bind (&DOMFrame::setup_sensitivity, this));
196                 _create->Bind (wxEVT_BUTTON, bind (&DOMFrame::create_kdms, this));
197                 _dkdm->Bind (wxEVT_TREE_SEL_CHANGED, boost::bind (&DOMFrame::setup_sensitivity, this));
198                 _dkdm->Bind (wxEVT_TREE_BEGIN_DRAG, boost::bind (&DOMFrame::dkdm_begin_drag, this, _1));
199                 _dkdm->Bind (wxEVT_TREE_END_DRAG, boost::bind (&DOMFrame::dkdm_end_drag, this, _1));
200                 _add_dkdm->Bind (wxEVT_BUTTON, bind (&DOMFrame::add_dkdm_clicked, this));
201                 _add_dkdm_folder->Bind (wxEVT_BUTTON, bind (&DOMFrame::add_dkdm_folder_clicked, this));
202                 _remove_dkdm->Bind (wxEVT_BUTTON, bind (&DOMFrame::remove_dkdm_clicked, this));
203                 _export_dkdm->Bind (wxEVT_BUTTON, bind (&DOMFrame::export_dkdm_clicked, this));
204
205                 setup_sensitivity ();
206         }
207
208 private:
209         void file_exit ()
210         {
211                 /* false here allows the close handler to veto the close request */
212                 Close (false);
213         }
214
215         void edit_preferences ()
216         {
217                 if (!_config_dialog) {
218                         _config_dialog = create_full_config_dialog ();
219                 }
220                 _config_dialog->Show (this);
221         }
222
223         void help_about ()
224         {
225                 auto d = new AboutDialog (this);
226                 d->ShowModal ();
227                 d->Destroy ();
228         }
229
230         void help_report_a_problem ()
231         {
232                 auto d = new ReportProblemDialog (this, shared_ptr<Film>());
233                 if (d->ShowModal () == wxID_OK) {
234                         d->report ();
235                 }
236                 d->Destroy ();
237         }
238
239         void setup_menu (wxMenuBar* m)
240         {
241                 auto file = new wxMenu;
242
243 #ifdef __WXOSX__
244                 file->Append (wxID_EXIT, _("&Exit"));
245 #else
246                 file->Append (wxID_EXIT, _("&Quit"));
247 #endif
248
249 #ifdef __WXOSX__
250                 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
251 #else
252                 wxMenu* edit = new wxMenu;
253                 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
254 #endif
255
256                 wxMenu* help = new wxMenu;
257 #ifdef __WXOSX__
258                 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
259 #else
260                 help->Append (wxID_ABOUT, _("About"));
261 #endif
262                 help->Append (ID_help_report_a_problem, _("Report a problem..."));
263
264                 m->Append (file, _("&File"));
265 #ifndef __WXOSX__
266                 m->Append (edit, _("&Edit"));
267 #endif
268                 m->Append (help, _("&Help"));
269         }
270
271         bool confirm_overwrite (boost::filesystem::path path)
272         {
273                 return confirm_dialog (
274                         this,
275                         wxString::Format (_("File %s already exists.  Do you want to overwrite it?"), std_to_wx(path.string()).data())
276                         );
277         }
278
279         /** @id if not 0 this is filled in with the wxTreeItemId of the selection */
280         shared_ptr<DKDMBase> selected_dkdm (wxTreeItemId* id = 0) const
281         {
282                 wxArrayTreeItemIds selections;
283                 _dkdm->GetSelections (selections);
284                 if (selections.GetCount() != 1) {
285                         if (id) {
286                                 *id = 0;
287                         }
288                         return shared_ptr<DKDMBase> ();
289                 }
290
291                 if (id) {
292                         *id = selections[0];
293                 }
294
295                 auto i = _dkdm_id.find (selections[0]);
296                 if (i == _dkdm_id.end()) {
297                         return shared_ptr<DKDMBase> ();
298                 }
299
300                 return i->second;
301         }
302
303         void create_kdms ()
304         {
305                 try {
306                         auto dkdm_base = selected_dkdm ();
307                         if (!dkdm_base) {
308                                 return;
309                         }
310
311                         list<KDMWithMetadataPtr> kdms;
312                         string title;
313
314                         auto dkdm = std::dynamic_pointer_cast<DKDM>(dkdm_base);
315                         if (dkdm) {
316
317                                 /* Decrypt the DKDM */
318                                 dcp::DecryptedKDM decrypted (dkdm->dkdm(), Config::instance()->decryption_chain()->key().get());
319                                 title = decrypted.content_title_text ();
320
321                                 /* This is the signer for our new KDMs */
322                                 auto signer = Config::instance()->signer_chain ();
323                                 if (!signer->valid ()) {
324                                         throw InvalidSignerError ();
325                                 }
326
327                                 for (auto i: _screens->screens()) {
328
329                                         if (!i->recipient) {
330                                                 continue;
331                                         }
332
333                                         dcp::LocalTime begin(_timing->from(), i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute());
334                                         dcp::LocalTime end(_timing->until(), i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute());
335
336                                         /* Make an empty KDM */
337                                         dcp::DecryptedKDM kdm (
338                                                 begin,
339                                                 end,
340                                                 decrypted.annotation_text().get_value_or (""),
341                                                 decrypted.content_title_text(),
342                                                 dcp::LocalTime().as_string()
343                                                 );
344
345                                         /* Add keys from the DKDM */
346                                         for (auto const& j: decrypted.keys()) {
347                                                 kdm.add_key (j);
348                                         }
349
350                                         auto const encrypted = kdm.encrypt(
351                                                         signer, i->recipient.get(), i->trusted_device_thumbprints(), _output->formulation(),
352                                                         !_output->forensic_mark_video(), _output->forensic_mark_audio() ? boost::optional<int>() : 0
353                                                         );
354
355                                         dcp::NameFormat::Map name_values;
356                                         name_values['c'] = i->cinema->name;
357                                         name_values['s'] = i->name;
358                                         name_values['f'] = title;
359                                         name_values['b'] = begin.date() + " " + begin.time_of_day(true, false);
360                                         name_values['e'] = end.date() + " " + end.time_of_day(true, false);
361                                         name_values['i'] = encrypted.cpl_id ();
362
363                                         /* Encrypt */
364                                         kdms.push_back (
365                                                 KDMWithMetadataPtr(
366                                                         new KDMWithMetadata(name_values, i->cinema.get(), i->cinema->emails, encrypted)
367                                                         )
368                                                 );
369                                 }
370                         }
371
372                         if (kdms.empty()) {
373                                 return;
374                         }
375
376                         auto result = _output->make (
377                                 kdms, title, bind (&DOMFrame::confirm_overwrite, this, _1)
378                                 );
379
380                         if (result.first) {
381                                 JobManager::instance()->add (result.first);
382                                 if (_job_view) {
383                                         _job_view->Destroy ();
384                                         _job_view = 0;
385                                 }
386                                 _job_view = new JobViewDialog (this, _("Send KDM emails"), result.first);
387                                 _job_view->ShowModal ();
388                         }
389
390                         if (result.second > 0) {
391                                 /* XXX: proper plural form support in wxWidgets? */
392                                 wxString s = result.second == 1 ? _("%d KDM written to %s") : _("%d KDMs written to %s");
393                                 message_dialog (
394                                         this,
395                                         wxString::Format (s, result.second, std_to_wx(_output->directory().string()).data())
396                                         );
397                         }
398                 } catch (dcp::NotEncryptedError& e) {
399                         error_dialog (this, _("CPL's content is not encrypted."));
400                 } catch (exception& e) {
401                         error_dialog (this, std_to_wx(e.what()));
402                 } catch (...) {
403                         error_dialog (this, _("An unknown exception occurred."));
404                 }
405         }
406
407         void setup_sensitivity ()
408         {
409                 _screens->setup_sensitivity ();
410                 _output->setup_sensitivity ();
411                 wxArrayTreeItemIds sel;
412                 _dkdm->GetSelections (sel);
413                 shared_ptr<DKDMGroup> group = dynamic_pointer_cast<DKDMGroup>(selected_dkdm());
414                 shared_ptr<DKDM> dkdm = dynamic_pointer_cast<DKDM>(selected_dkdm());
415                 _create->Enable (!_screens->screens().empty() && sel.GetCount() > 0);
416                 _remove_dkdm->Enable (sel.GetCount() > 0 && (!group || group->name() != "root"));
417                 _export_dkdm->Enable (sel.GetCount() > 0 && dkdm);
418         }
419
420         void dkdm_begin_drag (wxTreeEvent& ev)
421         {
422                 ev.Allow ();
423         }
424
425         void dkdm_end_drag (wxTreeEvent& ev)
426         {
427                 auto from = _dkdm_id.find (_dkdm->GetSelection ());
428                 auto to = _dkdm_id.find (ev.GetItem ());
429                 if (from == _dkdm_id.end() || to == _dkdm_id.end() || from->first == to->first) {
430                         return;
431                 }
432
433                 auto group = dynamic_pointer_cast<DKDMGroup> (to->second);
434                 if (!group) {
435                         group = to->second->parent();
436                 }
437
438                 DCPOMATIC_ASSERT (group);
439                 DCPOMATIC_ASSERT (from->second->parent ());
440
441                 from->second->parent()->remove (from->second);
442                 add_dkdm_model (from->second, group, dynamic_pointer_cast<DKDM>(to->second));
443
444                 _dkdm->Delete (from->first);
445                 _dkdm_id.erase (from->first);
446                 add_dkdm_view (from->second, dynamic_pointer_cast<DKDM>(to->second) ? to->first : optional<wxTreeItemId>());
447         }
448
449         void add_dkdm_clicked ()
450         {
451                 auto d = new wxFileDialog (this, _("Select DKDM file"));
452                 if (d->ShowModal() == wxID_OK) {
453                         auto chain = Config::instance()->decryption_chain();
454                         DCPOMATIC_ASSERT (chain->key());
455
456                         try {
457                                 dcp::EncryptedKDM ekdm(dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE));
458                                 /* Decrypt the DKDM to make sure that we can */
459                                 dcp::DecryptedKDM dkdm(ekdm, chain->key().get());
460
461                                 shared_ptr<DKDMBase> new_dkdm(new DKDM(ekdm));
462                                 auto group = dynamic_pointer_cast<DKDMGroup> (selected_dkdm ());
463                                 if (!group) {
464                                         group = Config::instance()->dkdms ();
465                                 }
466                                 add_dkdm_model (new_dkdm, group);
467                                 add_dkdm_view (new_dkdm);
468                         } catch (dcp::KDMFormatError& e) {
469                                 error_dialog (
470                                         this,
471                                         _("Could not read file as a KDM.  Perhaps it is badly formatted, or not a KDM at all."),
472                                         std_to_wx(e.what())
473                                         );
474                                 return;
475                         } catch (dcp::KDMDecryptionError &) {
476                                 error_dialog (
477                                         this,
478                                         _("Could not decrypt the DKDM.  Perhaps it was not created with the correct certificate.")
479                                         );
480                         } catch (dcp::MiscError& e) {
481                                 error_dialog (
482                                         this,
483                                         _("Could not read file as a KDM.  It is much too large.  Make sure you are loading a DKDM (XML) file."),
484                                         std_to_wx(e.what())
485                                         );
486                         }
487                 }
488                 d->Destroy ();
489         }
490
491         void add_dkdm_folder_clicked ()
492         {
493                 auto d = new NewDKDMFolderDialog (this);
494                 if (d->ShowModal() == wxID_OK) {
495                         shared_ptr<DKDMBase> new_dkdm (new DKDMGroup (wx_to_std (d->get ())));
496                         auto parent = dynamic_pointer_cast<DKDMGroup> (selected_dkdm ());
497                         if (!parent) {
498                                 parent = Config::instance()->dkdms ();
499                         }
500                         add_dkdm_model (new_dkdm, parent);
501                         add_dkdm_view (new_dkdm);
502                 }
503                 d->Destroy ();
504         }
505
506         /** @param dkdm Thing to add.
507          *  @param parent Parent group, or 0.
508          */
509         void add_dkdm_view (shared_ptr<DKDMBase> base, optional<wxTreeItemId> previous = optional<wxTreeItemId>())
510         {
511                 if (!base->parent()) {
512                         /* This is the root group */
513                         _dkdm_id[_dkdm->AddRoot("root")] = base;
514                 } else {
515                         /* Add base to the view */
516                         wxTreeItemId added;
517                         if (previous) {
518                                 added = _dkdm->InsertItem(dkdm_to_id(base->parent()), *previous, std_to_wx(base->name()));
519                         } else {
520                                 added = _dkdm->AppendItem(dkdm_to_id(base->parent()), std_to_wx(base->name()));
521                         }
522                         _dkdm_id[added] = base;
523                 }
524
525                 /* Add children */
526                 auto g = dynamic_pointer_cast<DKDMGroup>(base);
527                 if (g) {
528                         for (auto i: g->children()) {
529                                 add_dkdm_view (i);
530                         }
531                 }
532         }
533
534         /** @param group Group to add dkdm to */
535         void add_dkdm_model (shared_ptr<DKDMBase> dkdm, shared_ptr<DKDMGroup> group, shared_ptr<DKDM> previous = shared_ptr<DKDM> ())
536         {
537                 group->add (dkdm, previous);
538                 /* We're messing with a Config-owned object here, so tell it that something has changed.
539                    This isn't nice.
540                 */
541                 Config::instance()->changed ();
542         }
543
544         wxTreeItemId dkdm_to_id (shared_ptr<DKDMBase> dkdm)
545         {
546                 for (auto const& i: _dkdm_id) {
547                         if (i.second == dkdm) {
548                                 return i.first;
549                         }
550                 }
551                 DCPOMATIC_ASSERT (false);
552         }
553
554         void remove_dkdm_clicked ()
555         {
556                 auto removed = selected_dkdm ();
557                 if (!removed) {
558                         return;
559                 }
560
561                 if (NagDialog::maybe_nag (
562                             this, Config::NAG_DELETE_DKDM,
563                             _("You are about to remove a DKDM.  This will make it impossible to decrypt the DCP that the DKDM was made for, and it cannot be undone.  "
564                               "Are you sure?"),
565                             true)) {
566                         return;
567                 }
568
569                 _dkdm->Delete (dkdm_to_id (removed));
570                 auto dkdms = Config::instance()->dkdms ();
571                 dkdms->remove (removed);
572                 Config::instance()->changed ();
573         }
574
575         void export_dkdm_clicked ()
576         {
577                 auto removed = selected_dkdm ();
578                 if (!removed) {
579                         return;
580                 }
581
582                 auto dkdm = dynamic_pointer_cast<DKDM>(removed);
583                 if (!dkdm) {
584                         return;
585                 }
586
587                 auto d = new wxFileDialog (
588                         this, _("Select DKDM File"), wxEmptyString, wxEmptyString, wxT("XML files (*.xml)|*.xml"),
589                         wxFD_SAVE | wxFD_OVERWRITE_PROMPT
590                         );
591
592                 if (d->ShowModal() == wxID_OK) {
593                         dkdm->dkdm().as_xml(wx_to_std(d->GetPath()));
594                 }
595                 d->Destroy ();
596         }
597
598         wxPreferencesEditor* _config_dialog;
599         ScreensPanel* _screens;
600         KDMTimingPanel* _timing;
601         wxTreeCtrl* _dkdm;
602         typedef std::map<wxTreeItemId, std::shared_ptr<DKDMBase>> DKDMMap;
603         DKDMMap _dkdm_id;
604         wxButton* _add_dkdm;
605         wxButton* _add_dkdm_folder;
606         wxButton* _remove_dkdm;
607         wxButton* _export_dkdm;
608         wxButton* _create;
609         KDMOutputPanel* _output;
610         JobViewDialog* _job_view;
611 };
612
613 /** @class App
614  *  @brief The magic App class for wxWidgets.
615  */
616 class App : public wxApp
617 {
618 public:
619         App ()
620                 : wxApp ()
621                 , _frame (0)
622         {}
623
624 private:
625
626         bool OnInit ()
627         {
628                 wxSplashScreen* splash = nullptr;
629
630                 try {
631                         wxInitAllImageHandlers ();
632
633                         Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
634                         Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
635
636                         splash = maybe_show_splash ();
637
638                         SetAppName (_("DCP-o-matic KDM Creator"));
639
640                         if (!wxApp::OnInit()) {
641                                 return false;
642                         }
643
644 #ifdef DCPOMATIC_LINUX
645                         unsetenv ("UBUNTU_MENUPROXY");
646 #endif
647
648 #ifdef DCPOMATIC_OSX
649                         make_foreground_application ();
650 #endif
651
652                         dcpomatic_setup_path_encoding ();
653
654                         /* Enable i18n; this will create a Config object
655                            to look for a force-configured language.  This Config
656                            object will be wrong, however, because dcpomatic_setup
657                            hasn't yet been called and there aren't any filters etc.
658                            set up yet.
659                         */
660                         dcpomatic_setup_i18n ();
661
662                         /* Set things up, including filters etc.
663                            which will now be internationalised correctly.
664                         */
665                         dcpomatic_setup ();
666
667                         /* Force the configuration to be re-loaded correctly next
668                            time it is needed.
669                         */
670                         Config::drop ();
671
672                         _frame = new DOMFrame (_("DCP-o-matic KDM Creator"));
673                         SetTopWindow (_frame);
674                         _frame->Maximize ();
675                         if (splash) {
676                                 splash->Destroy ();
677                                 splash = 0;
678                         }
679                         _frame->Show ();
680
681                         signal_manager = new wxSignalManager (this);
682                         Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
683                 }
684                 catch (exception& e)
685                 {
686                         if (splash) {
687                                 splash->Destroy ();
688                         }
689                         error_dialog (0, _("DCP-o-matic could not start"), std_to_wx(e.what()));
690                 }
691
692                 return true;
693         }
694
695         /* An unhandled exception has occurred inside the main event loop */
696         bool OnExceptionInMainLoop ()
697         {
698                 try {
699                         throw;
700                 } catch (FileError& e) {
701                         error_dialog (
702                                 0,
703                                 wxString::Format (
704                                         _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
705                                         std_to_wx (e.what()),
706                                         std_to_wx (e.file().string().c_str ())
707                                         )
708                                 );
709                 } catch (exception& e) {
710                         error_dialog (
711                                 0,
712                                 wxString::Format (
713                                         _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
714                                         std_to_wx (e.what ())
715                                         )
716                                 );
717                 } catch (...) {
718                         error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
719                 }
720
721                 /* This will terminate the program */
722                 return false;
723         }
724
725         void OnUnhandledException ()
726         {
727                 error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
728         }
729
730         void idle ()
731         {
732                 signal_manager->ui_idle ();
733         }
734
735         void config_failed_to_load ()
736         {
737                 message_dialog (_frame, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
738         }
739
740         void config_warning (string m)
741         {
742                 message_dialog (_frame, std_to_wx (m));
743         }
744
745         DOMFrame* _frame;
746 };
747
748 IMPLEMENT_APP (App)