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