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"
39 #include <wx/stdpaths.h>
40 #include <wx/splash.h>
41 #include <wx/cmdline.h>
42 #include <boost/bind.hpp>
48 using boost::shared_ptr;
49 using boost::optional;
53 ID_view_scale_appropriate,
56 ID_view_scale_quarter,
57 ID_help_report_a_problem,
58 ID_tools_check_for_updates,
61 class DOMFrame : public wxFrame
65 : wxFrame (0, -1, _("DCP-o-matic Player"))
66 , _update_news_requested (false)
69 #if defined(DCPOMATIC_WINDOWS)
70 maybe_open_console ();
71 cout << "DCP-o-matic Player is starting." << "\n";
74 wxMenuBar* bar = new wxMenuBar;
78 #ifdef DCPOMATIC_WINDOWS
79 SetIcon (wxIcon (std_to_wx ("id")));
82 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
83 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
84 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>()), ID_view_scale_appropriate);
85 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(0)), ID_view_scale_full);
86 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(1)), ID_view_scale_half);
87 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(2)), ID_view_scale_quarter);
88 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
89 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
90 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
92 /* Use a panel as the only child of the Frame so that we avoid
93 the dark-grey background on Windows.
95 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
97 _viewer = new FilmViewer (overall_panel, false, false);
98 _info = new PlayerInformation (overall_panel, _viewer);
99 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
100 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
101 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
102 overall_panel->SetSizer (main_sizer);
104 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
107 void set_decode_reduction (optional<int> reduction)
109 _viewer->set_dcp_decode_reduction (reduction);
112 void load_dcp (boost::filesystem::path dir)
114 _film.reset (new Film (optional<boost::filesystem::path>()));
115 shared_ptr<DCPContent> dcp (new DCPContent (_film, dir));
116 _film->examine_and_add_content (dcp);
118 JobManager* jm = JobManager::instance ();
119 while (jm->work_to_do ()) {
120 /* XXX: progress dialog */
121 while (signal_manager->ui_idle ()) {}
125 while (signal_manager->ui_idle ()) {}
129 BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
130 if (i->finished_in_error()) {
131 errors += std_to_wx (i->error_summary()) + "\n";
134 error_dialog (this, errors);
138 _viewer->set_film (_film);
144 void setup_menu (wxMenuBar* m)
146 wxMenu* file = new wxMenu;
147 file->Append (ID_file_open, _("&Open...\tCtrl-O"));
150 file->Append (wxID_EXIT, _("&Exit"));
152 file->Append (wxID_EXIT, _("&Quit"));
156 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
158 wxMenu* edit = new wxMenu;
159 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
162 wxMenu* view = new wxMenu;
163 view->AppendRadioItem (ID_view_scale_appropriate, _("Set decode resolution to match display"));
164 view->AppendRadioItem (ID_view_scale_full, _("Decode at full resolution"));
165 view->AppendRadioItem (ID_view_scale_half, _("Decode at half resolution"));
166 view->AppendRadioItem (ID_view_scale_quarter, _("Decode at quarter resolution"));
168 wxMenu* tools = new wxMenu;
169 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
171 wxMenu* help = new wxMenu;
173 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
175 help->Append (wxID_ABOUT, _("About"));
177 help->Append (ID_help_report_a_problem, _("Report a problem..."));
179 m->Append (file, _("&File"));
181 m->Append (edit, _("&Edit"));
183 m->Append (view, _("&View"));
184 m->Append (tools, _("&Tools"));
185 m->Append (help, _("&Help"));
190 wxDirDialog* c = new wxDirDialog (
192 _("Select DCP to open"),
193 wxStandardPaths::Get().GetDocumentsDir(),
194 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
200 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
201 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
208 load_dcp (wx_to_std (c->GetPath ()));
219 void tools_check_for_updates ()
221 UpdateChecker::instance()->run ();
222 _update_news_requested = true;
227 AboutDialog* d = new AboutDialog (this);
232 void help_report_a_problem ()
234 ReportProblemDialog* d = new ReportProblemDialog (this);
235 if (d->ShowModal () == wxID_OK) {
241 void update_checker_state_changed ()
243 UpdateChecker* uc = UpdateChecker::instance ();
245 bool const announce =
246 _update_news_requested ||
247 (uc->stable() && Config::instance()->check_for_updates()) ||
248 (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
250 _update_news_requested = false;
256 if (uc->state() == UpdateChecker::YES) {
257 UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
258 dialog->ShowModal ();
260 } else if (uc->state() == UpdateChecker::FAILED) {
261 error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
263 error_dialog (this, _("There are no new versions of DCP-o-matic available."));
266 _update_news_requested = false;
269 bool _update_news_requested;
270 PlayerInformation* _info;
272 boost::shared_ptr<Film> _film;
275 static const wxCmdLineEntryDesc command_line_description[] = {
276 { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
277 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
281 * @brief The magic App class for wxWidgets.
283 class App : public wxApp
296 wxInitAllImageHandlers ();
298 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
300 wxSplashScreen* splash = 0;
302 if (!Config::have_existing ("config.xml")) {
304 boost::filesystem::path p = shared_path () / "splash.png";
305 if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
306 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
310 } catch (boost::filesystem::filesystem_error& e) {
311 /* Maybe we couldn't find the splash image; never mind */
314 SetAppName (_("DCP-o-matic Player"));
316 if (!wxApp::OnInit()) {
320 #ifdef DCPOMATIC_LINUX
321 unsetenv ("UBUNTU_MENUPROXY");
325 ProcessSerialNumber serial;
326 GetCurrentProcess (&serial);
327 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
330 dcpomatic_setup_path_encoding ();
332 /* Enable i18n; this will create a Config object
333 to look for a force-configured language. This Config
334 object will be wrong, however, because dcpomatic_setup
335 hasn't yet been called and there aren't any filters etc.
338 dcpomatic_setup_i18n ();
340 /* Set things up, including filters etc.
341 which will now be internationalised correctly.
345 /* Force the configuration to be re-loaded correctly next
350 _frame = new DOMFrame ();
351 SetTopWindow (_frame);
358 signal_manager = new wxSignalManager (this);
360 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
362 _frame->load_dcp (_dcp_to_load);
363 } catch (exception& e) {
364 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1 (%2)")), _dcp_to_load, e.what())));
368 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
370 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
371 _timer.reset (new wxTimer (this));
372 _timer->Start (1000);
374 if (Config::instance()->check_for_updates ()) {
375 UpdateChecker::instance()->run ();
382 error_dialog (0, wxString::Format ("DCP-o-matic Player could not start: %s", e.what ()));
386 void OnInitCmdLine (wxCmdLineParser& parser)
388 parser.SetDesc (command_line_description);
389 parser.SetSwitchChars (wxT ("-"));
392 bool OnCmdLineParsed (wxCmdLineParser& parser)
394 if (parser.GetParamCount() > 0) {
395 _dcp_to_load = wx_to_std (parser.GetParam (0));
401 void report_exception ()
405 } catch (FileError& e) {
409 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
410 std_to_wx (e.what()),
411 std_to_wx (e.file().string().c_str ())
414 } catch (exception& e) {
418 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
419 std_to_wx (e.what ())
423 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
427 /* An unhandled exception has occurred inside the main event loop */
428 bool OnExceptionInMainLoop ()
431 /* This will terminate the program */
435 void OnUnhandledException ()
442 signal_manager->ui_idle ();
448 EncodeServerFinder::instance()->rethrow ();
449 } catch (exception& e) {
450 error_dialog (0, std_to_wx (e.what ()));
454 void config_failed_to_load ()
456 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
460 shared_ptr<wxTimer> _timer;