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