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