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