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"
33 #include "lib/dcpomatic_socket.h"
34 #include <wx/aboutdlg.h>
35 #include <wx/stdpaths.h>
36 #include <wx/cmdline.h>
37 #include <wx/splash.h>
38 #include <wx/preferences.h>
40 #include <boost/foreach.hpp>
47 using boost::shared_ptr;
49 using boost::scoped_array;
51 static list<boost::filesystem::path> films_to_load;
55 ID_tools_encoding_servers,
60 setup_menu (wxMenuBar* m)
62 wxMenu* file = new wxMenu;
63 file->Append (ID_file_add_film, _("&Add Film...\tCtrl-A"));
65 file->Append (wxID_EXIT, _("&Exit"));
67 file->Append (wxID_EXIT, _("&Quit"));
71 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
73 wxMenu* edit = new wxMenu;
74 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
77 wxMenu* tools = new wxMenu;
78 tools->Append (ID_tools_encoding_servers, _("Encoding servers..."));
80 wxMenu* help = new wxMenu;
81 help->Append (ID_help_about, _("About"));
83 m->Append (file, _("&File"));
85 m->Append (edit, _("&Edit"));
87 m->Append (tools, _("&Tools"));
88 m->Append (help, _("&Help"));
91 class DOMFrame : public wxFrame
94 explicit DOMFrame (wxString const & title)
95 : wxFrame (NULL, -1, title)
96 , _sizer (new wxBoxSizer (wxVERTICAL))
98 , _servers_list_dialog (0)
100 wxMenuBar* bar = new wxMenuBar;
104 Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this, _1));
106 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_film, this), ID_file_add_film);
107 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_quit, this), wxID_EXIT);
108 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
109 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_encoding_servers, this), ID_tools_encoding_servers);
110 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), ID_help_about);
112 wxPanel* panel = new wxPanel (this);
113 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
114 s->Add (panel, 1, wxEXPAND);
117 JobManagerView* job_manager_view = new JobManagerView (panel, true);
118 _sizer->Add (job_manager_view, 1, wxALL | wxEXPAND, 6);
120 wxSizer* buttons = new wxBoxSizer (wxHORIZONTAL);
121 wxButton* add = new wxButton (panel, wxID_ANY, _("Add Film..."));
122 add->Bind (wxEVT_BUTTON, boost::bind (&DOMFrame::add_film, this));
123 buttons->Add (add, 1, wxALL, 6);
125 _sizer->Add (buttons, 0, wxALL, 6);
127 panel->SetSizer (_sizer);
129 Bind (wxEVT_CLOSE_WINDOW, boost::bind (&DOMFrame::close, this, _1));
130 Bind (wxEVT_SIZE, boost::bind (&DOMFrame::sized, this, _1));
133 void start_job (boost::filesystem::path path)
136 shared_ptr<Film> film (new Film (path));
137 film->read_metadata ();
139 } catch (std::exception& e) {
140 wxString p = std_to_wx (path.string ());
141 wxCharBuffer b = p.ToUTF8 ();
142 error_dialog (this, wxString::Format (_("Could not open film at %s"), p.data()), std_to_wx(e.what()));
147 void sized (wxSizeEvent& ev)
155 if (!JobManager::instance()->work_to_do ()) {
159 wxMessageDialog* d = new wxMessageDialog (
161 _("There are unfinished jobs; are you sure you want to quit?"),
162 _("Unfinished jobs"),
163 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
166 bool const r = d->ShowModal() == wxID_YES;
171 void close (wxCloseEvent& ev)
173 if (!should_close ()) {
181 void file_add_film ()
188 if (should_close ()) {
193 void edit_preferences ()
195 if (!_config_dialog) {
196 _config_dialog = create_full_config_dialog ();
198 _config_dialog->Show (this);
201 void tools_encoding_servers ()
203 if (!_servers_list_dialog) {
204 _servers_list_dialog = new ServersListDialog (this);
207 _servers_list_dialog->Show ();
212 AboutDialog* d = new AboutDialog (this);
219 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
221 c->SetPath (std_to_wx (_last_parent.get().string ()));
227 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
228 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
235 start_job (wx_to_std (c->GetPath ()));
238 _last_parent = boost::filesystem::path (wx_to_std (c->GetPath ())).parent_path ();
243 void config_changed (Config::Property what)
245 /* Instantly save any config changes when using the DCP-o-matic GUI */
246 if (what == Config::CINEMAS) {
248 Config::instance()->write_cinemas();
249 } catch (exception& e) {
253 _("Could not write to cinemas file at %s. Your changes have not been saved."),
254 std_to_wx (Config::instance()->cinemas_file().string()).data()
260 Config::instance()->write_config();
261 } catch (exception& e) {
265 _("Could not write to config file at %s. Your changes have not been saved."),
266 std_to_wx (Config::instance()->cinemas_file().string()).data()
273 boost::optional<boost::filesystem::path> _last_parent;
275 wxPreferencesEditor* _config_dialog;
276 ServersListDialog* _servers_list_dialog;
279 static const wxCmdLineEntryDesc command_line_description[] = {
280 { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
281 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
284 class JobServer : public Server
287 explicit JobServer (DOMFrame* frame)
288 : Server (BATCH_JOB_PORT)
292 void handle (shared_ptr<Socket> socket)
295 int const length = socket->read_uint32 ();
296 scoped_array<char> buffer (new char[length]);
297 socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
298 string s (buffer.get());
299 _frame->start_job (s);
300 socket->write (reinterpret_cast<uint8_t const *> ("OK"), 3);
310 class App : public wxApp
314 SetAppName (_("DCP-o-matic Batch Converter"));
315 is_batch_converter = true;
317 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
318 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
320 wxSplashScreen* splash = maybe_show_splash ();
322 if (!wxApp::OnInit()) {
326 #ifdef DCPOMATIC_LINUX
327 unsetenv ("UBUNTU_MENUPROXY");
330 dcpomatic_setup_path_encoding ();
332 /* Enable i18n; this will create a Config object
333 to look for a force-configured language. This Config
334 object will be wrong, however, because dcpomatic_setup
335 hasn't yet been called and there aren't any filters etc.
338 dcpomatic_setup_i18n ();
340 /* Set things up, including filters etc.
341 which will now be internationalised correctly.
345 /* Force the configuration to be re-loaded correctly next
350 _frame = new DOMFrame (_("DCP-o-matic Batch Converter"));
351 SetTopWindow (_frame);
358 JobServer* server = new JobServer (_frame);
359 new thread (boost::bind (&JobServer::run, server));
361 signal_manager = new wxSignalManager (this);
362 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
364 shared_ptr<Film> film;
365 BOOST_FOREACH (boost::filesystem::path i, films_to_load) {
366 if (boost::filesystem::is_directory (i)) {
368 film.reset (new Film (i));
369 film->read_metadata ();
371 } catch (exception& e) {
374 std_to_wx (String::compose (wx_to_std (_("Could not load film %1")), i.string())),
386 signal_manager->ui_idle ();
389 void OnInitCmdLine (wxCmdLineParser& parser)
391 parser.SetDesc (command_line_description);
392 parser.SetSwitchChars (wxT ("-"));
395 bool OnCmdLineParsed (wxCmdLineParser& parser)
397 for (size_t i = 0; i < parser.GetParamCount(); ++i) {
398 films_to_load.push_back (wx_to_std (parser.GetParam(i)));
404 void config_failed_to_load ()
406 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
409 void config_warning (string m)
411 message_dialog (_frame, std_to_wx (m));