2 Copyright (C) 2017 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "lib/cross.h"
22 #include "lib/config.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"
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/player_config_dialog.h"
40 #include <wx/stdpaths.h>
41 #include <wx/splash.h>
42 #include <wx/cmdline.h>
43 #include <wx/preferences.h>
45 #include <ApplicationServices/ApplicationServices.h>
47 #include <boost/bind.hpp>
57 using boost::shared_ptr;
58 using boost::optional;
64 ID_view_scale_appropriate,
67 ID_view_scale_quarter,
68 ID_help_report_a_problem,
69 ID_tools_check_for_updates,
72 class DOMFrame : public wxFrame
76 : wxFrame (0, -1, _("DCP-o-matic Player"))
77 , _update_news_requested (false)
83 #if defined(DCPOMATIC_WINDOWS)
84 maybe_open_console ();
85 cout << "DCP-o-matic Player is starting." << "\n";
88 wxMenuBar* bar = new wxMenuBar;
92 #ifdef DCPOMATIC_WINDOWS
93 SetIcon (wxIcon (std_to_wx ("id")));
96 _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this));
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);
111 /* Use a panel as the only child of the Frame so that we avoid
112 the dark-grey background on Windows.
114 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
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);
123 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
126 void set_decode_reduction (optional<int> reduction)
128 _viewer->set_dcp_decode_reduction (reduction);
131 void load_dcp (boost::filesystem::path dir)
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);
137 JobManager* jm = JobManager::instance ();
138 while (jm->work_to_do ()) {
139 /* XXX: progress dialog */
140 while (signal_manager->ui_idle ()) {}
144 while (signal_manager->ui_idle ()) {}
148 BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
149 if (i->finished_in_error()) {
150 errors += std_to_wx (i->error_summary()) + "\n";
153 error_dialog (this, errors);
157 _viewer->set_film (_film);
158 _info->triggered_update ();
163 void setup_menu (wxMenuBar* m)
165 wxMenu* file = new wxMenu;
166 file->Append (ID_file_open, _("&Open...\tCtrl-O"));
167 file->Append (ID_file_add_ov, _("&Add OV..."));
168 file->Append (ID_file_add_kdm, _("&Add KDM..."));
171 file->Append (wxID_EXIT, _("&Exit"));
173 file->Append (wxID_EXIT, _("&Quit"));
177 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
179 wxMenu* edit = new wxMenu;
180 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
183 wxMenu* view = new wxMenu;
184 view->AppendRadioItem (ID_view_scale_appropriate, _("Set decode resolution to match display"));
185 view->AppendRadioItem (ID_view_scale_full, _("Decode at full resolution"));
186 view->AppendRadioItem (ID_view_scale_half, _("Decode at half resolution"));
187 view->AppendRadioItem (ID_view_scale_quarter, _("Decode at quarter resolution"));
189 wxMenu* tools = new wxMenu;
190 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
192 wxMenu* help = new wxMenu;
194 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
196 help->Append (wxID_ABOUT, _("About"));
198 help->Append (ID_help_report_a_problem, _("Report a problem..."));
200 m->Append (file, _("&File"));
202 m->Append (edit, _("&Edit"));
204 m->Append (view, _("&View"));
205 m->Append (tools, _("&Tools"));
206 m->Append (help, _("&Help"));
211 wxDirDialog* c = new wxDirDialog (
213 _("Select DCP to open"),
214 wxStandardPaths::Get().GetDocumentsDir(),
215 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
221 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
222 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
229 load_dcp (wx_to_std (c->GetPath ()));
237 wxDirDialog* c = new wxDirDialog (
239 _("Select DCP to open as OV"),
240 wxStandardPaths::Get().GetDocumentsDir(),
241 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
247 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
248 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
255 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
256 DCPOMATIC_ASSERT (dcp);
257 dcp->add_ov (wx_to_std(c->GetPath()));
258 dcp->examine (shared_ptr<Job>());
262 _info->triggered_update ();
267 wxFileDialog* d = new wxFileDialog (this, _("Select KDM"));
269 if (d->ShowModal() == wxID_OK) {
270 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
271 DCPOMATIC_ASSERT (dcp);
273 dcp->add_kdm (dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE)));
274 } catch (exception& e) {
275 error_dialog (this, wxString::Format (_("Could not load KDM (%s)"), e.what ()));
280 dcp->examine (shared_ptr<Job>());
284 _info->triggered_update ();
292 void edit_preferences ()
294 if (!_config_dialog) {
295 _config_dialog = create_player_config_dialog ();
297 _config_dialog->Show (this);
300 void tools_check_for_updates ()
302 UpdateChecker::instance()->run ();
303 _update_news_requested = true;
308 AboutDialog* d = new AboutDialog (this);
313 void help_report_a_problem ()
315 ReportProblemDialog* d = new ReportProblemDialog (this);
316 if (d->ShowModal () == wxID_OK) {
322 void update_checker_state_changed ()
324 UpdateChecker* uc = UpdateChecker::instance ();
326 bool const announce =
327 _update_news_requested ||
328 (uc->stable() && Config::instance()->check_for_updates()) ||
329 (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
331 _update_news_requested = false;
337 if (uc->state() == UpdateChecker::YES) {
338 UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
339 dialog->ShowModal ();
341 } else if (uc->state() == UpdateChecker::FAILED) {
342 error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
344 error_dialog (this, _("There are no new versions of DCP-o-matic available."));
347 _update_news_requested = false;
350 void config_changed ()
352 /* Instantly save any config changes when using the player GUI */
354 Config::instance()->write_config();
355 } catch (exception& e) {
359 _("Could not write to config file at %s. Your changes have not been saved."),
360 std_to_wx (Config::instance()->cinemas_file().string()).data()
366 bool _update_news_requested;
367 PlayerInformation* _info;
368 wxPreferencesEditor* _config_dialog;
370 boost::shared_ptr<Film> _film;
371 boost::signals2::scoped_connection _config_changed_connection;
374 static const wxCmdLineEntryDesc command_line_description[] = {
375 { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
376 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
380 * @brief The magic App class for wxWidgets.
382 class App : public wxApp
395 wxInitAllImageHandlers ();
397 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
399 wxSplashScreen* splash = 0;
401 if (!Config::have_existing ("config.xml")) {
403 boost::filesystem::path p = shared_path () / "splash.png";
404 if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
405 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
409 } catch (boost::filesystem::filesystem_error& e) {
410 /* Maybe we couldn't find the splash image; never mind */
413 SetAppName (_("DCP-o-matic Player"));
415 if (!wxApp::OnInit()) {
419 #ifdef DCPOMATIC_LINUX
420 unsetenv ("UBUNTU_MENUPROXY");
424 ProcessSerialNumber serial;
425 GetCurrentProcess (&serial);
426 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
429 dcpomatic_setup_path_encoding ();
431 /* Enable i18n; this will create a Config object
432 to look for a force-configured language. This Config
433 object will be wrong, however, because dcpomatic_setup
434 hasn't yet been called and there aren't any filters etc.
437 dcpomatic_setup_i18n ();
439 /* Set things up, including filters etc.
440 which will now be internationalised correctly.
444 /* Force the configuration to be re-loaded correctly next
449 _frame = new DOMFrame ();
450 SetTopWindow (_frame);
457 signal_manager = new wxSignalManager (this);
459 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
461 _frame->load_dcp (_dcp_to_load);
462 } catch (exception& e) {
463 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1 (%2)")), _dcp_to_load, e.what())));
467 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
469 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
470 _timer.reset (new wxTimer (this));
471 _timer->Start (1000);
473 if (Config::instance()->check_for_updates ()) {
474 UpdateChecker::instance()->run ();
481 error_dialog (0, wxString::Format ("DCP-o-matic Player could not start: %s", e.what ()));
485 void OnInitCmdLine (wxCmdLineParser& parser)
487 parser.SetDesc (command_line_description);
488 parser.SetSwitchChars (wxT ("-"));
491 bool OnCmdLineParsed (wxCmdLineParser& parser)
493 if (parser.GetParamCount() > 0) {
494 _dcp_to_load = wx_to_std (parser.GetParam (0));
500 void report_exception ()
504 } catch (FileError& e) {
508 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
509 std_to_wx (e.what()),
510 std_to_wx (e.file().string().c_str ())
513 } catch (exception& e) {
517 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
518 std_to_wx (e.what ())
522 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
526 /* An unhandled exception has occurred inside the main event loop */
527 bool OnExceptionInMainLoop ()
530 /* This will terminate the program */
534 void OnUnhandledException ()
541 signal_manager->ui_idle ();
547 EncodeServerFinder::instance()->rethrow ();
548 } catch (exception& e) {
549 error_dialog (0, std_to_wx (e.what ()));
553 void config_failed_to_load ()
555 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
559 shared_ptr<wxTimer> _timer;