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