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/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 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
88 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
89 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
90 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>()), ID_view_scale_appropriate);
91 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(0)), ID_view_scale_full);
92 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(1)), ID_view_scale_half);
93 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(2)), ID_view_scale_quarter);
94 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
95 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
96 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
98 /* Use a panel as the only child of the Frame so that we avoid
99 the dark-grey background on Windows.
101 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
103 _viewer = new FilmViewer (overall_panel, false, false);
104 _info = new PlayerInformation (overall_panel, _viewer);
105 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
106 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
107 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
108 overall_panel->SetSizer (main_sizer);
110 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
113 void set_decode_reduction (optional<int> reduction)
115 _viewer->set_dcp_decode_reduction (reduction);
118 void load_dcp (boost::filesystem::path dir)
120 _film.reset (new Film (optional<boost::filesystem::path>()));
121 shared_ptr<DCPContent> dcp (new DCPContent (_film, dir));
122 _film->examine_and_add_content (dcp);
124 JobManager* jm = JobManager::instance ();
125 while (jm->work_to_do ()) {
126 /* XXX: progress dialog */
127 while (signal_manager->ui_idle ()) {}
131 while (signal_manager->ui_idle ()) {}
135 BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
136 if (i->finished_in_error()) {
137 errors += std_to_wx (i->error_summary()) + "\n";
140 error_dialog (this, errors);
144 _viewer->set_film (_film);
145 _info->triggered_update ();
150 void setup_menu (wxMenuBar* m)
152 wxMenu* file = new wxMenu;
153 file->Append (ID_file_open, _("&Open...\tCtrl-O"));
156 file->Append (wxID_EXIT, _("&Exit"));
158 file->Append (wxID_EXIT, _("&Quit"));
162 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
164 wxMenu* edit = new wxMenu;
165 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
168 wxMenu* view = new wxMenu;
169 view->AppendRadioItem (ID_view_scale_appropriate, _("Set decode resolution to match display"));
170 view->AppendRadioItem (ID_view_scale_full, _("Decode at full resolution"));
171 view->AppendRadioItem (ID_view_scale_half, _("Decode at half resolution"));
172 view->AppendRadioItem (ID_view_scale_quarter, _("Decode at quarter resolution"));
174 wxMenu* tools = new wxMenu;
175 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
177 wxMenu* help = new wxMenu;
179 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
181 help->Append (wxID_ABOUT, _("About"));
183 help->Append (ID_help_report_a_problem, _("Report a problem..."));
185 m->Append (file, _("&File"));
187 m->Append (edit, _("&Edit"));
189 m->Append (view, _("&View"));
190 m->Append (tools, _("&Tools"));
191 m->Append (help, _("&Help"));
196 wxDirDialog* c = new wxDirDialog (
198 _("Select DCP to open"),
199 wxStandardPaths::Get().GetDocumentsDir(),
200 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
206 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
207 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
214 load_dcp (wx_to_std (c->GetPath ()));
225 void edit_preferences ()
227 if (!_config_dialog) {
228 _config_dialog = create_config_dialog ();
230 _config_dialog->Show (this);
233 void tools_check_for_updates ()
235 UpdateChecker::instance()->run ();
236 _update_news_requested = true;
241 AboutDialog* d = new AboutDialog (this);
246 void help_report_a_problem ()
248 ReportProblemDialog* d = new ReportProblemDialog (this);
249 if (d->ShowModal () == wxID_OK) {
255 void update_checker_state_changed ()
257 UpdateChecker* uc = UpdateChecker::instance ();
259 bool const announce =
260 _update_news_requested ||
261 (uc->stable() && Config::instance()->check_for_updates()) ||
262 (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
264 _update_news_requested = false;
270 if (uc->state() == UpdateChecker::YES) {
271 UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
272 dialog->ShowModal ();
274 } else if (uc->state() == UpdateChecker::FAILED) {
275 error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
277 error_dialog (this, _("There are no new versions of DCP-o-matic available."));
280 _update_news_requested = false;
283 bool _update_news_requested;
284 PlayerInformation* _info;
285 wxPreferencesEditor* _config_dialog;
287 boost::shared_ptr<Film> _film;
290 static const wxCmdLineEntryDesc command_line_description[] = {
291 { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
292 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
296 * @brief The magic App class for wxWidgets.
298 class App : public wxApp
311 wxInitAllImageHandlers ();
313 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
315 wxSplashScreen* splash = 0;
317 if (!Config::have_existing ("config.xml")) {
319 boost::filesystem::path p = shared_path () / "splash.png";
320 if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
321 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
325 } catch (boost::filesystem::filesystem_error& e) {
326 /* Maybe we couldn't find the splash image; never mind */
329 SetAppName (_("DCP-o-matic Player"));
331 if (!wxApp::OnInit()) {
335 #ifdef DCPOMATIC_LINUX
336 unsetenv ("UBUNTU_MENUPROXY");
340 ProcessSerialNumber serial;
341 GetCurrentProcess (&serial);
342 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
345 dcpomatic_setup_path_encoding ();
347 /* Enable i18n; this will create a Config object
348 to look for a force-configured language. This Config
349 object will be wrong, however, because dcpomatic_setup
350 hasn't yet been called and there aren't any filters etc.
353 dcpomatic_setup_i18n ();
355 /* Set things up, including filters etc.
356 which will now be internationalised correctly.
360 /* Force the configuration to be re-loaded correctly next
365 _frame = new DOMFrame ();
366 SetTopWindow (_frame);
373 signal_manager = new wxSignalManager (this);
375 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
377 _frame->load_dcp (_dcp_to_load);
378 } catch (exception& e) {
379 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1 (%2)")), _dcp_to_load, e.what())));
383 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
385 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
386 _timer.reset (new wxTimer (this));
387 _timer->Start (1000);
389 if (Config::instance()->check_for_updates ()) {
390 UpdateChecker::instance()->run ();
397 error_dialog (0, wxString::Format ("DCP-o-matic Player could not start: %s", e.what ()));
401 void OnInitCmdLine (wxCmdLineParser& parser)
403 parser.SetDesc (command_line_description);
404 parser.SetSwitchChars (wxT ("-"));
407 bool OnCmdLineParsed (wxCmdLineParser& parser)
409 if (parser.GetParamCount() > 0) {
410 _dcp_to_load = wx_to_std (parser.GetParam (0));
416 void report_exception ()
420 } catch (FileError& e) {
424 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
425 std_to_wx (e.what()),
426 std_to_wx (e.file().string().c_str ())
429 } catch (exception& e) {
433 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
434 std_to_wx (e.what ())
438 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
442 /* An unhandled exception has occurred inside the main event loop */
443 bool OnExceptionInMainLoop ()
446 /* This will terminate the program */
450 void OnUnhandledException ()
457 signal_manager->ui_idle ();
463 EncodeServerFinder::instance()->rethrow ();
464 } catch (exception& e) {
465 error_dialog (0, std_to_wx (e.what ()));
469 void config_failed_to_load ()
471 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
475 shared_ptr<wxTimer> _timer;