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>
45 #include <ApplicationServices/ApplicationServices.h>
47 #include <boost/bind.hpp>
57 using boost::shared_ptr;
58 using boost::optional;
64 ID_view_scale_appropriate,
67 ID_view_scale_quarter,
68 ID_help_report_a_problem,
69 ID_tools_check_for_updates,
72 class DOMFrame : public wxFrame
76 : wxFrame (0, -1, _("DCP-o-matic Player"))
77 , _update_news_requested (false)
83 #if defined(DCPOMATIC_WINDOWS)
84 maybe_open_console ();
85 cout << "DCP-o-matic Player is starting." << "\n";
88 wxMenuBar* bar = new wxMenuBar;
92 #ifdef DCPOMATIC_WINDOWS
93 SetIcon (wxIcon (std_to_wx ("id")));
96 _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this));
98 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
99 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_ov, this), ID_file_add_ov);
100 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_kdm, this), ID_file_add_kdm);
101 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
102 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
103 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>()), ID_view_scale_appropriate);
104 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(0)), ID_view_scale_full);
105 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(1)), ID_view_scale_half);
106 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(2)), ID_view_scale_quarter);
107 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
108 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
109 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
111 /* Use a panel as the only child of the Frame so that we avoid
112 the dark-grey background on Windows.
114 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
116 _viewer = new FilmViewer (overall_panel, false, false);
117 _info = new PlayerInformation (overall_panel, _viewer);
118 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
119 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
120 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
121 overall_panel->SetSizer (main_sizer);
123 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
126 void set_decode_reduction (optional<int> reduction)
128 _viewer->set_dcp_decode_reduction (reduction);
131 void load_dcp (boost::filesystem::path dir)
133 _film.reset (new Film (optional<boost::filesystem::path>()));
134 shared_ptr<DCPContent> dcp (new DCPContent (_film, dir));
135 _film->examine_and_add_content (dcp, true);
137 JobManager* jm = JobManager::instance ();
139 while (jm->work_to_do() || signal_manager->ui_idle()) {
140 /* XXX: progress dialog */
144 DCPOMATIC_ASSERT (!jm->get().empty());
146 shared_ptr<Job> last = jm->get().back();
147 if (last->finished_in_error()) {
148 error_dialog (this, std_to_wx (last->error_summary()) + ".\n");
153 dcp->subtitle->set_use (true);
156 _viewer->set_film (_film);
157 _info->triggered_update ();
162 void setup_menu (wxMenuBar* m)
164 wxMenu* file = new wxMenu;
165 file->Append (ID_file_open, _("&Open...\tCtrl-O"));
166 file->Append (ID_file_add_ov, _("&Add OV..."));
167 file->Append (ID_file_add_kdm, _("&Add KDM..."));
170 file->Append (wxID_EXIT, _("&Exit"));
172 file->Append (wxID_EXIT, _("&Quit"));
176 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
178 wxMenu* edit = new wxMenu;
179 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
182 wxMenu* view = new wxMenu;
183 view->AppendRadioItem (ID_view_scale_appropriate, _("Set decode resolution to match display"));
184 view->AppendRadioItem (ID_view_scale_full, _("Decode at full resolution"));
185 view->AppendRadioItem (ID_view_scale_half, _("Decode at half resolution"));
186 view->AppendRadioItem (ID_view_scale_quarter, _("Decode at quarter resolution"));
188 wxMenu* tools = new wxMenu;
189 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
191 wxMenu* help = new wxMenu;
193 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
195 help->Append (wxID_ABOUT, _("About"));
197 help->Append (ID_help_report_a_problem, _("Report a problem..."));
199 m->Append (file, _("&File"));
201 m->Append (edit, _("&Edit"));
203 m->Append (view, _("&View"));
204 m->Append (tools, _("&Tools"));
205 m->Append (help, _("&Help"));
210 wxDirDialog* c = new wxDirDialog (
212 _("Select DCP to open"),
213 wxStandardPaths::Get().GetDocumentsDir(),
214 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
220 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
221 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
228 load_dcp (wx_to_std (c->GetPath ()));
236 wxDirDialog* c = new wxDirDialog (
238 _("Select DCP to open as OV"),
239 wxStandardPaths::Get().GetDocumentsDir(),
240 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
246 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
247 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
254 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
255 DCPOMATIC_ASSERT (dcp);
256 dcp->add_ov (wx_to_std(c->GetPath()));
257 dcp->examine (shared_ptr<Job>());
261 _info->triggered_update ();
266 wxFileDialog* d = new wxFileDialog (this, _("Select KDM"));
268 if (d->ShowModal() == wxID_OK) {
269 shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
270 DCPOMATIC_ASSERT (dcp);
272 dcp->add_kdm (dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE)));
273 } catch (exception& e) {
274 error_dialog (this, wxString::Format (_("Could not load KDM (%s)"), e.what ()));
279 dcp->examine (shared_ptr<Job>());
283 _info->triggered_update ();
291 void edit_preferences ()
293 if (!_config_dialog) {
294 _config_dialog = create_player_config_dialog ();
296 _config_dialog->Show (this);
299 void tools_check_for_updates ()
301 UpdateChecker::instance()->run ();
302 _update_news_requested = true;
307 AboutDialog* d = new AboutDialog (this);
312 void help_report_a_problem ()
314 ReportProblemDialog* d = new ReportProblemDialog (this);
315 if (d->ShowModal () == wxID_OK) {
321 void update_checker_state_changed ()
323 UpdateChecker* uc = UpdateChecker::instance ();
325 bool const announce =
326 _update_news_requested ||
327 (uc->stable() && Config::instance()->check_for_updates()) ||
328 (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
330 _update_news_requested = false;
336 if (uc->state() == UpdateChecker::YES) {
337 UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
338 dialog->ShowModal ();
340 } else if (uc->state() == UpdateChecker::FAILED) {
341 error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
343 error_dialog (this, _("There are no new versions of DCP-o-matic available."));
346 _update_news_requested = false;
349 void config_changed ()
351 /* Instantly save any config changes when using the player GUI */
353 Config::instance()->write_config();
354 } catch (exception& e) {
358 _("Could not write to config file at %s. Your changes have not been saved."),
359 std_to_wx (Config::instance()->cinemas_file().string()).data()
365 bool _update_news_requested;
366 PlayerInformation* _info;
367 wxPreferencesEditor* _config_dialog;
369 boost::shared_ptr<Film> _film;
370 boost::signals2::scoped_connection _config_changed_connection;
373 static const wxCmdLineEntryDesc command_line_description[] = {
374 { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
375 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
379 * @brief The magic App class for wxWidgets.
381 class App : public wxApp
394 wxInitAllImageHandlers ();
396 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
398 wxSplashScreen* splash = 0;
400 if (!Config::have_existing ("config.xml")) {
402 boost::filesystem::path p = shared_path () / "splash.png";
403 if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
404 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
408 } catch (boost::filesystem::filesystem_error& e) {
409 /* Maybe we couldn't find the splash image; never mind */
412 SetAppName (_("DCP-o-matic Player"));
414 if (!wxApp::OnInit()) {
418 #ifdef DCPOMATIC_LINUX
419 unsetenv ("UBUNTU_MENUPROXY");
423 ProcessSerialNumber serial;
424 GetCurrentProcess (&serial);
425 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
428 dcpomatic_setup_path_encoding ();
430 /* Enable i18n; this will create a Config object
431 to look for a force-configured language. This Config
432 object will be wrong, however, because dcpomatic_setup
433 hasn't yet been called and there aren't any filters etc.
436 dcpomatic_setup_i18n ();
438 /* Set things up, including filters etc.
439 which will now be internationalised correctly.
443 /* Force the configuration to be re-loaded correctly next
448 _frame = new DOMFrame ();
449 SetTopWindow (_frame);
456 signal_manager = new wxSignalManager (this);
458 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
460 _frame->load_dcp (_dcp_to_load);
461 } catch (exception& e) {
462 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1 (%2)")), _dcp_to_load, e.what())));
466 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
468 if (Config::instance()->check_for_updates ()) {
469 UpdateChecker::instance()->run ();
476 error_dialog (0, wxString::Format ("DCP-o-matic Player could not start: %s", e.what ()));
480 void OnInitCmdLine (wxCmdLineParser& parser)
482 parser.SetDesc (command_line_description);
483 parser.SetSwitchChars (wxT ("-"));
486 bool OnCmdLineParsed (wxCmdLineParser& parser)
488 if (parser.GetParamCount() > 0) {
489 _dcp_to_load = wx_to_std (parser.GetParam (0));
495 void report_exception ()
499 } catch (FileError& e) {
503 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
504 std_to_wx (e.what()),
505 std_to_wx (e.file().string().c_str ())
508 } catch (exception& e) {
512 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
513 std_to_wx (e.what ())
517 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
521 /* An unhandled exception has occurred inside the main event loop */
522 bool OnExceptionInMainLoop ()
525 /* This will terminate the program */
529 void OnUnhandledException ()
536 signal_manager->ui_idle ();
539 void config_failed_to_load ()
541 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));