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 "lib/verify_dcp_job.h"
33 #include "wx/wx_signal_manager.h"
34 #include "wx/wx_util.h"
35 #include "wx/about_dialog.h"
36 #include "wx/report_problem_dialog.h"
37 #include "wx/film_viewer.h"
38 #include "wx/player_information.h"
39 #include "wx/update_dialog.h"
40 #include "wx/player_config_dialog.h"
41 #include "wx/verify_dcp_dialog.h"
43 #include <wx/stdpaths.h>
44 #include <wx/splash.h>
45 #include <wx/cmdline.h>
46 #include <wx/preferences.h>
47 #include <wx/progdlg.h>
49 #include <ApplicationServices/ApplicationServices.h>
51 #include <boost/bind.hpp>
62 using boost::shared_ptr;
63 using boost::optional;
64 using boost::dynamic_pointer_cast;
71 /* Allow spare IDs after _history for the recent files list */
73 ID_view_scale_appropriate,
76 ID_view_scale_quarter,
77 ID_help_report_a_problem,
79 ID_tools_check_for_updates,
82 class DOMFrame : public wxFrame
86 : wxFrame (0, -1, _("DCP-o-matic Player"))
87 , _update_news_requested (false)
92 , _history_position (0)
93 , _history_separator (0)
97 #if defined(DCPOMATIC_WINDOWS)
98 maybe_open_console ();
99 cout << "DCP-o-matic Player is starting." << "\n";
102 wxMenuBar* bar = new wxMenuBar;
104 set_menu_sensitivity ();
107 #ifdef DCPOMATIC_WINDOWS
108 SetIcon (wxIcon (std_to_wx ("id")));
111 _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this));
114 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
115 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_ov, this), ID_file_add_ov);
116 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_kdm, this), ID_file_add_kdm);
117 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_history, this, _1), ID_file_history, ID_file_history + HISTORY_SIZE);
118 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_close, this), ID_file_close);
119 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
120 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
121 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>()), ID_view_scale_appropriate);
122 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(0)), ID_view_scale_full);
123 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(1)), ID_view_scale_half);
124 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(2)), ID_view_scale_quarter);
125 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
126 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
127 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_verify, this), ID_tools_verify);
128 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
130 /* Use a panel as the only child of the Frame so that we avoid
131 the dark-grey background on Windows.
133 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
135 _viewer = new FilmViewer (overall_panel, false, false);
136 _viewer->set_dcp_decode_reduction (Config::instance()->decode_reduction ());
137 _info = new PlayerInformation (overall_panel, _viewer);
138 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
139 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
140 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
141 overall_panel->SetSizer (main_sizer);
144 wxAcceleratorEntry* accel = new wxAcceleratorEntry[1];
145 accel[0].Set(wxACCEL_CTRL, static_cast<int>('W'), ID_file_close);
146 wxAcceleratorTable accel_table (1, accel);
147 SetAcceleratorTable (accel_table);
151 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
154 void set_decode_reduction (optional<int> reduction)
156 _viewer->set_dcp_decode_reduction (reduction);
157 _info->triggered_update ();
158 Config::instance()->set_decode_reduction (reduction);
161 void load_dcp (boost::filesystem::path dir)
163 _film.reset (new Film (optional<boost::filesystem::path>()));
164 shared_ptr<DCPContent> dcp;
166 dcp.reset (new DCPContent (_film, dir));
167 } catch (boost::filesystem::filesystem_error& e) {
168 error_dialog (this, _("Could not load DCP"), std_to_wx (e.what()));
172 _film->examine_and_add_content (dcp, true);
174 JobManager* jm = JobManager::instance ();
176 wxProgressDialog* progress = new wxProgressDialog (_("DCP-o-matic Player"), _("Loading DCP"));
178 while (jm->work_to_do() || signal_manager->ui_idle()) {
183 progress->Destroy ();
185 DCPOMATIC_ASSERT (!jm->get().empty());
187 shared_ptr<Job> last = jm->get().back();
188 if (last->finished_in_error()) {
189 error_dialog(this, std_to_wx(last->error_summary()) + ".\n", std_to_wx(last->error_details()));
194 dcp->subtitle->set_use (true);
197 Ratio const * r = Ratio::nearest_from_ratio(dcp->video->size().ratio());
199 _film->set_container(r);
202 _viewer->set_film (_film);
203 _viewer->set_position (DCPTime ());
204 _info->triggered_update ();
206 Config::instance()->add_to_player_history (dir);
208 set_menu_sensitivity ();
213 void setup_menu (wxMenuBar* m)
215 _file_menu = new wxMenu;
216 _file_menu->Append (ID_file_open, _("&Open...\tCtrl-O"));
217 _file_add_ov = _file_menu->Append (ID_file_add_ov, _("&Add OV..."));
218 _file_add_kdm = _file_menu->Append (ID_file_add_kdm, _("&Add KDM..."));
220 _history_position = _file_menu->GetMenuItems().GetCount();
222 _file_menu->AppendSeparator ();
223 _file_menu->Append (ID_file_close, _("&Close"));
224 _file_menu->AppendSeparator ();
227 _file_menu->Append (wxID_EXIT, _("&Exit"));
229 _file_menu->Append (wxID_EXIT, _("&Quit"));
233 _file_menu->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
235 wxMenu* edit = new wxMenu;
236 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
239 wxMenu* view = new wxMenu;
240 optional<int> c = Config::instance()->decode_reduction();
241 view->AppendRadioItem(ID_view_scale_appropriate, _("Set decode resolution to match display"))->Check(!static_cast<bool>(c));
242 view->AppendRadioItem(ID_view_scale_full, _("Decode at full resolution"))->Check(c && c.get() == 0);
243 view->AppendRadioItem(ID_view_scale_half, _("Decode at half resolution"))->Check(c && c.get() == 1);
244 view->AppendRadioItem(ID_view_scale_quarter, _("Decode at quarter resolution"))->Check(c && c.get() == 2);
246 wxMenu* tools = new wxMenu;
247 _tools_verify = tools->Append (ID_tools_verify, _("Verify DCP"));
248 tools->AppendSeparator ();
249 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
251 wxMenu* help = new wxMenu;
253 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
255 help->Append (wxID_ABOUT, _("About"));
257 help->Append (ID_help_report_a_problem, _("Report a problem..."));
259 m->Append (_file_menu, _("&File"));
261 m->Append (edit, _("&Edit"));
263 m->Append (view, _("&View"));
264 m->Append (tools, _("&Tools"));
265 m->Append (help, _("&Help"));
270 wxString d = wxStandardPaths::Get().GetDocumentsDir();
271 if (Config::instance()->last_player_load_directory()) {
272 d = std_to_wx (Config::instance()->last_player_load_directory()->string());
275 wxDirDialog* c = new wxDirDialog (this, _("Select DCP to open"), d, wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
280 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
281 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
288 boost::filesystem::path const dcp (wx_to_std (c->GetPath ()));
290 Config::instance()->set_last_player_load_directory (dcp.parent_path());
298 wxDirDialog* c = new wxDirDialog (
300 _("Select DCP to open as OV"),
301 wxStandardPaths::Get().GetDocumentsDir(),
302 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
308 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
309 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
316 DCPOMATIC_ASSERT (_film);
317 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
318 DCPOMATIC_ASSERT (dcp);
319 dcp->add_ov (wx_to_std(c->GetPath()));
320 dcp->examine (shared_ptr<Job>());
321 /* Maybe we just gained some subtitles */
323 dcp->subtitle->set_use (true);
328 _info->triggered_update ();
333 wxFileDialog* d = new wxFileDialog (this, _("Select KDM"));
335 if (d->ShowModal() == wxID_OK) {
336 DCPOMATIC_ASSERT (_film);
337 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
338 DCPOMATIC_ASSERT (dcp);
340 dcp->add_kdm (dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE)));
341 dcp->examine (shared_ptr<Job>());
342 } catch (exception& e) {
343 error_dialog (this, wxString::Format (_("Could not load KDM.")), std_to_wx(e.what()));
350 _info->triggered_update ();
353 void file_history (wxCommandEvent& event)
355 vector<boost::filesystem::path> history = Config::instance()->player_history ();
356 int n = event.GetId() - ID_file_history;
357 if (n >= 0 && n < static_cast<int> (history.size ())) {
358 load_dcp (history[n]);
364 _viewer->set_film (shared_ptr<Film>());
366 _info->triggered_update ();
367 set_menu_sensitivity ();
375 void edit_preferences ()
377 if (!_config_dialog) {
378 _config_dialog = create_player_config_dialog ();
380 _config_dialog->Show (this);
385 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
386 DCPOMATIC_ASSERT (dcp);
388 JobManager* jm = JobManager::instance ();
389 jm->add (shared_ptr<Job> (new VerifyDCPJob (dcp->directories())));
391 wxProgressDialog* progress = new wxProgressDialog (_("DCP-o-matic Player"), _("Verifying DCP"));
393 while (jm->work_to_do() || signal_manager->ui_idle()) {
398 progress->Destroy ();
400 DCPOMATIC_ASSERT (!jm->get().empty());
401 shared_ptr<VerifyDCPJob> last = dynamic_pointer_cast<VerifyDCPJob> (jm->get().back());
402 DCPOMATIC_ASSERT (last);
404 VerifyDCPDialog* d = new VerifyDCPDialog (this, last->notes ());
409 void tools_check_for_updates ()
411 UpdateChecker::instance()->run ();
412 _update_news_requested = true;
417 AboutDialog* d = new AboutDialog (this);
422 void help_report_a_problem ()
424 ReportProblemDialog* d = new ReportProblemDialog (this);
425 if (d->ShowModal () == wxID_OK) {
431 void update_checker_state_changed ()
433 UpdateChecker* uc = UpdateChecker::instance ();
435 bool const announce =
436 _update_news_requested ||
437 (uc->stable() && Config::instance()->check_for_updates()) ||
438 (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
440 _update_news_requested = false;
446 if (uc->state() == UpdateChecker::YES) {
447 UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
448 dialog->ShowModal ();
450 } else if (uc->state() == UpdateChecker::FAILED) {
451 error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
453 error_dialog (this, _("There are no new versions of DCP-o-matic available."));
456 _update_news_requested = false;
459 void config_changed ()
461 /* Instantly save any config changes when using the player GUI */
463 Config::instance()->write_config();
464 } catch (exception& e) {
468 _("Could not write to config file at %s. Your changes have not been saved."),
469 std_to_wx (Config::instance()->cinemas_file().string()).data()
474 for (int i = 0; i < _history_items; ++i) {
475 delete _file_menu->Remove (ID_file_history + i);
478 if (_history_separator) {
479 _file_menu->Remove (_history_separator);
481 delete _history_separator;
482 _history_separator = 0;
484 int pos = _history_position;
486 vector<boost::filesystem::path> history = Config::instance()->player_history ();
488 if (!history.empty ()) {
489 _history_separator = _file_menu->InsertSeparator (pos++);
492 for (size_t i = 0; i < history.size(); ++i) {
495 s = String::compose ("&%1 %2", i + 1, history[i].string());
497 s = history[i].string();
499 _file_menu->Insert (pos++, ID_file_history + i, std_to_wx (s));
502 _history_items = history.size ();
505 void set_menu_sensitivity ()
507 _tools_verify->Enable (static_cast<bool>(_film));
508 _file_add_ov->Enable (static_cast<bool>(_film));
509 _file_add_kdm->Enable (static_cast<bool>(_film));
512 bool _update_news_requested;
513 PlayerInformation* _info;
514 wxPreferencesEditor* _config_dialog;
517 int _history_position;
518 wxMenuItem* _history_separator;
520 boost::shared_ptr<Film> _film;
521 boost::signals2::scoped_connection _config_changed_connection;
522 wxMenuItem* _file_add_ov;
523 wxMenuItem* _file_add_kdm;
524 wxMenuItem* _tools_verify;
527 static const wxCmdLineEntryDesc command_line_description[] = {
528 { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
529 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
533 * @brief The magic App class for wxWidgets.
535 class App : public wxApp
548 wxInitAllImageHandlers ();
550 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
551 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
553 wxSplashScreen* splash = maybe_show_splash ();
555 SetAppName (_("DCP-o-matic Player"));
557 if (!wxApp::OnInit()) {
561 #ifdef DCPOMATIC_LINUX
562 unsetenv ("UBUNTU_MENUPROXY");
566 ProcessSerialNumber serial;
567 GetCurrentProcess (&serial);
568 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
571 dcpomatic_setup_path_encoding ();
573 /* Enable i18n; this will create a Config object
574 to look for a force-configured language. This Config
575 object will be wrong, however, because dcpomatic_setup
576 hasn't yet been called and there aren't any filters etc.
579 dcpomatic_setup_i18n ();
581 /* Set things up, including filters etc.
582 which will now be internationalised correctly.
586 /* Force the configuration to be re-loaded correctly next
591 _frame = new DOMFrame ();
592 SetTopWindow (_frame);
599 signal_manager = new wxSignalManager (this);
601 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
603 _frame->load_dcp (_dcp_to_load);
604 } catch (exception& e) {
605 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1.")), _dcp_to_load)), std_to_wx(e.what()));
609 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
611 if (Config::instance()->check_for_updates ()) {
612 UpdateChecker::instance()->run ();
619 error_dialog (0, _("DCP-o-matic Player could not start."), std_to_wx(e.what()));
623 void OnInitCmdLine (wxCmdLineParser& parser)
625 parser.SetDesc (command_line_description);
626 parser.SetSwitchChars (wxT ("-"));
629 bool OnCmdLineParsed (wxCmdLineParser& parser)
631 if (parser.GetParamCount() > 0) {
632 _dcp_to_load = wx_to_std (parser.GetParam (0));
638 void report_exception ()
642 } catch (FileError& e) {
646 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
647 std_to_wx (e.what()),
648 std_to_wx (e.file().string().c_str ())
651 } catch (exception& e) {
655 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
656 std_to_wx (e.what ())
660 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
664 /* An unhandled exception has occurred inside the main event loop */
665 bool OnExceptionInMainLoop ()
668 /* This will terminate the program */
672 void OnUnhandledException ()
679 signal_manager->ui_idle ();
682 void config_failed_to_load ()
684 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
687 void config_warning (string m)
689 message_dialog (_frame, std_to_wx (m));