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