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/dcp_content.h"
27 #include "lib/job_manager.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"
40 #include <wx/stdpaths.h>
41 #include <wx/splash.h>
42 #include <wx/cmdline.h>
43 #include <wx/preferences.h>
44 #include <wx/progdlg.h>
46 #include <ApplicationServices/ApplicationServices.h>
48 #include <boost/bind.hpp>
58 using boost::shared_ptr;
59 using boost::optional;
65 ID_view_scale_appropriate,
68 ID_view_scale_quarter,
69 ID_help_report_a_problem,
70 ID_tools_check_for_updates,
73 class DOMFrame : public wxFrame
77 : wxFrame (0, -1, _("DCP-o-matic Player"))
78 , _update_news_requested (false)
84 #if defined(DCPOMATIC_WINDOWS)
85 maybe_open_console ();
86 cout << "DCP-o-matic Player is starting." << "\n";
89 wxMenuBar* bar = new wxMenuBar;
93 #ifdef DCPOMATIC_WINDOWS
94 SetIcon (wxIcon (std_to_wx ("id")));
97 _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this));
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);
112 /* Use a panel as the only child of the Frame so that we avoid
113 the dark-grey background on Windows.
115 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
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);
124 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
127 void set_decode_reduction (optional<int> reduction)
129 _viewer->set_dcp_decode_reduction (reduction);
132 void load_dcp (boost::filesystem::path dir)
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, true);
138 JobManager* jm = JobManager::instance ();
140 wxProgressDialog* progress = new wxProgressDialog (_("DCP-o-matic Player"), _("Loading DCP"));
142 while (jm->work_to_do() || signal_manager->ui_idle()) {
147 progress->Destroy ();
149 DCPOMATIC_ASSERT (!jm->get().empty());
151 shared_ptr<Job> last = jm->get().back();
152 if (last->finished_in_error()) {
153 error_dialog (this, std_to_wx (last->error_summary()) + ".\n");
158 dcp->subtitle->set_use (true);
161 _viewer->set_film (_film);
162 _info->triggered_update ();
167 void setup_menu (wxMenuBar* m)
169 wxMenu* file = new wxMenu;
170 file->Append (ID_file_open, _("&Open...\tCtrl-O"));
171 file->Append (ID_file_add_ov, _("&Add OV..."));
172 file->Append (ID_file_add_kdm, _("&Add KDM..."));
175 file->Append (wxID_EXIT, _("&Exit"));
177 file->Append (wxID_EXIT, _("&Quit"));
181 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
183 wxMenu* edit = new wxMenu;
184 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
187 wxMenu* view = new wxMenu;
188 view->AppendRadioItem (ID_view_scale_appropriate, _("Set decode resolution to match display"));
189 view->AppendRadioItem (ID_view_scale_full, _("Decode at full resolution"));
190 view->AppendRadioItem (ID_view_scale_half, _("Decode at half resolution"));
191 view->AppendRadioItem (ID_view_scale_quarter, _("Decode at quarter resolution"));
193 wxMenu* tools = new wxMenu;
194 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
196 wxMenu* help = new wxMenu;
198 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
200 help->Append (wxID_ABOUT, _("About"));
202 help->Append (ID_help_report_a_problem, _("Report a problem..."));
204 m->Append (file, _("&File"));
206 m->Append (edit, _("&Edit"));
208 m->Append (view, _("&View"));
209 m->Append (tools, _("&Tools"));
210 m->Append (help, _("&Help"));
215 wxString d = wxStandardPaths::Get().GetDocumentsDir();
216 if (Config::instance()->last_player_load_directory()) {
217 d = std_to_wx (Config::instance()->last_player_load_directory()->string());
220 wxDirDialog* c = new wxDirDialog (this, _("Select DCP to open"), d, wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
225 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
226 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
233 boost::filesystem::path const dcp (wx_to_std (c->GetPath ()));
235 Config::instance()->set_last_player_load_directory (dcp.parent_path());
243 wxDirDialog* c = new wxDirDialog (
245 _("Select DCP to open as OV"),
246 wxStandardPaths::Get().GetDocumentsDir(),
247 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
253 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
254 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
261 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
262 DCPOMATIC_ASSERT (dcp);
263 dcp->add_ov (wx_to_std(c->GetPath()));
264 dcp->examine (shared_ptr<Job>());
268 _info->triggered_update ();
273 wxFileDialog* d = new wxFileDialog (this, _("Select KDM"));
275 if (d->ShowModal() == wxID_OK) {
276 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
277 DCPOMATIC_ASSERT (dcp);
279 dcp->add_kdm (dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE)));
280 } catch (exception& e) {
281 error_dialog (this, wxString::Format (_("Could not load KDM (%s)"), e.what ()));
286 dcp->examine (shared_ptr<Job>());
290 _info->triggered_update ();
298 void edit_preferences ()
300 if (!_config_dialog) {
301 _config_dialog = create_player_config_dialog ();
303 _config_dialog->Show (this);
306 void tools_check_for_updates ()
308 UpdateChecker::instance()->run ();
309 _update_news_requested = true;
314 AboutDialog* d = new AboutDialog (this);
319 void help_report_a_problem ()
321 ReportProblemDialog* d = new ReportProblemDialog (this);
322 if (d->ShowModal () == wxID_OK) {
328 void update_checker_state_changed ()
330 UpdateChecker* uc = UpdateChecker::instance ();
332 bool const announce =
333 _update_news_requested ||
334 (uc->stable() && Config::instance()->check_for_updates()) ||
335 (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
337 _update_news_requested = false;
343 if (uc->state() == UpdateChecker::YES) {
344 UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
345 dialog->ShowModal ();
347 } else if (uc->state() == UpdateChecker::FAILED) {
348 error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
350 error_dialog (this, _("There are no new versions of DCP-o-matic available."));
353 _update_news_requested = false;
356 void config_changed ()
358 /* Instantly save any config changes when using the player GUI */
360 Config::instance()->write_config();
361 } catch (exception& e) {
365 _("Could not write to config file at %s. Your changes have not been saved."),
366 std_to_wx (Config::instance()->cinemas_file().string()).data()
372 bool _update_news_requested;
373 PlayerInformation* _info;
374 wxPreferencesEditor* _config_dialog;
376 boost::shared_ptr<Film> _film;
377 boost::signals2::scoped_connection _config_changed_connection;
380 static const wxCmdLineEntryDesc command_line_description[] = {
381 { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
382 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
386 * @brief The magic App class for wxWidgets.
388 class App : public wxApp
401 wxInitAllImageHandlers ();
403 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
405 wxSplashScreen* splash = 0;
407 if (!Config::have_existing ("config.xml")) {
409 boost::filesystem::path p = shared_path () / "splash.png";
410 if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
411 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
415 } catch (boost::filesystem::filesystem_error& e) {
416 /* Maybe we couldn't find the splash image; never mind */
419 SetAppName (_("DCP-o-matic Player"));
421 if (!wxApp::OnInit()) {
425 #ifdef DCPOMATIC_LINUX
426 unsetenv ("UBUNTU_MENUPROXY");
430 ProcessSerialNumber serial;
431 GetCurrentProcess (&serial);
432 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
435 dcpomatic_setup_path_encoding ();
437 /* Enable i18n; this will create a Config object
438 to look for a force-configured language. This Config
439 object will be wrong, however, because dcpomatic_setup
440 hasn't yet been called and there aren't any filters etc.
443 dcpomatic_setup_i18n ();
445 /* Set things up, including filters etc.
446 which will now be internationalised correctly.
450 /* Force the configuration to be re-loaded correctly next
455 _frame = new DOMFrame ();
456 SetTopWindow (_frame);
463 signal_manager = new wxSignalManager (this);
465 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
467 _frame->load_dcp (_dcp_to_load);
468 } catch (exception& e) {
469 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1 (%2)")), _dcp_to_load, e.what())));
473 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
475 if (Config::instance()->check_for_updates ()) {
476 UpdateChecker::instance()->run ();
483 error_dialog (0, wxString::Format ("DCP-o-matic Player could not start: %s", e.what ()));
487 void OnInitCmdLine (wxCmdLineParser& parser)
489 parser.SetDesc (command_line_description);
490 parser.SetSwitchChars (wxT ("-"));
493 bool OnCmdLineParsed (wxCmdLineParser& parser)
495 if (parser.GetParamCount() > 0) {
496 _dcp_to_load = wx_to_std (parser.GetParam (0));
502 void report_exception ()
506 } catch (FileError& e) {
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 ())
515 } catch (exception& e) {
519 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
520 std_to_wx (e.what ())
524 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
528 /* An unhandled exception has occurred inside the main event loop */
529 bool OnExceptionInMainLoop ()
532 /* This will terminate the program */
536 void OnUnhandledException ()
543 signal_manager->ui_idle ();
546 void config_failed_to_load ()
548 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));