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