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;
66 ID_view_scale_appropriate,
69 ID_view_scale_quarter,
70 ID_help_report_a_problem,
71 ID_tools_check_for_updates,
74 class DOMFrame : public wxFrame
78 : wxFrame (0, -1, _("DCP-o-matic Player"))
79 , _update_news_requested (false)
85 #if defined(DCPOMATIC_WINDOWS)
86 maybe_open_console ();
87 cout << "DCP-o-matic Player is starting." << "\n";
90 wxMenuBar* bar = new wxMenuBar;
94 #ifdef DCPOMATIC_WINDOWS
95 SetIcon (wxIcon (std_to_wx ("id")));
98 _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this));
100 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
101 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_ov, this), ID_file_add_ov);
102 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_kdm, this), ID_file_add_kdm);
103 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_close, this), ID_file_close);
104 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
105 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
106 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>()), ID_view_scale_appropriate);
107 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(0)), ID_view_scale_full);
108 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(1)), ID_view_scale_half);
109 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(2)), ID_view_scale_quarter);
110 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
111 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
112 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
114 /* Use a panel as the only child of the Frame so that we avoid
115 the dark-grey background on Windows.
117 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
119 _viewer = new FilmViewer (overall_panel, false, false);
120 _info = new PlayerInformation (overall_panel, _viewer);
121 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
122 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
123 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
124 overall_panel->SetSizer (main_sizer);
126 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
129 void set_decode_reduction (optional<int> reduction)
131 _viewer->set_dcp_decode_reduction (reduction);
132 _info->triggered_update ();
135 void load_dcp (boost::filesystem::path dir)
137 _film.reset (new Film (optional<boost::filesystem::path>()));
138 shared_ptr<DCPContent> dcp (new DCPContent (_film, dir));
139 _film->examine_and_add_content (dcp, true);
141 JobManager* jm = JobManager::instance ();
143 wxProgressDialog* progress = new wxProgressDialog (_("DCP-o-matic Player"), _("Loading DCP"));
145 while (jm->work_to_do() || signal_manager->ui_idle()) {
150 progress->Destroy ();
152 DCPOMATIC_ASSERT (!jm->get().empty());
154 shared_ptr<Job> last = jm->get().back();
155 if (last->finished_in_error()) {
156 error_dialog (this, std_to_wx (last->error_summary()) + ".\n");
161 dcp->subtitle->set_use (true);
164 _viewer->set_film (_film);
165 _viewer->set_position (DCPTime ());
166 _info->triggered_update ();
171 void setup_menu (wxMenuBar* m)
173 wxMenu* file = new wxMenu;
174 file->Append (ID_file_open, _("&Open...\tCtrl-O"));
175 file->Append (ID_file_add_ov, _("&Add OV..."));
176 file->Append (ID_file_add_kdm, _("&Add KDM..."));
177 file->AppendSeparator ();
178 file->Append (ID_file_close, _("&Close"));
179 file->AppendSeparator ();
182 file->Append (wxID_EXIT, _("&Exit"));
184 file->Append (wxID_EXIT, _("&Quit"));
188 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
190 wxMenu* edit = new wxMenu;
191 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
194 wxMenu* view = new wxMenu;
195 view->AppendRadioItem (ID_view_scale_appropriate, _("Set decode resolution to match display"));
196 view->AppendRadioItem (ID_view_scale_full, _("Decode at full resolution"));
197 view->AppendRadioItem (ID_view_scale_half, _("Decode at half resolution"));
198 view->AppendRadioItem (ID_view_scale_quarter, _("Decode at quarter resolution"));
200 wxMenu* tools = new wxMenu;
201 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
203 wxMenu* help = new wxMenu;
205 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
207 help->Append (wxID_ABOUT, _("About"));
209 help->Append (ID_help_report_a_problem, _("Report a problem..."));
211 m->Append (file, _("&File"));
213 m->Append (edit, _("&Edit"));
215 m->Append (view, _("&View"));
216 m->Append (tools, _("&Tools"));
217 m->Append (help, _("&Help"));
222 wxString d = wxStandardPaths::Get().GetDocumentsDir();
223 if (Config::instance()->last_player_load_directory()) {
224 d = std_to_wx (Config::instance()->last_player_load_directory()->string());
227 wxDirDialog* c = new wxDirDialog (this, _("Select DCP to open"), d, wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
232 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
233 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
240 boost::filesystem::path const dcp (wx_to_std (c->GetPath ()));
242 Config::instance()->set_last_player_load_directory (dcp.parent_path());
250 wxDirDialog* c = new wxDirDialog (
252 _("Select DCP to open as OV"),
253 wxStandardPaths::Get().GetDocumentsDir(),
254 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
260 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
261 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
268 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
269 DCPOMATIC_ASSERT (dcp);
270 dcp->add_ov (wx_to_std(c->GetPath()));
271 dcp->examine (shared_ptr<Job>());
275 _info->triggered_update ();
280 wxFileDialog* d = new wxFileDialog (this, _("Select KDM"));
282 if (d->ShowModal() == wxID_OK) {
283 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
284 DCPOMATIC_ASSERT (dcp);
286 dcp->add_kdm (dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE)));
287 } catch (exception& e) {
288 error_dialog (this, wxString::Format (_("Could not load KDM (%s)"), std_to_wx(e.what())));
293 dcp->examine (shared_ptr<Job>());
297 _info->triggered_update ();
302 _viewer->set_film (shared_ptr<Film>());
303 _info->triggered_update ();
311 void edit_preferences ()
313 if (!_config_dialog) {
314 _config_dialog = create_player_config_dialog ();
316 _config_dialog->Show (this);
319 void tools_check_for_updates ()
321 UpdateChecker::instance()->run ();
322 _update_news_requested = true;
327 AboutDialog* d = new AboutDialog (this);
332 void help_report_a_problem ()
334 ReportProblemDialog* d = new ReportProblemDialog (this);
335 if (d->ShowModal () == wxID_OK) {
341 void update_checker_state_changed ()
343 UpdateChecker* uc = UpdateChecker::instance ();
345 bool const announce =
346 _update_news_requested ||
347 (uc->stable() && Config::instance()->check_for_updates()) ||
348 (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
350 _update_news_requested = false;
356 if (uc->state() == UpdateChecker::YES) {
357 UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
358 dialog->ShowModal ();
360 } else if (uc->state() == UpdateChecker::FAILED) {
361 error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
363 error_dialog (this, _("There are no new versions of DCP-o-matic available."));
366 _update_news_requested = false;
369 void config_changed ()
371 /* Instantly save any config changes when using the player GUI */
373 Config::instance()->write_config();
374 } catch (exception& e) {
378 _("Could not write to config file at %s. Your changes have not been saved."),
379 std_to_wx (Config::instance()->cinemas_file().string()).data()
385 bool _update_news_requested;
386 PlayerInformation* _info;
387 wxPreferencesEditor* _config_dialog;
389 boost::shared_ptr<Film> _film;
390 boost::signals2::scoped_connection _config_changed_connection;
393 static const wxCmdLineEntryDesc command_line_description[] = {
394 { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
395 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
399 * @brief The magic App class for wxWidgets.
401 class App : public wxApp
414 wxInitAllImageHandlers ();
416 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
418 wxSplashScreen* splash = 0;
420 if (!Config::have_existing ("config.xml")) {
422 boost::filesystem::path p = shared_path () / "splash.png";
423 if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
424 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
428 } catch (boost::filesystem::filesystem_error& e) {
429 /* Maybe we couldn't find the splash image; never mind */
432 SetAppName (_("DCP-o-matic Player"));
434 if (!wxApp::OnInit()) {
438 #ifdef DCPOMATIC_LINUX
439 unsetenv ("UBUNTU_MENUPROXY");
443 ProcessSerialNumber serial;
444 GetCurrentProcess (&serial);
445 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
448 dcpomatic_setup_path_encoding ();
450 /* Enable i18n; this will create a Config object
451 to look for a force-configured language. This Config
452 object will be wrong, however, because dcpomatic_setup
453 hasn't yet been called and there aren't any filters etc.
456 dcpomatic_setup_i18n ();
458 /* Set things up, including filters etc.
459 which will now be internationalised correctly.
463 /* Force the configuration to be re-loaded correctly next
468 _frame = new DOMFrame ();
469 SetTopWindow (_frame);
476 signal_manager = new wxSignalManager (this);
478 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
480 _frame->load_dcp (_dcp_to_load);
481 } catch (exception& e) {
482 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1 (%2)")), _dcp_to_load, e.what())));
486 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
488 if (Config::instance()->check_for_updates ()) {
489 UpdateChecker::instance()->run ();
496 error_dialog (0, wxString::Format ("DCP-o-matic Player could not start: %s", std_to_wx(e.what())));
500 void OnInitCmdLine (wxCmdLineParser& parser)
502 parser.SetDesc (command_line_description);
503 parser.SetSwitchChars (wxT ("-"));
506 bool OnCmdLineParsed (wxCmdLineParser& parser)
508 if (parser.GetParamCount() > 0) {
509 _dcp_to_load = wx_to_std (parser.GetParam (0));
515 void report_exception ()
519 } catch (FileError& e) {
523 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
524 std_to_wx (e.what()),
525 std_to_wx (e.file().string().c_str ())
528 } catch (exception& e) {
532 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
533 std_to_wx (e.what ())
537 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
541 /* An unhandled exception has occurred inside the main event loop */
542 bool OnExceptionInMainLoop ()
545 /* This will terminate the program */
549 void OnUnhandledException ()
556 signal_manager->ui_idle ();
559 void config_failed_to_load ()
561 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));