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