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