2 Copyright (C) 2017 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>
58 using boost::shared_ptr;
59 using boost::optional;
66 ID_view_scale_appropriate,
69 ID_view_scale_quarter,
70 ID_help_report_a_problem,
71 ID_tools_check_for_updates,
74 class DOMFrame : public wxFrame
78 : wxFrame (0, -1, _("DCP-o-matic Player"))
79 , _update_news_requested (false)
85 #if defined(DCPOMATIC_WINDOWS)
86 maybe_open_console ();
87 cout << "DCP-o-matic Player is starting." << "\n";
90 wxMenuBar* bar = new wxMenuBar;
94 #ifdef DCPOMATIC_WINDOWS
95 SetIcon (wxIcon (std_to_wx ("id")));
98 _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this));
100 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
101 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_ov, this), ID_file_add_ov);
102 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_kdm, this), ID_file_add_kdm);
103 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_close, this), ID_file_close);
104 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
105 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
106 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>()), ID_view_scale_appropriate);
107 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(0)), ID_view_scale_full);
108 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(1)), ID_view_scale_half);
109 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(2)), ID_view_scale_quarter);
110 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
111 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
112 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
114 /* Use a panel as the only child of the Frame so that we avoid
115 the dark-grey background on Windows.
117 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
119 _viewer = new FilmViewer (overall_panel, false, false);
120 _info = new PlayerInformation (overall_panel, _viewer);
121 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
122 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
123 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
124 overall_panel->SetSizer (main_sizer);
126 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
129 void set_decode_reduction (optional<int> reduction)
131 _viewer->set_dcp_decode_reduction (reduction);
132 _info->triggered_update ();
135 void load_dcp (boost::filesystem::path dir)
137 _film.reset (new Film (optional<boost::filesystem::path>()));
138 shared_ptr<DCPContent> dcp (new DCPContent (_film, dir));
139 _film->examine_and_add_content (dcp, true);
141 JobManager* jm = JobManager::instance ();
143 wxProgressDialog* progress = new wxProgressDialog (_("DCP-o-matic Player"), _("Loading DCP"));
145 while (jm->work_to_do() || signal_manager->ui_idle()) {
150 progress->Destroy ();
152 DCPOMATIC_ASSERT (!jm->get().empty());
154 shared_ptr<Job> last = jm->get().back();
155 if (last->finished_in_error()) {
156 error_dialog (this, std_to_wx (last->error_summary()) + ".\n");
161 dcp->subtitle->set_use (true);
164 _viewer->set_film (_film);
165 _info->triggered_update ();
170 void setup_menu (wxMenuBar* m)
172 wxMenu* file = new wxMenu;
173 file->Append (ID_file_open, _("&Open...\tCtrl-O"));
174 file->Append (ID_file_add_ov, _("&Add OV..."));
175 file->Append (ID_file_add_kdm, _("&Add KDM..."));
176 file->AppendSeparator ();
177 file->Append (ID_file_close, _("&Close"));
178 file->AppendSeparator ();
181 file->Append (wxID_EXIT, _("&Exit"));
183 file->Append (wxID_EXIT, _("&Quit"));
187 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
189 wxMenu* edit = new wxMenu;
190 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
193 wxMenu* view = new wxMenu;
194 view->AppendRadioItem (ID_view_scale_appropriate, _("Set decode resolution to match display"));
195 view->AppendRadioItem (ID_view_scale_full, _("Decode at full resolution"));
196 view->AppendRadioItem (ID_view_scale_half, _("Decode at half resolution"));
197 view->AppendRadioItem (ID_view_scale_quarter, _("Decode at quarter resolution"));
199 wxMenu* tools = new wxMenu;
200 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
202 wxMenu* help = new wxMenu;
204 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
206 help->Append (wxID_ABOUT, _("About"));
208 help->Append (ID_help_report_a_problem, _("Report a problem..."));
210 m->Append (file, _("&File"));
212 m->Append (edit, _("&Edit"));
214 m->Append (view, _("&View"));
215 m->Append (tools, _("&Tools"));
216 m->Append (help, _("&Help"));
221 wxString d = wxStandardPaths::Get().GetDocumentsDir();
222 if (Config::instance()->last_player_load_directory()) {
223 d = std_to_wx (Config::instance()->last_player_load_directory()->string());
226 wxDirDialog* c = new wxDirDialog (this, _("Select DCP to open"), d, wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
231 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
232 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
239 boost::filesystem::path const dcp (wx_to_std (c->GetPath ()));
241 Config::instance()->set_last_player_load_directory (dcp.parent_path());
249 wxDirDialog* c = new wxDirDialog (
251 _("Select DCP to open as OV"),
252 wxStandardPaths::Get().GetDocumentsDir(),
253 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
259 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
260 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
267 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
268 DCPOMATIC_ASSERT (dcp);
269 dcp->add_ov (wx_to_std(c->GetPath()));
270 dcp->examine (shared_ptr<Job>());
274 _info->triggered_update ();
279 wxFileDialog* d = new wxFileDialog (this, _("Select KDM"));
281 if (d->ShowModal() == wxID_OK) {
282 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
283 DCPOMATIC_ASSERT (dcp);
285 dcp->add_kdm (dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE)));
286 } catch (exception& e) {
287 error_dialog (this, wxString::Format (_("Could not load KDM (%s)"), std_to_wx(e.what())));
292 dcp->examine (shared_ptr<Job>());
296 _info->triggered_update ();
301 _viewer->set_film (shared_ptr<Film>());
302 _info->triggered_update ();
310 void edit_preferences ()
312 if (!_config_dialog) {
313 _config_dialog = create_player_config_dialog ();
315 _config_dialog->Show (this);
318 void tools_check_for_updates ()
320 UpdateChecker::instance()->run ();
321 _update_news_requested = true;
326 AboutDialog* d = new AboutDialog (this);
331 void help_report_a_problem ()
333 ReportProblemDialog* d = new ReportProblemDialog (this);
334 if (d->ShowModal () == wxID_OK) {
340 void update_checker_state_changed ()
342 UpdateChecker* uc = UpdateChecker::instance ();
344 bool const announce =
345 _update_news_requested ||
346 (uc->stable() && Config::instance()->check_for_updates()) ||
347 (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
349 _update_news_requested = false;
355 if (uc->state() == UpdateChecker::YES) {
356 UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
357 dialog->ShowModal ();
359 } else if (uc->state() == UpdateChecker::FAILED) {
360 error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
362 error_dialog (this, _("There are no new versions of DCP-o-matic available."));
365 _update_news_requested = false;
368 void config_changed ()
370 /* Instantly save any config changes when using the player GUI */
372 Config::instance()->write_config();
373 } catch (exception& e) {
377 _("Could not write to config file at %s. Your changes have not been saved."),
378 std_to_wx (Config::instance()->cinemas_file().string()).data()
384 bool _update_news_requested;
385 PlayerInformation* _info;
386 wxPreferencesEditor* _config_dialog;
388 boost::shared_ptr<Film> _film;
389 boost::signals2::scoped_connection _config_changed_connection;
392 static const wxCmdLineEntryDesc command_line_description[] = {
393 { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
394 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
398 * @brief The magic App class for wxWidgets.
400 class App : public wxApp
413 wxInitAllImageHandlers ();
415 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
417 wxSplashScreen* splash = 0;
419 if (!Config::have_existing ("config.xml")) {
421 boost::filesystem::path p = shared_path () / "splash.png";
422 if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
423 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
427 } catch (boost::filesystem::filesystem_error& e) {
428 /* Maybe we couldn't find the splash image; never mind */
431 SetAppName (_("DCP-o-matic Player"));
433 if (!wxApp::OnInit()) {
437 #ifdef DCPOMATIC_LINUX
438 unsetenv ("UBUNTU_MENUPROXY");
442 ProcessSerialNumber serial;
443 GetCurrentProcess (&serial);
444 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
447 dcpomatic_setup_path_encoding ();
449 /* Enable i18n; this will create a Config object
450 to look for a force-configured language. This Config
451 object will be wrong, however, because dcpomatic_setup
452 hasn't yet been called and there aren't any filters etc.
455 dcpomatic_setup_i18n ();
457 /* Set things up, including filters etc.
458 which will now be internationalised correctly.
462 /* Force the configuration to be re-loaded correctly next
467 _frame = new DOMFrame ();
468 SetTopWindow (_frame);
475 signal_manager = new wxSignalManager (this);
477 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
479 _frame->load_dcp (_dcp_to_load);
480 } catch (exception& e) {
481 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1 (%2)")), _dcp_to_load, e.what())));
485 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
487 if (Config::instance()->check_for_updates ()) {
488 UpdateChecker::instance()->run ();
495 error_dialog (0, wxString::Format ("DCP-o-matic Player could not start: %s", std_to_wx(e.what())));
499 void OnInitCmdLine (wxCmdLineParser& parser)
501 parser.SetDesc (command_line_description);
502 parser.SetSwitchChars (wxT ("-"));
505 bool OnCmdLineParsed (wxCmdLineParser& parser)
507 if (parser.GetParamCount() > 0) {
508 _dcp_to_load = wx_to_std (parser.GetParam (0));
514 void report_exception ()
518 } catch (FileError& e) {
522 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
523 std_to_wx (e.what()),
524 std_to_wx (e.file().string().c_str ())
527 } catch (exception& e) {
531 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
532 std_to_wx (e.what ())
536 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
540 /* An unhandled exception has occurred inside the main event loop */
541 bool OnExceptionInMainLoop ()
544 /* This will terminate the program */
548 void OnUnhandledException ()
555 signal_manager->ui_idle ();
558 void config_failed_to_load ()
560 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));