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 "wx/wx_signal_manager.h"
31 #include "wx/wx_util.h"
32 #include "wx/about_dialog.h"
33 #include "wx/report_problem_dialog.h"
34 #include "wx/film_viewer.h"
35 #include "wx/player_information.h"
36 #include "wx/update_dialog.h"
38 #include <wx/stdpaths.h>
39 #include <wx/splash.h>
40 #include <wx/cmdline.h>
41 #include <boost/bind.hpp>
47 using boost::shared_ptr;
48 using boost::optional;
52 ID_help_report_a_problem,
53 ID_tools_check_for_updates,
56 class DOMFrame : public wxFrame
60 : wxFrame (0, -1, _("DCP-o-matic Player"))
61 , _update_news_requested (false)
64 #if defined(DCPOMATIC_WINDOWS)
65 maybe_open_console ();
66 cout << "DCP-o-matic Player is starting." << "\n";
69 wxMenuBar* bar = new wxMenuBar;
73 #ifdef DCPOMATIC_WINDOWS
74 SetIcon (wxIcon (std_to_wx ("id")));
77 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
78 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
79 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
80 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
81 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
83 /* Use a panel as the only child of the Frame so that we avoid
84 the dark-grey background on Windows.
86 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
88 _viewer = new FilmViewer (overall_panel, false, false);
89 _info = new PlayerInformation (overall_panel, _viewer);
90 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
91 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
92 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
93 overall_panel->SetSizer (main_sizer);
95 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
98 void load_dcp (boost::filesystem::path dir)
100 _film.reset (new Film (optional<boost::filesystem::path>()));
101 shared_ptr<DCPContent> dcp (new DCPContent (_film, dir));
102 _film->examine_and_add_content (dcp);
104 JobManager* jm = JobManager::instance ();
105 while (jm->work_to_do ()) {
106 /* XXX: progress dialog */
107 while (signal_manager->ui_idle ()) {}
111 while (signal_manager->ui_idle ()) {}
115 BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
116 if (i->finished_in_error()) {
117 errors += std_to_wx (i->error_summary()) + "\n";
120 error_dialog (this, errors);
124 _viewer->set_film (_film);
130 void setup_menu (wxMenuBar* m)
132 wxMenu* file = new wxMenu;
133 file->Append (ID_file_open, _("&Open...\tCtrl-O"));
136 file->Append (wxID_EXIT, _("&Exit"));
138 file->Append (wxID_EXIT, _("&Quit"));
142 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
144 wxMenu* edit = new wxMenu;
145 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
148 wxMenu* tools = new wxMenu;
149 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
151 wxMenu* help = new wxMenu;
153 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
155 help->Append (wxID_ABOUT, _("About"));
157 help->Append (ID_help_report_a_problem, _("Report a problem..."));
159 m->Append (file, _("&File"));
160 m->Append (tools, _("&Tools"));
161 m->Append (help, _("&Help"));
166 wxDirDialog* c = new wxDirDialog (
168 _("Select DCP to open"),
169 wxStandardPaths::Get().GetDocumentsDir(),
170 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
176 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
177 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
184 load_dcp (wx_to_std (c->GetPath ()));
195 void tools_check_for_updates ()
197 UpdateChecker::instance()->run ();
198 _update_news_requested = true;
203 AboutDialog* d = new AboutDialog (this);
208 void help_report_a_problem ()
210 ReportProblemDialog* d = new ReportProblemDialog (this);
211 if (d->ShowModal () == wxID_OK) {
217 void update_checker_state_changed ()
219 UpdateChecker* uc = UpdateChecker::instance ();
221 bool const announce =
222 _update_news_requested ||
223 (uc->stable() && Config::instance()->check_for_updates()) ||
224 (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
226 _update_news_requested = false;
232 if (uc->state() == UpdateChecker::YES) {
233 UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
234 dialog->ShowModal ();
236 } else if (uc->state() == UpdateChecker::FAILED) {
237 error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
239 error_dialog (this, _("There are no new versions of DCP-o-matic available."));
242 _update_news_requested = false;
245 bool _update_news_requested;
246 PlayerInformation* _info;
248 boost::shared_ptr<Film> _film;
251 static const wxCmdLineEntryDesc command_line_description[] = {
252 { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
253 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
257 * @brief The magic App class for wxWidgets.
259 class App : public wxApp
272 wxInitAllImageHandlers ();
274 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
276 wxSplashScreen* splash = 0;
278 if (!Config::have_existing ("config.xml")) {
280 boost::filesystem::path p = shared_path () / "splash.png";
281 if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
282 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
286 } catch (boost::filesystem::filesystem_error& e) {
287 /* Maybe we couldn't find the splash image; never mind */
290 SetAppName (_("DCP-o-matic Player"));
292 if (!wxApp::OnInit()) {
296 #ifdef DCPOMATIC_LINUX
297 unsetenv ("UBUNTU_MENUPROXY");
301 ProcessSerialNumber serial;
302 GetCurrentProcess (&serial);
303 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
306 dcpomatic_setup_path_encoding ();
308 /* Enable i18n; this will create a Config object
309 to look for a force-configured language. This Config
310 object will be wrong, however, because dcpomatic_setup
311 hasn't yet been called and there aren't any filters etc.
314 dcpomatic_setup_i18n ();
316 /* Set things up, including filters etc.
317 which will now be internationalised correctly.
321 /* Force the configuration to be re-loaded correctly next
326 _frame = new DOMFrame ();
327 SetTopWindow (_frame);
334 signal_manager = new wxSignalManager (this);
336 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
338 _frame->load_dcp (_dcp_to_load);
339 } catch (exception& e) {
340 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1 (%2)")), _dcp_to_load, e.what())));
344 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
346 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
347 _timer.reset (new wxTimer (this));
348 _timer->Start (1000);
350 if (Config::instance()->check_for_updates ()) {
351 UpdateChecker::instance()->run ();
358 error_dialog (0, wxString::Format ("DCP-o-matic Player could not start: %s", e.what ()));
362 void OnInitCmdLine (wxCmdLineParser& parser)
364 parser.SetDesc (command_line_description);
365 parser.SetSwitchChars (wxT ("-"));
368 bool OnCmdLineParsed (wxCmdLineParser& parser)
370 if (parser.GetParamCount() > 0) {
371 _dcp_to_load = wx_to_std (parser.GetParam (0));
377 void report_exception ()
381 } catch (FileError& e) {
385 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
386 std_to_wx (e.what()),
387 std_to_wx (e.file().string().c_str ())
390 } catch (exception& e) {
394 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
395 std_to_wx (e.what ())
399 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
403 /* An unhandled exception has occurred inside the main event loop */
404 bool OnExceptionInMainLoop ()
407 /* This will terminate the program */
411 void OnUnhandledException ()
418 signal_manager->ui_idle ();
424 EncodeServerFinder::instance()->rethrow ();
425 } catch (exception& e) {
426 error_dialog (0, std_to_wx (e.what ()));
430 void config_failed_to_load ()
432 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
436 shared_ptr<wxTimer> _timer;