56c1db7b106da239a88cccefe7e1c3df759bb5c6
[dcpomatic.git] / src / tools / dcpomatic.cc
1 /*
2     Copyright (C) 2012-2021 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 /** @file  src/tools/dcpomatic.cc
22  *  @brief The main DCP-o-matic GUI.
23  */
24
25 #include "wx/standard_controls.h"
26 #include "wx/film_viewer.h"
27 #include "wx/film_editor.h"
28 #include "wx/job_manager_view.h"
29 #include "wx/full_config_dialog.h"
30 #include "wx/wx_util.h"
31 #include "wx/film_name_location_dialog.h"
32 #include "wx/wx_signal_manager.h"
33 #include "wx/recreate_chain_dialog.h"
34 #include "wx/about_dialog.h"
35 #include "wx/kdm_dialog.h"
36 #include "wx/dkdm_dialog.h"
37 #include "wx/self_dkdm_dialog.h"
38 #include "wx/servers_list_dialog.h"
39 #include "wx/hints_dialog.h"
40 #include "wx/update_dialog.h"
41 #include "wx/content_panel.h"
42 #include "wx/report_problem_dialog.h"
43 #include "wx/video_waveform_dialog.h"
44 #include "wx/system_information_dialog.h"
45 #include "wx/save_template_dialog.h"
46 #include "wx/templates_dialog.h"
47 #include "wx/nag_dialog.h"
48 #include "wx/export_subtitles_dialog.h"
49 #include "wx/export_video_file_dialog.h"
50 #include "wx/paste_dialog.h"
51 #include "wx/focus_manager.h"
52 #include "wx/html_dialog.h"
53 #include "wx/send_i18n_dialog.h"
54 #include "wx/i18n_hook.h"
55 #include "lib/film.h"
56 #include "lib/analytics.h"
57 #include "lib/emailer.h"
58 #include "lib/config.h"
59 #include "lib/util.h"
60 #include "lib/video_content.h"
61 #include "lib/content.h"
62 #include "lib/version.h"
63 #include "lib/signal_manager.h"
64 #include "lib/log.h"
65 #include "lib/screen.h"
66 #include "lib/job_manager.h"
67 #include "lib/exceptions.h"
68 #include "lib/cinema.h"
69 #include "lib/kdm_with_metadata.h"
70 #include "lib/send_kdm_email_job.h"
71 #include "lib/encode_server_finder.h"
72 #include "lib/update_checker.h"
73 #include "lib/cross.h"
74 #include "lib/content_factory.h"
75 #include "lib/compose.hpp"
76 #include "lib/dcpomatic_socket.h"
77 #include "lib/hints.h"
78 #include "lib/dcp_content.h"
79 #include "lib/ffmpeg_encoder.h"
80 #include "lib/transcode_job.h"
81 #include "lib/dkdm_wrapper.h"
82 #include "lib/audio_content.h"
83 #include "lib/check_content_change_job.h"
84 #include "lib/text_content.h"
85 #include "lib/dcpomatic_log.h"
86 #include "lib/subtitle_encoder.h"
87 #include "lib/warnings.h"
88 #include <dcp/exceptions.h>
89 #include <dcp/raw_convert.h>
90 DCPOMATIC_DISABLE_WARNINGS
91 #include <wx/generic/aboutdlgg.h>
92 #include <wx/stdpaths.h>
93 #include <wx/cmdline.h>
94 #include <wx/preferences.h>
95 #include <wx/splash.h>
96 #include <wx/wxhtml.h>
97 DCPOMATIC_ENABLE_WARNINGS
98 #ifdef __WXGTK__
99 #include <X11/Xlib.h>
100 #endif
101 #ifdef __WXMSW__
102 #include <shellapi.h>
103 #endif
104 #include <boost/filesystem.hpp>
105 #include <boost/algorithm/string.hpp>
106 #include <iostream>
107 #include <fstream>
108 /* This is OK as it's only used with DCPOMATIC_WINDOWS */
109 #include <sstream>
110
111 #ifdef check
112 #undef check
113 #endif
114
115 using std::cout;
116 using std::wcout;
117 using std::string;
118 using std::vector;
119 using std::wstring;
120 using std::wstringstream;
121 using std::map;
122 using std::make_pair;
123 using std::list;
124 using std::exception;
125 using std::make_shared;
126 using std::shared_ptr;
127 using std::dynamic_pointer_cast;
128 using boost::optional;
129 using boost::function;
130 using boost::is_any_of;
131 using boost::algorithm::find;
132 #if BOOST_VERSION >= 106100
133 using namespace boost::placeholders;
134 #endif
135 using dcp::raw_convert;
136
137 class FilmChangedClosingDialog
138 {
139 public:
140         explicit FilmChangedClosingDialog (string name)
141         {
142                 _dialog = new wxMessageDialog (
143                         0,
144                         wxString::Format (_("Save changes to film \"%s\" before closing?"), std_to_wx (name).data()),
145                         /// TRANSLATORS: this is the heading for a dialog box, which tells the user that the current
146                         /// project (Film) has been changed since it was last saved.
147                         _("Film changed"),
148                         wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_QUESTION
149                         );
150
151                 _dialog->SetYesNoCancelLabels (
152                         _("Save film and close"), _("Close without saving film"), _("Don't close")
153                         );
154         }
155
156         ~FilmChangedClosingDialog ()
157         {
158                 _dialog->Destroy ();
159         }
160
161         FilmChangedClosingDialog (FilmChangedClosingDialog const&) = delete;
162         FilmChangedClosingDialog& operator= (FilmChangedClosingDialog const&) = delete;
163
164         int run ()
165         {
166                 return _dialog->ShowModal ();
167         }
168
169 private:
170         wxMessageDialog* _dialog;
171 };
172
173
174 class FilmChangedDuplicatingDialog
175 {
176 public:
177         explicit FilmChangedDuplicatingDialog (string name)
178         {
179                 _dialog = new wxMessageDialog (
180                         0,
181                         wxString::Format (_("Save changes to film \"%s\" before duplicating?"), std_to_wx (name).data()),
182                         /// TRANSLATORS: this is the heading for a dialog box, which tells the user that the current
183                         /// project (Film) has been changed since it was last saved.
184                         _("Film changed"),
185                         wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_QUESTION
186                         );
187
188                 _dialog->SetYesNoCancelLabels (
189                         _("Save film and duplicate"), _("Duplicate without saving film"), _("Don't duplicate")
190                         );
191         }
192
193         ~FilmChangedDuplicatingDialog ()
194         {
195                 _dialog->Destroy ();
196         }
197
198         FilmChangedDuplicatingDialog (FilmChangedDuplicatingDialog const&) = delete;
199         FilmChangedDuplicatingDialog& operator= (FilmChangedDuplicatingDialog const&) = delete;
200
201         int run ()
202         {
203                 return _dialog->ShowModal ();
204         }
205
206 private:
207         wxMessageDialog* _dialog;
208 };
209
210 #define ALWAYS                        0x0
211 #define NEEDS_FILM                    0x1
212 #define NOT_DURING_DCP_CREATION       0x2
213 #define NEEDS_CPL                     0x4
214 #define NEEDS_SINGLE_SELECTED_CONTENT 0x8
215 #define NEEDS_SELECTED_CONTENT        0x10
216 #define NEEDS_SELECTED_VIDEO_CONTENT  0x20
217 #define NEEDS_CLIPBOARD               0x40
218 #define NEEDS_ENCRYPTION              0x80
219
220 map<wxMenuItem*, int> menu_items;
221
222 enum {
223         ID_file_new = 1,
224         ID_file_open,
225         ID_file_save,
226         ID_file_save_as_template,
227         ID_file_duplicate,
228         ID_file_duplicate_and_open,
229         ID_file_history,
230         /* Allow spare IDs after _history for the recent files list */
231         ID_file_close = 100,
232         ID_edit_copy,
233         ID_edit_paste,
234         ID_edit_select_all,
235         ID_jobs_make_dcp,
236         ID_jobs_make_dcp_batch,
237         ID_jobs_make_kdms,
238         ID_jobs_make_dkdms,
239         ID_jobs_make_self_dkdm,
240         ID_jobs_export_video_file,
241         ID_jobs_export_subtitles,
242         ID_jobs_send_dcp_to_tms,
243         ID_jobs_show_dcp,
244         ID_jobs_open_dcp_in_player,
245         ID_view_closed_captions,
246         ID_view_video_waveform,
247         ID_tools_hints,
248         ID_tools_encoding_servers,
249         ID_tools_manage_templates,
250         ID_tools_check_for_updates,
251         ID_tools_send_translations,
252         ID_tools_system_information,
253         ID_tools_restore_default_preferences,
254         ID_help_report_a_problem,
255         /* IDs for shortcuts (with no associated menu item) */
256         ID_add_file,
257         ID_remove,
258         ID_start_stop,
259         ID_timeline,
260         ID_back_frame,
261         ID_forward_frame
262 };
263
264 class DOMFrame : public wxFrame
265 {
266 public:
267         explicit DOMFrame (wxString const & title)
268                 : wxFrame (NULL, -1, title)
269                 , _video_waveform_dialog (0)
270                 , _system_information_dialog (0)
271                 , _hints_dialog (0)
272                 , _servers_list_dialog (0)
273                 , _config_dialog (0)
274                 , _kdm_dialog (0)
275                 , _dkdm_dialog (0)
276                 , _templates_dialog (0)
277                 , _file_menu (0)
278                 , _history_items (0)
279                 , _history_position (0)
280                 , _history_separator (0)
281                 , _update_news_requested (false)
282                 , _first_shown_called (false)
283         {
284 #if defined(DCPOMATIC_WINDOWS)
285                 if (Config::instance()->win32_console ()) {
286                         AllocConsole();
287
288                         HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
289                         int hCrt = _open_osfhandle((intptr_t) handle_out, _O_TEXT);
290                         FILE* hf_out = _fdopen(hCrt, "w");
291                         setvbuf(hf_out, NULL, _IONBF, 1);
292                         *stdout = *hf_out;
293
294                         HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
295                         hCrt = _open_osfhandle((intptr_t) handle_in, _O_TEXT);
296                         FILE* hf_in = _fdopen(hCrt, "r");
297                         setvbuf(hf_in, NULL, _IONBF, 128);
298                         *stdin = *hf_in;
299
300                         cout << "DCP-o-matic is starting." << "\n";
301                 }
302 #endif
303
304                 auto bar = new wxMenuBar;
305                 setup_menu (bar);
306                 SetMenuBar (bar);
307
308 #ifdef DCPOMATIC_WINDOWS
309                 SetIcon (wxIcon (std_to_wx ("id")));
310 #endif
311
312                 _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this, _1));
313                 config_changed (Config::OTHER);
314
315                 _analytics_message_connection = Analytics::instance()->Message.connect(boost::bind(&DOMFrame::analytics_message, this, _1, _2));
316
317                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_new, this),                ID_file_new);
318                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this),               ID_file_open);
319                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_save, this),               ID_file_save);
320                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_save_as_template, this),   ID_file_save_as_template);
321                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_duplicate, this),          ID_file_duplicate);
322                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_duplicate_and_open, this), ID_file_duplicate_and_open);
323                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_close, this),              ID_file_close);
324                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_history, this, _1),        ID_file_history, ID_file_history + HISTORY_SIZE);
325                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this),               wxID_EXIT);
326                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_copy, this),               ID_edit_copy);
327                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_paste, this),              ID_edit_paste);
328                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_select_all, this),         ID_edit_select_all);
329                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this),        wxID_PREFERENCES);
330                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::jobs_make_dcp, this),           ID_jobs_make_dcp);
331                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::jobs_make_kdms, this),          ID_jobs_make_kdms);
332                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::jobs_make_dkdms, this),         ID_jobs_make_dkdms);
333                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::jobs_make_dcp_batch, this),     ID_jobs_make_dcp_batch);
334                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::jobs_make_self_dkdm, this),     ID_jobs_make_self_dkdm);
335                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::jobs_export_video_file, this),  ID_jobs_export_video_file);
336                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::jobs_export_subtitles, this),   ID_jobs_export_subtitles);
337                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::jobs_send_dcp_to_tms, this),    ID_jobs_send_dcp_to_tms);
338                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::jobs_show_dcp, this),           ID_jobs_show_dcp);
339                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::jobs_open_dcp_in_player, this), ID_jobs_open_dcp_in_player);
340                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::view_closed_captions, this),    ID_view_closed_captions);
341                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::view_video_waveform, this),     ID_view_video_waveform);
342                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_hints, this),             ID_tools_hints);
343                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_encoding_servers, this),  ID_tools_encoding_servers);
344                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_manage_templates, this),  ID_tools_manage_templates);
345                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
346                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_send_translations, this), ID_tools_send_translations);
347                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_system_information, this),ID_tools_system_information);
348                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_restore_default_preferences, this), ID_tools_restore_default_preferences);
349                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this),              wxID_ABOUT);
350                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this),   ID_help_report_a_problem);
351
352                 Bind (wxEVT_CLOSE_WINDOW, boost::bind (&DOMFrame::close, this, _1));
353                 Bind (wxEVT_SHOW, boost::bind (&DOMFrame::show, this, _1));
354
355                 /* Use a panel as the only child of the Frame so that we avoid
356                    the dark-grey background on Windows.
357                 */
358                 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
359
360                 _film_viewer.reset (new FilmViewer (overall_panel));
361                 _controls = new StandardControls (overall_panel, _film_viewer, true);
362                 _film_editor = new FilmEditor (overall_panel, _film_viewer);
363                 auto job_manager_view = new JobManagerView (overall_panel, false);
364
365                 auto right_sizer = new wxBoxSizer (wxVERTICAL);
366                 right_sizer->Add (_film_viewer->panel(), 2, wxEXPAND | wxALL, 6);
367                 right_sizer->Add (_controls, 0, wxEXPAND | wxALL, 6);
368                 right_sizer->Add (job_manager_view, 1, wxEXPAND | wxALL, 6);
369
370                 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
371                 main_sizer->Add (_film_editor, 0, wxEXPAND | wxALL, 6);
372                 main_sizer->Add (right_sizer, 1, wxEXPAND | wxALL, 6);
373
374                 set_menu_sensitivity ();
375
376                 _film_editor->FileChanged.connect (bind (&DOMFrame::file_changed, this, _1));
377                 _film_editor->content_panel()->SelectionChanged.connect (boost::bind (&DOMFrame::set_menu_sensitivity, this));
378                 file_changed ("");
379
380                 JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&DOMFrame::set_menu_sensitivity, this));
381
382                 overall_panel->SetSizer (main_sizer);
383
384                 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
385
386                 FocusManager::instance()->SetFocus.connect (boost::bind (&DOMFrame::remove_accelerators, this));
387                 FocusManager::instance()->KillFocus.connect (boost::bind (&DOMFrame::add_accelerators, this));
388                 add_accelerators ();
389         }
390
391         void add_accelerators ()
392         {
393 #ifdef __WXOSX__
394                 int accelerators = 7;
395 #else
396                 int accelerators = 6;
397 #endif
398                 wxAcceleratorEntry* accel = new wxAcceleratorEntry[accelerators];
399                 accel[0].Set (wxACCEL_CTRL, static_cast<int>('A'), ID_add_file);
400                 accel[1].Set (wxACCEL_NORMAL, WXK_DELETE, ID_remove);
401                 accel[2].Set (wxACCEL_NORMAL, WXK_SPACE, ID_start_stop);
402                 accel[3].Set (wxACCEL_CTRL, static_cast<int>('T'), ID_timeline);
403                 accel[4].Set (wxACCEL_NORMAL, WXK_LEFT, ID_back_frame);
404                 accel[5].Set (wxACCEL_NORMAL, WXK_RIGHT, ID_forward_frame);
405 #ifdef __WXOSX__
406                 accel[6].Set (wxACCEL_CTRL, static_cast<int>('W'), ID_file_close);
407 #endif
408                 Bind (wxEVT_MENU, boost::bind (&ContentPanel::add_file_clicked, _film_editor->content_panel()), ID_add_file);
409                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::remove_clicked, this, _1), ID_remove);
410                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::start_stop_pressed, this), ID_start_stop);
411                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::timeline_pressed, this), ID_timeline);
412                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::back_frame, this), ID_back_frame);
413                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::forward_frame, this), ID_forward_frame);
414                 wxAcceleratorTable accel_table (accelerators, accel);
415                 SetAcceleratorTable (accel_table);
416                 delete[] accel;
417         }
418
419         void remove_accelerators ()
420         {
421                 SetAcceleratorTable (wxAcceleratorTable ());
422         }
423
424         void remove_clicked (wxCommandEvent& ev)
425         {
426                 if (_film_editor->content_panel()->remove_clicked (true)) {
427                         ev.Skip ();
428                 }
429         }
430
431         void new_film (boost::filesystem::path path, optional<string> template_name)
432         {
433                 auto film = make_shared<Film>(path);
434                 if (template_name) {
435                         film->use_template (template_name.get());
436                 }
437                 film->set_name (path.filename().generic_string());
438                 film->write_metadata ();
439                 set_film (film);
440         }
441
442         void load_film (boost::filesystem::path file)
443         try
444         {
445                 auto film = make_shared<Film>(file);
446                 auto const notes = film->read_metadata ();
447
448                 if (film->state_version() == 4) {
449                         error_dialog (
450                                 0,
451                                 _("This film was created with an old version of DVD-o-matic and may not load correctly "
452                                   "in this version.  Please check the film's settings carefully.")
453                                 );
454                 }
455
456                 for (auto i: notes) {
457                         error_dialog (0, std_to_wx(i));
458                 }
459
460                 set_film (film);
461
462                 JobManager::instance()->add(shared_ptr<Job>(new CheckContentChangeJob(film)));
463         }
464         catch (FileNotFoundError& e) {
465                 auto const dir = e.file().parent_path();
466                 if (boost::filesystem::exists(dir / "ASSETMAP") || boost::filesystem::exists(dir / "ASSETMAP.xml")) {
467                         error_dialog (
468                                 this, _("Could not open this folder as a DCP-o-matic project."),
469                                 _("It looks like you are trying to open a DCP.  File -> Open is for loading DCP-o-matic projects, not DCPs.  To import a DCP, create a new project with File -> New and then click the \"Add DCP...\" button.")
470                                 );
471                 } else {
472                         auto const p = std_to_wx(file.string ());
473                         error_dialog (this, wxString::Format(_("Could not open film at %s"), p.data()), std_to_wx(e.what()));
474                 }
475
476         } catch (std::exception& e) {
477                 auto const p = std_to_wx (file.string());
478                 error_dialog (this, wxString::Format(_("Could not open film at %s"), p.data()), std_to_wx(e.what()));
479         }
480
481         void set_film (shared_ptr<Film> film)
482         {
483                 _film = film;
484                 _film_viewer->set_film (_film);
485                 _film_editor->set_film (_film);
486                 _controls->set_film (_film);
487                 if (_video_waveform_dialog) {
488                         _video_waveform_dialog->Destroy ();
489                         _video_waveform_dialog = 0;
490                 }
491                 set_menu_sensitivity ();
492                 if (_film && _film->directory()) {
493                         Config::instance()->add_to_history (_film->directory().get());
494                 }
495                 if (_film) {
496                         _film->Change.connect (boost::bind (&DOMFrame::film_change, this, _1));
497                         _film->Message.connect (boost::bind(&DOMFrame::film_message, this, _1));
498                         dcpomatic_log = _film->log ();
499                 }
500         }
501
502         shared_ptr<Film> film () const {
503                 return _film;
504         }
505
506 private:
507
508         void show (wxShowEvent& ev)
509         {
510                 if (ev.IsShown() && !_first_shown_called) {
511                         _film_editor->first_shown ();
512                         _first_shown_called = true;
513                 }
514         }
515
516         void film_message (string m)
517         {
518                 message_dialog (this, std_to_wx(m));
519         }
520
521         void film_change (ChangeType type)
522         {
523                 if (type == ChangeType::DONE) {
524                         set_menu_sensitivity ();
525                 }
526         }
527
528         void file_changed (boost::filesystem::path f)
529         {
530                 auto s = wx_to_std(_("DCP-o-matic"));
531                 if (!f.empty ()) {
532                         s += " - " + f.string ();
533                 }
534
535                 SetTitle (std_to_wx (s));
536         }
537
538         void file_new ()
539         {
540                 auto d = new FilmNameLocationDialog (this, _("New Film"), true);
541                 int const r = d->ShowModal ();
542
543                 if (r == wxID_OK && d->check_path() && maybe_save_then_delete_film<FilmChangedClosingDialog>()) {
544                         try {
545                                 new_film (d->path(), d->template_name());
546                         } catch (boost::filesystem::filesystem_error& e) {
547 #ifdef DCPOMATIC_WINDOWS
548                                 string bad_chars = "<>:\"/|?*";
549                                 string const filename = d->path().filename().string();
550                                 string found_bad_chars;
551                                 for (size_t i = 0; i < bad_chars.length(); ++i) {
552                                         if (filename.find(bad_chars[i]) != string::npos && found_bad_chars.find(bad_chars[i]) == string::npos) {
553                                                 found_bad_chars += bad_chars[i];
554                                         }
555                                 }
556                                 wxString message = _("Could not create folder to store film.");
557                                 message += "  ";
558                                 if (!found_bad_chars.empty()) {
559                                         message += wxString::Format (_("Try removing the %s characters from your folder name."), std_to_wx(found_bad_chars).data());
560                                 } else {
561                                         message += _("Please check that you do not have Windows controlled folder access enabled for DCP-o-matic.");
562                                 }
563                                 error_dialog (this, message, std_to_wx(e.what()));
564 #else
565                                 error_dialog (this, _("Could not create folder to store film."), std_to_wx(e.what()));
566 #endif
567                         }
568                 }
569
570                 d->Destroy ();
571         }
572
573         void file_open ()
574         {
575                 auto c = new wxDirDialog (
576                         this,
577                         _("Select film to open"),
578                         std_to_wx (Config::instance()->default_directory_or (wx_to_std (wxStandardPaths::Get().GetDocumentsDir())).string ()),
579                         wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
580                         );
581
582                 int r;
583                 while (true) {
584                         r = c->ShowModal ();
585                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
586                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
587                         } else {
588                                 break;
589                         }
590                 }
591
592                 if (r == wxID_OK && maybe_save_then_delete_film<FilmChangedClosingDialog>()) {
593                         load_film (wx_to_std (c->GetPath ()));
594                 }
595
596                 c->Destroy ();
597         }
598
599         void file_save ()
600         {
601                 _film->write_metadata ();
602         }
603
604         void file_save_as_template ()
605         {
606                 auto d = new SaveTemplateDialog (this);
607                 int const r = d->ShowModal ();
608                 if (r == wxID_OK) {
609                         Config::instance()->save_template (_film, d->name ());
610                 }
611                 d->Destroy ();
612         }
613
614         void file_duplicate ()
615         {
616                 auto d = new FilmNameLocationDialog (this, _("Duplicate Film"), false);
617                 int const r = d->ShowModal ();
618
619                 if (r == wxID_OK && d->check_path() && maybe_save_film<FilmChangedDuplicatingDialog>()) {
620                         shared_ptr<Film> film (new Film (d->path()));
621                         film->copy_from (_film);
622                         film->set_name (d->path().filename().generic_string());
623                         film->write_metadata ();
624                 }
625
626                 d->Destroy ();
627         }
628
629         void file_duplicate_and_open ()
630         {
631                 auto d = new FilmNameLocationDialog (this, _("Duplicate Film"), false);
632                 int const r = d->ShowModal ();
633
634                 if (r == wxID_OK && d->check_path() && maybe_save_film<FilmChangedDuplicatingDialog>()) {
635                         shared_ptr<Film> film (new Film (d->path()));
636                         film->copy_from (_film);
637                         film->set_name (d->path().filename().generic_string());
638                         film->write_metadata ();
639                         set_film (film);
640                 }
641
642                 d->Destroy ();
643         }
644
645         void file_close ()
646         {
647                 if (_film && _film->dirty ()) {
648
649                         auto dialog = new FilmChangedClosingDialog (_film->name ());
650                         int const r = dialog->run ();
651                         delete dialog;
652
653                         switch (r) {
654                         case wxID_NO:
655                                 /* Don't save and carry on to close */
656                                 break;
657                         case wxID_YES:
658                                 /* Save and carry on to close */
659                                 _film->write_metadata ();
660                                 break;
661                         case wxID_CANCEL:
662                                 /* Stop */
663                                 return;
664                         }
665                 }
666
667                 set_film (shared_ptr<Film>());
668         }
669
670         void file_history (wxCommandEvent& event)
671         {
672                 auto history = Config::instance()->history ();
673                 int n = event.GetId() - ID_file_history;
674                 if (n >= 0 && n < static_cast<int> (history.size ()) && maybe_save_then_delete_film<FilmChangedClosingDialog>()) {
675                         load_film (history[n]);
676                 }
677         }
678
679         void file_exit ()
680         {
681                 /* false here allows the close handler to veto the close request */
682                 Close (false);
683         }
684
685         void edit_copy ()
686         {
687                 auto const sel = _film_editor->content_panel()->selected();
688                 DCPOMATIC_ASSERT (sel.size() == 1);
689                 _clipboard = sel.front()->clone();
690         }
691
692         void edit_paste ()
693         {
694                 DCPOMATIC_ASSERT (_clipboard);
695
696                 auto d = new PasteDialog (this, static_cast<bool>(_clipboard->video), static_cast<bool>(_clipboard->audio), !_clipboard->text.empty());
697                 if (d->ShowModal() == wxID_OK) {
698                         for (auto i: _film_editor->content_panel()->selected()) {
699                                 if (d->video() && i->video) {
700                                         DCPOMATIC_ASSERT (_clipboard->video);
701                                         i->video->take_settings_from (_clipboard->video);
702                                 }
703                                 if (d->audio() && i->audio) {
704                                         DCPOMATIC_ASSERT (_clipboard->audio);
705                                         i->audio->take_settings_from (_clipboard->audio);
706                                 }
707
708                                 if (d->text()) {
709                                         auto j = i->text.begin ();
710                                         auto k = _clipboard->text.begin ();
711                                         while (j != i->text.end() && k != _clipboard->text.end()) {
712                                                 (*j)->take_settings_from (*k);
713                                                 ++j;
714                                                 ++k;
715                                         }
716                                 }
717                         }
718                 }
719                 d->Destroy ();
720         }
721
722         void edit_select_all ()
723         {
724                 _film_editor->content_panel()->select_all();
725         }
726
727         void edit_preferences ()
728         {
729                 if (!_config_dialog) {
730                         _config_dialog = create_full_config_dialog ();
731                 }
732                 _config_dialog->Show (this);
733         }
734
735         void tools_restore_default_preferences ()
736         {
737                 auto d = new wxMessageDialog (
738                         0,
739                         _("Are you sure you want to restore preferences to their defaults?  This cannot be undone."),
740                         _("Restore default preferences"),
741                         wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
742                         );
743
744                 int const r = d->ShowModal ();
745                 d->Destroy ();
746
747                 if (r == wxID_YES) {
748                         Config::restore_defaults ();
749                 }
750         }
751
752         void jobs_make_dcp ()
753         {
754                 double required;
755                 double available;
756                 bool can_hard_link;
757
758                 if (!_film->should_be_enough_disk_space (required, available, can_hard_link)) {
759                         wxString message;
760                         if (can_hard_link) {
761                                 message = wxString::Format (_("The DCP for this film will take up about %.1f GB, and the disk that you are using only has %.1f GB available.  Do you want to continue anyway?"), required, available);
762                         } else {
763                                 message = wxString::Format (_("The DCP and intermediate files for this film will take up about %.1f GB, and the disk that you are using only has %.1f GB available.  You would need half as much space if the filesystem supported hard links, but it does not.  Do you want to continue anyway?"), required, available);
764                         }
765                         if (!confirm_dialog (this, message)) {
766                                 return;
767                         }
768                 }
769
770                 if (Config::instance()->show_hints_before_make_dcp()) {
771                         auto hints = new HintsDialog (this, _film, false);
772                         int const r = hints->ShowModal();
773                         hints->Destroy ();
774                         if (r == wxID_CANCEL) {
775                                 return;
776                         }
777                 }
778
779                 if (_film->encrypted ()) {
780                         NagDialog::maybe_nag (
781                                 this,
782                                 Config::NAG_ENCRYPTED_METADATA,
783                                 _("You are making an encrypted DCP.  It will not be possible to make KDMs for this DCP unless you have copies of "
784                                   "the <tt>metadata.xml</tt> file within the film and the metadata files within the DCP.\n\n"
785                                   "You should ensure that these files are <span weight=\"bold\" size=\"larger\">BACKED UP</span> "
786                                   "if you want to make KDMs for this film.")
787                                 );
788                 }
789
790                 /* Remove any existing DCP if the user agrees */
791                 boost::filesystem::path const dcp_dir = _film->dir (_film->dcp_name(), false);
792                 if (boost::filesystem::exists (dcp_dir)) {
793                         if (!confirm_dialog (this, wxString::Format (_("Do you want to overwrite the existing DCP %s?"), std_to_wx(dcp_dir.string()).data()))) {
794                                 return;
795                         }
796                         boost::filesystem::remove_all (dcp_dir);
797                 }
798
799                 try {
800                         /* It seems to make sense to auto-save metadata here, since the make DCP may last
801                            a long time, and crashes/power failures are moderately likely.
802                         */
803                         _film->write_metadata ();
804                         _film->make_dcp (true);
805                 } catch (BadSettingError& e) {
806                         error_dialog (this, wxString::Format (_("Bad setting for %s."), std_to_wx(e.setting()).data()), std_to_wx(e.what()));
807                 } catch (std::exception& e) {
808                         error_dialog (this, wxString::Format (_("Could not make DCP.")), std_to_wx(e.what()));
809                 }
810         }
811
812         void jobs_make_kdms ()
813         {
814                 if (!_film) {
815                         return;
816                 }
817
818                 if (_kdm_dialog) {
819                         _kdm_dialog->Destroy ();
820                         _kdm_dialog = 0;
821                 }
822
823                 _kdm_dialog = new KDMDialog (this, _film);
824                 _kdm_dialog->Show ();
825         }
826
827         void jobs_make_dkdms ()
828         {
829                 if (!_film) {
830                         return;
831                 }
832
833                 if (_dkdm_dialog) {
834                         _dkdm_dialog->Destroy ();
835                         _dkdm_dialog = 0;
836                 }
837
838                 _dkdm_dialog = new DKDMDialog (this, _film);
839                 _dkdm_dialog->Show ();
840         }
841
842         /** @return false if we succeeded, true if not */
843         bool send_to_other_tool (int port, function<void()> start, string message)
844         {
845                 /* i = 0; try to connect via socket
846                    i = 1; try again, and then try to start the tool
847                    i = 2 onwards; try again.
848                 */
849                 for (int i = 0; i < 8; ++i) {
850                         try {
851                                 boost::asio::io_service io_service;
852                                 boost::asio::ip::tcp::resolver resolver (io_service);
853                                 boost::asio::ip::tcp::resolver::query query ("127.0.0.1", raw_convert<string> (port));
854                                 boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve (query);
855                                 Socket socket (5);
856                                 socket.connect (*endpoint_iterator);
857                                 DCPOMATIC_ASSERT (_film->directory ());
858                                 socket.write (message.length() + 1);
859                                 socket.write ((uint8_t *) message.c_str(), message.length() + 1);
860                                 /* OK\0 */
861                                 uint8_t ok[3];
862                                 socket.read (ok, 3);
863                                 return false;
864                         } catch (exception& e) {
865
866                         }
867
868                         if (i == 1) {
869                                 start ();
870                         }
871
872                         dcpomatic_sleep_seconds (1);
873                 }
874
875                 return true;
876         }
877
878         void jobs_make_dcp_batch ()
879         {
880                 if (!_film) {
881                         return;
882                 }
883
884                 if (Config::instance()->show_hints_before_make_dcp()) {
885                         auto hints = new HintsDialog (this, _film, false);
886                         int const r = hints->ShowModal();
887                         hints->Destroy ();
888                         if (r == wxID_CANCEL) {
889                                 return;
890                         }
891                 }
892
893                 _film->write_metadata ();
894
895                 if (send_to_other_tool (BATCH_JOB_PORT, &start_batch_converter, _film->directory()->string())) {
896 #ifdef DCPOMATIC_OSX
897                         error_dialog (this, _("Could not start the batch converter.  You may need to download it from dcpomatic.com."));
898 #else
899                         error_dialog (this, _("Could not find batch converter."));
900 #endif
901                 }
902         }
903
904         void jobs_open_dcp_in_player ()
905         {
906                 if (!_film) {
907                         return;
908                 }
909
910                 if (send_to_other_tool (PLAYER_PLAY_PORT, &start_player, _film->dir(_film->dcp_name(false)).string())) {
911 #ifdef DCPOMATIC_OSX
912                         error_dialog (this, _("Could not start the player.  You may need to download it from dcpomatic.com."));
913 #else
914                         error_dialog (this, _("Could not find player."));
915 #endif
916                 }
917         }
918
919         void jobs_make_self_dkdm ()
920         {
921                 if (!_film) {
922                         return;
923                 }
924
925                 auto d = new SelfDKDMDialog (this, _film);
926                 if (d->ShowModal () != wxID_OK) {
927                         d->Destroy ();
928                         return;
929                 }
930
931                 NagDialog::maybe_nag (
932                         this,
933                         Config::NAG_DKDM_CONFIG,
934                         wxString::Format (
935                                 _("You are making a DKDM which is encrypted by a private key held in"
936                                   "\n\n<tt>%s</tt>\n\nIt is <span weight=\"bold\" size=\"larger\">VITALLY IMPORTANT</span> "
937                                   "that you <span weight=\"bold\" size=\"larger\">BACK UP THIS FILE</span> since if it is lost "
938                                   "your DKDMs (and the DCPs they protect) will become useless."), std_to_wx(Config::config_file().string()).data()
939                                 )
940                         );
941
942
943                 dcp::LocalTime from (Config::instance()->signer_chain()->leaf().not_before());
944                 from.add_days (1);
945                 dcp::LocalTime to (Config::instance()->signer_chain()->leaf().not_after());
946                 to.add_days (-1);
947
948                 optional<dcp::EncryptedKDM> kdm;
949                 try {
950                         kdm = _film->make_kdm (
951                                 Config::instance()->decryption_chain()->leaf(),
952                                 vector<string>(),
953                                 d->cpl (),
954                                 from, to,
955                                 dcp::Formulation::MODIFIED_TRANSITIONAL_1,
956                                 true,
957                                 0
958                                 );
959                 } catch (dcp::NotEncryptedError& e) {
960                         error_dialog (this, _("CPL's content is not encrypted."));
961                 } catch (exception& e) {
962                         error_dialog (this, e.what ());
963                 } catch (...) {
964                         error_dialog (this, _("An unknown exception occurred."));
965                 }
966
967                 if (kdm) {
968                         if (d->internal ()) {
969                                 auto dkdms = Config::instance()->dkdms();
970                                 dkdms->add (shared_ptr<DKDM> (new DKDM (kdm.get())));
971                                 Config::instance()->changed ();
972                         } else {
973                                 auto path = d->directory() / (_film->dcp_name(false) + "_DKDM.xml");
974                                 kdm->as_xml (path);
975                         }
976                 }
977
978                 d->Destroy ();
979         }
980
981
982         void jobs_export_video_file ()
983         {
984                 auto d = new ExportVideoFileDialog (this, _film->isdcf_name(true));
985                 if (d->ShowModal() == wxID_OK) {
986                         if (boost::filesystem::exists(d->path())) {
987                                 bool ok = confirm_dialog(
988                                                 this,
989                                                 wxString::Format (_("File %s already exists.  Do you want to overwrite it?"), std_to_wx(d->path().string()).data())
990                                                 );
991
992                                 if (!ok) {
993                                         d->Destroy ();
994                                         return;
995                                 }
996                         }
997
998                         auto job = make_shared<TranscodeJob>(_film);
999                         job->set_encoder (
1000                                 make_shared<FFmpegEncoder> (
1001                                         _film, job, d->path(), d->format(), d->mixdown_to_stereo(), d->split_reels(), d->split_streams(), d->x264_crf())
1002                                 );
1003                         JobManager::instance()->add (job);
1004                 }
1005                 d->Destroy ();
1006         }
1007
1008
1009         void jobs_export_subtitles ()
1010         {
1011                 auto d = new ExportSubtitlesDialog (this, _film->reels().size(), _film->interop());
1012                 if (d->ShowModal() == wxID_OK) {
1013                         auto job = make_shared<TranscodeJob>(_film);
1014                         job->set_encoder (
1015                                 make_shared<SubtitleEncoder>(_film, job, d->path(), _film->isdcf_name(true), d->split_reels(), d->include_font())
1016                                 );
1017                         JobManager::instance()->add (job);
1018                 }
1019                 d->Destroy ();
1020         }
1021
1022
1023         void jobs_send_dcp_to_tms ()
1024         {
1025                 _film->send_dcp_to_tms ();
1026         }
1027
1028         void jobs_show_dcp ()
1029         {
1030                 DCPOMATIC_ASSERT (_film->directory ());
1031 #ifdef DCPOMATIC_WINDOWS
1032                 wstringstream args;
1033                 args << "/select," << _film->dir (_film->dcp_name(false));
1034                 ShellExecute (0, L"open", L"explorer.exe", args.str().c_str(), 0, SW_SHOWDEFAULT);
1035 #endif
1036
1037 #ifdef DCPOMATIC_LINUX
1038                 int r = system ("which nautilus");
1039                 if (WEXITSTATUS (r) == 0) {
1040                         r = system (String::compose("nautilus \"%1\"", _film->directory()->string()).c_str());
1041                         if (WEXITSTATUS (r)) {
1042                                 error_dialog (this, _("Could not show DCP."), _("Could not run nautilus"));
1043                         }
1044                 } else {
1045                         int r = system ("which konqueror");
1046                         if (WEXITSTATUS (r) == 0) {
1047                                 r = system (String::compose ("konqueror \"%1\"", _film->directory()->string()).c_str());
1048                                 if (WEXITSTATUS (r)) {
1049                                         error_dialog (this, _("Could not show DCP"), _("Could not run konqueror"));
1050                                 }
1051                         }
1052                 }
1053 #endif
1054
1055 #ifdef DCPOMATIC_OSX
1056                 int r = system (String::compose ("open -R \"%1\"", _film->dir (_film->dcp_name(false)).string()).c_str());
1057                 if (WEXITSTATUS (r)) {
1058                         error_dialog (this, _("Could not show DCP"));
1059                 }
1060 #endif
1061         }
1062
1063         void view_closed_captions ()
1064         {
1065                 _film_viewer->show_closed_captions ();
1066         }
1067
1068         void view_video_waveform ()
1069         {
1070                 if (!_video_waveform_dialog) {
1071                         _video_waveform_dialog = new VideoWaveformDialog (this, _film, _film_viewer);
1072                 }
1073
1074                 _video_waveform_dialog->Show ();
1075         }
1076
1077         void tools_system_information ()
1078         {
1079                 if (!_system_information_dialog) {
1080                         _system_information_dialog = new SystemInformationDialog (this, _film_viewer);
1081                 }
1082
1083                 _system_information_dialog->Show ();
1084         }
1085
1086         void tools_hints ()
1087         {
1088                 if (!_hints_dialog) {
1089                         _hints_dialog = new HintsDialog (this, _film, true);
1090                 }
1091
1092                 _hints_dialog->Show ();
1093         }
1094
1095         void tools_encoding_servers ()
1096         {
1097                 if (!_servers_list_dialog) {
1098                         _servers_list_dialog = new ServersListDialog (this);
1099                 }
1100
1101                 _servers_list_dialog->Show ();
1102         }
1103
1104         void tools_manage_templates ()
1105         {
1106                 if (!_templates_dialog) {
1107                         _templates_dialog = new TemplatesDialog (this);
1108                 }
1109
1110                 _templates_dialog->Show ();
1111         }
1112
1113         void tools_check_for_updates ()
1114         {
1115                 UpdateChecker::instance()->run ();
1116                 _update_news_requested = true;
1117         }
1118
1119         void tools_send_translations ()
1120         {
1121                 auto d = new SendI18NDialog (this);
1122                 if (d->ShowModal() == wxID_OK) {
1123                         string body;
1124                         body += d->name() + "\n";
1125                         body += d->language() + "\n";
1126                         body += string(dcpomatic_version) + " " + string(dcpomatic_git_commit) + "\n";
1127                         body += "--\n";
1128                         auto translations = I18NHook::translations ();
1129                         for (auto i: translations) {
1130                                 body += i.first + "\n" + i.second + "\n\n";
1131                         }
1132                         list<string> to = { "carl@dcpomatic.com" };
1133                         Emailer emailer (d->email(), to, "DCP-o-matic translations", body);
1134                         emailer.send ("main.carlh.net", 2525, EmailProtocol::STARTTLS);
1135                 }
1136
1137                 d->Destroy ();
1138         }
1139
1140         void help_about ()
1141         {
1142                 auto d = new AboutDialog (this);
1143                 d->ShowModal ();
1144                 d->Destroy ();
1145         }
1146
1147         void help_report_a_problem ()
1148         {
1149                 auto d = new ReportProblemDialog (this, _film);
1150                 if (d->ShowModal () == wxID_OK) {
1151                         d->report ();
1152                 }
1153                 d->Destroy ();
1154         }
1155
1156         bool should_close ()
1157         {
1158                 if (!JobManager::instance()->work_to_do ()) {
1159                         return true;
1160                 }
1161
1162                 auto d = new wxMessageDialog (
1163                         0,
1164                         _("There are unfinished jobs; are you sure you want to quit?"),
1165                         _("Unfinished jobs"),
1166                         wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
1167                         );
1168
1169                 bool const r = d->ShowModal() == wxID_YES;
1170                 d->Destroy ();
1171                 return r;
1172         }
1173
1174         void close (wxCloseEvent& ev)
1175         {
1176                 if (!should_close ()) {
1177                         ev.Veto ();
1178                         return;
1179                 }
1180
1181                 if (_film && _film->dirty ()) {
1182
1183                         auto dialog = new FilmChangedClosingDialog (_film->name ());
1184                         int const r = dialog->run ();
1185                         delete dialog;
1186
1187                         switch (r) {
1188                         case wxID_NO:
1189                                 /* Don't save and carry on to close */
1190                                 break;
1191                         case wxID_YES:
1192                                 /* Save and carry on to close */
1193                                 _film->write_metadata ();
1194                                 break;
1195                         case wxID_CANCEL:
1196                                 /* Veto the event and stop */
1197                                 ev.Veto ();
1198                                 return;
1199                         }
1200                 }
1201
1202                 /* We don't want to hear about any more configuration changes, since they
1203                    cause the File menu to be altered, which itself will be deleted around
1204                    now (without, as far as I can see, any way for us to find out).
1205                 */
1206                 _config_changed_connection.disconnect ();
1207
1208                 /* Also stop hearing about analytics-related stuff */
1209                 _analytics_message_connection.disconnect ();
1210
1211                 ev.Skip ();
1212         }
1213
1214         void set_menu_sensitivity ()
1215         {
1216                 auto jobs = JobManager::instance()->get ();
1217                 auto i = jobs.begin();
1218                 while (i != jobs.end() && (*i)->json_name() != "transcode") {
1219                         ++i;
1220                 }
1221                 bool const dcp_creation = (i != jobs.end ()) && !(*i)->finished ();
1222                 bool const have_cpl = _film && !_film->cpls().empty ();
1223                 bool const have_single_selected_content = _film_editor->content_panel()->selected().size() == 1;
1224                 bool const have_selected_content = !_film_editor->content_panel()->selected().empty();
1225                 bool const have_selected_video_content = !_film_editor->content_panel()->selected_video().empty();
1226
1227                 for (auto j: menu_items) {
1228
1229                         bool enabled = true;
1230
1231                         if ((j.second & NEEDS_FILM) && !_film) {
1232                                 enabled = false;
1233                         }
1234
1235                         if ((j.second & NOT_DURING_DCP_CREATION) && dcp_creation) {
1236                                 enabled = false;
1237                         }
1238
1239                         if ((j.second & NEEDS_CPL) && !have_cpl) {
1240                                 enabled = false;
1241                         }
1242
1243                         if ((j.second & NEEDS_SELECTED_CONTENT) && !have_selected_content) {
1244                                 enabled = false;
1245                         }
1246
1247                         if ((j.second & NEEDS_SINGLE_SELECTED_CONTENT) && !have_single_selected_content) {
1248                                 enabled = false;
1249                         }
1250
1251                         if ((j.second & NEEDS_SELECTED_VIDEO_CONTENT) && !have_selected_video_content) {
1252                                 enabled = false;
1253                         }
1254
1255                         if ((j.second & NEEDS_CLIPBOARD) && !_clipboard) {
1256                                 enabled = false;
1257                         }
1258
1259                         if ((j.second & NEEDS_ENCRYPTION) && (!_film || !_film->encrypted())) {
1260                                 enabled = false;
1261                         }
1262
1263                         j.first->Enable (enabled);
1264                 }
1265         }
1266
1267         /** @return true if the operation that called this method
1268          *  should continue, false to abort it.
1269          */
1270         template <class T>
1271         bool maybe_save_film ()
1272         {
1273                 if (!_film) {
1274                         return true;
1275                 }
1276
1277                 if (_film->dirty ()) {
1278                         T d (_film->name ());
1279                         switch (d.run ()) {
1280                         case wxID_NO:
1281                                 return true;
1282                         case wxID_YES:
1283                                 _film->write_metadata ();
1284                                 return true;
1285                         case wxID_CANCEL:
1286                                 return false;
1287                         }
1288                 }
1289
1290                 return true;
1291         }
1292
1293         template <class T>
1294         bool maybe_save_then_delete_film ()
1295         {
1296                 bool const r = maybe_save_film<T> ();
1297                 if (r) {
1298                         _film.reset ();
1299                 }
1300                 return r;
1301         }
1302
1303         void add_item (wxMenu* menu, wxString text, int id, int sens)
1304         {
1305                 auto item = menu->Append (id, text);
1306                 menu_items.insert (make_pair (item, sens));
1307         }
1308
1309         void setup_menu (wxMenuBar* m)
1310         {
1311                 _file_menu = new wxMenu;
1312                 add_item (_file_menu, _("New...\tCtrl-N"), ID_file_new, ALWAYS);
1313                 add_item (_file_menu, _("&Open...\tCtrl-O"), ID_file_open, ALWAYS);
1314                 _file_menu->AppendSeparator ();
1315                 add_item (_file_menu, _("&Save\tCtrl-S"), ID_file_save, NEEDS_FILM);
1316                 _file_menu->AppendSeparator ();
1317                 add_item (_file_menu, _("Save as &template..."), ID_file_save_as_template, NEEDS_FILM);
1318                 add_item (_file_menu, _("Duplicate..."), ID_file_duplicate, NEEDS_FILM);
1319                 add_item (_file_menu, _("Duplicate and open..."), ID_file_duplicate_and_open, NEEDS_FILM);
1320
1321                 _history_position = _file_menu->GetMenuItems().GetCount();
1322
1323                 _file_menu->AppendSeparator ();
1324                 add_item (_file_menu, _("&Close\tCtrl-W"), ID_file_close, NEEDS_FILM);
1325
1326 #ifndef __WXOSX__
1327                 _file_menu->AppendSeparator ();
1328 #endif
1329
1330 #ifdef __WXOSX__
1331                 add_item (_file_menu, _("&Exit"), wxID_EXIT, ALWAYS);
1332 #else
1333                 add_item (_file_menu, _("&Quit"), wxID_EXIT, ALWAYS);
1334 #endif
1335
1336                 auto edit = new wxMenu;
1337                 add_item (edit, _("Copy settings\tCtrl-C"), ID_edit_copy, NEEDS_FILM | NOT_DURING_DCP_CREATION | NEEDS_SINGLE_SELECTED_CONTENT);
1338                 add_item (edit, _("Paste settings...\tCtrl-V"), ID_edit_paste, NEEDS_FILM | NOT_DURING_DCP_CREATION | NEEDS_SELECTED_CONTENT | NEEDS_CLIPBOARD);
1339                 edit->AppendSeparator ();
1340                 add_item (edit, _("Select all\tShift-Ctrl-A"), ID_edit_select_all, NEEDS_FILM);
1341
1342 #ifdef __WXOSX__
1343                 add_item (_file_menu, _("&Preferences...\tCtrl-P"), wxID_PREFERENCES, ALWAYS);
1344 #else
1345                 edit->AppendSeparator ();
1346                 add_item (edit, _("&Preferences...\tCtrl-P"), wxID_PREFERENCES, ALWAYS);
1347 #endif
1348
1349                 auto jobs_menu = new wxMenu;
1350                 add_item (jobs_menu, _("&Make DCP\tCtrl-M"), ID_jobs_make_dcp, NEEDS_FILM | NOT_DURING_DCP_CREATION);
1351                 add_item (jobs_menu, _("Make DCP in &batch converter\tCtrl-B"), ID_jobs_make_dcp_batch, NEEDS_FILM | NOT_DURING_DCP_CREATION);
1352                 jobs_menu->AppendSeparator ();
1353                 add_item (jobs_menu, _("Make &KDMs...\tCtrl-K"), ID_jobs_make_kdms, NEEDS_FILM);
1354                 add_item (jobs_menu, _("Make &DKDMs...\tCtrl-D"), ID_jobs_make_dkdms, NEEDS_FILM);
1355                 add_item (jobs_menu, _("Make DKDM for DCP-o-matic..."), ID_jobs_make_self_dkdm, NEEDS_FILM | NEEDS_ENCRYPTION);
1356                 jobs_menu->AppendSeparator ();
1357                 add_item (jobs_menu, _("Export video file...\tCtrl-E"), ID_jobs_export_video_file, NEEDS_FILM);
1358                 add_item (jobs_menu, _("Export subtitles..."), ID_jobs_export_subtitles, NEEDS_FILM);
1359                 jobs_menu->AppendSeparator ();
1360                 add_item (jobs_menu, _("&Send DCP to TMS"), ID_jobs_send_dcp_to_tms, NEEDS_FILM | NOT_DURING_DCP_CREATION | NEEDS_CPL);
1361
1362 #if defined(DCPOMATIC_OSX)
1363                 add_item (jobs_menu, _("S&how DCP in Finder"), ID_jobs_show_dcp, NEEDS_FILM | NOT_DURING_DCP_CREATION | NEEDS_CPL);
1364 #elif defined(DCPOMATIC_WINDOWS)
1365                 add_item (jobs_menu, _("S&how DCP in Explorer"), ID_jobs_show_dcp, NEEDS_FILM | NOT_DURING_DCP_CREATION | NEEDS_CPL);
1366 #else
1367                 add_item (jobs_menu, _("S&how DCP in Files"), ID_jobs_show_dcp, NEEDS_FILM | NOT_DURING_DCP_CREATION | NEEDS_CPL);
1368 #endif
1369
1370                 add_item (jobs_menu, _("Open DCP in &player"), ID_jobs_open_dcp_in_player, NEEDS_FILM | NOT_DURING_DCP_CREATION | NEEDS_CPL);
1371
1372                 auto view = new wxMenu;
1373                 add_item (view, _("Closed captions..."), ID_view_closed_captions, NEEDS_FILM);
1374                 add_item (view, _("Video waveform..."), ID_view_video_waveform, NEEDS_FILM);
1375
1376                 auto tools = new wxMenu;
1377                 add_item (tools, _("Hints..."), ID_tools_hints, NEEDS_FILM);
1378                 add_item (tools, _("Encoding servers..."), ID_tools_encoding_servers, 0);
1379                 add_item (tools, _("Manage templates..."), ID_tools_manage_templates, 0);
1380                 add_item (tools, _("Check for updates"), ID_tools_check_for_updates, 0);
1381                 add_item (tools, _("Send translations..."), ID_tools_send_translations, 0);
1382                 add_item (tools, _("System information..."), ID_tools_system_information, 0);
1383                 tools->AppendSeparator ();
1384                 add_item (tools, _("Restore default preferences"), ID_tools_restore_default_preferences, ALWAYS);
1385
1386                 wxMenu* help = new wxMenu;
1387 #ifdef __WXOSX__
1388                 add_item (help, _("About DCP-o-matic"), wxID_ABOUT, ALWAYS);
1389 #else
1390                 add_item (help, _("About"), wxID_ABOUT, ALWAYS);
1391 #endif
1392                 add_item (help, _("Report a problem..."), ID_help_report_a_problem, NEEDS_FILM);
1393
1394                 m->Append (_file_menu, _("&File"));
1395                 m->Append (edit, _("&Edit"));
1396                 m->Append (jobs_menu, _("&Jobs"));
1397                 m->Append (view, _("&View"));
1398                 m->Append (tools, _("&Tools"));
1399                 m->Append (help, _("&Help"));
1400         }
1401
1402         void config_changed (Config::Property what)
1403         {
1404                 /* Instantly save any config changes when using the DCP-o-matic GUI */
1405                 if (what == Config::CINEMAS) {
1406                         try {
1407                                 Config::instance()->write_cinemas();
1408                         } catch (exception& e) {
1409                                 error_dialog (
1410                                         this,
1411                                         wxString::Format (
1412                                                 _("Could not write to cinemas file at %s.  Your changes have not been saved."),
1413                                                 std_to_wx (Config::instance()->cinemas_file().string()).data()
1414                                                 )
1415                                         );
1416                         }
1417                 } else {
1418                         try {
1419                                 Config::instance()->write_config();
1420                         } catch (exception& e) {
1421                                 error_dialog (
1422                                         this,
1423                                         wxString::Format (
1424                                                 _("Could not write to config file at %s.  Your changes have not been saved."),
1425                                                 std_to_wx (Config::instance()->cinemas_file().string()).data()
1426                                                 )
1427                                         );
1428                         }
1429                 }
1430
1431                 for (int i = 0; i < _history_items; ++i) {
1432                         delete _file_menu->Remove (ID_file_history + i);
1433                 }
1434
1435                 if (_history_separator) {
1436                         _file_menu->Remove (_history_separator);
1437                 }
1438                 delete _history_separator;
1439                 _history_separator = 0;
1440
1441                 int pos = _history_position;
1442
1443                 /* Clear out non-existant history items before we re-build the menu */
1444                 Config::instance()->clean_history ();
1445                 auto history = Config::instance()->history();
1446
1447                 if (!history.empty ()) {
1448                         _history_separator = _file_menu->InsertSeparator (pos++);
1449                 }
1450
1451                 for (size_t i = 0; i < history.size(); ++i) {
1452                         string s;
1453                         if (i < 9) {
1454                                 s = String::compose ("&%1 %2", i + 1, history[i].string());
1455                         } else {
1456                                 s = history[i].string();
1457                         }
1458                         _file_menu->Insert (pos++, ID_file_history + i, std_to_wx (s));
1459                 }
1460
1461                 _history_items = history.size ();
1462
1463                 dcpomatic_log->set_types (Config::instance()->log_types());
1464         }
1465
1466         void update_checker_state_changed ()
1467         {
1468                 auto uc = UpdateChecker::instance ();
1469
1470                 bool const announce =
1471                         _update_news_requested ||
1472                         (uc->stable() && Config::instance()->check_for_updates()) ||
1473                         (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
1474
1475                 _update_news_requested = false;
1476
1477                 if (!announce) {
1478                         return;
1479                 }
1480
1481                 if (uc->state() == UpdateChecker::State::YES) {
1482                         auto dialog = new UpdateDialog (this, uc->stable(), uc->test());
1483                         dialog->ShowModal ();
1484                         dialog->Destroy ();
1485                 } else if (uc->state() == UpdateChecker::State::FAILED) {
1486                         error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
1487                 } else {
1488                         error_dialog (this, _("There are no new versions of DCP-o-matic available."));
1489                 }
1490
1491                 _update_news_requested = false;
1492         }
1493
1494         void start_stop_pressed ()
1495         {
1496                 if (_film_viewer->playing()) {
1497                         _film_viewer->stop();
1498                 } else {
1499                         _film_viewer->start();
1500                 }
1501         }
1502
1503         void timeline_pressed ()
1504         {
1505                 _film_editor->content_panel()->timeline_clicked ();
1506         }
1507
1508         void back_frame ()
1509         {
1510                 _film_viewer->seek_by (-_film_viewer->one_video_frame(), true);
1511         }
1512
1513         void forward_frame ()
1514         {
1515                 _film_viewer->seek_by (_film_viewer->one_video_frame(), true);
1516         }
1517
1518         void analytics_message (string title, string html)
1519         {
1520                 auto d = new HTMLDialog(this, std_to_wx(title), std_to_wx(html));
1521                 d->ShowModal();
1522                 d->Destroy();
1523         }
1524
1525         FilmEditor* _film_editor;
1526         std::shared_ptr<FilmViewer> _film_viewer;
1527         StandardControls* _controls;
1528         VideoWaveformDialog* _video_waveform_dialog;
1529         SystemInformationDialog* _system_information_dialog;
1530         HintsDialog* _hints_dialog;
1531         ServersListDialog* _servers_list_dialog;
1532         wxPreferencesEditor* _config_dialog;
1533         KDMDialog* _kdm_dialog;
1534         DKDMDialog* _dkdm_dialog;
1535         TemplatesDialog* _templates_dialog;
1536         wxMenu* _file_menu;
1537         shared_ptr<Film> _film;
1538         int _history_items;
1539         int _history_position;
1540         wxMenuItem* _history_separator;
1541         boost::signals2::scoped_connection _config_changed_connection;
1542         boost::signals2::scoped_connection _analytics_message_connection;
1543         bool _update_news_requested;
1544         shared_ptr<Content> _clipboard;
1545         bool _first_shown_called;
1546 };
1547
1548 static const wxCmdLineEntryDesc command_line_description[] = {
1549         { wxCMD_LINE_SWITCH, "n", "new", "create new film", wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
1550         { wxCMD_LINE_OPTION, "c", "content", "add content file / directory", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
1551         { wxCMD_LINE_OPTION, "d", "dcp", "add content DCP", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
1552         { wxCMD_LINE_SWITCH, "v", "version", "show DCP-o-matic version", wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
1553         { wxCMD_LINE_OPTION, "", "config", "directory containing config.xml and cinemas.xml", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
1554         { wxCMD_LINE_PARAM, 0, 0, "film to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
1555         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
1556 };
1557
1558 /** @class App
1559  *  @brief The magic App class for wxWidgets.
1560  */
1561 class App : public wxApp
1562 {
1563 public:
1564         App ()
1565                 : wxApp ()
1566                 , _frame (0)
1567                 , _splash (0)
1568         {
1569 #ifdef DCPOMATIC_LINUX
1570                 XInitThreads ();
1571 #endif
1572         }
1573
1574 private:
1575
1576         bool OnInit ()
1577         {
1578                 try {
1579                         wxInitAllImageHandlers ();
1580
1581                         Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
1582                         Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
1583
1584                         _splash = maybe_show_splash ();
1585
1586                         SetAppName (_("DCP-o-matic"));
1587
1588                         if (!wxApp::OnInit()) {
1589                                 return false;
1590                         }
1591
1592 #ifdef DCPOMATIC_LINUX
1593                         unsetenv ("UBUNTU_MENUPROXY");
1594 #endif
1595
1596 #ifdef DCPOMATIC_OSX
1597                         dcpomatic_sleep_seconds (1);
1598                         make_foreground_application ();
1599 #endif
1600
1601                         dcpomatic_setup_path_encoding ();
1602
1603                         /* Enable i18n; this will create a Config object
1604                            to look for a force-configured language.  This Config
1605                            object will be wrong, however, because dcpomatic_setup
1606                            hasn't yet been called and there aren't any filters etc.
1607                            set up yet.
1608                         */
1609                         dcpomatic_setup_i18n ();
1610
1611                         /* Set things up, including filters etc.
1612                            which will now be internationalised correctly.
1613                         */
1614                         dcpomatic_setup ();
1615
1616                         /* Force the configuration to be re-loaded correctly next
1617                            time it is needed.
1618                         */
1619                         Config::drop ();
1620
1621                         /* We only look out for bad configuration from here on, as before
1622                            dcpomatic_setup() we haven't got OpenSSL ready so there will be
1623                            incorrect certificate chain validity errors.
1624                         */
1625                         Config::Bad.connect (boost::bind(&App::config_bad, this, _1));
1626
1627                         _frame = new DOMFrame (_("DCP-o-matic"));
1628                         SetTopWindow (_frame);
1629                         _frame->Maximize ();
1630                         close_splash ();
1631
1632                         if (running_32_on_64 ()) {
1633                                 NagDialog::maybe_nag (
1634                                         _frame, Config::NAG_32_ON_64,
1635                                         _("You are running the 32-bit version of DCP-o-matic on a 64-bit version of Windows.  This will limit the memory available to DCP-o-matic and may cause errors.  You are strongly advised to install the 64-bit version of DCP-o-matic."),
1636                                         false);
1637                         }
1638
1639                         _frame->Show ();
1640
1641                         signal_manager = new wxSignalManager (this);
1642                         Bind (wxEVT_IDLE, boost::bind (&App::idle, this, _1));
1643
1644                         if (!_film_to_load.empty() && boost::filesystem::is_directory (_film_to_load)) {
1645                                 try {
1646                                         _frame->load_film (_film_to_load);
1647                                 } catch (exception& e) {
1648                                         error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load film %1 (%2)")), _film_to_load)), std_to_wx(e.what()));
1649                                 }
1650                         }
1651
1652                         if (!_film_to_create.empty ()) {
1653                                 _frame->new_film (_film_to_create, optional<string> ());
1654                                 if (!_content_to_add.empty ()) {
1655                                         for (auto i: content_factory(_content_to_add)) {
1656                                                 _frame->film()->examine_and_add_content (i);
1657                                         }
1658                                 }
1659                                 if (!_dcp_to_add.empty ()) {
1660                                         _frame->film()->examine_and_add_content(shared_ptr<DCPContent>(new DCPContent(_dcp_to_add)));
1661                                 }
1662                         }
1663
1664                         Bind (wxEVT_TIMER, boost::bind (&App::check, this));
1665                         _timer.reset (new wxTimer (this));
1666                         _timer->Start (1000);
1667
1668                         if (Config::instance()->check_for_updates ()) {
1669                                 UpdateChecker::instance()->run ();
1670                         }
1671                 }
1672                 catch (exception& e)
1673                 {
1674                         if (_splash) {
1675                                 _splash->Destroy ();
1676                                 _splash = nullptr;
1677                         }
1678                         error_dialog (nullptr, wxString::Format ("DCP-o-matic could not start."), std_to_wx(e.what()));
1679                 }
1680
1681                 return true;
1682         }
1683
1684         void OnInitCmdLine (wxCmdLineParser& parser)
1685         {
1686                 parser.SetDesc (command_line_description);
1687                 parser.SetSwitchChars (wxT ("-"));
1688         }
1689
1690         bool OnCmdLineParsed (wxCmdLineParser& parser)
1691         {
1692                 if (parser.Found (wxT("version"))) {
1693                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
1694                         exit (EXIT_SUCCESS);
1695                 }
1696
1697                 if (parser.GetParamCount() > 0) {
1698                         if (parser.Found (wxT ("new"))) {
1699                                 _film_to_create = wx_to_std (parser.GetParam (0));
1700                         } else {
1701                                 _film_to_load = wx_to_std (parser.GetParam (0));
1702                         }
1703                 }
1704
1705                 wxString content;
1706                 if (parser.Found (wxT ("content"), &content)) {
1707                         _content_to_add = wx_to_std (content);
1708                 }
1709
1710                 wxString dcp;
1711                 if (parser.Found (wxT ("dcp"), &dcp)) {
1712                         _dcp_to_add = wx_to_std (dcp);
1713                 }
1714
1715                 wxString config;
1716                 if (parser.Found (wxT("config"), &config)) {
1717                         State::override_path = wx_to_std (config);
1718                 }
1719
1720                 return true;
1721         }
1722
1723         void report_exception ()
1724         {
1725                 try {
1726                         throw;
1727                 } catch (FileError& e) {
1728                         error_dialog (
1729                                 nullptr,
1730                                 wxString::Format(
1731                                         _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
1732                                         std_to_wx (e.what()),
1733                                         std_to_wx (e.file().string().c_str())
1734                                         )
1735                                 );
1736                 } catch (boost::filesystem::filesystem_error& e) {
1737                         error_dialog (
1738                                 nullptr,
1739                                 wxString::Format(
1740                                         _("An exception occurred: %s (%s) (%s)\n\n") + REPORT_PROBLEM,
1741                                         std_to_wx (e.what()),
1742                                         std_to_wx (e.path1().string()),
1743                                         std_to_wx (e.path2().string())
1744                                         )
1745                                 );
1746                 } catch (exception& e) {
1747                         error_dialog (
1748                                 nullptr,
1749                                 wxString::Format(
1750                                         _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
1751                                         std_to_wx (e.what ())
1752                                         )
1753                                 );
1754                 } catch (...) {
1755                         error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
1756                 }
1757         }
1758
1759         /* An unhandled exception has occurred inside the main event loop */
1760         bool OnExceptionInMainLoop ()
1761         {
1762                 report_exception ();
1763                 /* This will terminate the program */
1764                 return false;
1765         }
1766
1767         void OnUnhandledException ()
1768         {
1769                 report_exception ();
1770         }
1771
1772         void idle (wxIdleEvent& ev)
1773         {
1774                 signal_manager->ui_idle ();
1775                 ev.Skip ();
1776         }
1777
1778         void check ()
1779         {
1780                 try {
1781                         EncodeServerFinder::instance()->rethrow ();
1782                 } catch (exception& e) {
1783                         error_dialog (0, std_to_wx (e.what ()));
1784                 }
1785         }
1786
1787         void close_splash ()
1788         {
1789                 if (_splash) {
1790                         _splash->Destroy ();
1791                         _splash = 0;
1792                 }
1793         }
1794
1795         void config_failed_to_load ()
1796         {
1797                 message_dialog (_frame, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
1798         }
1799
1800         void config_warning (string m)
1801         {
1802                 message_dialog (_frame, std_to_wx(m));
1803         }
1804
1805         bool config_bad (Config::BadReason reason)
1806         {
1807                 /* Destroy the splash screen here, as otherwise bad things seem to happen (for reasons unknown)
1808                    when we open our recreate dialog, close it, *then* try to Destroy the splash (the Destroy fails).
1809                 */
1810                 _splash->Destroy ();
1811                 _splash = nullptr;
1812
1813                 auto config = Config::instance();
1814                 switch (reason) {
1815                 case Config::BAD_SIGNER_UTF8_STRINGS:
1816                 {
1817                         if (config->nagged(Config::NAG_BAD_SIGNER_CHAIN)) {
1818                                 return false;
1819                         }
1820                         auto d = new RecreateChainDialog (
1821                                 _frame, _("Recreate signing certificates"),
1822                                 _("The certificate chain that DCP-o-matic uses for signing DCPs and KDMs contains a small error\n"
1823                                   "which will prevent DCPs from being validated correctly on some systems.  Do you want to re-create\n"
1824                                   "the certificate chain for signing DCPs and KDMs?"),
1825                                 _("Do nothing"),
1826                                 Config::NAG_BAD_SIGNER_CHAIN
1827                                 );
1828                         int const r = d->ShowModal ();
1829                         d->Destroy ();
1830                         return r == wxID_OK;
1831                 }
1832                 case Config::BAD_SIGNER_INCONSISTENT:
1833                 {
1834                         auto d = new RecreateChainDialog (
1835                                 _frame, _("Recreate signing certificates"),
1836                                 _("The certificate chain that DCP-o-matic uses for signing DCPs and KDMs is inconsistent and\n"
1837                                   "cannot be used.  DCP-o-matic cannot start unless you re-create it.  Do you want to re-create\n"
1838                                   "the certificate chain for signing DCPs and KDMs?"),
1839                                 _("Close DCP-o-matic")
1840                                 );
1841                         int const r = d->ShowModal ();
1842                         d->Destroy ();
1843                         if (r != wxID_OK) {
1844                                 exit (EXIT_FAILURE);
1845                         }
1846                         return true;
1847                 }
1848                 case Config::BAD_DECRYPTION_INCONSISTENT:
1849                 {
1850                         auto d = new RecreateChainDialog (
1851                                 _frame, _("Recreate KDM decryption chain"),
1852                                 _("The certificate chain that DCP-o-matic uses for decrypting KDMs is inconsistent and\n"
1853                                   "cannot be used.  DCP-o-matic cannot start unless you re-create it.  Do you want to re-create\n"
1854                                   "the certificate chain for decrypting KDMs?  You may want to say \"No\" here and back up your\n"
1855                                   "configuration before continuing."),
1856                                 _("Close DCP-o-matic")
1857                                 );
1858                         int const r = d->ShowModal ();
1859                         d->Destroy ();
1860                         if (r != wxID_OK) {
1861                                 exit (EXIT_FAILURE);
1862                         }
1863                         return true;
1864                 }
1865                 default:
1866                         DCPOMATIC_ASSERT (false);
1867                 }
1868         }
1869
1870         DOMFrame* _frame;
1871         wxSplashScreen* _splash;
1872         shared_ptr<wxTimer> _timer;
1873         string _film_to_load;
1874         string _film_to_create;
1875         string _content_to_add;
1876         string _dcp_to_add;
1877 };
1878
1879 IMPLEMENT_APP (App)