Disable DKDM remove/export buttons as appropriate.
[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                 _export_dkdm = new Button (overall_panel, _("Export..."));
164                 dkdm_buttons->Add (_export_dkdm, 0, wxALL | wxEXPAND, DCPOMATIC_BUTTON_STACK_GAP);
165                 dkdm_sizer->Add (dkdm_buttons, 0, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
166                 right->Add (dkdm_sizer, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_Y_GAP);
167
168                 add_dkdm_view (Config::instance()->dkdms());
169
170                 h = new StaticText (overall_panel, _("Output"));
171                 h->SetFont (subheading_font);
172                 right->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
173                 /* XXX: hard-coded non-interop here */
174                 _output = new KDMOutputPanel (overall_panel, false);
175                 right->Add (_output, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
176
177                 _create = new Button (overall_panel, _("Create KDMs"));
178                 right->Add (_create, 0, wxALL, DCPOMATIC_SIZER_GAP);
179
180                 main_sizer->Add (horizontal, 1, wxALL | wxEXPAND, DCPOMATIC_DIALOG_BORDER);
181                 overall_panel->SetSizer (main_sizer);
182
183                 /* Instantly save any config changes when using a DCP-o-matic GUI */
184                 Config::instance()->Changed.connect (boost::bind (&Config::write, Config::instance ()));
185
186                 _screens->ScreensChanged.connect (boost::bind (&DOMFrame::setup_sensitivity, this));
187                 _create->Bind (wxEVT_BUTTON, bind (&DOMFrame::create_kdms, this));
188                 _dkdm->Bind (wxEVT_TREE_SEL_CHANGED, boost::bind (&DOMFrame::setup_sensitivity, this));
189                 _dkdm->Bind (wxEVT_TREE_BEGIN_DRAG, boost::bind (&DOMFrame::dkdm_begin_drag, this, _1));
190                 _dkdm->Bind (wxEVT_TREE_END_DRAG, boost::bind (&DOMFrame::dkdm_end_drag, this, _1));
191                 _add_dkdm->Bind (wxEVT_BUTTON, bind (&DOMFrame::add_dkdm_clicked, this));
192                 _add_dkdm_folder->Bind (wxEVT_BUTTON, bind (&DOMFrame::add_dkdm_folder_clicked, this));
193                 _remove_dkdm->Bind (wxEVT_BUTTON, bind (&DOMFrame::remove_dkdm_clicked, this));
194                 _export_dkdm->Bind (wxEVT_BUTTON, bind (&DOMFrame::export_dkdm_clicked, this));
195
196                 setup_sensitivity ();
197         }
198
199 private:
200         void file_exit ()
201         {
202                 /* false here allows the close handler to veto the close request */
203                 Close (false);
204         }
205
206         void edit_preferences ()
207         {
208                 if (!_config_dialog) {
209                         _config_dialog = create_full_config_dialog ();
210                 }
211                 _config_dialog->Show (this);
212         }
213
214         void help_about ()
215         {
216                 AboutDialog* d = new AboutDialog (this);
217                 d->ShowModal ();
218                 d->Destroy ();
219         }
220
221         void help_report_a_problem ()
222         {
223                 ReportProblemDialog* d = new ReportProblemDialog (this, shared_ptr<Film> ());
224                 if (d->ShowModal () == wxID_OK) {
225                         d->report ();
226                 }
227                 d->Destroy ();
228         }
229
230         void setup_menu (wxMenuBar* m)
231         {
232                 wxMenu* file = new wxMenu;
233
234 #ifdef __WXOSX__
235                 file->Append (wxID_EXIT, _("&Exit"));
236 #else
237                 file->Append (wxID_EXIT, _("&Quit"));
238 #endif
239
240 #ifdef __WXOSX__
241                 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
242 #else
243                 wxMenu* edit = new wxMenu;
244                 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
245 #endif
246
247                 wxMenu* help = new wxMenu;
248 #ifdef __WXOSX__
249                 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
250 #else
251                 help->Append (wxID_ABOUT, _("About"));
252 #endif
253                 help->Append (ID_help_report_a_problem, _("Report a problem..."));
254
255                 m->Append (file, _("&File"));
256 #ifndef __WXOSX__
257                 m->Append (edit, _("&Edit"));
258 #endif
259                 m->Append (help, _("&Help"));
260         }
261
262         bool confirm_overwrite (boost::filesystem::path path)
263         {
264                 return confirm_dialog (
265                         this,
266                         wxString::Format (_("File %s already exists.  Do you want to overwrite it?"), std_to_wx(path.string()).data())
267                         );
268         }
269
270         /** @id if not 0 this is filled in with the wxTreeItemId of the selection */
271         shared_ptr<DKDMBase> selected_dkdm (wxTreeItemId* id = 0) const
272         {
273                 wxArrayTreeItemIds selections;
274                 _dkdm->GetSelections (selections);
275                 if (selections.GetCount() != 1) {
276                         if (id) {
277                                 *id = 0;
278                         }
279                         return shared_ptr<DKDMBase> ();
280                 }
281
282                 if (id) {
283                         *id = selections[0];
284                 }
285
286                 DKDMMap::const_iterator i = _dkdm_id.find (selections[0]);
287                 if (i == _dkdm_id.end()) {
288                         return shared_ptr<DKDMBase> ();
289                 }
290
291                 return i->second;
292         }
293
294         void create_kdms ()
295         {
296                 try {
297                         shared_ptr<DKDMBase> dkdm_base = selected_dkdm ();
298                         if (!dkdm_base) {
299                                 return;
300                         }
301                         shared_ptr<DKDM> dkdm = boost::dynamic_pointer_cast<DKDM> (dkdm_base);
302                         if (!dkdm) {
303                                 return;
304                         }
305
306                         /* Decrypt the DKDM */
307                         dcp::DecryptedKDM decrypted (dkdm->dkdm(), Config::instance()->decryption_chain()->key().get());
308
309                         /* This is the signer for our new KDMs */
310                         shared_ptr<const dcp::CertificateChain> signer = Config::instance()->signer_chain ();
311                         if (!signer->valid ()) {
312                                 throw InvalidSignerError ();
313                         }
314
315                         list<ScreenKDM> screen_kdms;
316                         BOOST_FOREACH (shared_ptr<Screen> i, _screens->screens()) {
317
318                                 if (!i->recipient) {
319                                         continue;
320                                 }
321
322                                 /* Make an empty KDM */
323                                 dcp::DecryptedKDM kdm (
324                                         dcp::LocalTime (_timing->from(), i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute()),
325                                         dcp::LocalTime (_timing->until(), i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute()),
326                                         decrypted.annotation_text().get_value_or (""),
327                                         decrypted.content_title_text(),
328                                         dcp::LocalTime().as_string()
329                                         );
330
331                                 /* Add keys from the DKDM */
332                                 BOOST_FOREACH (dcp::DecryptedKDMKey const & j, decrypted.keys()) {
333                                         kdm.add_key (j);
334                                 }
335
336                                 /* Encrypt */
337                                 screen_kdms.push_back (
338                                         ScreenKDM (
339                                                 i,
340                                                 kdm.encrypt (
341                                                         signer, i->recipient.get(), i->trusted_device_thumbprints(), _output->formulation(),
342                                                         !_output->forensic_mark_video(), _output->forensic_mark_audio() ? boost::optional<int>() : 0
343                                                         )
344                                                 )
345                                         );
346                         }
347
348                         pair<shared_ptr<Job>, int> result = _output->make (
349                                 screen_kdms, decrypted.content_title_text(), _timing, bind (&DOMFrame::confirm_overwrite, this, _1)
350                                 );
351
352                         if (result.first) {
353                                 JobManager::instance()->add (result.first);
354                                 if (_job_view) {
355                                         _job_view->Destroy ();
356                                         _job_view = 0;
357                                 }
358                                 _job_view = new JobViewDialog (this, _("Send KDM emails"), result.first);
359                                 _job_view->ShowModal ();
360                         }
361
362                         if (result.second > 0) {
363                                 /* XXX: proper plural form support in wxWidgets? */
364                                 wxString s = result.second == 1 ? _("%d KDM written to %s") : _("%d KDMs written to %s");
365                                 message_dialog (
366                                         this,
367                                         wxString::Format (s, result.second, std_to_wx(_output->directory().string()).data())
368                                         );
369                         }
370                 } catch (dcp::NotEncryptedError& e) {
371                         error_dialog (this, _("CPL's content is not encrypted."));
372                 } catch (exception& e) {
373                         error_dialog (this, std_to_wx(e.what()));
374                 } catch (...) {
375                         error_dialog (this, _("An unknown exception occurred."));
376                 }
377         }
378
379         void setup_sensitivity ()
380         {
381                 _screens->setup_sensitivity ();
382                 _output->setup_sensitivity ();
383                 wxArrayTreeItemIds sel;
384                 _dkdm->GetSelections (sel);
385                 shared_ptr<DKDMGroup> group = dynamic_pointer_cast<DKDMGroup>(selected_dkdm());
386                 shared_ptr<DKDM> dkdm = dynamic_pointer_cast<DKDM>(selected_dkdm());
387                 _create->Enable (!_screens->screens().empty() && sel.GetCount() > 0);
388                 _remove_dkdm->Enable (sel.GetCount() > 0 && (!group || group->name() != "root"));
389                 _export_dkdm->Enable (sel.GetCount() > 0 && dkdm);
390         }
391
392         void dkdm_begin_drag (wxTreeEvent& ev)
393         {
394                 ev.Allow ();
395         }
396
397         void dkdm_end_drag (wxTreeEvent& ev)
398         {
399                 DKDMMap::iterator from = _dkdm_id.find (_dkdm->GetSelection ());
400                 DKDMMap::iterator to = _dkdm_id.find (ev.GetItem ());
401                 if (from == _dkdm_id.end() || to == _dkdm_id.end() || from->first == to->first) {
402                         return;
403                 }
404
405                 shared_ptr<DKDMGroup> group = dynamic_pointer_cast<DKDMGroup> (to->second);
406                 if (!group) {
407                         group = to->second->parent();
408                 }
409
410                 DCPOMATIC_ASSERT (group);
411                 DCPOMATIC_ASSERT (from->second->parent ());
412
413                 from->second->parent()->remove (from->second);
414                 add_dkdm_model (from->second, group, dynamic_pointer_cast<DKDM>(to->second));
415
416                 _dkdm->Delete (from->first);
417                 _dkdm_id.erase (from->first);
418                 add_dkdm_view (from->second, dynamic_pointer_cast<DKDM>(to->second) ? to->first : optional<wxTreeItemId>());
419         }
420
421         void add_dkdm_clicked ()
422         {
423                 wxFileDialog* d = new wxFileDialog (this, _("Select DKDM file"));
424                 if (d->ShowModal() == wxID_OK) {
425                         shared_ptr<DKDMBase> new_dkdm;
426                         try {
427                                 dcp::EncryptedKDM ekdm(dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE));
428
429                                 /* Decrypt the DKDM to make sure that we can */
430                                 shared_ptr<const dcp::CertificateChain> chain = Config::instance()->decryption_chain();
431                                 DCPOMATIC_ASSERT (chain->key());
432                                 dcp::DecryptedKDM dkdm(ekdm, chain->key().get());
433
434                                 new_dkdm.reset(new DKDM(ekdm));
435                                 shared_ptr<DKDMGroup> group = dynamic_pointer_cast<DKDMGroup> (selected_dkdm ());
436                                 if (!group) {
437                                         group = Config::instance()->dkdms ();
438                                 }
439                                 add_dkdm_model (new_dkdm, group);
440                                 add_dkdm_view (new_dkdm);
441                         } catch (dcp::KDMFormatError& e) {
442                                 error_dialog (
443                                         this,
444                                         _("Could not read file as a KDM.  Perhaps it is badly formatted, or not a KDM at all."),
445                                         std_to_wx(e.what())
446                                         );
447                                 return;
448                         } catch (dcp::KDMDecryptionError &) {
449                                 error_dialog (
450                                         this,
451                                         _("Could not decrypt the DKDM.  Perhaps it was not created with the correct certificate.")
452                                         );
453                         } catch (dcp::MiscError& e) {
454                                 error_dialog (
455                                         this,
456                                         _("Could not read file as a KDM.  It is much too large.  Make sure you are loading a DKDM (XML) file."),
457                                         std_to_wx(e.what())
458                                         );
459                         }
460                 }
461                 d->Destroy ();
462         }
463
464         void add_dkdm_folder_clicked ()
465         {
466                 NewDKDMFolderDialog* d = new NewDKDMFolderDialog (this);
467                 if (d->ShowModal() == wxID_OK) {
468                         shared_ptr<DKDMBase> new_dkdm (new DKDMGroup (wx_to_std (d->get ())));
469                         shared_ptr<DKDMGroup> parent = dynamic_pointer_cast<DKDMGroup> (selected_dkdm ());
470                         if (!parent) {
471                                 parent = Config::instance()->dkdms ();
472                         }
473                         add_dkdm_model (new_dkdm, parent);
474                         add_dkdm_view (new_dkdm);
475                 }
476                 d->Destroy ();
477         }
478
479         /** @param dkdm Thing to add.
480          *  @param parent Parent group, or 0.
481          */
482         void add_dkdm_view (shared_ptr<DKDMBase> base, optional<wxTreeItemId> previous = optional<wxTreeItemId>())
483         {
484                 if (!base->parent()) {
485                         /* This is the root group */
486                         _dkdm_id[_dkdm->AddRoot("root")] = base;
487                 } else {
488                         /* Add base to the view */
489                         wxTreeItemId added;
490                         if (previous) {
491                                 added = _dkdm->InsertItem(dkdm_to_id(base->parent()), *previous, std_to_wx(base->name()));
492                         } else {
493                                 added = _dkdm->AppendItem(dkdm_to_id(base->parent()), std_to_wx(base->name()));
494                         }
495                         _dkdm_id[added] = base;
496                 }
497
498                 /* Add children */
499                 shared_ptr<DKDMGroup> g = dynamic_pointer_cast<DKDMGroup> (base);
500                 if (g) {
501                         BOOST_FOREACH (shared_ptr<DKDMBase> i, g->children()) {
502                                 add_dkdm_view (i);
503                         }
504                 }
505         }
506
507         /** @param group Group to add dkdm to */
508         void add_dkdm_model (shared_ptr<DKDMBase> dkdm, shared_ptr<DKDMGroup> group, shared_ptr<DKDM> previous = shared_ptr<DKDM> ())
509         {
510                 group->add (dkdm, previous);
511                 /* We're messing with a Config-owned object here, so tell it that something has changed.
512                    This isn't nice.
513                 */
514                 Config::instance()->changed ();
515         }
516
517         wxTreeItemId dkdm_to_id (shared_ptr<DKDMBase> dkdm)
518         {
519                 for (DKDMMap::iterator i = _dkdm_id.begin(); i != _dkdm_id.end(); ++i) {
520                         if (i->second == dkdm) {
521                                 return i->first;
522                         }
523                 }
524                 DCPOMATIC_ASSERT (false);
525         }
526
527         void remove_dkdm_clicked ()
528         {
529                 shared_ptr<DKDMBase> removed = selected_dkdm ();
530                 if (!removed) {
531                         return;
532                 }
533
534                 if (NagDialog::maybe_nag (
535                             this, Config::NAG_DELETE_DKDM,
536                             _("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.  "
537                               "Are you sure?"),
538                             true)) {
539                         return;
540                 }
541
542                 _dkdm->Delete (dkdm_to_id (removed));
543                 shared_ptr<DKDMGroup> dkdms = Config::instance()->dkdms ();
544                 dkdms->remove (removed);
545                 Config::instance()->changed ();
546         }
547
548         void export_dkdm_clicked ()
549         {
550                 shared_ptr<DKDMBase> removed = selected_dkdm ();
551                 if (!removed) {
552                         return;
553                 }
554
555                 shared_ptr<DKDM> dkdm = dynamic_pointer_cast<DKDM>(removed);
556                 if (!dkdm) {
557                         return;
558                 }
559
560                 wxFileDialog* d = new wxFileDialog (
561                         this, _("Select DKDM File"), wxEmptyString, wxEmptyString, wxT("XML files (*.xml)|*.xml"),
562                         wxFD_SAVE | wxFD_OVERWRITE_PROMPT
563                         );
564
565                 if (d->ShowModal() == wxID_OK) {
566                         dkdm->dkdm().as_xml(wx_to_std(d->GetPath()));
567                 }
568                 d->Destroy ();
569         }
570
571         wxPreferencesEditor* _config_dialog;
572         ScreensPanel* _screens;
573         KDMTimingPanel* _timing;
574         wxTreeCtrl* _dkdm;
575         typedef std::map<wxTreeItemId, boost::shared_ptr<DKDMBase> > DKDMMap;
576         DKDMMap _dkdm_id;
577         wxButton* _add_dkdm;
578         wxButton* _add_dkdm_folder;
579         wxButton* _remove_dkdm;
580         wxButton* _export_dkdm;
581         wxButton* _create;
582         KDMOutputPanel* _output;
583         JobViewDialog* _job_view;
584 };
585
586 /** @class App
587  *  @brief The magic App class for wxWidgets.
588  */
589 class App : public wxApp
590 {
591 public:
592         App ()
593                 : wxApp ()
594                 , _frame (0)
595         {}
596
597 private:
598
599         bool OnInit ()
600         {
601                 wxSplashScreen* splash = 0;
602
603                 try {
604                         wxInitAllImageHandlers ();
605
606                         Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
607                         Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
608
609                         splash = maybe_show_splash ();
610
611                         SetAppName (_("DCP-o-matic KDM Creator"));
612
613                         if (!wxApp::OnInit()) {
614                                 return false;
615                         }
616
617 #ifdef DCPOMATIC_LINUX
618                         unsetenv ("UBUNTU_MENUPROXY");
619 #endif
620
621 #ifdef __WXOSX__
622                         ProcessSerialNumber serial;
623                         GetCurrentProcess (&serial);
624                         TransformProcessType (&serial, kProcessTransformToForegroundApplication);
625 #endif
626
627                         dcpomatic_setup_path_encoding ();
628
629                         /* Enable i18n; this will create a Config object
630                            to look for a force-configured language.  This Config
631                            object will be wrong, however, because dcpomatic_setup
632                            hasn't yet been called and there aren't any filters etc.
633                            set up yet.
634                         */
635                         dcpomatic_setup_i18n ();
636
637                         /* Set things up, including filters etc.
638                            which will now be internationalised correctly.
639                         */
640                         dcpomatic_setup ();
641
642                         /* Force the configuration to be re-loaded correctly next
643                            time it is needed.
644                         */
645                         Config::drop ();
646
647                         _frame = new DOMFrame (_("DCP-o-matic KDM Creator"));
648                         SetTopWindow (_frame);
649                         _frame->Maximize ();
650                         if (splash) {
651                                 splash->Destroy ();
652                                 splash = 0;
653                         }
654                         _frame->Show ();
655
656                         signal_manager = new wxSignalManager (this);
657                         Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
658                 }
659                 catch (exception& e)
660                 {
661                         if (splash) {
662                                 splash->Destroy ();
663                         }
664                         error_dialog (0, _("DCP-o-matic could not start"), std_to_wx(e.what()));
665                 }
666
667                 return true;
668         }
669
670         /* An unhandled exception has occurred inside the main event loop */
671         bool OnExceptionInMainLoop ()
672         {
673                 try {
674                         throw;
675                 } catch (FileError& e) {
676                         error_dialog (
677                                 0,
678                                 wxString::Format (
679                                         _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
680                                         std_to_wx (e.what()),
681                                         std_to_wx (e.file().string().c_str ())
682                                         )
683                                 );
684                 } catch (exception& e) {
685                         error_dialog (
686                                 0,
687                                 wxString::Format (
688                                         _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
689                                         std_to_wx (e.what ())
690                                         )
691                                 );
692                 } catch (...) {
693                         error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
694                 }
695
696                 /* This will terminate the program */
697                 return false;
698         }
699
700         void OnUnhandledException ()
701         {
702                 error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
703         }
704
705         void idle ()
706         {
707                 signal_manager->ui_idle ();
708         }
709
710         void config_failed_to_load ()
711         {
712                 message_dialog (_frame, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
713         }
714
715         void config_warning (string m)
716         {
717                 message_dialog (_frame, std_to_wx (m));
718         }
719
720         DOMFrame* _frame;
721 };
722
723 IMPLEMENT_APP (App)