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 "wx/dcpomatic_button.h"
28 #include "lib/version.h"
29 #include "lib/compose.hpp"
30 #include "lib/config.h"
33 #include "lib/job_manager.h"
35 #include "lib/dcpomatic_socket.h"
36 #include <wx/aboutdlg.h>
37 #include <wx/stdpaths.h>
38 #include <wx/cmdline.h>
39 #include <wx/splash.h>
40 #include <wx/preferences.h>
42 #include <boost/foreach.hpp>
49 using boost::shared_ptr;
51 using boost::scoped_array;
53 static list<boost::filesystem::path> films_to_load;
57 ID_tools_encoding_servers,
62 setup_menu (wxMenuBar* m)
64 wxMenu* file = new wxMenu;
65 file->Append (ID_file_add_film, _("&Add Film...\tCtrl-A"));
67 file->Append (wxID_EXIT, _("&Exit"));
69 file->Append (wxID_EXIT, _("&Quit"));
73 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
75 wxMenu* edit = new wxMenu;
76 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
79 wxMenu* tools = new wxMenu;
80 tools->Append (ID_tools_encoding_servers, _("Encoding servers..."));
82 wxMenu* help = new wxMenu;
83 help->Append (ID_help_about, _("About"));
85 m->Append (file, _("&File"));
87 m->Append (edit, _("&Edit"));
89 m->Append (tools, _("&Tools"));
90 m->Append (help, _("&Help"));
93 class DOMFrame : public wxFrame
96 explicit DOMFrame (wxString const & title)
97 : wxFrame (NULL, -1, title)
98 , _sizer (new wxBoxSizer (wxVERTICAL))
100 , _servers_list_dialog (0)
102 wxMenuBar* bar = new wxMenuBar;
106 Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this, _1));
108 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_film, this), ID_file_add_film);
109 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_quit, this), wxID_EXIT);
110 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
111 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_encoding_servers, this), ID_tools_encoding_servers);
112 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), ID_help_about);
114 wxPanel* panel = new wxPanel (this);
115 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
116 s->Add (panel, 1, wxEXPAND);
119 JobManagerView* job_manager_view = new JobManagerView (panel, true);
120 _sizer->Add (job_manager_view, 1, wxALL | wxEXPAND, 6);
122 wxSizer* buttons = new wxBoxSizer (wxHORIZONTAL);
123 wxButton* add = new Button (panel, _("Add Film..."));
124 add->Bind (wxEVT_BUTTON, boost::bind (&DOMFrame::add_film, this));
125 buttons->Add (add, 1, wxALL, 6);
126 _pause = new Button (panel, _("Pause"));
127 _pause->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::pause, this));
128 buttons->Add (_pause, 1, wxALL, 6);
129 _resume = new Button (panel, _("Resume"));
130 _resume->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::resume, this));
131 buttons->Add (_resume, 1, wxALL, 6);
133 setup_sensitivity ();
135 _sizer->Add (buttons, 0, wxALL, 6);
137 panel->SetSizer (_sizer);
139 Bind (wxEVT_CLOSE_WINDOW, boost::bind (&DOMFrame::close, this, _1));
140 Bind (wxEVT_SIZE, boost::bind (&DOMFrame::sized, this, _1));
143 void setup_sensitivity ()
145 _pause->Enable (!JobManager::instance()->paused());
146 _resume->Enable (JobManager::instance()->paused());
151 JobManager::instance()->pause ();
152 setup_sensitivity ();
157 JobManager::instance()->resume ();
158 setup_sensitivity ();
161 void start_job (boost::filesystem::path path)
164 shared_ptr<Film> film (new Film (path));
165 film->read_metadata ();
167 } catch (std::exception& e) {
168 wxString p = std_to_wx (path.string ());
169 wxCharBuffer b = p.ToUTF8 ();
170 error_dialog (this, wxString::Format (_("Could not open film at %s"), p.data()), std_to_wx(e.what()));
175 void sized (wxSizeEvent& ev)
183 if (!JobManager::instance()->work_to_do ()) {
187 wxMessageDialog* d = new wxMessageDialog (
189 _("There are unfinished jobs; are you sure you want to quit?"),
190 _("Unfinished jobs"),
191 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
194 bool const r = d->ShowModal() == wxID_YES;
199 void close (wxCloseEvent& ev)
201 if (!should_close ()) {
209 void file_add_film ()
216 if (should_close ()) {
221 void edit_preferences ()
223 if (!_config_dialog) {
224 _config_dialog = create_full_config_dialog ();
226 _config_dialog->Show (this);
229 void tools_encoding_servers ()
231 if (!_servers_list_dialog) {
232 _servers_list_dialog = new ServersListDialog (this);
235 _servers_list_dialog->Show ();
240 AboutDialog* d = new AboutDialog (this);
247 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
249 c->SetPath (std_to_wx (_last_parent.get().string ()));
255 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
256 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
263 start_job (wx_to_std (c->GetPath ()));
266 _last_parent = boost::filesystem::path (wx_to_std (c->GetPath ())).parent_path ();
271 void config_changed (Config::Property what)
273 /* Instantly save any config changes when using the DCP-o-matic GUI */
274 if (what == Config::CINEMAS) {
276 Config::instance()->write_cinemas();
277 } catch (exception& e) {
281 _("Could not write to cinemas file at %s. Your changes have not been saved."),
282 std_to_wx (Config::instance()->cinemas_file().string()).data()
288 Config::instance()->write_config();
289 } catch (exception& e) {
293 _("Could not write to config file at %s. Your changes have not been saved."),
294 std_to_wx (Config::instance()->cinemas_file().string()).data()
301 boost::optional<boost::filesystem::path> _last_parent;
303 wxPreferencesEditor* _config_dialog;
304 ServersListDialog* _servers_list_dialog;
309 static const wxCmdLineEntryDesc command_line_description[] = {
310 { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
311 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
314 class JobServer : public Server
317 explicit JobServer (DOMFrame* frame)
318 : Server (BATCH_JOB_PORT)
322 void handle (shared_ptr<Socket> socket)
325 int const length = socket->read_uint32 ();
326 scoped_array<char> buffer (new char[length]);
327 socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
328 string s (buffer.get());
329 _frame->start_job (s);
330 socket->write (reinterpret_cast<uint8_t const *> ("OK"), 3);
340 class App : public wxApp
344 SetAppName (_("DCP-o-matic Batch Converter"));
345 is_batch_converter = true;
347 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
348 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
350 wxSplashScreen* splash = maybe_show_splash ();
352 if (!wxApp::OnInit()) {
356 #ifdef DCPOMATIC_LINUX
357 unsetenv ("UBUNTU_MENUPROXY");
360 dcpomatic_setup_path_encoding ();
362 /* Enable i18n; this will create a Config object
363 to look for a force-configured language. This Config
364 object will be wrong, however, because dcpomatic_setup
365 hasn't yet been called and there aren't any filters etc.
368 dcpomatic_setup_i18n ();
370 /* Set things up, including filters etc.
371 which will now be internationalised correctly.
375 /* Force the configuration to be re-loaded correctly next
380 _frame = new DOMFrame (_("DCP-o-matic Batch Converter"));
381 SetTopWindow (_frame);
388 JobServer* server = new JobServer (_frame);
389 new thread (boost::bind (&JobServer::run, server));
391 signal_manager = new wxSignalManager (this);
392 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
394 shared_ptr<Film> film;
395 BOOST_FOREACH (boost::filesystem::path i, films_to_load) {
396 if (boost::filesystem::is_directory (i)) {
398 film.reset (new Film (i));
399 film->read_metadata ();
401 } catch (exception& e) {
404 std_to_wx (String::compose (wx_to_std (_("Could not load film %1")), i.string())),
416 signal_manager->ui_idle ();
419 void OnInitCmdLine (wxCmdLineParser& parser)
421 parser.SetDesc (command_line_description);
422 parser.SetSwitchChars (wxT ("-"));
425 bool OnCmdLineParsed (wxCmdLineParser& parser)
427 for (size_t i = 0; i < parser.GetParamCount(); ++i) {
428 films_to_load.push_back (wx_to_std (parser.GetParam(i)));
434 void config_failed_to_load ()
436 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
439 void config_warning (string m)
441 message_dialog (_frame, std_to_wx (m));