2 Copyright (C) 2013-2018 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 "wx/wx_util.h"
22 #include "wx/about_dialog.h"
23 #include "wx/wx_signal_manager.h"
24 #include "wx/job_manager_view.h"
25 #include "wx/full_config_dialog.h"
26 #include "wx/servers_list_dialog.h"
27 #include "lib/version.h"
28 #include "lib/compose.hpp"
29 #include "lib/config.h"
32 #include "lib/job_manager.h"
34 #include "lib/dcpomatic_socket.h"
35 #include <wx/aboutdlg.h>
36 #include <wx/stdpaths.h>
37 #include <wx/cmdline.h>
38 #include <wx/splash.h>
39 #include <wx/preferences.h>
41 #include <boost/foreach.hpp>
48 using boost::shared_ptr;
50 using boost::scoped_array;
52 static list<boost::filesystem::path> films_to_load;
56 ID_tools_encoding_servers,
61 setup_menu (wxMenuBar* m)
63 wxMenu* file = new wxMenu;
64 file->Append (ID_file_add_film, _("&Add Film...\tCtrl-A"));
66 file->Append (wxID_EXIT, _("&Exit"));
68 file->Append (wxID_EXIT, _("&Quit"));
72 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
74 wxMenu* edit = new wxMenu;
75 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
78 wxMenu* tools = new wxMenu;
79 tools->Append (ID_tools_encoding_servers, _("Encoding servers..."));
81 wxMenu* help = new wxMenu;
82 help->Append (ID_help_about, _("About"));
84 m->Append (file, _("&File"));
86 m->Append (edit, _("&Edit"));
88 m->Append (tools, _("&Tools"));
89 m->Append (help, _("&Help"));
92 class DOMFrame : public wxFrame
95 explicit DOMFrame (wxString const & title)
96 : wxFrame (NULL, -1, title)
97 , _sizer (new wxBoxSizer (wxVERTICAL))
99 , _servers_list_dialog (0)
101 wxMenuBar* bar = new wxMenuBar;
105 Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this, _1));
107 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_film, this), ID_file_add_film);
108 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_quit, this), wxID_EXIT);
109 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
110 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_encoding_servers, this), ID_tools_encoding_servers);
111 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), ID_help_about);
113 wxPanel* panel = new wxPanel (this);
114 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
115 s->Add (panel, 1, wxEXPAND);
118 JobManagerView* job_manager_view = new JobManagerView (panel, true);
119 _sizer->Add (job_manager_view, 1, wxALL | wxEXPAND, 6);
121 wxSizer* buttons = new wxBoxSizer (wxHORIZONTAL);
122 wxButton* add = new wxButton (panel, wxID_ANY, _("Add Film..."));
123 add->Bind (wxEVT_BUTTON, boost::bind (&DOMFrame::add_film, this));
124 buttons->Add (add, 1, wxALL, 6);
125 _pause = new wxButton (panel, wxID_ANY, _("Pause"));
126 _pause->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::pause, this));
127 buttons->Add (_pause, 1, wxALL, 6);
128 _resume = new wxButton (panel, wxID_ANY, _("Resume"));
129 _resume->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::resume, this));
130 buttons->Add (_resume, 1, wxALL, 6);
132 setup_sensitivity ();
134 _sizer->Add (buttons, 0, wxALL, 6);
136 panel->SetSizer (_sizer);
138 Bind (wxEVT_CLOSE_WINDOW, boost::bind (&DOMFrame::close, this, _1));
139 Bind (wxEVT_SIZE, boost::bind (&DOMFrame::sized, this, _1));
142 void setup_sensitivity ()
144 _pause->Enable (!JobManager::instance()->paused());
145 _resume->Enable (JobManager::instance()->paused());
150 JobManager::instance()->pause ();
151 setup_sensitivity ();
156 JobManager::instance()->resume ();
157 setup_sensitivity ();
160 void start_job (boost::filesystem::path path)
163 shared_ptr<Film> film (new Film (path));
164 film->read_metadata ();
166 } catch (std::exception& e) {
167 wxString p = std_to_wx (path.string ());
168 wxCharBuffer b = p.ToUTF8 ();
169 error_dialog (this, wxString::Format (_("Could not open film at %s"), p.data()), std_to_wx(e.what()));
174 void sized (wxSizeEvent& ev)
182 if (!JobManager::instance()->work_to_do ()) {
186 wxMessageDialog* d = new wxMessageDialog (
188 _("There are unfinished jobs; are you sure you want to quit?"),
189 _("Unfinished jobs"),
190 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
193 bool const r = d->ShowModal() == wxID_YES;
198 void close (wxCloseEvent& ev)
200 if (!should_close ()) {
208 void file_add_film ()
215 if (should_close ()) {
220 void edit_preferences ()
222 if (!_config_dialog) {
223 _config_dialog = create_full_config_dialog ();
225 _config_dialog->Show (this);
228 void tools_encoding_servers ()
230 if (!_servers_list_dialog) {
231 _servers_list_dialog = new ServersListDialog (this);
234 _servers_list_dialog->Show ();
239 AboutDialog* d = new AboutDialog (this);
246 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
248 c->SetPath (std_to_wx (_last_parent.get().string ()));
254 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
255 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
262 start_job (wx_to_std (c->GetPath ()));
265 _last_parent = boost::filesystem::path (wx_to_std (c->GetPath ())).parent_path ();
270 void config_changed (Config::Property what)
272 /* Instantly save any config changes when using the DCP-o-matic GUI */
273 if (what == Config::CINEMAS) {
275 Config::instance()->write_cinemas();
276 } catch (exception& e) {
280 _("Could not write to cinemas file at %s. Your changes have not been saved."),
281 std_to_wx (Config::instance()->cinemas_file().string()).data()
287 Config::instance()->write_config();
288 } catch (exception& e) {
292 _("Could not write to config file at %s. Your changes have not been saved."),
293 std_to_wx (Config::instance()->cinemas_file().string()).data()
300 boost::optional<boost::filesystem::path> _last_parent;
302 wxPreferencesEditor* _config_dialog;
303 ServersListDialog* _servers_list_dialog;
308 static const wxCmdLineEntryDesc command_line_description[] = {
309 { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
310 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
313 class JobServer : public Server
316 explicit JobServer (DOMFrame* frame)
317 : Server (BATCH_JOB_PORT)
321 void handle (shared_ptr<Socket> socket)
324 int const length = socket->read_uint32 ();
325 scoped_array<char> buffer (new char[length]);
326 socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
327 string s (buffer.get());
328 _frame->start_job (s);
329 socket->write (reinterpret_cast<uint8_t const *> ("OK"), 3);
339 class App : public wxApp
343 SetAppName (_("DCP-o-matic Batch Converter"));
344 is_batch_converter = true;
346 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
347 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
349 wxSplashScreen* splash = maybe_show_splash ();
351 if (!wxApp::OnInit()) {
355 #ifdef DCPOMATIC_LINUX
356 unsetenv ("UBUNTU_MENUPROXY");
359 dcpomatic_setup_path_encoding ();
361 /* Enable i18n; this will create a Config object
362 to look for a force-configured language. This Config
363 object will be wrong, however, because dcpomatic_setup
364 hasn't yet been called and there aren't any filters etc.
367 dcpomatic_setup_i18n ();
369 /* Set things up, including filters etc.
370 which will now be internationalised correctly.
374 /* Force the configuration to be re-loaded correctly next
379 _frame = new DOMFrame (_("DCP-o-matic Batch Converter"));
380 SetTopWindow (_frame);
387 JobServer* server = new JobServer (_frame);
388 new thread (boost::bind (&JobServer::run, server));
390 signal_manager = new wxSignalManager (this);
391 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
393 shared_ptr<Film> film;
394 BOOST_FOREACH (boost::filesystem::path i, films_to_load) {
395 if (boost::filesystem::is_directory (i)) {
397 film.reset (new Film (i));
398 film->read_metadata ();
400 } catch (exception& e) {
403 std_to_wx (String::compose (wx_to_std (_("Could not load film %1")), i.string())),
415 signal_manager->ui_idle ();
418 void OnInitCmdLine (wxCmdLineParser& parser)
420 parser.SetDesc (command_line_description);
421 parser.SetSwitchChars (wxT ("-"));
424 bool OnCmdLineParsed (wxCmdLineParser& parser)
426 for (size_t i = 0; i < parser.GetParamCount(); ++i) {
427 films_to_load.push_back (wx_to_std (parser.GetParam(i)));
433 void config_failed_to_load ()
435 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
438 void config_warning (string m)
440 message_dialog (_frame, std_to_wx (m));