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