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/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>
53 using boost::shared_ptr;
54 using boost::optional;
58 ID_view_scale_appropriate,
61 ID_view_scale_quarter,
62 ID_help_report_a_problem,
63 ID_tools_check_for_updates,
66 class DOMFrame : public wxFrame
70 : wxFrame (0, -1, _("DCP-o-matic Player"))
71 , _update_news_requested (false)
77 #if defined(DCPOMATIC_WINDOWS)
78 maybe_open_console ();
79 cout << "DCP-o-matic Player is starting." << "\n";
82 wxMenuBar* bar = new wxMenuBar;
86 #ifdef DCPOMATIC_WINDOWS
87 SetIcon (wxIcon (std_to_wx ("id")));
90 _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this));
92 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
93 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
94 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
95 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>()), ID_view_scale_appropriate);
96 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(0)), ID_view_scale_full);
97 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(1)), ID_view_scale_half);
98 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(2)), ID_view_scale_quarter);
99 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
100 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
101 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
103 /* Use a panel as the only child of the Frame so that we avoid
104 the dark-grey background on Windows.
106 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
108 _viewer = new FilmViewer (overall_panel, false, false);
109 _info = new PlayerInformation (overall_panel, _viewer);
110 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
111 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
112 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
113 overall_panel->SetSizer (main_sizer);
115 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
118 void set_decode_reduction (optional<int> reduction)
120 _viewer->set_dcp_decode_reduction (reduction);
123 void load_dcp (boost::filesystem::path dir)
125 _film.reset (new Film (optional<boost::filesystem::path>()));
126 shared_ptr<DCPContent> dcp (new DCPContent (_film, dir));
127 _film->examine_and_add_content (dcp);
129 JobManager* jm = JobManager::instance ();
130 while (jm->work_to_do ()) {
131 /* XXX: progress dialog */
132 while (signal_manager->ui_idle ()) {}
136 while (signal_manager->ui_idle ()) {}
140 BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
141 if (i->finished_in_error()) {
142 errors += std_to_wx (i->error_summary()) + "\n";
145 error_dialog (this, errors);
149 _viewer->set_film (_film);
150 _info->triggered_update ();
155 void setup_menu (wxMenuBar* m)
157 wxMenu* file = new wxMenu;
158 file->Append (ID_file_open, _("&Open...\tCtrl-O"));
161 file->Append (wxID_EXIT, _("&Exit"));
163 file->Append (wxID_EXIT, _("&Quit"));
167 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
169 wxMenu* edit = new wxMenu;
170 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
173 wxMenu* view = new wxMenu;
174 view->AppendRadioItem (ID_view_scale_appropriate, _("Set decode resolution to match display"));
175 view->AppendRadioItem (ID_view_scale_full, _("Decode at full resolution"));
176 view->AppendRadioItem (ID_view_scale_half, _("Decode at half resolution"));
177 view->AppendRadioItem (ID_view_scale_quarter, _("Decode at quarter resolution"));
179 wxMenu* tools = new wxMenu;
180 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
182 wxMenu* help = new wxMenu;
184 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
186 help->Append (wxID_ABOUT, _("About"));
188 help->Append (ID_help_report_a_problem, _("Report a problem..."));
190 m->Append (file, _("&File"));
192 m->Append (edit, _("&Edit"));
194 m->Append (view, _("&View"));
195 m->Append (tools, _("&Tools"));
196 m->Append (help, _("&Help"));
201 wxDirDialog* c = new wxDirDialog (
203 _("Select DCP to open"),
204 wxStandardPaths::Get().GetDocumentsDir(),
205 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
211 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
212 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
219 load_dcp (wx_to_std (c->GetPath ()));
230 void edit_preferences ()
232 if (!_config_dialog) {
233 _config_dialog = create_player_config_dialog ();
235 _config_dialog->Show (this);
238 void tools_check_for_updates ()
240 UpdateChecker::instance()->run ();
241 _update_news_requested = true;
246 AboutDialog* d = new AboutDialog (this);
251 void help_report_a_problem ()
253 ReportProblemDialog* d = new ReportProblemDialog (this);
254 if (d->ShowModal () == wxID_OK) {
260 void update_checker_state_changed ()
262 UpdateChecker* uc = UpdateChecker::instance ();
264 bool const announce =
265 _update_news_requested ||
266 (uc->stable() && Config::instance()->check_for_updates()) ||
267 (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
269 _update_news_requested = false;
275 if (uc->state() == UpdateChecker::YES) {
276 UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
277 dialog->ShowModal ();
279 } else if (uc->state() == UpdateChecker::FAILED) {
280 error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
282 error_dialog (this, _("There are no new versions of DCP-o-matic available."));
285 _update_news_requested = false;
288 void config_changed ()
290 /* Instantly save any config changes when using the player GUI */
292 Config::instance()->write_config();
293 } catch (exception& e) {
297 _("Could not write to config file at %s. Your changes have not been saved."),
298 std_to_wx (Config::instance()->cinemas_file().string()).data()
304 bool _update_news_requested;
305 PlayerInformation* _info;
306 wxPreferencesEditor* _config_dialog;
308 boost::shared_ptr<Film> _film;
309 boost::signals2::scoped_connection _config_changed_connection;
312 static const wxCmdLineEntryDesc command_line_description[] = {
313 { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
314 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
318 * @brief The magic App class for wxWidgets.
320 class App : public wxApp
333 wxInitAllImageHandlers ();
335 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
337 wxSplashScreen* splash = 0;
339 if (!Config::have_existing ("config.xml")) {
341 boost::filesystem::path p = shared_path () / "splash.png";
342 if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
343 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
347 } catch (boost::filesystem::filesystem_error& e) {
348 /* Maybe we couldn't find the splash image; never mind */
351 SetAppName (_("DCP-o-matic Player"));
353 if (!wxApp::OnInit()) {
357 #ifdef DCPOMATIC_LINUX
358 unsetenv ("UBUNTU_MENUPROXY");
362 ProcessSerialNumber serial;
363 GetCurrentProcess (&serial);
364 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
367 dcpomatic_setup_path_encoding ();
369 /* Enable i18n; this will create a Config object
370 to look for a force-configured language. This Config
371 object will be wrong, however, because dcpomatic_setup
372 hasn't yet been called and there aren't any filters etc.
375 dcpomatic_setup_i18n ();
377 /* Set things up, including filters etc.
378 which will now be internationalised correctly.
382 /* Force the configuration to be re-loaded correctly next
387 _frame = new DOMFrame ();
388 SetTopWindow (_frame);
395 signal_manager = new wxSignalManager (this);
397 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
399 _frame->load_dcp (_dcp_to_load);
400 } catch (exception& e) {
401 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1 (%2)")), _dcp_to_load, e.what())));
405 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
407 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
408 _timer.reset (new wxTimer (this));
409 _timer->Start (1000);
411 if (Config::instance()->check_for_updates ()) {
412 UpdateChecker::instance()->run ();
419 error_dialog (0, wxString::Format ("DCP-o-matic Player could not start: %s", e.what ()));
423 void OnInitCmdLine (wxCmdLineParser& parser)
425 parser.SetDesc (command_line_description);
426 parser.SetSwitchChars (wxT ("-"));
429 bool OnCmdLineParsed (wxCmdLineParser& parser)
431 if (parser.GetParamCount() > 0) {
432 _dcp_to_load = wx_to_std (parser.GetParam (0));
438 void report_exception ()
442 } catch (FileError& e) {
446 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
447 std_to_wx (e.what()),
448 std_to_wx (e.file().string().c_str ())
451 } catch (exception& e) {
455 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
456 std_to_wx (e.what ())
460 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
464 /* An unhandled exception has occurred inside the main event loop */
465 bool OnExceptionInMainLoop ()
468 /* This will terminate the program */
472 void OnUnhandledException ()
479 signal_manager->ui_idle ();
485 EncodeServerFinder::instance()->rethrow ();
486 } catch (exception& e) {
487 error_dialog (0, std_to_wx (e.what ()));
491 void config_failed_to_load ()
493 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
497 shared_ptr<wxTimer> _timer;