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