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