Add space shortcut to start/stop playback (#1201).
[dcpomatic.git] / src / tools / dcpomatic_player.cc
1 /*
2     Copyright (C) 2017-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "lib/cross.h"
22 #include "lib/config.h"
23 #include "lib/util.h"
24 #include "lib/update_checker.h"
25 #include "lib/compose.hpp"
26 #include "lib/dcp_content.h"
27 #include "lib/job_manager.h"
28 #include "lib/job.h"
29 #include "lib/video_content.h"
30 #include "lib/subtitle_content.h"
31 #include "lib/ratio.h"
32 #include "lib/verify_dcp_job.h"
33 #include "lib/dcp_examiner.h"
34 #include "wx/wx_signal_manager.h"
35 #include "wx/wx_util.h"
36 #include "wx/about_dialog.h"
37 #include "wx/report_problem_dialog.h"
38 #include "wx/film_viewer.h"
39 #include "wx/player_information.h"
40 #include "wx/update_dialog.h"
41 #include "wx/player_config_dialog.h"
42 #include "wx/verify_dcp_dialog.h"
43 #include <wx/wx.h>
44 #include <wx/stdpaths.h>
45 #include <wx/splash.h>
46 #include <wx/cmdline.h>
47 #include <wx/preferences.h>
48 #include <wx/progdlg.h>
49 #ifdef __WXOSX__
50 #include <ApplicationServices/ApplicationServices.h>
51 #endif
52 #include <boost/bind.hpp>
53 #include <iostream>
54
55 #ifdef check
56 #undef check
57 #endif
58
59 #define MAX_CPLS 32
60
61 using std::string;
62 using std::cout;
63 using std::list;
64 using std::exception;
65 using std::vector;
66 using boost::shared_ptr;
67 using boost::optional;
68 using boost::dynamic_pointer_cast;
69
70 enum {
71         ID_file_open = 1,
72         ID_file_add_ov,
73         ID_file_add_kdm,
74         ID_file_history,
75         /* Allow spare IDs after _history for the recent files list */
76         ID_file_close = 100,
77         ID_view_cpl,
78         /* Allow spare IDs for CPLs */
79         ID_view_scale_appropriate = 200,
80         ID_view_scale_full,
81         ID_view_scale_half,
82         ID_view_scale_quarter,
83         ID_help_report_a_problem,
84         ID_tools_verify,
85         ID_tools_check_for_updates,
86         /* IDs for shortcuts (with no associated menu item) */
87         ID_start_stop
88 };
89
90 class DOMFrame : public wxFrame
91 {
92 public:
93         DOMFrame ()
94                 : wxFrame (0, -1, _("DCP-o-matic Player"))
95                 , _update_news_requested (false)
96                 , _info (0)
97                 , _config_dialog (0)
98                 , _file_menu (0)
99                 , _history_items (0)
100                 , _history_position (0)
101                 , _history_separator (0)
102                 , _viewer (0)
103         {
104
105 #if defined(DCPOMATIC_WINDOWS)
106                 maybe_open_console ();
107                 cout << "DCP-o-matic Player is starting." << "\n";
108 #endif
109
110                 wxMenuBar* bar = new wxMenuBar;
111                 setup_menu (bar);
112                 set_menu_sensitivity ();
113                 SetMenuBar (bar);
114
115 #ifdef DCPOMATIC_WINDOWS
116                 SetIcon (wxIcon (std_to_wx ("id")));
117 #endif
118
119                 _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this));
120                 config_changed ();
121
122                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
123                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_ov, this), ID_file_add_ov);
124                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_kdm, this), ID_file_add_kdm);
125                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_history, this, _1), ID_file_history, ID_file_history + HISTORY_SIZE);
126                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_close, this), ID_file_close);
127                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
128                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
129                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::view_cpl, this, _1), ID_view_cpl, ID_view_cpl + MAX_CPLS);
130                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(0)), ID_view_scale_full);
131                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(1)), ID_view_scale_half);
132                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(2)), ID_view_scale_quarter);
133                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
134                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
135                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_verify, this), ID_tools_verify);
136                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
137
138                 /* Use a panel as the only child of the Frame so that we avoid
139                    the dark-grey background on Windows.
140                 */
141                 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
142
143                 _viewer = new FilmViewer (overall_panel, false, false);
144                 _viewer->set_dcp_decode_reduction (Config::instance()->decode_reduction ());
145                 _info = new PlayerInformation (overall_panel, _viewer);
146                 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
147                 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
148                 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
149                 overall_panel->SetSizer (main_sizer);
150
151 #ifdef __WXOSX__
152                 int accelerators = 2;
153 #else
154                 int accelerators = 1;
155 #endif
156
157                 wxAcceleratorEntry* accel = new wxAcceleratorEntry[accelerators];
158                 accel[0].Set(wxACCEL_NORMAL, WXK_SPACE, ID_start_stop);
159 #ifdef __WXOSX__
160                 accel[1].Set(wxACCEL_CTRL, static_cast<int>('W'), ID_file_close);
161 #endif
162                 wxAcceleratorTable accel_table (accelerators, accel);
163                 SetAcceleratorTable (accel_table);
164                 delete[] accel;
165
166                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::start_stop_pressed, this), ID_start_stop);
167
168                 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
169         }
170
171         void set_decode_reduction (optional<int> reduction)
172         {
173                 _viewer->set_dcp_decode_reduction (reduction);
174                 _info->triggered_update ();
175                 Config::instance()->set_decode_reduction (reduction);
176         }
177
178         void load_dcp (boost::filesystem::path dir)
179         {
180                 _film.reset (new Film (optional<boost::filesystem::path>()));
181                 shared_ptr<DCPContent> dcp;
182                 try {
183                         dcp.reset (new DCPContent (_film, dir));
184                 } catch (boost::filesystem::filesystem_error& e) {
185                         error_dialog (this, _("Could not load DCP"), std_to_wx (e.what()));
186                         return;
187                 }
188
189                 _film->examine_and_add_content (dcp, true);
190
191                 JobManager* jm = JobManager::instance ();
192
193                 wxProgressDialog* progress = new wxProgressDialog (_("DCP-o-matic Player"), _("Loading DCP"));
194
195                 while (jm->work_to_do() || signal_manager->ui_idle()) {
196                         dcpomatic_sleep (1);
197                         progress->Pulse ();
198                 }
199
200                 progress->Destroy ();
201
202                 DCPOMATIC_ASSERT (!jm->get().empty());
203
204                 shared_ptr<Job> last = jm->get().back();
205                 if (last->finished_in_error()) {
206                         error_dialog(this, std_to_wx(last->error_summary()) + ".\n", std_to_wx(last->error_details()));
207                         return;
208                 }
209
210                 if (dcp->subtitle) {
211                         dcp->subtitle->set_use (true);
212                 }
213
214                 Ratio const * r = Ratio::nearest_from_ratio(dcp->video->size().ratio());
215                 if (r) {
216                         _film->set_container(r);
217                 }
218
219                 _viewer->set_film (_film);
220                 _viewer->set_position (DCPTime ());
221                 _info->triggered_update ();
222
223                 Config::instance()->add_to_player_history (dir);
224
225                 set_menu_sensitivity ();
226
227                 wxMenuItemList old = _cpl_menu->GetMenuItems();
228                 for (wxMenuItemList::iterator i = old.begin(); i != old.end(); ++i) {
229                         _cpl_menu->Remove (*i);
230                 }
231
232                 DCPExaminer ex (dcp);
233                 int id = ID_view_cpl;
234                 BOOST_FOREACH (shared_ptr<dcp::CPL> i, ex.cpls()) {
235                         wxMenuItem* j = _cpl_menu->AppendRadioItem(id, i->id());
236                         if (!dcp->cpl() || i->id() == *dcp->cpl()) {
237                                 j->Check(true);
238                         }
239                 }
240         }
241
242 private:
243
244         void setup_menu (wxMenuBar* m)
245         {
246                 _file_menu = new wxMenu;
247                 _file_menu->Append (ID_file_open, _("&Open...\tCtrl-O"));
248                 _file_add_ov = _file_menu->Append (ID_file_add_ov, _("&Add OV..."));
249                 _file_add_kdm = _file_menu->Append (ID_file_add_kdm, _("&Add KDM..."));
250
251                 _history_position = _file_menu->GetMenuItems().GetCount();
252
253                 _file_menu->AppendSeparator ();
254                 _file_menu->Append (ID_file_close, _("&Close"));
255                 _file_menu->AppendSeparator ();
256
257 #ifdef __WXOSX__
258                 _file_menu->Append (wxID_EXIT, _("&Exit"));
259 #else
260                 _file_menu->Append (wxID_EXIT, _("&Quit"));
261 #endif
262
263 #ifdef __WXOSX__
264                 _file_menu->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
265 #else
266                 wxMenu* edit = new wxMenu;
267                 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
268 #endif
269
270                 _cpl_menu = new wxMenu;
271
272                 wxMenu* view = new wxMenu;
273                 optional<int> c = Config::instance()->decode_reduction();
274                 _view_cpl = view->Append(ID_view_cpl, _("CPL"), _cpl_menu);
275                 view->AppendSeparator();
276                 view->AppendRadioItem(ID_view_scale_appropriate, _("Set decode resolution to match display"))->Check(!static_cast<bool>(c));
277                 view->AppendRadioItem(ID_view_scale_full, _("Decode at full resolution"))->Check(c && c.get() == 0);
278                 view->AppendRadioItem(ID_view_scale_half, _("Decode at half resolution"))->Check(c && c.get() == 1);
279                 view->AppendRadioItem(ID_view_scale_quarter, _("Decode at quarter resolution"))->Check(c && c.get() == 2);
280
281                 wxMenu* tools = new wxMenu;
282                 _tools_verify = tools->Append (ID_tools_verify, _("Verify DCP"));
283                 tools->AppendSeparator ();
284                 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
285
286                 wxMenu* help = new wxMenu;
287 #ifdef __WXOSX__
288                 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
289 #else
290                 help->Append (wxID_ABOUT, _("About"));
291 #endif
292                 help->Append (ID_help_report_a_problem, _("Report a problem..."));
293
294                 m->Append (_file_menu, _("&File"));
295 #ifndef __WXOSX__
296                 m->Append (edit, _("&Edit"));
297 #endif
298                 m->Append (view, _("&View"));
299                 m->Append (tools, _("&Tools"));
300                 m->Append (help, _("&Help"));
301         }
302
303         void file_open ()
304         {
305                 wxString d = wxStandardPaths::Get().GetDocumentsDir();
306                 if (Config::instance()->last_player_load_directory()) {
307                         d = std_to_wx (Config::instance()->last_player_load_directory()->string());
308                 }
309
310                 wxDirDialog* c = new wxDirDialog (this, _("Select DCP to open"), d, wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
311
312                 int r;
313                 while (true) {
314                         r = c->ShowModal ();
315                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
316                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
317                         } else {
318                                 break;
319                         }
320                 }
321
322                 if (r == wxID_OK) {
323                         boost::filesystem::path const dcp (wx_to_std (c->GetPath ()));
324                         load_dcp (dcp);
325                         Config::instance()->set_last_player_load_directory (dcp.parent_path());
326                 }
327
328                 c->Destroy ();
329         }
330
331         void file_add_ov ()
332         {
333                 wxDirDialog* c = new wxDirDialog (
334                         this,
335                         _("Select DCP to open as OV"),
336                         wxStandardPaths::Get().GetDocumentsDir(),
337                         wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
338                         );
339
340                 int r;
341                 while (true) {
342                         r = c->ShowModal ();
343                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
344                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
345                         } else {
346                                 break;
347                         }
348                 }
349
350                 if (r == wxID_OK) {
351                         DCPOMATIC_ASSERT (_film);
352                         shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
353                         DCPOMATIC_ASSERT (dcp);
354                         dcp->add_ov (wx_to_std(c->GetPath()));
355                         dcp->examine (shared_ptr<Job>());
356                         /* Maybe we just gained some subtitles */
357                         if (dcp->subtitle) {
358                                 dcp->subtitle->set_use (true);
359                         }
360                 }
361
362                 c->Destroy ();
363                 _info->triggered_update ();
364         }
365
366         void file_add_kdm ()
367         {
368                 wxFileDialog* d = new wxFileDialog (this, _("Select KDM"));
369
370                 if (d->ShowModal() == wxID_OK) {
371                         DCPOMATIC_ASSERT (_film);
372                         shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
373                         DCPOMATIC_ASSERT (dcp);
374                         try {
375                                 dcp->add_kdm (dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE)));
376                                 dcp->examine (shared_ptr<Job>());
377                         } catch (exception& e) {
378                                 error_dialog (this, wxString::Format (_("Could not load KDM.")), std_to_wx(e.what()));
379                                 d->Destroy ();
380                                 return;
381                         }
382                 }
383
384                 d->Destroy ();
385                 _info->triggered_update ();
386         }
387
388         void file_history (wxCommandEvent& event)
389         {
390                 vector<boost::filesystem::path> history = Config::instance()->player_history ();
391                 int n = event.GetId() - ID_file_history;
392                 if (n >= 0 && n < static_cast<int> (history.size ())) {
393                         load_dcp (history[n]);
394                 }
395         }
396
397         void file_close ()
398         {
399                 _viewer->set_film (shared_ptr<Film>());
400                 _film.reset ();
401                 _info->triggered_update ();
402                 set_menu_sensitivity ();
403         }
404
405         void file_exit ()
406         {
407                 Close ();
408         }
409
410         void edit_preferences ()
411         {
412                 if (!_config_dialog) {
413                         _config_dialog = create_player_config_dialog ();
414                 }
415                 _config_dialog->Show (this);
416         }
417
418         void view_cpl (wxCommandEvent& ev)
419         {
420                 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
421                 DCPOMATIC_ASSERT (dcp);
422                 DCPExaminer ex (dcp);
423                 int id = ev.GetId() - ID_view_cpl;
424                 DCPOMATIC_ASSERT (id >= 0);
425                 DCPOMATIC_ASSERT (id < int(ex.cpls().size()));
426                 list<shared_ptr<dcp::CPL> > cpls = ex.cpls();
427                 list<shared_ptr<dcp::CPL> >::iterator i = cpls.begin();
428                 while (id > 0) {
429                         ++i;
430                         --id;
431                 }
432
433                 dcp->set_cpl ((*i)->id());
434                 dcp->examine (shared_ptr<Job>());
435         }
436
437         void tools_verify ()
438         {
439                 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
440                 DCPOMATIC_ASSERT (dcp);
441
442                 JobManager* jm = JobManager::instance ();
443                 jm->add (shared_ptr<Job> (new VerifyDCPJob (dcp->directories())));
444
445                 wxProgressDialog* progress = new wxProgressDialog (_("DCP-o-matic Player"), _("Verifying DCP"));
446
447                 while (jm->work_to_do() || signal_manager->ui_idle()) {
448                         dcpomatic_sleep (1);
449                         progress->Pulse ();
450                 }
451
452                 progress->Destroy ();
453
454                 DCPOMATIC_ASSERT (!jm->get().empty());
455                 shared_ptr<VerifyDCPJob> last = dynamic_pointer_cast<VerifyDCPJob> (jm->get().back());
456                 DCPOMATIC_ASSERT (last);
457
458                 VerifyDCPDialog* d = new VerifyDCPDialog (this, last->notes ());
459                 d->ShowModal ();
460                 d->Destroy ();
461         }
462
463         void tools_check_for_updates ()
464         {
465                 UpdateChecker::instance()->run ();
466                 _update_news_requested = true;
467         }
468
469         void help_about ()
470         {
471                 AboutDialog* d = new AboutDialog (this);
472                 d->ShowModal ();
473                 d->Destroy ();
474         }
475
476         void help_report_a_problem ()
477         {
478                 ReportProblemDialog* d = new ReportProblemDialog (this);
479                 if (d->ShowModal () == wxID_OK) {
480                         d->report ();
481                 }
482                 d->Destroy ();
483         }
484
485         void update_checker_state_changed ()
486         {
487                 UpdateChecker* uc = UpdateChecker::instance ();
488
489                 bool const announce =
490                         _update_news_requested ||
491                         (uc->stable() && Config::instance()->check_for_updates()) ||
492                         (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
493
494                 _update_news_requested = false;
495
496                 if (!announce) {
497                         return;
498                 }
499
500                 if (uc->state() == UpdateChecker::YES) {
501                         UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
502                         dialog->ShowModal ();
503                         dialog->Destroy ();
504                 } else if (uc->state() == UpdateChecker::FAILED) {
505                         error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
506                 } else {
507                         error_dialog (this, _("There are no new versions of DCP-o-matic available."));
508                 }
509
510                 _update_news_requested = false;
511         }
512
513         void config_changed ()
514         {
515                 /* Instantly save any config changes when using the player GUI */
516                 try {
517                         Config::instance()->write_config();
518                 } catch (exception& e) {
519                         error_dialog (
520                                 this,
521                                 wxString::Format (
522                                         _("Could not write to config file at %s.  Your changes have not been saved."),
523                                         std_to_wx (Config::instance()->cinemas_file().string()).data()
524                                         )
525                                 );
526                 }
527
528                 for (int i = 0; i < _history_items; ++i) {
529                         delete _file_menu->Remove (ID_file_history + i);
530                 }
531
532                 if (_history_separator) {
533                         _file_menu->Remove (_history_separator);
534                 }
535                 delete _history_separator;
536                 _history_separator = 0;
537
538                 int pos = _history_position;
539
540                 vector<boost::filesystem::path> history = Config::instance()->player_history ();
541
542                 if (!history.empty ()) {
543                         _history_separator = _file_menu->InsertSeparator (pos++);
544                 }
545
546                 for (size_t i = 0; i < history.size(); ++i) {
547                         string s;
548                         if (i < 9) {
549                                 s = String::compose ("&%1 %2", i + 1, history[i].string());
550                         } else {
551                                 s = history[i].string();
552                         }
553                         _file_menu->Insert (pos++, ID_file_history + i, std_to_wx (s));
554                 }
555
556                 _history_items = history.size ();
557         }
558
559         void set_menu_sensitivity ()
560         {
561                 _tools_verify->Enable (static_cast<bool>(_film));
562                 _file_add_ov->Enable (static_cast<bool>(_film));
563                 _file_add_kdm->Enable (static_cast<bool>(_film));
564                 _view_cpl->Enable (static_cast<bool>(_film));
565         }
566
567         void start_stop_pressed ()
568         {
569                 if (_viewer->playing()) {
570                         _viewer->stop();
571                 } else {
572                         _viewer->start();
573                 }
574         }
575
576         bool _update_news_requested;
577         PlayerInformation* _info;
578         wxPreferencesEditor* _config_dialog;
579         wxMenu* _file_menu;
580         wxMenuItem* _view_cpl;
581         wxMenu* _cpl_menu;
582         int _history_items;
583         int _history_position;
584         wxMenuItem* _history_separator;
585         FilmViewer* _viewer;
586         boost::shared_ptr<Film> _film;
587         boost::signals2::scoped_connection _config_changed_connection;
588         wxMenuItem* _file_add_ov;
589         wxMenuItem* _file_add_kdm;
590         wxMenuItem* _tools_verify;
591 };
592
593 static const wxCmdLineEntryDesc command_line_description[] = {
594         { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
595         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
596 };
597
598 /** @class App
599  *  @brief The magic App class for wxWidgets.
600  */
601 class App : public wxApp
602 {
603 public:
604         App ()
605                 : wxApp ()
606                 , _frame (0)
607         {}
608
609 private:
610
611         bool OnInit ()
612         try
613         {
614                 wxInitAllImageHandlers ();
615
616                 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
617                 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
618
619                 wxSplashScreen* splash = maybe_show_splash ();
620
621                 SetAppName (_("DCP-o-matic Player"));
622
623                 if (!wxApp::OnInit()) {
624                         return false;
625                 }
626
627 #ifdef DCPOMATIC_LINUX
628                 unsetenv ("UBUNTU_MENUPROXY");
629 #endif
630
631 #ifdef __WXOSX__
632                 ProcessSerialNumber serial;
633                 GetCurrentProcess (&serial);
634                 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
635 #endif
636
637                 dcpomatic_setup_path_encoding ();
638
639                 /* Enable i18n; this will create a Config object
640                    to look for a force-configured language.  This Config
641                    object will be wrong, however, because dcpomatic_setup
642                    hasn't yet been called and there aren't any filters etc.
643                    set up yet.
644                 */
645                 dcpomatic_setup_i18n ();
646
647                 /* Set things up, including filters etc.
648                    which will now be internationalised correctly.
649                 */
650                 dcpomatic_setup ();
651
652                 /* Force the configuration to be re-loaded correctly next
653                    time it is needed.
654                 */
655                 Config::drop ();
656
657                 _frame = new DOMFrame ();
658                 SetTopWindow (_frame);
659                 _frame->Maximize ();
660                 if (splash) {
661                         splash->Destroy ();
662                 }
663                 _frame->Show ();
664
665                 signal_manager = new wxSignalManager (this);
666
667                 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
668                         try {
669                                 _frame->load_dcp (_dcp_to_load);
670                         } catch (exception& e) {
671                                 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1.")), _dcp_to_load)), std_to_wx(e.what()));
672                         }
673                 }
674
675                 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
676
677                 if (Config::instance()->check_for_updates ()) {
678                         UpdateChecker::instance()->run ();
679                 }
680
681                 return true;
682         }
683         catch (exception& e)
684         {
685                 error_dialog (0, _("DCP-o-matic Player could not start."), std_to_wx(e.what()));
686                 return true;
687         }
688
689         void OnInitCmdLine (wxCmdLineParser& parser)
690         {
691                 parser.SetDesc (command_line_description);
692                 parser.SetSwitchChars (wxT ("-"));
693         }
694
695         bool OnCmdLineParsed (wxCmdLineParser& parser)
696         {
697                 if (parser.GetParamCount() > 0) {
698                         _dcp_to_load = wx_to_std (parser.GetParam (0));
699                 }
700
701                 return true;
702         }
703
704         void report_exception ()
705         {
706                 try {
707                         throw;
708                 } catch (FileError& e) {
709                         error_dialog (
710                                 0,
711                                 wxString::Format (
712                                         _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
713                                         std_to_wx (e.what()),
714                                         std_to_wx (e.file().string().c_str ())
715                                         )
716                                 );
717                 } catch (exception& e) {
718                         error_dialog (
719                                 0,
720                                 wxString::Format (
721                                         _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
722                                         std_to_wx (e.what ())
723                                         )
724                                 );
725                 } catch (...) {
726                         error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
727                 }
728         }
729
730         /* An unhandled exception has occurred inside the main event loop */
731         bool OnExceptionInMainLoop ()
732         {
733                 report_exception ();
734                 /* This will terminate the program */
735                 return false;
736         }
737
738         void OnUnhandledException ()
739         {
740                 report_exception ();
741         }
742
743         void idle ()
744         {
745                 signal_manager->ui_idle ();
746         }
747
748         void config_failed_to_load ()
749         {
750                 message_dialog (_frame, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
751         }
752
753         void config_warning (string m)
754         {
755                 message_dialog (_frame, std_to_wx (m));
756         }
757
758         DOMFrame* _frame;
759         string _dcp_to_load;
760 };
761
762 IMPLEMENT_APP (App)