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