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"
29 #include "wx/wx_signal_manager.h"
30 #include "wx/wx_util.h"
31 #include "wx/about_dialog.h"
32 #include "wx/report_problem_dialog.h"
33 #include "wx/film_viewer.h"
34 #include "wx/update_dialog.h"
36 #include <wx/stdpaths.h>
37 #include <wx/splash.h>
38 #include <wx/cmdline.h>
39 #include <boost/bind.hpp>
43 using boost::shared_ptr;
44 using boost::optional;
48 ID_help_report_a_problem,
49 ID_tools_check_for_updates,
52 class DOMFrame : public wxFrame
56 : wxFrame (0, -1, _("DCP-o-matic Player"))
57 , _update_news_requested (false)
60 #if defined(DCPOMATIC_WINDOWS)
61 maybe_open_console ();
62 cout << "DCP-o-matic Player is starting." << "\n";
65 wxMenuBar* bar = new wxMenuBar;
69 #ifdef DCPOMATIC_WINDOWS
70 SetIcon (wxIcon (std_to_wx ("id")));
73 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
74 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
75 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
76 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
77 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
79 /* Use a panel as the only child of the Frame so that we avoid
80 the dark-grey background on Windows.
82 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
84 _viewer = new FilmViewer (overall_panel, false, false);
85 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
86 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
87 overall_panel->SetSizer (main_sizer);
89 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
92 void load_dcp (boost::filesystem::path dir)
94 _film.reset (new Film (optional<boost::filesystem::path>()));
95 shared_ptr<DCPContent> dcp (new DCPContent (_film, dir));
96 _film->examine_and_add_content (dcp);
98 JobManager* jm = JobManager::instance ();
99 while (jm->work_to_do ()) {
100 /* XXX: progress dialog */
101 while (signal_manager->ui_idle ()) {}
105 /* XXX: report errors */
107 _viewer->set_film (_film);
112 void setup_menu (wxMenuBar* m)
114 wxMenu* file = new wxMenu;
115 file->Append (ID_file_open, _("&Open...\tCtrl-O"));
118 file->AppendSeparator ();
122 file->Append (wxID_EXIT, _("&Exit"));
124 file->Append (wxID_EXIT, _("&Quit"));
128 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
130 wxMenu* edit = new wxMenu;
131 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
134 wxMenu* tools = new wxMenu;
135 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
137 wxMenu* help = new wxMenu;
139 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
141 help->Append (wxID_ABOUT, _("About"));
143 help->Append (ID_help_report_a_problem, _("Report a problem..."));
145 m->Append (file, _("&File"));
146 m->Append (tools, _("&Tools"));
147 m->Append (help, _("&Help"));
152 wxDirDialog* c = new wxDirDialog (
154 _("Select DCP to open"),
155 wxStandardPaths::Get().GetDocumentsDir(),
156 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
162 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
163 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
170 load_dcp (wx_to_std (c->GetPath ()));
181 void tools_check_for_updates ()
183 UpdateChecker::instance()->run ();
184 _update_news_requested = true;
189 AboutDialog* d = new AboutDialog (this);
194 void help_report_a_problem ()
196 ReportProblemDialog* d = new ReportProblemDialog (this);
197 if (d->ShowModal () == wxID_OK) {
203 void update_checker_state_changed ()
205 UpdateChecker* uc = UpdateChecker::instance ();
207 bool const announce =
208 _update_news_requested ||
209 (uc->stable() && Config::instance()->check_for_updates()) ||
210 (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
212 _update_news_requested = false;
218 if (uc->state() == UpdateChecker::YES) {
219 UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
220 dialog->ShowModal ();
222 } else if (uc->state() == UpdateChecker::FAILED) {
223 error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
225 error_dialog (this, _("There are no new versions of DCP-o-matic available."));
228 _update_news_requested = false;
231 bool _update_news_requested;
233 boost::shared_ptr<Film> _film;
236 static const wxCmdLineEntryDesc command_line_description[] = {
237 { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
238 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
242 * @brief The magic App class for wxWidgets.
244 class App : public wxApp
257 wxInitAllImageHandlers ();
259 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
261 wxSplashScreen* splash = 0;
263 if (!Config::have_existing ("config.xml")) {
265 boost::filesystem::path p = shared_path () / "splash.png";
266 if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
267 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
271 } catch (boost::filesystem::filesystem_error& e) {
272 /* Maybe we couldn't find the splash image; never mind */
275 SetAppName (_("DCP-o-matic Player"));
277 if (!wxApp::OnInit()) {
281 #ifdef DCPOMATIC_LINUX
282 unsetenv ("UBUNTU_MENUPROXY");
286 ProcessSerialNumber serial;
287 GetCurrentProcess (&serial);
288 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
291 dcpomatic_setup_path_encoding ();
293 /* Enable i18n; this will create a Config object
294 to look for a force-configured language. This Config
295 object will be wrong, however, because dcpomatic_setup
296 hasn't yet been called and there aren't any filters etc.
299 dcpomatic_setup_i18n ();
301 /* Set things up, including filters etc.
302 which will now be internationalised correctly.
306 /* Force the configuration to be re-loaded correctly next
311 _frame = new DOMFrame ();
312 SetTopWindow (_frame);
319 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
321 _frame->load_dcp (_dcp_to_load);
322 } catch (exception& e) {
323 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1 (%2)")), _dcp_to_load, e.what())));
327 signal_manager = new wxSignalManager (this);
328 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
330 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
331 _timer.reset (new wxTimer (this));
332 _timer->Start (1000);
334 if (Config::instance()->check_for_updates ()) {
335 UpdateChecker::instance()->run ();
342 error_dialog (0, wxString::Format ("DCP-o-matic Player could not start: %s", e.what ()));
346 void OnInitCmdLine (wxCmdLineParser& parser)
348 parser.SetDesc (command_line_description);
349 parser.SetSwitchChars (wxT ("-"));
352 bool OnCmdLineParsed (wxCmdLineParser& parser)
354 if (parser.GetParamCount() > 0) {
355 _dcp_to_load = wx_to_std (parser.GetParam (0));
361 void report_exception ()
365 } catch (FileError& e) {
369 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
370 std_to_wx (e.what()),
371 std_to_wx (e.file().string().c_str ())
374 } catch (exception& e) {
378 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
379 std_to_wx (e.what ())
383 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
387 /* An unhandled exception has occurred inside the main event loop */
388 bool OnExceptionInMainLoop ()
391 /* This will terminate the program */
395 void OnUnhandledException ()
402 signal_manager->ui_idle ();
408 EncodeServerFinder::instance()->rethrow ();
409 } catch (exception& e) {
410 error_dialog (0, std_to_wx (e.what ()));
414 void config_failed_to_load ()
416 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
420 shared_ptr<wxTimer> _timer;