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