Fix some comment typos.
[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/cross.h"
62 #include "lib/util.h"
63 #include "lib/video_content.h"
64 #include "lib/content.h"
65 #include "lib/version.h"
66 #include "lib/signal_manager.h"
67 #include "lib/log.h"
68 #include "lib/screen.h"
69 #include "lib/job_manager.h"
70 #include "lib/exceptions.h"
71 #include "lib/cinema.h"
72 #include "lib/kdm_with_metadata.h"
73 #include "lib/send_kdm_email_job.h"
74 #include "lib/encode_server_finder.h"
75 #include "lib/update_checker.h"
76 #include "lib/cross.h"
77 #include "lib/content_factory.h"
78 #include "lib/compose.hpp"
79 #include "lib/dcpomatic_socket.h"
80 #include "lib/hints.h"
81 #include "lib/dcp_content.h"
82 #include "lib/ffmpeg_encoder.h"
83 #include "lib/transcode_job.h"
84 #include "lib/dkdm_wrapper.h"
85 #include "lib/audio_content.h"
86 #include "lib/check_content_change_job.h"
87 #include "lib/text_content.h"
88 #include "lib/dcpomatic_log.h"
89 #include "lib/subtitle_encoder.h"
90 #include "lib/warnings.h"
91 #include <dcp/exceptions.h>
92 #include <dcp/raw_convert.h>
93 DCPOMATIC_DISABLE_WARNINGS
94 #include <wx/generic/aboutdlgg.h>
95 #include <wx/stdpaths.h>
96 #include <wx/cmdline.h>
97 #include <wx/preferences.h>
98 #include <wx/splash.h>
99 #include <wx/wxhtml.h>
100 DCPOMATIC_ENABLE_WARNINGS
101 #ifdef __WXGTK__
102 #include <X11/Xlib.h>
103 #endif
104 #ifdef __WXMSW__
105 #include <shellapi.h>
106 #endif
107 #include <boost/filesystem.hpp>
108 #include <boost/algorithm/string.hpp>
109 #include <iostream>
110 #include <fstream>
111 /* This is OK as it's only used with DCPOMATIC_WINDOWS */
112 #include <sstream>
113
114 #ifdef check
115 #undef check
116 #endif
117
118
119 using std::cout;
120 using std::dynamic_pointer_cast;
121 using std::exception;
122 using std::function;
123 using std::list;
124 using std::make_pair;
125 using std::make_shared;
126 using std::map;
127 using std::shared_ptr;
128 using std::string;
129 using std::vector;
130 using std::wcout;
131 using boost::optional;
132 using boost::is_any_of;
133 using boost::algorithm::find;
134 #if BOOST_VERSION >= 106100
135 using namespace boost::placeholders;
136 #endif
137 using dcp::raw_convert;
138
139
140 class FilmChangedClosingDialog
141 {
142 public:
143         explicit FilmChangedClosingDialog (string name)
144         {
145                 _dialog = new wxMessageDialog (
146                         nullptr,
147                         wxString::Format(_("Save changes to film \"%s\" before closing?"), std_to_wx (name).data()),
148                         /// TRANSLATORS: this is the heading for a dialog box, which tells the user that the current
149                         /// project (Film) has been changed since it was last saved.
150                         _("Film changed"),
151                         wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_QUESTION
152                         );
153
154                 _dialog->SetYesNoCancelLabels (
155                         _("Save film and close"), _("Close without saving film"), _("Don't close")
156                         );
157         }
158
159         ~FilmChangedClosingDialog ()
160         {
161                 _dialog->Destroy ();
162         }
163
164         FilmChangedClosingDialog (FilmChangedClosingDialog const&) = delete;
165         FilmChangedClosingDialog& operator= (FilmChangedClosingDialog const&) = delete;
166
167         int run ()
168         {
169                 return _dialog->ShowModal ();
170         }
171
172 private:
173         wxMessageDialog* _dialog;
174 };
175
176
177 class FilmChangedDuplicatingDialog
178 {
179 public:
180         explicit FilmChangedDuplicatingDialog (string name)
181         {
182                 _dialog = new wxMessageDialog (
183                         nullptr,
184                         wxString::Format(_("Save changes to film \"%s\" before duplicating?"), std_to_wx (name).data()),
185                         /// TRANSLATORS: this is the heading for a dialog box, which tells the user that the current
186                         /// project (Film) has been changed since it was last saved.
187                         _("Film changed"),
188                         wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_QUESTION
189                         );
190
191                 _dialog->SetYesNoCancelLabels (
192                         _("Save film and duplicate"), _("Duplicate without saving film"), _("Don't duplicate")
193                         );
194         }
195
196         ~FilmChangedDuplicatingDialog ()
197         {
198                 _dialog->Destroy ();
199         }
200
201         FilmChangedDuplicatingDialog (FilmChangedDuplicatingDialog const&) = delete;
202         FilmChangedDuplicatingDialog& operator= (FilmChangedDuplicatingDialog const&) = delete;
203
204         int run ()
205         {
206                 return _dialog->ShowModal ();
207         }
208
209 private:
210         wxMessageDialog* _dialog;
211 };
212
213
214 #define ALWAYS                        0x0
215 #define NEEDS_FILM                    0x1
216 #define NOT_DURING_DCP_CREATION       0x2
217 #define NEEDS_CPL                     0x4
218 #define NEEDS_SINGLE_SELECTED_CONTENT 0x8
219 #define NEEDS_SELECTED_CONTENT        0x10
220 #define NEEDS_SELECTED_VIDEO_CONTENT  0x20
221 #define NEEDS_CLIPBOARD               0x40
222 #define NEEDS_ENCRYPTION              0x80
223
224
225 map<wxMenuItem*, int> menu_items;
226
227 enum {
228         ID_file_new = 1,
229         ID_file_open,
230         ID_file_save,
231         ID_file_save_as_template,
232         ID_file_duplicate,
233         ID_file_duplicate_and_open,
234         ID_file_history,
235         /* Allow spare IDs after _history for the recent files list */
236         ID_file_close = 100,
237         ID_edit_copy,
238         ID_edit_paste,
239         ID_edit_select_all,
240         ID_jobs_make_dcp,
241         ID_jobs_make_dcp_batch,
242         ID_jobs_make_kdms,
243         ID_jobs_make_dkdms,
244         ID_jobs_make_self_dkdm,
245         ID_jobs_export_video_file,
246         ID_jobs_export_subtitles,
247         ID_jobs_send_dcp_to_tms,
248         ID_jobs_show_dcp,
249         ID_jobs_open_dcp_in_player,
250         ID_view_closed_captions,
251         ID_view_video_waveform,
252         ID_tools_hints,
253         ID_tools_encoding_servers,
254         ID_tools_manage_templates,
255         ID_tools_check_for_updates,
256         ID_tools_send_translations,
257         ID_tools_system_information,
258         ID_tools_restore_default_preferences,
259         ID_tools_export_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::tools_export_preferences, this), ID_tools_export_preferences);
343                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this),              wxID_ABOUT);
344                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this),   ID_help_report_a_problem);
345
346                 Bind (wxEVT_CLOSE_WINDOW, boost::bind (&DOMFrame::close, this, _1));
347                 Bind (wxEVT_SHOW, boost::bind (&DOMFrame::show, this, _1));
348
349                 /* Use a panel as the only child of the Frame so that we avoid
350                    the dark-grey background on Windows.
351                 */
352                 auto overall_panel = new wxPanel (this, wxID_ANY);
353
354                 _film_viewer.reset (new FilmViewer (overall_panel));
355                 _controls = new StandardControls (overall_panel, _film_viewer, true);
356                 _film_editor = new FilmEditor (overall_panel, _film_viewer);
357                 auto job_manager_view = new JobManagerView (overall_panel, false);
358
359                 auto right_sizer = new wxBoxSizer (wxVERTICAL);
360                 right_sizer->Add (_film_viewer->panel(), 2, wxEXPAND | wxALL, 6);
361                 right_sizer->Add (_controls, 0, wxEXPAND | wxALL, 6);
362                 right_sizer->Add (job_manager_view, 1, wxEXPAND | wxALL, 6);
363
364                 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
365                 main_sizer->Add (_film_editor, 0, wxEXPAND | wxALL, 6);
366                 main_sizer->Add (right_sizer, 1, wxEXPAND | wxALL, 6);
367
368                 set_menu_sensitivity ();
369
370                 _film_editor->FileChanged.connect (bind (&DOMFrame::file_changed, this, _1));
371                 _film_editor->content_panel()->SelectionChanged.connect (boost::bind (&DOMFrame::set_menu_sensitivity, this));
372                 file_changed ("");
373
374                 JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&DOMFrame::set_menu_sensitivity, this));
375
376                 overall_panel->SetSizer (main_sizer);
377
378                 UpdateChecker::instance()->StateChanged.connect(boost::bind(&DOMFrame::update_checker_state_changed, this));
379
380                 FocusManager::instance()->SetFocus.connect (boost::bind (&DOMFrame::remove_accelerators, this));
381                 FocusManager::instance()->KillFocus.connect (boost::bind (&DOMFrame::add_accelerators, this));
382                 add_accelerators ();
383         }
384
385         void add_accelerators ()
386         {
387 #ifdef __WXOSX__
388                 int accelerators = 7;
389 #else
390                 int accelerators = 6;
391 #endif
392                 auto accel = new wxAcceleratorEntry[accelerators];
393                 /* [Shortcut] Ctrl+A:Add file(s) to the film */
394                 accel[0].Set (wxACCEL_CTRL, static_cast<int>('A'), ID_add_file);
395                 /* [Shortcut] Delete:Remove selected content from film */
396                 accel[1].Set (wxACCEL_NORMAL, WXK_DELETE, ID_remove);
397                 /* [Shortcut] Space:Start/stop playback */
398                 accel[2].Set (wxACCEL_NORMAL, WXK_SPACE, ID_start_stop);
399                 /* [Shortcut] Ctrl+T:Open timeline window */
400                 accel[3].Set (wxACCEL_CTRL, static_cast<int>('T'), ID_timeline);
401                 /* [Shortcut] Left arrow:Move back one frame */
402                 accel[4].Set (wxACCEL_NORMAL, WXK_LEFT, ID_back_frame);
403                 /* [Shortcut] Right arrow:Move forward one 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 = nullptr;
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 tools_export_preferences ()
753         {
754                 auto dialog = new wxFileDialog (
755                         this, _("Specify ZIP file"), wxEmptyString, wxT("dcpomatic_config.zip"), wxT("ZIP files (*.zip)|*.zip"),
756                         wxFD_SAVE | wxFD_OVERWRITE_PROMPT
757                         );
758
759                 int const r = dialog->ShowModal ();
760                 dialog->Destroy ();
761
762                 if (r == wxID_OK) {
763                         save_all_config_as_zip (wx_to_std(dialog->GetPath()));
764                 }
765         }
766
767         void jobs_make_dcp ()
768         {
769                 double required;
770                 double available;
771                 bool can_hard_link;
772
773                 if (!_film->should_be_enough_disk_space (required, available, can_hard_link)) {
774                         wxString message;
775                         if (can_hard_link) {
776                                 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);
777                         } else {
778                                 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);
779                         }
780                         if (!confirm_dialog (this, message)) {
781                                 return;
782                         }
783                 }
784
785                 if (Config::instance()->show_hints_before_make_dcp()) {
786                         auto hints = new HintsDialog (this, _film, false);
787                         int const r = hints->ShowModal();
788                         hints->Destroy ();
789                         if (r == wxID_CANCEL) {
790                                 return;
791                         }
792                 }
793
794                 if (_film->encrypted ()) {
795                         NagDialog::maybe_nag (
796                                 this,
797                                 Config::NAG_ENCRYPTED_METADATA,
798                                 _("You are making an encrypted DCP.  It will not be possible to make KDMs for this DCP unless you have copies of "
799                                   "the <tt>metadata.xml</tt> file within the film and the metadata files within the DCP.\n\n"
800                                   "You should ensure that these files are <span weight=\"bold\" size=\"larger\">BACKED UP</span> "
801                                   "if you want to make KDMs for this film.")
802                                 );
803                 }
804
805                 /* Remove any existing DCP if the user agrees */
806                 auto const dcp_dir = _film->dir (_film->dcp_name(), false);
807                 if (boost::filesystem::exists(dcp_dir)) {
808                         if (!confirm_dialog (this, wxString::Format (_("Do you want to overwrite the existing DCP %s?"), std_to_wx(dcp_dir.string()).data()))) {
809                                 return;
810                         }
811                         boost::filesystem::remove_all (dcp_dir);
812                 }
813
814                 try {
815                         /* It seems to make sense to auto-save metadata here, since the make DCP may last
816                            a long time, and crashes/power failures are moderately likely.
817                         */
818                         _film->write_metadata ();
819                         _film->make_dcp (TranscodeJob::ChangedBehaviour::EXAMINE_THEN_STOP);
820                 } catch (BadSettingError& e) {
821                         error_dialog (this, wxString::Format (_("Bad setting for %s."), std_to_wx(e.setting()).data()), std_to_wx(e.what()));
822                 } catch (std::exception& e) {
823                         error_dialog (this, wxString::Format (_("Could not make DCP.")), std_to_wx(e.what()));
824                 }
825         }
826
827         void jobs_make_kdms ()
828         {
829                 if (!_film) {
830                         return;
831                 }
832
833                 if (_kdm_dialog) {
834                         _kdm_dialog->Destroy ();
835                         _kdm_dialog = 0;
836                 }
837
838                 _kdm_dialog = new KDMDialog (this, _film);
839                 _kdm_dialog->Show ();
840         }
841
842         void jobs_make_dkdms ()
843         {
844                 if (!_film) {
845                         return;
846                 }
847
848                 if (_dkdm_dialog) {
849                         _dkdm_dialog->Destroy ();
850                         _dkdm_dialog = nullptr;
851                 }
852
853                 _dkdm_dialog = new DKDMDialog (this, _film);
854                 _dkdm_dialog->Show ();
855         }
856
857         /** @return false if we succeeded, true if not */
858         bool send_to_other_tool (int port, function<void()> start, string message)
859         {
860                 /* i = 0; try to connect via socket
861                    i = 1; try again, and then try to start the tool
862                    i = 2 onwards; try again.
863                 */
864                 for (int i = 0; i < 8; ++i) {
865                         try {
866                                 boost::asio::io_service io_service;
867                                 boost::asio::ip::tcp::resolver resolver (io_service);
868                                 boost::asio::ip::tcp::resolver::query query ("127.0.0.1", raw_convert<string> (port));
869                                 boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve (query);
870                                 Socket socket (5);
871                                 socket.connect (*endpoint_iterator);
872                                 DCPOMATIC_ASSERT (_film->directory ());
873                                 socket.write (message.length() + 1);
874                                 socket.write ((uint8_t *) message.c_str(), message.length() + 1);
875                                 /* OK\0 */
876                                 uint8_t ok[3];
877                                 socket.read (ok, 3);
878                                 return false;
879                         } catch (exception& e) {
880
881                         }
882
883                         if (i == 1) {
884                                 start ();
885                         }
886
887                         dcpomatic_sleep_seconds (1);
888                 }
889
890                 return true;
891         }
892
893         void jobs_make_dcp_batch ()
894         {
895                 if (!_film) {
896                         return;
897                 }
898
899                 if (Config::instance()->show_hints_before_make_dcp()) {
900                         auto hints = new HintsDialog (this, _film, false);
901                         int const r = hints->ShowModal();
902                         hints->Destroy ();
903                         if (r == wxID_CANCEL) {
904                                 return;
905                         }
906                 }
907
908                 _film->write_metadata ();
909
910                 if (send_to_other_tool (BATCH_JOB_PORT, &start_batch_converter, _film->directory()->string())) {
911 #ifdef DCPOMATIC_OSX
912                         error_dialog (this, _("Could not start the batch converter.  You may need to download it from dcpomatic.com."));
913 #else
914                         error_dialog (this, _("Could not find batch converter."));
915 #endif
916                 }
917         }
918
919         void jobs_open_dcp_in_player ()
920         {
921                 if (!_film) {
922                         return;
923                 }
924
925                 if (send_to_other_tool (PLAYER_PLAY_PORT, &start_player, _film->dir(_film->dcp_name(false)).string())) {
926 #ifdef DCPOMATIC_OSX
927                         error_dialog (this, _("Could not start the player.  You may need to download it from dcpomatic.com."));
928 #else
929                         error_dialog (this, _("Could not find player."));
930 #endif
931                 }
932         }
933
934         void jobs_make_self_dkdm ()
935         {
936                 if (!_film) {
937                         return;
938                 }
939
940                 auto d = new SelfDKDMDialog (this, _film);
941                 if (d->ShowModal () != wxID_OK) {
942                         d->Destroy ();
943                         return;
944                 }
945
946                 NagDialog::maybe_nag (
947                         this,
948                         Config::NAG_DKDM_CONFIG,
949                         wxString::Format (
950                                 _("You are making a DKDM which is encrypted by a private key held in"
951                                   "\n\n<tt>%s</tt>\n\nIt is <span weight=\"bold\" size=\"larger\">VITALLY IMPORTANT</span> "
952                                   "that you <span weight=\"bold\" size=\"larger\">BACK UP THIS FILE</span> since if it is lost "
953                                   "your DKDMs (and the DCPs they protect) will become useless."), std_to_wx(Config::config_read_file().string()).data()
954                                 )
955                         );
956
957
958                 dcp::LocalTime from (Config::instance()->signer_chain()->leaf().not_before());
959                 from.add_days (1);
960                 dcp::LocalTime to (Config::instance()->signer_chain()->leaf().not_after());
961                 to.add_days (-1);
962
963                 optional<dcp::EncryptedKDM> kdm;
964                 try {
965                         kdm = _film->make_kdm (
966                                 Config::instance()->decryption_chain()->leaf(),
967                                 vector<string>(),
968                                 d->cpl (),
969                                 from, to,
970                                 dcp::Formulation::MODIFIED_TRANSITIONAL_1,
971                                 true,
972                                 0
973                                 );
974                 } catch (dcp::NotEncryptedError& e) {
975                         error_dialog (this, _("CPL's content is not encrypted."));
976                 } catch (exception& e) {
977                         error_dialog (this, e.what ());
978                 } catch (...) {
979                         error_dialog (this, _("An unknown exception occurred."));
980                 }
981
982                 if (kdm) {
983                         if (d->internal ()) {
984                                 auto dkdms = Config::instance()->dkdms();
985                                 dkdms->add (make_shared<DKDM>(kdm.get()));
986                                 Config::instance()->changed ();
987                         } else {
988                                 auto path = d->directory() / (_film->dcp_name(false) + "_DKDM.xml");
989                                 kdm->as_xml (path);
990                         }
991                 }
992
993                 d->Destroy ();
994         }
995
996
997         void jobs_export_video_file ()
998         {
999                 auto d = new ExportVideoFileDialog (this, _film->isdcf_name(true));
1000                 if (d->ShowModal() == wxID_OK) {
1001                         if (boost::filesystem::exists(d->path())) {
1002                                 bool ok = confirm_dialog(
1003                                                 this,
1004                                                 wxString::Format (_("File %s already exists.  Do you want to overwrite it?"), std_to_wx(d->path().string()).data())
1005                                                 );
1006
1007                                 if (!ok) {
1008                                         d->Destroy ();
1009                                         return;
1010                                 }
1011                         }
1012
1013                         auto job = make_shared<TranscodeJob>(_film, TranscodeJob::ChangedBehaviour::EXAMINE_THEN_STOP);
1014                         job->set_encoder (
1015                                 make_shared<FFmpegEncoder> (
1016                                         _film, job, d->path(), d->format(), d->mixdown_to_stereo(), d->split_reels(), d->split_streams(), d->x264_crf())
1017                                 );
1018                         JobManager::instance()->add (job);
1019                 }
1020                 d->Destroy ();
1021         }
1022
1023
1024         void jobs_export_subtitles ()
1025         {
1026                 auto d = new ExportSubtitlesDialog (this, _film->reels().size(), _film->interop());
1027                 if (d->ShowModal() == wxID_OK) {
1028                         auto job = make_shared<TranscodeJob>(_film, TranscodeJob::ChangedBehaviour::EXAMINE_THEN_STOP);
1029                         job->set_encoder (
1030                                 make_shared<SubtitleEncoder>(_film, job, d->path(), _film->isdcf_name(true), d->split_reels(), d->include_font())
1031                                 );
1032                         JobManager::instance()->add (job);
1033                 }
1034                 d->Destroy ();
1035         }
1036
1037
1038         void jobs_send_dcp_to_tms ()
1039         {
1040                 _film->send_dcp_to_tms ();
1041         }
1042
1043         void jobs_show_dcp ()
1044         {
1045                 DCPOMATIC_ASSERT (_film->directory ());
1046                 if (show_in_file_manager(_film->directory().get(), _film->dir(_film->dcp_name(false)))) {
1047                         error_dialog (this, _("Could not show DCP."));
1048                 }
1049         }
1050
1051         void view_closed_captions ()
1052         {
1053                 _film_viewer->show_closed_captions ();
1054         }
1055
1056         void view_video_waveform ()
1057         {
1058                 if (!_video_waveform_dialog) {
1059                         _video_waveform_dialog = new VideoWaveformDialog (this, _film, _film_viewer);
1060                 }
1061
1062                 _video_waveform_dialog->Show ();
1063         }
1064
1065         void tools_system_information ()
1066         {
1067                 if (!_system_information_dialog) {
1068                         _system_information_dialog = new SystemInformationDialog (this, _film_viewer);
1069                 }
1070
1071                 _system_information_dialog->Show ();
1072         }
1073
1074         void tools_hints ()
1075         {
1076                 if (!_hints_dialog) {
1077                         _hints_dialog = new HintsDialog (this, _film, true);
1078                 }
1079
1080                 _hints_dialog->Show ();
1081         }
1082
1083         void tools_encoding_servers ()
1084         {
1085                 if (!_servers_list_dialog) {
1086                         _servers_list_dialog = new ServersListDialog (this);
1087                 }
1088
1089                 _servers_list_dialog->Show ();
1090         }
1091
1092         void tools_manage_templates ()
1093         {
1094                 if (!_templates_dialog) {
1095                         _templates_dialog = new TemplatesDialog (this);
1096                 }
1097
1098                 _templates_dialog->Show ();
1099         }
1100
1101         void tools_check_for_updates ()
1102         {
1103                 _update_news_requested = true;
1104                 UpdateChecker::instance()->run();
1105         }
1106
1107         void tools_send_translations ()
1108         {
1109                 auto d = new SendI18NDialog (this);
1110                 if (d->ShowModal() == wxID_OK) {
1111                         string body;
1112                         body += d->name() + "\n";
1113                         body += d->language() + "\n";
1114                         body += string(dcpomatic_version) + " " + string(dcpomatic_git_commit) + "\n";
1115                         body += "--\n";
1116                         auto translations = I18NHook::translations ();
1117                         for (auto i: translations) {
1118                                 body += i.first + "\n" + i.second + "\n\n";
1119                         }
1120                         list<string> to = { "carl@dcpomatic.com" };
1121                         if (d->email().find("@") == string::npos) {
1122                                 error_dialog (this, _("You must enter a valid email address when sending translations, "
1123                                                       "otherwise the DCP-o-matic maintainers cannot credit you or contact you with questions."));
1124                         } else {
1125                                 Emailer emailer (d->email(), to, "DCP-o-matic translations", body);
1126                                 try {
1127                                         emailer.send ("main.carlh.net", 2525, EmailProtocol::STARTTLS);
1128                                 } catch (NetworkError& e) {
1129                                         error_dialog (this, _("Could not send translations"), std_to_wx(e.what()));
1130                                 }
1131                         }
1132                 }
1133
1134                 d->Destroy ();
1135         }
1136
1137         void help_about ()
1138         {
1139                 auto d = new AboutDialog (this);
1140                 d->ShowModal ();
1141                 d->Destroy ();
1142         }
1143
1144         void help_report_a_problem ()
1145         {
1146                 auto d = new ReportProblemDialog (this, _film);
1147                 if (d->ShowModal () == wxID_OK) {
1148                         d->report ();
1149                 }
1150                 d->Destroy ();
1151         }
1152
1153         bool should_close ()
1154         {
1155                 if (!JobManager::instance()->work_to_do ()) {
1156                         return true;
1157                 }
1158
1159                 auto d = new wxMessageDialog (
1160                         0,
1161                         _("There are unfinished jobs; are you sure you want to quit?"),
1162                         _("Unfinished jobs"),
1163                         wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
1164                         );
1165
1166                 bool const r = d->ShowModal() == wxID_YES;
1167                 d->Destroy ();
1168                 return r;
1169         }
1170
1171         void close (wxCloseEvent& ev)
1172         {
1173                 if (!should_close ()) {
1174                         ev.Veto ();
1175                         return;
1176                 }
1177
1178                 if (_film && _film->dirty ()) {
1179
1180                         auto dialog = new FilmChangedClosingDialog (_film->name ());
1181                         int const r = dialog->run ();
1182                         delete dialog;
1183
1184                         switch (r) {
1185                         case wxID_NO:
1186                                 /* Don't save and carry on to close */
1187                                 break;
1188                         case wxID_YES:
1189                                 /* Save and carry on to close */
1190                                 _film->write_metadata ();
1191                                 break;
1192                         case wxID_CANCEL:
1193                                 /* Veto the event and stop */
1194                                 ev.Veto ();
1195                                 return;
1196                         }
1197                 }
1198
1199                 /* We don't want to hear about any more configuration changes, since they
1200                    cause the File menu to be altered, which itself will be deleted around
1201                    now (without, as far as I can see, any way for us to find out).
1202                 */
1203                 _config_changed_connection.disconnect ();
1204
1205                 /* Also stop hearing about analytics-related stuff */
1206                 _analytics_message_connection.disconnect ();
1207
1208                 ev.Skip ();
1209         }
1210
1211         void set_menu_sensitivity ()
1212         {
1213                 auto jobs = JobManager::instance()->get ();
1214                 auto i = jobs.begin();
1215                 while (i != jobs.end() && (*i)->json_name() != "transcode") {
1216                         ++i;
1217                 }
1218                 bool const dcp_creation = (i != jobs.end ()) && !(*i)->finished ();
1219                 bool const have_cpl = _film && !_film->cpls().empty ();
1220                 bool const have_single_selected_content = _film_editor->content_panel()->selected().size() == 1;
1221                 bool const have_selected_content = !_film_editor->content_panel()->selected().empty();
1222                 bool const have_selected_video_content = !_film_editor->content_panel()->selected_video().empty();
1223
1224                 for (auto j: menu_items) {
1225
1226                         bool enabled = true;
1227
1228                         if ((j.second & NEEDS_FILM) && !_film) {
1229                                 enabled = false;
1230                         }
1231
1232                         if ((j.second & NOT_DURING_DCP_CREATION) && dcp_creation) {
1233                                 enabled = false;
1234                         }
1235
1236                         if ((j.second & NEEDS_CPL) && !have_cpl) {
1237                                 enabled = false;
1238                         }
1239
1240                         if ((j.second & NEEDS_SELECTED_CONTENT) && !have_selected_content) {
1241                                 enabled = false;
1242                         }
1243
1244                         if ((j.second & NEEDS_SINGLE_SELECTED_CONTENT) && !have_single_selected_content) {
1245                                 enabled = false;
1246                         }
1247
1248                         if ((j.second & NEEDS_SELECTED_VIDEO_CONTENT) && !have_selected_video_content) {
1249                                 enabled = false;
1250                         }
1251
1252                         if ((j.second & NEEDS_CLIPBOARD) && !_clipboard) {
1253                                 enabled = false;
1254                         }
1255
1256                         if ((j.second & NEEDS_ENCRYPTION) && (!_film || !_film->encrypted())) {
1257                                 enabled = false;
1258                         }
1259
1260                         j.first->Enable (enabled);
1261                 }
1262         }
1263
1264         /** @return true if the operation that called this method
1265          *  should continue, false to abort it.
1266          */
1267         template <class T>
1268         bool maybe_save_film ()
1269         {
1270                 if (!_film) {
1271                         return true;
1272                 }
1273
1274                 if (_film->dirty ()) {
1275                         T d (_film->name ());
1276                         switch (d.run ()) {
1277                         case wxID_NO:
1278                                 return true;
1279                         case wxID_YES:
1280                                 _film->write_metadata ();
1281                                 return true;
1282                         case wxID_CANCEL:
1283                                 return false;
1284                         }
1285                 }
1286
1287                 return true;
1288         }
1289
1290         template <class T>
1291         bool maybe_save_then_delete_film ()
1292         {
1293                 bool const r = maybe_save_film<T> ();
1294                 if (r) {
1295                         _film.reset ();
1296                 }
1297                 return r;
1298         }
1299
1300         void add_item (wxMenu* menu, wxString text, int id, int sens)
1301         {
1302                 auto item = menu->Append (id, text);
1303                 menu_items.insert (make_pair (item, sens));
1304         }
1305
1306         void setup_menu (wxMenuBar* m)
1307         {
1308                 _file_menu = new wxMenu;
1309                 /* [Shortcut] Ctrl+N:New film */
1310                 add_item (_file_menu, _("New...\tCtrl-N"), ID_file_new, ALWAYS);
1311                 /* [Shortcut] Ctrl+O:Open existing film */
1312                 add_item (_file_menu, _("&Open...\tCtrl-O"), ID_file_open, ALWAYS);
1313                 _file_menu->AppendSeparator ();
1314                 /* [Shortcut] Ctrl+S:Save current film */
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                 /* [Shortcut] Ctrl+W:Close current film */
1325                 add_item (_file_menu, _("&Close\tCtrl-W"), ID_file_close, NEEDS_FILM);
1326
1327 #ifndef __WXOSX__
1328                 _file_menu->AppendSeparator ();
1329 #endif
1330
1331 #ifdef __WXOSX__
1332                 add_item (_file_menu, _("&Exit"), wxID_EXIT, ALWAYS);
1333 #else
1334                 add_item (_file_menu, _("&Quit"), wxID_EXIT, ALWAYS);
1335 #endif
1336
1337                 auto edit = new wxMenu;
1338                 /* [Shortcut] Ctrl+C:Copy settings from currently selected content */
1339                 add_item (edit, _("Copy settings\tCtrl-C"), ID_edit_copy, NEEDS_FILM | NOT_DURING_DCP_CREATION | NEEDS_SINGLE_SELECTED_CONTENT);
1340                 /* [Shortcut] Ctrl+V:Paste settings into currently selected content */
1341                 add_item (edit, _("Paste settings...\tCtrl-V"), ID_edit_paste, NEEDS_FILM | NOT_DURING_DCP_CREATION | NEEDS_SELECTED_CONTENT | NEEDS_CLIPBOARD);
1342                 edit->AppendSeparator ();
1343                 /* [Shortcut] Shift+Ctrl+A:Select all content */
1344                 add_item (edit, _("Select all\tShift-Ctrl-A"), ID_edit_select_all, NEEDS_FILM);
1345
1346 #ifdef __WXOSX__
1347                 add_item (_file_menu, _("&Preferences...\tCtrl-P"), wxID_PREFERENCES, ALWAYS);
1348 #else
1349                 edit->AppendSeparator ();
1350                 /* [Shortcut] Ctrl+P:Open preferences window */
1351                 add_item (edit, _("&Preferences...\tCtrl-P"), wxID_PREFERENCES, ALWAYS);
1352 #endif
1353
1354                 auto jobs_menu = new wxMenu;
1355                 /* [Shortcut] Ctrl+M:Make DCP */
1356                 add_item (jobs_menu, _("&Make DCP\tCtrl-M"), ID_jobs_make_dcp, NEEDS_FILM | NOT_DURING_DCP_CREATION);
1357                 /* [Shortcut] Ctrl+B:Make DCP in the batch converter*/
1358                 add_item (jobs_menu, _("Make DCP in &batch converter\tCtrl-B"), ID_jobs_make_dcp_batch, NEEDS_FILM | NOT_DURING_DCP_CREATION);
1359                 jobs_menu->AppendSeparator ();
1360                 /* [Shortcut] Ctrl+K:Make KDMs */
1361                 add_item (jobs_menu, _("Make &KDMs...\tCtrl-K"), ID_jobs_make_kdms, NEEDS_FILM);
1362                 /* [Shortcut] Ctrl+D:Make DKDMs */
1363                 add_item (jobs_menu, _("Make &DKDMs...\tCtrl-D"), ID_jobs_make_dkdms, NEEDS_FILM);
1364                 add_item (jobs_menu, _("Make DKDM for DCP-o-matic..."), ID_jobs_make_self_dkdm, NEEDS_FILM | NEEDS_ENCRYPTION);
1365                 jobs_menu->AppendSeparator ();
1366                 /* [Shortcut] Ctrl+E:Export video file */
1367                 add_item (jobs_menu, _("Export video file...\tCtrl-E"), ID_jobs_export_video_file, NEEDS_FILM);
1368                 add_item (jobs_menu, _("Export subtitles..."), ID_jobs_export_subtitles, NEEDS_FILM);
1369                 jobs_menu->AppendSeparator ();
1370                 add_item (jobs_menu, _("&Send DCP to TMS"), ID_jobs_send_dcp_to_tms, NEEDS_FILM | NOT_DURING_DCP_CREATION | NEEDS_CPL);
1371
1372 #if defined(DCPOMATIC_OSX)
1373                 add_item (jobs_menu, _("S&how DCP in Finder"), ID_jobs_show_dcp, NEEDS_FILM | NOT_DURING_DCP_CREATION | NEEDS_CPL);
1374 #elif defined(DCPOMATIC_WINDOWS)
1375                 add_item (jobs_menu, _("S&how DCP in Explorer"), ID_jobs_show_dcp, NEEDS_FILM | NOT_DURING_DCP_CREATION | NEEDS_CPL);
1376 #else
1377                 add_item (jobs_menu, _("S&how DCP in Files"), ID_jobs_show_dcp, NEEDS_FILM | NOT_DURING_DCP_CREATION | NEEDS_CPL);
1378 #endif
1379
1380                 add_item (jobs_menu, _("Open DCP in &player"), ID_jobs_open_dcp_in_player, NEEDS_FILM | NOT_DURING_DCP_CREATION | NEEDS_CPL);
1381
1382                 auto view = new wxMenu;
1383                 add_item (view, _("Closed captions..."), ID_view_closed_captions, NEEDS_FILM);
1384                 add_item (view, _("Video waveform..."), ID_view_video_waveform, NEEDS_FILM);
1385
1386                 auto tools = new wxMenu;
1387                 add_item (tools, _("Hints..."), ID_tools_hints, NEEDS_FILM);
1388                 add_item (tools, _("Encoding servers..."), ID_tools_encoding_servers, 0);
1389                 add_item (tools, _("Manage templates..."), ID_tools_manage_templates, 0);
1390                 add_item (tools, _("Check for updates"), ID_tools_check_for_updates, 0);
1391                 add_item (tools, _("Send translations..."), ID_tools_send_translations, 0);
1392                 add_item (tools, _("System information..."), ID_tools_system_information, 0);
1393                 tools->AppendSeparator ();
1394                 add_item (tools, _("Restore default preferences"), ID_tools_restore_default_preferences, ALWAYS);
1395                 tools->AppendSeparator ();
1396                 add_item (tools, _("Export preferences..."), ID_tools_export_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-existent 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_UTF8)) {
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_UTF8
1839                                 );
1840                         int const r = d->ShowModal ();
1841                         d->Destroy ();
1842                         return r == wxID_OK;
1843                 }
1844                 case Config::BAD_SIGNER_VALIDITY_TOO_LONG:
1845                 {
1846                         if (config->nagged(Config::NAG_BAD_SIGNER_CHAIN_VALIDITY)) {
1847                                 return false;
1848                         }
1849                         auto d = new RecreateChainDialog (
1850                                 _frame, _("Recreate signing certificates"),
1851                                 _("The certificate chain that DCP-o-matic uses for signing DCPs and KDMs has a validity period\n"
1852                                   "that is too long.  This will cause problems playing back DCPs on some systems.\n"
1853                                   "Do you want to re-create the certificate chain for signing DCPs and KDMs?"),
1854                                 _("Do nothing"),
1855                                 Config::NAG_BAD_SIGNER_CHAIN_VALIDITY
1856                                 );
1857                         int const r = d->ShowModal ();
1858                         d->Destroy ();
1859                         return r == wxID_OK;
1860                 }
1861                 case Config::BAD_SIGNER_INCONSISTENT:
1862                 {
1863                         auto d = new RecreateChainDialog (
1864                                 _frame, _("Recreate signing certificates"),
1865                                 _("The certificate chain that DCP-o-matic uses for signing DCPs and KDMs is inconsistent and\n"
1866                                   "cannot be used.  DCP-o-matic cannot start unless you re-create it.  Do you want to re-create\n"
1867                                   "the certificate chain for signing DCPs and KDMs?"),
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                 case Config::BAD_DECRYPTION_INCONSISTENT:
1878                 {
1879                         auto d = new RecreateChainDialog (
1880                                 _frame, _("Recreate KDM decryption chain"),
1881                                 _("The certificate chain that DCP-o-matic uses for decrypting KDMs is inconsistent and\n"
1882                                   "cannot be used.  DCP-o-matic cannot start unless you re-create it.  Do you want to re-create\n"
1883                                   "the certificate chain for decrypting KDMs?  You may want to say \"No\" here and back up your\n"
1884                                   "configuration before continuing."),
1885                                 _("Close DCP-o-matic")
1886                                 );
1887                         int const r = d->ShowModal ();
1888                         d->Destroy ();
1889                         if (r != wxID_OK) {
1890                                 exit (EXIT_FAILURE);
1891                         }
1892                         return true;
1893                 }
1894                 default:
1895                         DCPOMATIC_ASSERT (false);
1896                 }
1897         }
1898
1899         DOMFrame* _frame = nullptr;
1900         wxSplashScreen* _splash = nullptr;
1901         shared_ptr<wxTimer> _timer;
1902         string _film_to_load;
1903         string _film_to_create;
1904         string _content_to_add;
1905         string _dcp_to_add;
1906 };
1907
1908
1909 IMPLEMENT_APP (App)