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 _info = new PlayerInformation (overall_panel, _viewer);
132 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
133 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
134 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
135 overall_panel->SetSizer (main_sizer);
138 wxAcceleratorEntry* accel = new wxAcceleratorEntry[1];
139 accel[0].Set(wxACCEL_CTRL, static_cast<int>('W'), ID_file_close);
140 wxAcceleratorTable accel_table (1, accel);
141 SetAcceleratorTable (accel_table);
145 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
148 void set_decode_reduction (optional<int> reduction)
150 _viewer->set_dcp_decode_reduction (reduction);
151 _info->triggered_update ();
152 Config::instance()->set_decode_reduction (reduction);
155 void load_dcp (boost::filesystem::path dir)
157 _film.reset (new Film (optional<boost::filesystem::path>()));
158 shared_ptr<DCPContent> dcp;
160 dcp.reset (new DCPContent (_film, dir));
161 } catch (boost::filesystem::filesystem_error& e) {
162 error_dialog (this, _("Could not load DCP"), std_to_wx (e.what()));
166 _film->examine_and_add_content (dcp, true);
168 JobManager* jm = JobManager::instance ();
170 wxProgressDialog* progress = new wxProgressDialog (_("DCP-o-matic Player"), _("Loading DCP"));
172 while (jm->work_to_do() || signal_manager->ui_idle()) {
177 progress->Destroy ();
179 DCPOMATIC_ASSERT (!jm->get().empty());
181 shared_ptr<Job> last = jm->get().back();
182 if (last->finished_in_error()) {
183 error_dialog (this, std_to_wx (last->error_summary()) + ".\n");
188 dcp->subtitle->set_use (true);
191 Ratio const * r = Ratio::nearest_from_ratio(dcp->video->size().ratio());
193 _film->set_container(r);
196 _viewer->set_film (_film);
197 _viewer->set_position (DCPTime ());
198 _info->triggered_update ();
200 Config::instance()->add_to_player_history (dir);
202 set_menu_sensitivity ();
207 void setup_menu (wxMenuBar* m)
209 _file_menu = new wxMenu;
210 _file_menu->Append (ID_file_open, _("&Open...\tCtrl-O"));
211 _file_add_ov = _file_menu->Append (ID_file_add_ov, _("&Add OV..."));
212 _file_add_kdm = _file_menu->Append (ID_file_add_kdm, _("&Add KDM..."));
214 _history_position = _file_menu->GetMenuItems().GetCount();
216 _file_menu->AppendSeparator ();
217 _file_menu->Append (ID_file_close, _("&Close"));
218 _file_menu->AppendSeparator ();
221 _file_menu->Append (wxID_EXIT, _("&Exit"));
223 _file_menu->Append (wxID_EXIT, _("&Quit"));
227 _file_menu->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
229 wxMenu* edit = new wxMenu;
230 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
233 wxMenu* view = new wxMenu;
234 optional<int> c = Config::instance()->decode_reduction();
235 view->AppendRadioItem(ID_view_scale_appropriate, _("Set decode resolution to match display"))->Check(!static_cast<bool>(c));
236 view->AppendRadioItem(ID_view_scale_full, _("Decode at full resolution"))->Check(c && c.get() == 0);
237 view->AppendRadioItem(ID_view_scale_half, _("Decode at half resolution"))->Check(c && c.get() == 1);
238 view->AppendRadioItem(ID_view_scale_quarter, _("Decode at quarter resolution"))->Check(c && c.get() == 2);
240 wxMenu* tools = new wxMenu;
241 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
243 wxMenu* help = new wxMenu;
245 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
247 help->Append (wxID_ABOUT, _("About"));
249 help->Append (ID_help_report_a_problem, _("Report a problem..."));
251 m->Append (_file_menu, _("&File"));
253 m->Append (edit, _("&Edit"));
255 m->Append (view, _("&View"));
256 m->Append (tools, _("&Tools"));
257 m->Append (help, _("&Help"));
262 wxString d = wxStandardPaths::Get().GetDocumentsDir();
263 if (Config::instance()->last_player_load_directory()) {
264 d = std_to_wx (Config::instance()->last_player_load_directory()->string());
267 wxDirDialog* c = new wxDirDialog (this, _("Select DCP to open"), d, wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
272 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
273 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
280 boost::filesystem::path const dcp (wx_to_std (c->GetPath ()));
282 Config::instance()->set_last_player_load_directory (dcp.parent_path());
290 wxDirDialog* c = new wxDirDialog (
292 _("Select DCP to open as OV"),
293 wxStandardPaths::Get().GetDocumentsDir(),
294 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
300 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
301 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
308 DCPOMATIC_ASSERT (_film);
309 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
310 DCPOMATIC_ASSERT (dcp);
311 dcp->add_ov (wx_to_std(c->GetPath()));
312 dcp->examine (shared_ptr<Job>());
316 _info->triggered_update ();
321 wxFileDialog* d = new wxFileDialog (this, _("Select KDM"));
323 if (d->ShowModal() == wxID_OK) {
324 DCPOMATIC_ASSERT (_film);
325 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
326 DCPOMATIC_ASSERT (dcp);
328 dcp->add_kdm (dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE)));
329 dcp->examine (shared_ptr<Job>());
330 } catch (exception& e) {
331 error_dialog (this, wxString::Format (_("Could not load KDM.")), std_to_wx(e.what()));
338 _info->triggered_update ();
341 void file_history (wxCommandEvent& event)
343 vector<boost::filesystem::path> history = Config::instance()->player_history ();
344 int n = event.GetId() - ID_file_history;
345 if (n >= 0 && n < static_cast<int> (history.size ())) {
346 load_dcp (history[n]);
352 _viewer->set_film (shared_ptr<Film>());
354 _info->triggered_update ();
355 set_menu_sensitivity ();
363 void edit_preferences ()
365 if (!_config_dialog) {
366 _config_dialog = create_player_config_dialog ();
368 _config_dialog->Show (this);
371 void tools_check_for_updates ()
373 UpdateChecker::instance()->run ();
374 _update_news_requested = true;
379 AboutDialog* d = new AboutDialog (this);
384 void help_report_a_problem ()
386 ReportProblemDialog* d = new ReportProblemDialog (this);
387 if (d->ShowModal () == wxID_OK) {
393 void update_checker_state_changed ()
395 UpdateChecker* uc = UpdateChecker::instance ();
397 bool const announce =
398 _update_news_requested ||
399 (uc->stable() && Config::instance()->check_for_updates()) ||
400 (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
402 _update_news_requested = false;
408 if (uc->state() == UpdateChecker::YES) {
409 UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
410 dialog->ShowModal ();
412 } else if (uc->state() == UpdateChecker::FAILED) {
413 error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
415 error_dialog (this, _("There are no new versions of DCP-o-matic available."));
418 _update_news_requested = false;
421 void config_changed ()
423 /* Instantly save any config changes when using the player GUI */
425 Config::instance()->write_config();
426 } catch (exception& e) {
430 _("Could not write to config file at %s. Your changes have not been saved."),
431 std_to_wx (Config::instance()->cinemas_file().string()).data()
436 for (int i = 0; i < _history_items; ++i) {
437 delete _file_menu->Remove (ID_file_history + i);
440 if (_history_separator) {
441 _file_menu->Remove (_history_separator);
443 delete _history_separator;
444 _history_separator = 0;
446 int pos = _history_position;
448 vector<boost::filesystem::path> history = Config::instance()->player_history ();
450 if (!history.empty ()) {
451 _history_separator = _file_menu->InsertSeparator (pos++);
454 for (size_t i = 0; i < history.size(); ++i) {
457 s = String::compose ("&%1 %2", i + 1, history[i].string());
459 s = history[i].string();
461 _file_menu->Insert (pos++, ID_file_history + i, std_to_wx (s));
464 _history_items = history.size ();
467 void set_menu_sensitivity ()
469 _file_add_ov->Enable (static_cast<bool>(_film));
470 _file_add_kdm->Enable (static_cast<bool>(_film));
473 bool _update_news_requested;
474 PlayerInformation* _info;
475 wxPreferencesEditor* _config_dialog;
478 int _history_position;
479 wxMenuItem* _history_separator;
481 boost::shared_ptr<Film> _film;
482 boost::signals2::scoped_connection _config_changed_connection;
483 wxMenuItem* _file_add_ov;
484 wxMenuItem* _file_add_kdm;
487 static const wxCmdLineEntryDesc command_line_description[] = {
488 { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
489 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
493 * @brief The magic App class for wxWidgets.
495 class App : public wxApp
508 wxInitAllImageHandlers ();
510 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
511 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
513 wxSplashScreen* splash = maybe_show_splash ();
515 SetAppName (_("DCP-o-matic Player"));
517 if (!wxApp::OnInit()) {
521 #ifdef DCPOMATIC_LINUX
522 unsetenv ("UBUNTU_MENUPROXY");
526 ProcessSerialNumber serial;
527 GetCurrentProcess (&serial);
528 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
531 dcpomatic_setup_path_encoding ();
533 /* Enable i18n; this will create a Config object
534 to look for a force-configured language. This Config
535 object will be wrong, however, because dcpomatic_setup
536 hasn't yet been called and there aren't any filters etc.
539 dcpomatic_setup_i18n ();
541 /* Set things up, including filters etc.
542 which will now be internationalised correctly.
546 /* Force the configuration to be re-loaded correctly next
551 _frame = new DOMFrame ();
552 SetTopWindow (_frame);
559 signal_manager = new wxSignalManager (this);
561 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
563 _frame->load_dcp (_dcp_to_load);
564 } catch (exception& e) {
565 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1.")), _dcp_to_load)), std_to_wx(e.what()));
569 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
571 if (Config::instance()->check_for_updates ()) {
572 UpdateChecker::instance()->run ();
579 error_dialog (0, _("DCP-o-matic Player could not start."), std_to_wx(e.what()));
583 void OnInitCmdLine (wxCmdLineParser& parser)
585 parser.SetDesc (command_line_description);
586 parser.SetSwitchChars (wxT ("-"));
589 bool OnCmdLineParsed (wxCmdLineParser& parser)
591 if (parser.GetParamCount() > 0) {
592 _dcp_to_load = wx_to_std (parser.GetParam (0));
598 void report_exception ()
602 } catch (FileError& e) {
606 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
607 std_to_wx (e.what()),
608 std_to_wx (e.file().string().c_str ())
611 } catch (exception& e) {
615 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
616 std_to_wx (e.what ())
620 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
624 /* An unhandled exception has occurred inside the main event loop */
625 bool OnExceptionInMainLoop ()
628 /* This will terminate the program */
632 void OnUnhandledException ()
639 signal_manager->ui_idle ();
642 void config_failed_to_load ()
644 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
647 void config_warning (string m)
649 message_dialog (_frame, std_to_wx (m));