2 Copyright (C) 2017-2018 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 "lib/ratio.h"
32 #include "wx/wx_signal_manager.h"
33 #include "wx/wx_util.h"
34 #include "wx/about_dialog.h"
35 #include "wx/report_problem_dialog.h"
36 #include "wx/film_viewer.h"
37 #include "wx/player_information.h"
38 #include "wx/update_dialog.h"
39 #include "wx/player_config_dialog.h"
41 #include <wx/stdpaths.h>
42 #include <wx/splash.h>
43 #include <wx/cmdline.h>
44 #include <wx/preferences.h>
45 #include <wx/progdlg.h>
47 #include <ApplicationServices/ApplicationServices.h>
49 #include <boost/bind.hpp>
60 using boost::shared_ptr;
61 using boost::optional;
68 /* Allow spare IDs after _history for the recent files list */
70 ID_view_scale_appropriate,
73 ID_view_scale_quarter,
74 ID_help_report_a_problem,
75 ID_tools_check_for_updates,
78 class DOMFrame : public wxFrame
82 : wxFrame (0, -1, _("DCP-o-matic Player"))
83 , _update_news_requested (false)
88 , _history_position (0)
89 , _history_separator (0)
93 #if defined(DCPOMATIC_WINDOWS)
94 maybe_open_console ();
95 cout << "DCP-o-matic Player is starting." << "\n";
98 wxMenuBar* bar = new wxMenuBar;
100 set_menu_sensitivity ();
103 #ifdef DCPOMATIC_WINDOWS
104 SetIcon (wxIcon (std_to_wx ("id")));
107 _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this));
110 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
111 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_ov, this), ID_file_add_ov);
112 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_kdm, this), ID_file_add_kdm);
113 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_history, this, _1), ID_file_history, ID_file_history + HISTORY_SIZE);
114 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_close, this), ID_file_close);
115 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
116 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
117 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>()), ID_view_scale_appropriate);
118 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(0)), ID_view_scale_full);
119 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(1)), ID_view_scale_half);
120 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(2)), ID_view_scale_quarter);
121 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
122 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
123 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
125 /* Use a panel as the only child of the Frame so that we avoid
126 the dark-grey background on Windows.
128 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
130 _viewer = new FilmViewer (overall_panel, false, false);
131 _viewer->set_dcp_decode_reduction (Config::instance()->decode_reduction ());
132 _info = new PlayerInformation (overall_panel, _viewer);
133 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
134 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
135 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
136 overall_panel->SetSizer (main_sizer);
139 wxAcceleratorEntry* accel = new wxAcceleratorEntry[1];
140 accel[0].Set(wxACCEL_CTRL, static_cast<int>('W'), ID_file_close);
141 wxAcceleratorTable accel_table (1, accel);
142 SetAcceleratorTable (accel_table);
146 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
149 void set_decode_reduction (optional<int> reduction)
151 _viewer->set_dcp_decode_reduction (reduction);
152 _info->triggered_update ();
153 Config::instance()->set_decode_reduction (reduction);
156 void load_dcp (boost::filesystem::path dir)
158 _film.reset (new Film (optional<boost::filesystem::path>()));
159 shared_ptr<DCPContent> dcp;
161 dcp.reset (new DCPContent (_film, dir));
162 } catch (boost::filesystem::filesystem_error& e) {
163 error_dialog (this, _("Could not load DCP"), std_to_wx (e.what()));
167 _film->examine_and_add_content (dcp, true);
169 JobManager* jm = JobManager::instance ();
171 wxProgressDialog* progress = new wxProgressDialog (_("DCP-o-matic Player"), _("Loading DCP"));
173 while (jm->work_to_do() || signal_manager->ui_idle()) {
178 progress->Destroy ();
180 DCPOMATIC_ASSERT (!jm->get().empty());
182 shared_ptr<Job> last = jm->get().back();
183 if (last->finished_in_error()) {
184 error_dialog (this, std_to_wx (last->error_summary()) + ".\n");
189 dcp->subtitle->set_use (true);
192 Ratio const * r = Ratio::nearest_from_ratio(dcp->video->size().ratio());
194 _film->set_container(r);
197 _viewer->set_film (_film);
198 _viewer->set_position (DCPTime ());
199 _info->triggered_update ();
201 Config::instance()->add_to_player_history (dir);
203 set_menu_sensitivity ();
208 void setup_menu (wxMenuBar* m)
210 _file_menu = new wxMenu;
211 _file_menu->Append (ID_file_open, _("&Open...\tCtrl-O"));
212 _file_add_ov = _file_menu->Append (ID_file_add_ov, _("&Add OV..."));
213 _file_add_kdm = _file_menu->Append (ID_file_add_kdm, _("&Add KDM..."));
215 _history_position = _file_menu->GetMenuItems().GetCount();
217 _file_menu->AppendSeparator ();
218 _file_menu->Append (ID_file_close, _("&Close"));
219 _file_menu->AppendSeparator ();
222 _file_menu->Append (wxID_EXIT, _("&Exit"));
224 _file_menu->Append (wxID_EXIT, _("&Quit"));
228 _file_menu->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
230 wxMenu* edit = new wxMenu;
231 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
234 wxMenu* view = new wxMenu;
235 optional<int> c = Config::instance()->decode_reduction();
236 view->AppendRadioItem(ID_view_scale_appropriate, _("Set decode resolution to match display"))->Check(!static_cast<bool>(c));
237 view->AppendRadioItem(ID_view_scale_full, _("Decode at full resolution"))->Check(c && c.get() == 0);
238 view->AppendRadioItem(ID_view_scale_half, _("Decode at half resolution"))->Check(c && c.get() == 1);
239 view->AppendRadioItem(ID_view_scale_quarter, _("Decode at quarter resolution"))->Check(c && c.get() == 2);
241 wxMenu* tools = new wxMenu;
242 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
244 wxMenu* help = new wxMenu;
246 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
248 help->Append (wxID_ABOUT, _("About"));
250 help->Append (ID_help_report_a_problem, _("Report a problem..."));
252 m->Append (_file_menu, _("&File"));
254 m->Append (edit, _("&Edit"));
256 m->Append (view, _("&View"));
257 m->Append (tools, _("&Tools"));
258 m->Append (help, _("&Help"));
263 wxString d = wxStandardPaths::Get().GetDocumentsDir();
264 if (Config::instance()->last_player_load_directory()) {
265 d = std_to_wx (Config::instance()->last_player_load_directory()->string());
268 wxDirDialog* c = new wxDirDialog (this, _("Select DCP to open"), d, wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
273 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
274 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
281 boost::filesystem::path const dcp (wx_to_std (c->GetPath ()));
283 Config::instance()->set_last_player_load_directory (dcp.parent_path());
291 wxDirDialog* c = new wxDirDialog (
293 _("Select DCP to open as OV"),
294 wxStandardPaths::Get().GetDocumentsDir(),
295 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
301 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
302 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
309 DCPOMATIC_ASSERT (_film);
310 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
311 DCPOMATIC_ASSERT (dcp);
312 dcp->add_ov (wx_to_std(c->GetPath()));
313 dcp->examine (shared_ptr<Job>());
317 _info->triggered_update ();
322 wxFileDialog* d = new wxFileDialog (this, _("Select KDM"));
324 if (d->ShowModal() == wxID_OK) {
325 DCPOMATIC_ASSERT (_film);
326 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
327 DCPOMATIC_ASSERT (dcp);
329 dcp->add_kdm (dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE)));
330 dcp->examine (shared_ptr<Job>());
331 } catch (exception& e) {
332 error_dialog (this, wxString::Format (_("Could not load KDM.")), std_to_wx(e.what()));
339 _info->triggered_update ();
342 void file_history (wxCommandEvent& event)
344 vector<boost::filesystem::path> history = Config::instance()->player_history ();
345 int n = event.GetId() - ID_file_history;
346 if (n >= 0 && n < static_cast<int> (history.size ())) {
347 load_dcp (history[n]);
353 _viewer->set_film (shared_ptr<Film>());
355 _info->triggered_update ();
356 set_menu_sensitivity ();
364 void edit_preferences ()
366 if (!_config_dialog) {
367 _config_dialog = create_player_config_dialog ();
369 _config_dialog->Show (this);
372 void tools_check_for_updates ()
374 UpdateChecker::instance()->run ();
375 _update_news_requested = true;
380 AboutDialog* d = new AboutDialog (this);
385 void help_report_a_problem ()
387 ReportProblemDialog* d = new ReportProblemDialog (this);
388 if (d->ShowModal () == wxID_OK) {
394 void update_checker_state_changed ()
396 UpdateChecker* uc = UpdateChecker::instance ();
398 bool const announce =
399 _update_news_requested ||
400 (uc->stable() && Config::instance()->check_for_updates()) ||
401 (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
403 _update_news_requested = false;
409 if (uc->state() == UpdateChecker::YES) {
410 UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
411 dialog->ShowModal ();
413 } else if (uc->state() == UpdateChecker::FAILED) {
414 error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
416 error_dialog (this, _("There are no new versions of DCP-o-matic available."));
419 _update_news_requested = false;
422 void config_changed ()
424 /* Instantly save any config changes when using the player GUI */
426 Config::instance()->write_config();
427 } catch (exception& e) {
431 _("Could not write to config file at %s. Your changes have not been saved."),
432 std_to_wx (Config::instance()->cinemas_file().string()).data()
437 for (int i = 0; i < _history_items; ++i) {
438 delete _file_menu->Remove (ID_file_history + i);
441 if (_history_separator) {
442 _file_menu->Remove (_history_separator);
444 delete _history_separator;
445 _history_separator = 0;
447 int pos = _history_position;
449 vector<boost::filesystem::path> history = Config::instance()->player_history ();
451 if (!history.empty ()) {
452 _history_separator = _file_menu->InsertSeparator (pos++);
455 for (size_t i = 0; i < history.size(); ++i) {
458 s = String::compose ("&%1 %2", i + 1, history[i].string());
460 s = history[i].string();
462 _file_menu->Insert (pos++, ID_file_history + i, std_to_wx (s));
465 _history_items = history.size ();
468 void set_menu_sensitivity ()
470 _file_add_ov->Enable (static_cast<bool>(_film));
471 _file_add_kdm->Enable (static_cast<bool>(_film));
474 bool _update_news_requested;
475 PlayerInformation* _info;
476 wxPreferencesEditor* _config_dialog;
479 int _history_position;
480 wxMenuItem* _history_separator;
482 boost::shared_ptr<Film> _film;
483 boost::signals2::scoped_connection _config_changed_connection;
484 wxMenuItem* _file_add_ov;
485 wxMenuItem* _file_add_kdm;
488 static const wxCmdLineEntryDesc command_line_description[] = {
489 { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
490 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
494 * @brief The magic App class for wxWidgets.
496 class App : public wxApp
509 wxInitAllImageHandlers ();
511 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
512 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
514 wxSplashScreen* splash = maybe_show_splash ();
516 SetAppName (_("DCP-o-matic Player"));
518 if (!wxApp::OnInit()) {
522 #ifdef DCPOMATIC_LINUX
523 unsetenv ("UBUNTU_MENUPROXY");
527 ProcessSerialNumber serial;
528 GetCurrentProcess (&serial);
529 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
532 dcpomatic_setup_path_encoding ();
534 /* Enable i18n; this will create a Config object
535 to look for a force-configured language. This Config
536 object will be wrong, however, because dcpomatic_setup
537 hasn't yet been called and there aren't any filters etc.
540 dcpomatic_setup_i18n ();
542 /* Set things up, including filters etc.
543 which will now be internationalised correctly.
547 /* Force the configuration to be re-loaded correctly next
552 _frame = new DOMFrame ();
553 SetTopWindow (_frame);
560 signal_manager = new wxSignalManager (this);
562 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
564 _frame->load_dcp (_dcp_to_load);
565 } catch (exception& e) {
566 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1.")), _dcp_to_load)), std_to_wx(e.what()));
570 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
572 if (Config::instance()->check_for_updates ()) {
573 UpdateChecker::instance()->run ();
580 error_dialog (0, _("DCP-o-matic Player could not start."), std_to_wx(e.what()));
584 void OnInitCmdLine (wxCmdLineParser& parser)
586 parser.SetDesc (command_line_description);
587 parser.SetSwitchChars (wxT ("-"));
590 bool OnCmdLineParsed (wxCmdLineParser& parser)
592 if (parser.GetParamCount() > 0) {
593 _dcp_to_load = wx_to_std (parser.GetParam (0));
599 void report_exception ()
603 } catch (FileError& e) {
607 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
608 std_to_wx (e.what()),
609 std_to_wx (e.file().string().c_str ())
612 } catch (exception& e) {
616 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
617 std_to_wx (e.what ())
621 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
625 /* An unhandled exception has occurred inside the main event loop */
626 bool OnExceptionInMainLoop ()
629 /* This will terminate the program */
633 void OnUnhandledException ()
640 signal_manager->ui_idle ();
643 void config_failed_to_load ()
645 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
648 void config_warning (string m)
650 message_dialog (_frame, std_to_wx (m));