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/encode_server_finder.h"
27 #include "lib/dcp_content.h"
28 #include "lib/job_manager.h"
30 #include "lib/video_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/full_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 <boost/bind.hpp>
50 using boost::shared_ptr;
51 using boost::optional;
55 ID_view_scale_appropriate,
58 ID_view_scale_quarter,
59 ID_help_report_a_problem,
60 ID_tools_check_for_updates,
63 class DOMFrame : public wxFrame
67 : wxFrame (0, -1, _("DCP-o-matic Player"))
68 , _update_news_requested (false)
74 #if defined(DCPOMATIC_WINDOWS)
75 maybe_open_console ();
76 cout << "DCP-o-matic Player is starting." << "\n";
79 wxMenuBar* bar = new wxMenuBar;
83 #ifdef DCPOMATIC_WINDOWS
84 SetIcon (wxIcon (std_to_wx ("id")));
87 _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this));
89 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
90 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
91 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
92 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>()), ID_view_scale_appropriate);
93 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(0)), ID_view_scale_full);
94 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(1)), ID_view_scale_half);
95 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(2)), ID_view_scale_quarter);
96 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
97 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
98 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
100 /* Use a panel as the only child of the Frame so that we avoid
101 the dark-grey background on Windows.
103 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
105 _viewer = new FilmViewer (overall_panel, false, false);
106 _info = new PlayerInformation (overall_panel, _viewer);
107 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
108 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
109 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
110 overall_panel->SetSizer (main_sizer);
112 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
115 void set_decode_reduction (optional<int> reduction)
117 _viewer->set_dcp_decode_reduction (reduction);
120 void load_dcp (boost::filesystem::path dir)
122 _film.reset (new Film (optional<boost::filesystem::path>()));
123 shared_ptr<DCPContent> dcp (new DCPContent (_film, dir));
124 _film->examine_and_add_content (dcp);
126 JobManager* jm = JobManager::instance ();
127 while (jm->work_to_do ()) {
128 /* XXX: progress dialog */
129 while (signal_manager->ui_idle ()) {}
133 while (signal_manager->ui_idle ()) {}
137 BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
138 if (i->finished_in_error()) {
139 errors += std_to_wx (i->error_summary()) + "\n";
142 error_dialog (this, errors);
146 _viewer->set_film (_film);
147 _info->triggered_update ();
152 void setup_menu (wxMenuBar* m)
154 wxMenu* file = new wxMenu;
155 file->Append (ID_file_open, _("&Open...\tCtrl-O"));
158 file->Append (wxID_EXIT, _("&Exit"));
160 file->Append (wxID_EXIT, _("&Quit"));
164 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
166 wxMenu* edit = new wxMenu;
167 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
170 wxMenu* view = new wxMenu;
171 view->AppendRadioItem (ID_view_scale_appropriate, _("Set decode resolution to match display"));
172 view->AppendRadioItem (ID_view_scale_full, _("Decode at full resolution"));
173 view->AppendRadioItem (ID_view_scale_half, _("Decode at half resolution"));
174 view->AppendRadioItem (ID_view_scale_quarter, _("Decode at quarter resolution"));
176 wxMenu* tools = new wxMenu;
177 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
179 wxMenu* help = new wxMenu;
181 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
183 help->Append (wxID_ABOUT, _("About"));
185 help->Append (ID_help_report_a_problem, _("Report a problem..."));
187 m->Append (file, _("&File"));
189 m->Append (edit, _("&Edit"));
191 m->Append (view, _("&View"));
192 m->Append (tools, _("&Tools"));
193 m->Append (help, _("&Help"));
198 wxDirDialog* c = new wxDirDialog (
200 _("Select DCP to open"),
201 wxStandardPaths::Get().GetDocumentsDir(),
202 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
208 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
209 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
216 load_dcp (wx_to_std (c->GetPath ()));
227 void edit_preferences ()
229 if (!_config_dialog) {
230 _config_dialog = create_full_config_dialog ();
232 _config_dialog->Show (this);
235 void tools_check_for_updates ()
237 UpdateChecker::instance()->run ();
238 _update_news_requested = true;
243 AboutDialog* d = new AboutDialog (this);
248 void help_report_a_problem ()
250 ReportProblemDialog* d = new ReportProblemDialog (this);
251 if (d->ShowModal () == wxID_OK) {
257 void update_checker_state_changed ()
259 UpdateChecker* uc = UpdateChecker::instance ();
261 bool const announce =
262 _update_news_requested ||
263 (uc->stable() && Config::instance()->check_for_updates()) ||
264 (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
266 _update_news_requested = false;
272 if (uc->state() == UpdateChecker::YES) {
273 UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
274 dialog->ShowModal ();
276 } else if (uc->state() == UpdateChecker::FAILED) {
277 error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
279 error_dialog (this, _("There are no new versions of DCP-o-matic available."));
282 _update_news_requested = false;
285 void config_changed ()
287 /* Instantly save any config changes when using the player GUI */
289 Config::instance()->write_config();
290 } catch (exception& e) {
294 _("Could not write to config file at %s. Your changes have not been saved."),
295 std_to_wx (Config::instance()->cinemas_file().string()).data()
301 bool _update_news_requested;
302 PlayerInformation* _info;
303 wxPreferencesEditor* _config_dialog;
305 boost::shared_ptr<Film> _film;
306 boost::signals2::scoped_connection _config_changed_connection;
309 static const wxCmdLineEntryDesc command_line_description[] = {
310 { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
311 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
315 * @brief The magic App class for wxWidgets.
317 class App : public wxApp
330 wxInitAllImageHandlers ();
332 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
334 wxSplashScreen* splash = 0;
336 if (!Config::have_existing ("config.xml")) {
338 boost::filesystem::path p = shared_path () / "splash.png";
339 if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
340 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
344 } catch (boost::filesystem::filesystem_error& e) {
345 /* Maybe we couldn't find the splash image; never mind */
348 SetAppName (_("DCP-o-matic Player"));
350 if (!wxApp::OnInit()) {
354 #ifdef DCPOMATIC_LINUX
355 unsetenv ("UBUNTU_MENUPROXY");
359 ProcessSerialNumber serial;
360 GetCurrentProcess (&serial);
361 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
364 dcpomatic_setup_path_encoding ();
366 /* Enable i18n; this will create a Config object
367 to look for a force-configured language. This Config
368 object will be wrong, however, because dcpomatic_setup
369 hasn't yet been called and there aren't any filters etc.
372 dcpomatic_setup_i18n ();
374 /* Set things up, including filters etc.
375 which will now be internationalised correctly.
379 /* Force the configuration to be re-loaded correctly next
384 _frame = new DOMFrame ();
385 SetTopWindow (_frame);
392 signal_manager = new wxSignalManager (this);
394 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
396 _frame->load_dcp (_dcp_to_load);
397 } catch (exception& e) {
398 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1 (%2)")), _dcp_to_load, e.what())));
402 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
404 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
405 _timer.reset (new wxTimer (this));
406 _timer->Start (1000);
408 if (Config::instance()->check_for_updates ()) {
409 UpdateChecker::instance()->run ();
416 error_dialog (0, wxString::Format ("DCP-o-matic Player could not start: %s", e.what ()));
420 void OnInitCmdLine (wxCmdLineParser& parser)
422 parser.SetDesc (command_line_description);
423 parser.SetSwitchChars (wxT ("-"));
426 bool OnCmdLineParsed (wxCmdLineParser& parser)
428 if (parser.GetParamCount() > 0) {
429 _dcp_to_load = wx_to_std (parser.GetParam (0));
435 void report_exception ()
439 } catch (FileError& e) {
443 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
444 std_to_wx (e.what()),
445 std_to_wx (e.file().string().c_str ())
448 } catch (exception& e) {
452 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
453 std_to_wx (e.what ())
457 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
461 /* An unhandled exception has occurred inside the main event loop */
462 bool OnExceptionInMainLoop ()
465 /* This will terminate the program */
469 void OnUnhandledException ()
476 signal_manager->ui_idle ();
482 EncodeServerFinder::instance()->rethrow ();
483 } catch (exception& e) {
484 error_dialog (0, std_to_wx (e.what ()));
488 void config_failed_to_load ()
490 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
494 shared_ptr<wxTimer> _timer;