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