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