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