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_help_report_a_problem,
54 ID_tools_check_for_updates,
57 class DOMFrame : public wxFrame
61 : wxFrame (0, -1, _("DCP-o-matic Player"))
62 , _update_news_requested (false)
65 #if defined(DCPOMATIC_WINDOWS)
66 maybe_open_console ();
67 cout << "DCP-o-matic Player is starting." << "\n";
70 wxMenuBar* bar = new wxMenuBar;
74 #ifdef DCPOMATIC_WINDOWS
75 SetIcon (wxIcon (std_to_wx ("id")));
78 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
79 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
80 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
81 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
82 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
84 /* Use a panel as the only child of the Frame so that we avoid
85 the dark-grey background on Windows.
87 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
89 _viewer = new FilmViewer (overall_panel, false, false);
90 _info = new PlayerInformation (overall_panel, _viewer);
91 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
92 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
93 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
94 overall_panel->SetSizer (main_sizer);
96 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
99 void load_dcp (boost::filesystem::path dir)
101 _film.reset (new Film (optional<boost::filesystem::path>()));
102 shared_ptr<DCPContent> dcp (new DCPContent (_film, dir));
103 _film->examine_and_add_content (dcp);
105 JobManager* jm = JobManager::instance ();
106 while (jm->work_to_do ()) {
107 /* XXX: progress dialog */
108 while (signal_manager->ui_idle ()) {}
112 while (signal_manager->ui_idle ()) {}
116 BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
117 if (i->finished_in_error()) {
118 errors += std_to_wx (i->error_summary()) + "\n";
121 error_dialog (this, errors);
125 _viewer->set_film (_film);
131 void setup_menu (wxMenuBar* m)
133 wxMenu* file = new wxMenu;
134 file->Append (ID_file_open, _("&Open...\tCtrl-O"));
137 file->Append (wxID_EXIT, _("&Exit"));
139 file->Append (wxID_EXIT, _("&Quit"));
143 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
145 wxMenu* edit = new wxMenu;
146 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
149 wxMenu* tools = new wxMenu;
150 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
152 wxMenu* help = new wxMenu;
154 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
156 help->Append (wxID_ABOUT, _("About"));
158 help->Append (ID_help_report_a_problem, _("Report a problem..."));
160 m->Append (file, _("&File"));
161 m->Append (tools, _("&Tools"));
162 m->Append (help, _("&Help"));
167 wxDirDialog* c = new wxDirDialog (
169 _("Select DCP to open"),
170 wxStandardPaths::Get().GetDocumentsDir(),
171 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
177 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
178 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
185 load_dcp (wx_to_std (c->GetPath ()));
196 void tools_check_for_updates ()
198 UpdateChecker::instance()->run ();
199 _update_news_requested = true;
204 AboutDialog* d = new AboutDialog (this);
209 void help_report_a_problem ()
211 ReportProblemDialog* d = new ReportProblemDialog (this);
212 if (d->ShowModal () == wxID_OK) {
218 void update_checker_state_changed ()
220 UpdateChecker* uc = UpdateChecker::instance ();
222 bool const announce =
223 _update_news_requested ||
224 (uc->stable() && Config::instance()->check_for_updates()) ||
225 (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
227 _update_news_requested = false;
233 if (uc->state() == UpdateChecker::YES) {
234 UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
235 dialog->ShowModal ();
237 } else if (uc->state() == UpdateChecker::FAILED) {
238 error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
240 error_dialog (this, _("There are no new versions of DCP-o-matic available."));
243 _update_news_requested = false;
246 bool _update_news_requested;
247 PlayerInformation* _info;
249 boost::shared_ptr<Film> _film;
252 static const wxCmdLineEntryDesc command_line_description[] = {
253 { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
254 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
258 * @brief The magic App class for wxWidgets.
260 class App : public wxApp
273 wxInitAllImageHandlers ();
275 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
277 wxSplashScreen* splash = 0;
279 if (!Config::have_existing ("config.xml")) {
281 boost::filesystem::path p = shared_path () / "splash.png";
282 if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
283 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
287 } catch (boost::filesystem::filesystem_error& e) {
288 /* Maybe we couldn't find the splash image; never mind */
291 SetAppName (_("DCP-o-matic Player"));
293 if (!wxApp::OnInit()) {
297 #ifdef DCPOMATIC_LINUX
298 unsetenv ("UBUNTU_MENUPROXY");
302 ProcessSerialNumber serial;
303 GetCurrentProcess (&serial);
304 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
307 dcpomatic_setup_path_encoding ();
309 /* Enable i18n; this will create a Config object
310 to look for a force-configured language. This Config
311 object will be wrong, however, because dcpomatic_setup
312 hasn't yet been called and there aren't any filters etc.
315 dcpomatic_setup_i18n ();
317 /* Set things up, including filters etc.
318 which will now be internationalised correctly.
322 /* Force the configuration to be re-loaded correctly next
327 _frame = new DOMFrame ();
328 SetTopWindow (_frame);
335 signal_manager = new wxSignalManager (this);
337 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
339 _frame->load_dcp (_dcp_to_load);
340 } catch (exception& e) {
341 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1 (%2)")), _dcp_to_load, e.what())));
345 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
347 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
348 _timer.reset (new wxTimer (this));
349 _timer->Start (1000);
351 if (Config::instance()->check_for_updates ()) {
352 UpdateChecker::instance()->run ();
359 error_dialog (0, wxString::Format ("DCP-o-matic Player could not start: %s", e.what ()));
363 void OnInitCmdLine (wxCmdLineParser& parser)
365 parser.SetDesc (command_line_description);
366 parser.SetSwitchChars (wxT ("-"));
369 bool OnCmdLineParsed (wxCmdLineParser& parser)
371 if (parser.GetParamCount() > 0) {
372 _dcp_to_load = wx_to_std (parser.GetParam (0));
378 void report_exception ()
382 } catch (FileError& e) {
386 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
387 std_to_wx (e.what()),
388 std_to_wx (e.file().string().c_str ())
391 } catch (exception& e) {
395 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
396 std_to_wx (e.what ())
400 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
404 /* An unhandled exception has occurred inside the main event loop */
405 bool OnExceptionInMainLoop ()
408 /* This will terminate the program */
412 void OnUnhandledException ()
419 signal_manager->ui_idle ();
425 EncodeServerFinder::instance()->rethrow ();
426 } catch (exception& e) {
427 error_dialog (0, std_to_wx (e.what ()));
431 void config_failed_to_load ()
433 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
437 shared_ptr<wxTimer> _timer;