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