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 "lib/dcp_examiner.h"
34 #include "wx/wx_signal_manager.h"
35 #include "wx/wx_util.h"
36 #include "wx/about_dialog.h"
37 #include "wx/report_problem_dialog.h"
38 #include "wx/film_viewer.h"
39 #include "wx/player_information.h"
40 #include "wx/update_dialog.h"
41 #include "wx/player_config_dialog.h"
42 #include "wx/verify_dcp_dialog.h"
44 #include <wx/stdpaths.h>
45 #include <wx/splash.h>
46 #include <wx/cmdline.h>
47 #include <wx/preferences.h>
48 #include <wx/progdlg.h>
50 #include <ApplicationServices/ApplicationServices.h>
52 #include <boost/bind.hpp>
66 using boost::shared_ptr;
67 using boost::optional;
68 using boost::dynamic_pointer_cast;
75 /* Allow spare IDs after _history for the recent files list */
78 /* Allow spare IDs for CPLs */
79 ID_view_scale_appropriate = 200,
82 ID_view_scale_quarter,
83 ID_help_report_a_problem,
85 ID_tools_check_for_updates,
88 class DOMFrame : public wxFrame
92 : wxFrame (0, -1, _("DCP-o-matic Player"))
93 , _update_news_requested (false)
98 , _history_position (0)
99 , _history_separator (0)
103 #if defined(DCPOMATIC_WINDOWS)
104 maybe_open_console ();
105 cout << "DCP-o-matic Player is starting." << "\n";
108 wxMenuBar* bar = new wxMenuBar;
110 set_menu_sensitivity ();
113 #ifdef DCPOMATIC_WINDOWS
114 SetIcon (wxIcon (std_to_wx ("id")));
117 _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this));
120 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
121 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_ov, this), ID_file_add_ov);
122 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_kdm, this), ID_file_add_kdm);
123 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_history, this, _1), ID_file_history, ID_file_history + HISTORY_SIZE);
124 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_close, this), ID_file_close);
125 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
126 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
127 Bind (wxEVT_MENU, boost::bind (&DOMFrame::view_cpl, this, _1), ID_view_cpl, ID_view_cpl + MAX_CPLS);
128 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(0)), ID_view_scale_full);
129 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(1)), ID_view_scale_half);
130 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(2)), ID_view_scale_quarter);
131 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
132 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
133 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_verify, this), ID_tools_verify);
134 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
136 /* Use a panel as the only child of the Frame so that we avoid
137 the dark-grey background on Windows.
139 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
141 _viewer = new FilmViewer (overall_panel, false, false);
142 _viewer->set_dcp_decode_reduction (Config::instance()->decode_reduction ());
143 _info = new PlayerInformation (overall_panel, _viewer);
144 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
145 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
146 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
147 overall_panel->SetSizer (main_sizer);
150 wxAcceleratorEntry* accel = new wxAcceleratorEntry[1];
151 accel[0].Set(wxACCEL_CTRL, static_cast<int>('W'), ID_file_close);
152 wxAcceleratorTable accel_table (1, accel);
153 SetAcceleratorTable (accel_table);
157 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
160 void set_decode_reduction (optional<int> reduction)
162 _viewer->set_dcp_decode_reduction (reduction);
163 _info->triggered_update ();
164 Config::instance()->set_decode_reduction (reduction);
167 void load_dcp (boost::filesystem::path dir)
169 _film.reset (new Film (optional<boost::filesystem::path>()));
170 shared_ptr<DCPContent> dcp;
172 dcp.reset (new DCPContent (_film, dir));
173 } catch (boost::filesystem::filesystem_error& e) {
174 error_dialog (this, _("Could not load DCP"), std_to_wx (e.what()));
178 _film->examine_and_add_content (dcp, true);
180 JobManager* jm = JobManager::instance ();
182 wxProgressDialog* progress = new wxProgressDialog (_("DCP-o-matic Player"), _("Loading DCP"));
184 while (jm->work_to_do() || signal_manager->ui_idle()) {
189 progress->Destroy ();
191 DCPOMATIC_ASSERT (!jm->get().empty());
193 shared_ptr<Job> last = jm->get().back();
194 if (last->finished_in_error()) {
195 error_dialog(this, std_to_wx(last->error_summary()) + ".\n", std_to_wx(last->error_details()));
200 dcp->subtitle->set_use (true);
203 Ratio const * r = Ratio::nearest_from_ratio(dcp->video->size().ratio());
205 _film->set_container(r);
208 _viewer->set_film (_film);
209 _viewer->set_position (DCPTime ());
210 _info->triggered_update ();
212 Config::instance()->add_to_player_history (dir);
214 set_menu_sensitivity ();
216 wxMenuItemList old = _cpl_menu->GetMenuItems();
217 for (wxMenuItemList::iterator i = old.begin(); i != old.end(); ++i) {
218 _cpl_menu->Remove (*i);
221 DCPExaminer ex (dcp);
222 int id = ID_view_cpl;
223 BOOST_FOREACH (shared_ptr<dcp::CPL> i, ex.cpls()) {
224 wxMenuItem* j = _cpl_menu->AppendRadioItem(id, i->id());
225 if (!dcp->cpl() || i->id() == *dcp->cpl()) {
233 void setup_menu (wxMenuBar* m)
235 _file_menu = new wxMenu;
236 _file_menu->Append (ID_file_open, _("&Open...\tCtrl-O"));
237 _file_add_ov = _file_menu->Append (ID_file_add_ov, _("&Add OV..."));
238 _file_add_kdm = _file_menu->Append (ID_file_add_kdm, _("&Add KDM..."));
240 _history_position = _file_menu->GetMenuItems().GetCount();
242 _file_menu->AppendSeparator ();
243 _file_menu->Append (ID_file_close, _("&Close"));
244 _file_menu->AppendSeparator ();
247 _file_menu->Append (wxID_EXIT, _("&Exit"));
249 _file_menu->Append (wxID_EXIT, _("&Quit"));
253 _file_menu->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
255 wxMenu* edit = new wxMenu;
256 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
259 _cpl_menu = new wxMenu;
261 wxMenu* view = new wxMenu;
262 optional<int> c = Config::instance()->decode_reduction();
263 _view_cpl = view->Append(ID_view_cpl, _("CPL"), _cpl_menu);
264 view->AppendSeparator();
265 view->AppendRadioItem(ID_view_scale_appropriate, _("Set decode resolution to match display"))->Check(!static_cast<bool>(c));
266 view->AppendRadioItem(ID_view_scale_full, _("Decode at full resolution"))->Check(c && c.get() == 0);
267 view->AppendRadioItem(ID_view_scale_half, _("Decode at half resolution"))->Check(c && c.get() == 1);
268 view->AppendRadioItem(ID_view_scale_quarter, _("Decode at quarter resolution"))->Check(c && c.get() == 2);
270 wxMenu* tools = new wxMenu;
271 _tools_verify = tools->Append (ID_tools_verify, _("Verify DCP"));
272 tools->AppendSeparator ();
273 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
275 wxMenu* help = new wxMenu;
277 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
279 help->Append (wxID_ABOUT, _("About"));
281 help->Append (ID_help_report_a_problem, _("Report a problem..."));
283 m->Append (_file_menu, _("&File"));
285 m->Append (edit, _("&Edit"));
287 m->Append (view, _("&View"));
288 m->Append (tools, _("&Tools"));
289 m->Append (help, _("&Help"));
294 wxString d = wxStandardPaths::Get().GetDocumentsDir();
295 if (Config::instance()->last_player_load_directory()) {
296 d = std_to_wx (Config::instance()->last_player_load_directory()->string());
299 wxDirDialog* c = new wxDirDialog (this, _("Select DCP to open"), d, wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
304 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
305 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
312 boost::filesystem::path const dcp (wx_to_std (c->GetPath ()));
314 Config::instance()->set_last_player_load_directory (dcp.parent_path());
322 wxDirDialog* c = new wxDirDialog (
324 _("Select DCP to open as OV"),
325 wxStandardPaths::Get().GetDocumentsDir(),
326 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
332 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
333 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
340 DCPOMATIC_ASSERT (_film);
341 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
342 DCPOMATIC_ASSERT (dcp);
343 dcp->add_ov (wx_to_std(c->GetPath()));
344 dcp->examine (shared_ptr<Job>());
345 /* Maybe we just gained some subtitles */
347 dcp->subtitle->set_use (true);
352 _info->triggered_update ();
357 wxFileDialog* d = new wxFileDialog (this, _("Select KDM"));
359 if (d->ShowModal() == wxID_OK) {
360 DCPOMATIC_ASSERT (_film);
361 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
362 DCPOMATIC_ASSERT (dcp);
364 dcp->add_kdm (dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE)));
365 dcp->examine (shared_ptr<Job>());
366 } catch (exception& e) {
367 error_dialog (this, wxString::Format (_("Could not load KDM.")), std_to_wx(e.what()));
374 _info->triggered_update ();
377 void file_history (wxCommandEvent& event)
379 vector<boost::filesystem::path> history = Config::instance()->player_history ();
380 int n = event.GetId() - ID_file_history;
381 if (n >= 0 && n < static_cast<int> (history.size ())) {
382 load_dcp (history[n]);
388 _viewer->set_film (shared_ptr<Film>());
390 _info->triggered_update ();
391 set_menu_sensitivity ();
399 void edit_preferences ()
401 if (!_config_dialog) {
402 _config_dialog = create_player_config_dialog ();
404 _config_dialog->Show (this);
407 void view_cpl (wxCommandEvent& ev)
409 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
410 DCPOMATIC_ASSERT (dcp);
411 DCPExaminer ex (dcp);
412 int id = ev.GetId() - ID_view_cpl;
413 DCPOMATIC_ASSERT (id >= 0);
414 DCPOMATIC_ASSERT (id < int(ex.cpls().size()));
415 list<shared_ptr<dcp::CPL> > cpls = ex.cpls();
416 list<shared_ptr<dcp::CPL> >::iterator i = cpls.begin();
422 dcp->set_cpl ((*i)->id());
423 dcp->examine (shared_ptr<Job>());
428 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
429 DCPOMATIC_ASSERT (dcp);
431 JobManager* jm = JobManager::instance ();
432 jm->add (shared_ptr<Job> (new VerifyDCPJob (dcp->directories())));
434 wxProgressDialog* progress = new wxProgressDialog (_("DCP-o-matic Player"), _("Verifying DCP"));
436 while (jm->work_to_do() || signal_manager->ui_idle()) {
441 progress->Destroy ();
443 DCPOMATIC_ASSERT (!jm->get().empty());
444 shared_ptr<VerifyDCPJob> last = dynamic_pointer_cast<VerifyDCPJob> (jm->get().back());
445 DCPOMATIC_ASSERT (last);
447 VerifyDCPDialog* d = new VerifyDCPDialog (this, last->notes ());
452 void tools_check_for_updates ()
454 UpdateChecker::instance()->run ();
455 _update_news_requested = true;
460 AboutDialog* d = new AboutDialog (this);
465 void help_report_a_problem ()
467 ReportProblemDialog* d = new ReportProblemDialog (this);
468 if (d->ShowModal () == wxID_OK) {
474 void update_checker_state_changed ()
476 UpdateChecker* uc = UpdateChecker::instance ();
478 bool const announce =
479 _update_news_requested ||
480 (uc->stable() && Config::instance()->check_for_updates()) ||
481 (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
483 _update_news_requested = false;
489 if (uc->state() == UpdateChecker::YES) {
490 UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
491 dialog->ShowModal ();
493 } else if (uc->state() == UpdateChecker::FAILED) {
494 error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
496 error_dialog (this, _("There are no new versions of DCP-o-matic available."));
499 _update_news_requested = false;
502 void config_changed ()
504 /* Instantly save any config changes when using the player GUI */
506 Config::instance()->write_config();
507 } catch (exception& e) {
511 _("Could not write to config file at %s. Your changes have not been saved."),
512 std_to_wx (Config::instance()->cinemas_file().string()).data()
517 for (int i = 0; i < _history_items; ++i) {
518 delete _file_menu->Remove (ID_file_history + i);
521 if (_history_separator) {
522 _file_menu->Remove (_history_separator);
524 delete _history_separator;
525 _history_separator = 0;
527 int pos = _history_position;
529 vector<boost::filesystem::path> history = Config::instance()->player_history ();
531 if (!history.empty ()) {
532 _history_separator = _file_menu->InsertSeparator (pos++);
535 for (size_t i = 0; i < history.size(); ++i) {
538 s = String::compose ("&%1 %2", i + 1, history[i].string());
540 s = history[i].string();
542 _file_menu->Insert (pos++, ID_file_history + i, std_to_wx (s));
545 _history_items = history.size ();
548 void set_menu_sensitivity ()
550 _tools_verify->Enable (static_cast<bool>(_film));
551 _file_add_ov->Enable (static_cast<bool>(_film));
552 _file_add_kdm->Enable (static_cast<bool>(_film));
553 _view_cpl->Enable (static_cast<bool>(_film));
556 bool _update_news_requested;
557 PlayerInformation* _info;
558 wxPreferencesEditor* _config_dialog;
560 wxMenuItem* _view_cpl;
563 int _history_position;
564 wxMenuItem* _history_separator;
566 boost::shared_ptr<Film> _film;
567 boost::signals2::scoped_connection _config_changed_connection;
568 wxMenuItem* _file_add_ov;
569 wxMenuItem* _file_add_kdm;
570 wxMenuItem* _tools_verify;
573 static const wxCmdLineEntryDesc command_line_description[] = {
574 { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
575 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
579 * @brief The magic App class for wxWidgets.
581 class App : public wxApp
594 wxInitAllImageHandlers ();
596 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
597 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
599 wxSplashScreen* splash = maybe_show_splash ();
601 SetAppName (_("DCP-o-matic Player"));
603 if (!wxApp::OnInit()) {
607 #ifdef DCPOMATIC_LINUX
608 unsetenv ("UBUNTU_MENUPROXY");
612 ProcessSerialNumber serial;
613 GetCurrentProcess (&serial);
614 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
617 dcpomatic_setup_path_encoding ();
619 /* Enable i18n; this will create a Config object
620 to look for a force-configured language. This Config
621 object will be wrong, however, because dcpomatic_setup
622 hasn't yet been called and there aren't any filters etc.
625 dcpomatic_setup_i18n ();
627 /* Set things up, including filters etc.
628 which will now be internationalised correctly.
632 /* Force the configuration to be re-loaded correctly next
637 _frame = new DOMFrame ();
638 SetTopWindow (_frame);
645 signal_manager = new wxSignalManager (this);
647 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
649 _frame->load_dcp (_dcp_to_load);
650 } catch (exception& e) {
651 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1.")), _dcp_to_load)), std_to_wx(e.what()));
655 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
657 if (Config::instance()->check_for_updates ()) {
658 UpdateChecker::instance()->run ();
665 error_dialog (0, _("DCP-o-matic Player could not start."), std_to_wx(e.what()));
669 void OnInitCmdLine (wxCmdLineParser& parser)
671 parser.SetDesc (command_line_description);
672 parser.SetSwitchChars (wxT ("-"));
675 bool OnCmdLineParsed (wxCmdLineParser& parser)
677 if (parser.GetParamCount() > 0) {
678 _dcp_to_load = wx_to_std (parser.GetParam (0));
684 void report_exception ()
688 } catch (FileError& e) {
692 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
693 std_to_wx (e.what()),
694 std_to_wx (e.file().string().c_str ())
697 } catch (exception& e) {
701 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
702 std_to_wx (e.what ())
706 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
710 /* An unhandled exception has occurred inside the main event loop */
711 bool OnExceptionInMainLoop ()
714 /* This will terminate the program */
718 void OnUnhandledException ()
725 signal_manager->ui_idle ();
728 void config_failed_to_load ()
730 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
733 void config_warning (string m)
735 message_dialog (_frame, std_to_wx (m));