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 "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>
59 using boost::shared_ptr;
60 using boost::optional;
67 /* Allow spare IDs after _history for the recent files list */
69 ID_view_scale_appropriate,
72 ID_view_scale_quarter,
73 ID_help_report_a_problem,
74 ID_tools_check_for_updates,
77 class DOMFrame : public wxFrame
81 : wxFrame (0, -1, _("DCP-o-matic Player"))
82 , _update_news_requested (false)
87 , _history_position (0)
88 , _history_separator (0)
92 #if defined(DCPOMATIC_WINDOWS)
93 maybe_open_console ();
94 cout << "DCP-o-matic Player is starting." << "\n";
97 wxMenuBar* bar = new wxMenuBar;
99 set_menu_sensitivity ();
102 #ifdef DCPOMATIC_WINDOWS
103 SetIcon (wxIcon (std_to_wx ("id")));
106 _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this));
109 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
110 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_ov, this), ID_file_add_ov);
111 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_kdm, this), ID_file_add_kdm);
112 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_history, this, _1), ID_file_history, ID_file_history + HISTORY_SIZE);
113 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_close, this), ID_file_close);
114 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
115 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
116 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>()), ID_view_scale_appropriate);
117 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(0)), ID_view_scale_full);
118 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(1)), ID_view_scale_half);
119 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(2)), ID_view_scale_quarter);
120 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
121 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
122 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
124 /* Use a panel as the only child of the Frame so that we avoid
125 the dark-grey background on Windows.
127 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
129 _viewer = new FilmViewer (overall_panel, false, false);
130 _info = new PlayerInformation (overall_panel, _viewer);
131 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
132 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
133 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
134 overall_panel->SetSizer (main_sizer);
136 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
139 void set_decode_reduction (optional<int> reduction)
141 _viewer->set_dcp_decode_reduction (reduction);
142 _info->triggered_update ();
145 void load_dcp (boost::filesystem::path dir)
147 _film.reset (new Film (optional<boost::filesystem::path>()));
148 shared_ptr<DCPContent> dcp (new DCPContent (_film, dir));
149 _film->examine_and_add_content (dcp, true);
151 JobManager* jm = JobManager::instance ();
153 wxProgressDialog* progress = new wxProgressDialog (_("DCP-o-matic Player"), _("Loading DCP"));
155 while (jm->work_to_do() || signal_manager->ui_idle()) {
160 progress->Destroy ();
162 DCPOMATIC_ASSERT (!jm->get().empty());
164 shared_ptr<Job> last = jm->get().back();
165 if (last->finished_in_error()) {
166 error_dialog (this, std_to_wx (last->error_summary()) + ".\n");
171 dcp->subtitle->set_use (true);
174 _viewer->set_film (_film);
175 _viewer->set_position (DCPTime ());
176 _info->triggered_update ();
178 Config::instance()->add_to_player_history (dir);
180 set_menu_sensitivity ();
185 void setup_menu (wxMenuBar* m)
187 _file_menu = new wxMenu;
188 _file_menu->Append (ID_file_open, _("&Open...\tCtrl-O"));
189 _file_add_ov = _file_menu->Append (ID_file_add_ov, _("&Add OV..."));
190 _file_add_kdm = _file_menu->Append (ID_file_add_kdm, _("&Add KDM..."));
192 _history_position = _file_menu->GetMenuItems().GetCount();
194 _file_menu->AppendSeparator ();
195 _file_menu->Append (ID_file_close, _("&Close"));
196 _file_menu->AppendSeparator ();
199 _file_menu->Append (wxID_EXIT, _("&Exit"));
201 _file_menu->Append (wxID_EXIT, _("&Quit"));
205 _file_menu->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
207 wxMenu* edit = new wxMenu;
208 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
211 wxMenu* view = new wxMenu;
212 view->AppendRadioItem (ID_view_scale_appropriate, _("Set decode resolution to match display"));
213 view->AppendRadioItem (ID_view_scale_full, _("Decode at full resolution"));
214 view->AppendRadioItem (ID_view_scale_half, _("Decode at half resolution"));
215 view->AppendRadioItem (ID_view_scale_quarter, _("Decode at quarter resolution"));
217 wxMenu* tools = new wxMenu;
218 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
220 wxMenu* help = new wxMenu;
222 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
224 help->Append (wxID_ABOUT, _("About"));
226 help->Append (ID_help_report_a_problem, _("Report a problem..."));
228 m->Append (_file_menu, _("&File"));
230 m->Append (edit, _("&Edit"));
232 m->Append (view, _("&View"));
233 m->Append (tools, _("&Tools"));
234 m->Append (help, _("&Help"));
239 wxString d = wxStandardPaths::Get().GetDocumentsDir();
240 if (Config::instance()->last_player_load_directory()) {
241 d = std_to_wx (Config::instance()->last_player_load_directory()->string());
244 wxDirDialog* c = new wxDirDialog (this, _("Select DCP to open"), d, wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
249 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
250 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
257 boost::filesystem::path const dcp (wx_to_std (c->GetPath ()));
259 Config::instance()->set_last_player_load_directory (dcp.parent_path());
267 wxDirDialog* c = new wxDirDialog (
269 _("Select DCP to open as OV"),
270 wxStandardPaths::Get().GetDocumentsDir(),
271 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
277 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
278 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
285 DCPOMATIC_ASSERT (_film);
286 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
287 DCPOMATIC_ASSERT (dcp);
288 dcp->add_ov (wx_to_std(c->GetPath()));
289 dcp->examine (shared_ptr<Job>());
293 _info->triggered_update ();
298 wxFileDialog* d = new wxFileDialog (this, _("Select KDM"));
300 if (d->ShowModal() == wxID_OK) {
301 DCPOMATIC_ASSERT (_film);
302 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
303 DCPOMATIC_ASSERT (dcp);
305 dcp->add_kdm (dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE)));
306 dcp->examine (shared_ptr<Job>());
307 } catch (exception& e) {
308 error_dialog (this, wxString::Format (_("Could not load KDM.")), std_to_wx(e.what()));
315 _info->triggered_update ();
318 void file_history (wxCommandEvent& event)
320 vector<boost::filesystem::path> history = Config::instance()->player_history ();
321 int n = event.GetId() - ID_file_history;
322 if (n >= 0 && n < static_cast<int> (history.size ())) {
323 load_dcp (history[n]);
329 _viewer->set_film (shared_ptr<Film>());
331 _info->triggered_update ();
332 set_menu_sensitivity ();
340 void edit_preferences ()
342 if (!_config_dialog) {
343 _config_dialog = create_player_config_dialog ();
345 _config_dialog->Show (this);
348 void tools_check_for_updates ()
350 UpdateChecker::instance()->run ();
351 _update_news_requested = true;
356 AboutDialog* d = new AboutDialog (this);
361 void help_report_a_problem ()
363 ReportProblemDialog* d = new ReportProblemDialog (this);
364 if (d->ShowModal () == wxID_OK) {
370 void update_checker_state_changed ()
372 UpdateChecker* uc = UpdateChecker::instance ();
374 bool const announce =
375 _update_news_requested ||
376 (uc->stable() && Config::instance()->check_for_updates()) ||
377 (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
379 _update_news_requested = false;
385 if (uc->state() == UpdateChecker::YES) {
386 UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
387 dialog->ShowModal ();
389 } else if (uc->state() == UpdateChecker::FAILED) {
390 error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
392 error_dialog (this, _("There are no new versions of DCP-o-matic available."));
395 _update_news_requested = false;
398 void config_changed ()
400 /* Instantly save any config changes when using the player GUI */
402 Config::instance()->write_config();
403 } catch (exception& e) {
407 _("Could not write to config file at %s. Your changes have not been saved."),
408 std_to_wx (Config::instance()->cinemas_file().string()).data()
413 for (int i = 0; i < _history_items; ++i) {
414 delete _file_menu->Remove (ID_file_history + i);
417 if (_history_separator) {
418 _file_menu->Remove (_history_separator);
420 delete _history_separator;
421 _history_separator = 0;
423 int pos = _history_position;
425 vector<boost::filesystem::path> history = Config::instance()->player_history ();
427 if (!history.empty ()) {
428 _history_separator = _file_menu->InsertSeparator (pos++);
431 for (size_t i = 0; i < history.size(); ++i) {
434 s = String::compose ("&%1 %2", i + 1, history[i].string());
436 s = history[i].string();
438 _file_menu->Insert (pos++, ID_file_history + i, std_to_wx (s));
441 _history_items = history.size ();
444 void set_menu_sensitivity ()
446 _file_add_ov->Enable (static_cast<bool>(_film));
447 _file_add_kdm->Enable (static_cast<bool>(_film));
450 bool _update_news_requested;
451 PlayerInformation* _info;
452 wxPreferencesEditor* _config_dialog;
455 int _history_position;
456 wxMenuItem* _history_separator;
458 boost::shared_ptr<Film> _film;
459 boost::signals2::scoped_connection _config_changed_connection;
460 wxMenuItem* _file_add_ov;
461 wxMenuItem* _file_add_kdm;
464 static const wxCmdLineEntryDesc command_line_description[] = {
465 { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
466 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
470 * @brief The magic App class for wxWidgets.
472 class App : public wxApp
485 wxInitAllImageHandlers ();
487 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
488 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
490 wxSplashScreen* splash = maybe_show_splash ();
492 SetAppName (_("DCP-o-matic Player"));
494 if (!wxApp::OnInit()) {
498 #ifdef DCPOMATIC_LINUX
499 unsetenv ("UBUNTU_MENUPROXY");
503 ProcessSerialNumber serial;
504 GetCurrentProcess (&serial);
505 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
508 dcpomatic_setup_path_encoding ();
510 /* Enable i18n; this will create a Config object
511 to look for a force-configured language. This Config
512 object will be wrong, however, because dcpomatic_setup
513 hasn't yet been called and there aren't any filters etc.
516 dcpomatic_setup_i18n ();
518 /* Set things up, including filters etc.
519 which will now be internationalised correctly.
523 /* Force the configuration to be re-loaded correctly next
528 _frame = new DOMFrame ();
529 SetTopWindow (_frame);
536 signal_manager = new wxSignalManager (this);
538 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
540 _frame->load_dcp (_dcp_to_load);
541 } catch (exception& e) {
542 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1.")), _dcp_to_load)), std_to_wx(e.what()));
546 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
548 if (Config::instance()->check_for_updates ()) {
549 UpdateChecker::instance()->run ();
556 error_dialog (0, _("DCP-o-matic Player could not start."), std_to_wx(e.what()));
560 void OnInitCmdLine (wxCmdLineParser& parser)
562 parser.SetDesc (command_line_description);
563 parser.SetSwitchChars (wxT ("-"));
566 bool OnCmdLineParsed (wxCmdLineParser& parser)
568 if (parser.GetParamCount() > 0) {
569 _dcp_to_load = wx_to_std (parser.GetParam (0));
575 void report_exception ()
579 } catch (FileError& e) {
583 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
584 std_to_wx (e.what()),
585 std_to_wx (e.file().string().c_str ())
588 } catch (exception& e) {
592 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
593 std_to_wx (e.what ())
597 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
601 /* An unhandled exception has occurred inside the main event loop */
602 bool OnExceptionInMainLoop ()
605 /* This will terminate the program */
609 void OnUnhandledException ()
616 signal_manager->ui_idle ();
619 void config_failed_to_load ()
621 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
624 void config_warning (string m)
626 message_dialog (_frame, std_to_wx (m));