2 Copyright (C) 2013-2019 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 "lib/transcode_job.h"
37 #include <wx/aboutdlg.h>
38 #include <wx/stdpaths.h>
39 #include <wx/cmdline.h>
40 #include <wx/splash.h>
41 #include <wx/preferences.h>
43 #include <boost/foreach.hpp>
52 using boost::shared_ptr;
54 using boost::scoped_array;
55 using boost::dynamic_pointer_cast;
56 #if BOOST_VERSION >= 106100
57 using namespace boost::placeholders;
60 static list<boost::filesystem::path> films_to_load;
64 ID_tools_encoding_servers,
69 setup_menu (wxMenuBar* m)
71 wxMenu* file = new wxMenu;
72 file->Append (ID_file_add_film, _("&Add Film...\tCtrl-A"));
74 file->Append (wxID_EXIT, _("&Exit"));
76 file->Append (wxID_EXIT, _("&Quit"));
80 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
82 wxMenu* edit = new wxMenu;
83 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
86 wxMenu* tools = new wxMenu;
87 tools->Append (ID_tools_encoding_servers, _("Encoding servers..."));
89 wxMenu* help = new wxMenu;
90 help->Append (ID_help_about, _("About"));
92 m->Append (file, _("&File"));
94 m->Append (edit, _("&Edit"));
96 m->Append (tools, _("&Tools"));
97 m->Append (help, _("&Help"));
100 class DOMFrame : public wxFrame
103 explicit DOMFrame (wxString const & title)
104 : wxFrame (NULL, -1, title)
105 , _sizer (new wxBoxSizer (wxVERTICAL))
107 , _servers_list_dialog (0)
109 wxMenuBar* bar = new wxMenuBar;
113 Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this, _1));
115 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_film, this), ID_file_add_film);
116 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_quit, this), wxID_EXIT);
117 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
118 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_encoding_servers, this), ID_tools_encoding_servers);
119 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), ID_help_about);
121 wxPanel* panel = new wxPanel (this);
122 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
123 s->Add (panel, 1, wxEXPAND);
126 JobManagerView* job_manager_view = new JobManagerView (panel, true);
127 _sizer->Add (job_manager_view, 1, wxALL | wxEXPAND, 6);
129 wxSizer* buttons = new wxBoxSizer (wxHORIZONTAL);
130 wxButton* add = new Button (panel, _("Add Film..."));
131 add->Bind (wxEVT_BUTTON, boost::bind (&DOMFrame::add_film, this));
132 buttons->Add (add, 1, wxALL, 6);
133 _pause = new Button (panel, _("Pause"));
134 _pause->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::pause, this));
135 buttons->Add (_pause, 1, wxALL, 6);
136 _resume = new Button (panel, _("Resume"));
137 _resume->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::resume, this));
138 buttons->Add (_resume, 1, wxALL, 6);
140 setup_sensitivity ();
142 _sizer->Add (buttons, 0, wxALL, 6);
144 panel->SetSizer (_sizer);
146 Bind (wxEVT_CLOSE_WINDOW, boost::bind (&DOMFrame::close, this, _1));
147 Bind (wxEVT_SIZE, boost::bind (&DOMFrame::sized, this, _1));
150 void setup_sensitivity ()
152 _pause->Enable (!JobManager::instance()->paused());
153 _resume->Enable (JobManager::instance()->paused());
158 JobManager::instance()->pause ();
159 setup_sensitivity ();
164 JobManager::instance()->resume ();
165 setup_sensitivity ();
168 void start_job (boost::filesystem::path path)
171 shared_ptr<Film> film (new Film (path));
172 film->read_metadata ();
174 double total_required;
178 film->should_be_enough_disk_space (total_required, available, can_hard_link);
180 set<shared_ptr<const Film> > films;
182 BOOST_FOREACH (shared_ptr<Job> i, JobManager::instance()->get()) {
183 films.insert (i->film());
186 BOOST_FOREACH (shared_ptr<const Film> i, films) {
188 BOOST_FOREACH (shared_ptr<Job> j, JobManager::instance()->get()) {
189 if (i == j->film() && dynamic_pointer_cast<TranscodeJob>(j)) {
190 progress = j->progress().get_value_or(0);
195 i->should_be_enough_disk_space (required, available, can_hard_link);
196 total_required += (1 - progress) * required;
199 if ((total_required - available) > 1) {
200 if (!confirm_dialog (
203 _("The DCPs for this film and the films already in the queue will take up about %.1f GB. The "
204 "disks that you are using only have %.1f GB available. Do you want to add this film to the queue anyway?"),
205 total_required, available))) {
211 } catch (std::exception& e) {
212 wxString p = std_to_wx (path.string ());
213 wxCharBuffer b = p.ToUTF8 ();
214 error_dialog (this, wxString::Format (_("Could not open film at %s"), p.data()), std_to_wx(e.what()));
219 void sized (wxSizeEvent& ev)
227 if (!JobManager::instance()->work_to_do ()) {
231 wxMessageDialog* d = new wxMessageDialog (
233 _("There are unfinished jobs; are you sure you want to quit?"),
234 _("Unfinished jobs"),
235 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
238 bool const r = d->ShowModal() == wxID_YES;
243 void close (wxCloseEvent& ev)
245 if (!should_close ()) {
253 void file_add_film ()
260 if (should_close ()) {
265 void edit_preferences ()
267 if (!_config_dialog) {
268 _config_dialog = create_full_config_dialog ();
270 _config_dialog->Show (this);
273 void tools_encoding_servers ()
275 if (!_servers_list_dialog) {
276 _servers_list_dialog = new ServersListDialog (this);
279 _servers_list_dialog->Show ();
284 AboutDialog* d = new AboutDialog (this);
291 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
293 c->SetPath (std_to_wx (_last_parent.get().string ()));
299 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
300 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
307 start_job (wx_to_std (c->GetPath ()));
310 _last_parent = boost::filesystem::path (wx_to_std (c->GetPath ())).parent_path ();
315 void config_changed (Config::Property what)
317 /* Instantly save any config changes when using the DCP-o-matic GUI */
318 if (what == Config::CINEMAS) {
320 Config::instance()->write_cinemas();
321 } catch (exception& e) {
325 _("Could not write to cinemas file at %s. Your changes have not been saved."),
326 std_to_wx (Config::instance()->cinemas_file().string()).data()
332 Config::instance()->write_config();
333 } catch (exception& e) {
337 _("Could not write to config file at %s. Your changes have not been saved."),
338 std_to_wx (Config::instance()->cinemas_file().string()).data()
345 boost::optional<boost::filesystem::path> _last_parent;
347 wxPreferencesEditor* _config_dialog;
348 ServersListDialog* _servers_list_dialog;
353 static const wxCmdLineEntryDesc command_line_description[] = {
354 { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
355 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
358 class JobServer : public Server
361 explicit JobServer (DOMFrame* frame)
362 : Server (BATCH_JOB_PORT)
366 void handle (shared_ptr<Socket> socket)
369 int const length = socket->read_uint32 ();
370 scoped_array<char> buffer (new char[length]);
371 socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
372 string s (buffer.get());
373 _frame->start_job (s);
374 socket->write (reinterpret_cast<uint8_t const *> ("OK"), 3);
384 class App : public wxApp
388 wxInitAllImageHandlers ();
390 SetAppName (_("DCP-o-matic Batch Converter"));
391 is_batch_converter = true;
393 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
394 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
396 wxSplashScreen* splash = maybe_show_splash ();
398 if (!wxApp::OnInit()) {
402 #ifdef DCPOMATIC_LINUX
403 unsetenv ("UBUNTU_MENUPROXY");
406 dcpomatic_setup_path_encoding ();
408 /* Enable i18n; this will create a Config object
409 to look for a force-configured language. This Config
410 object will be wrong, however, because dcpomatic_setup
411 hasn't yet been called and there aren't any filters etc.
414 dcpomatic_setup_i18n ();
416 /* Set things up, including filters etc.
417 which will now be internationalised correctly.
421 /* Force the configuration to be re-loaded correctly next
426 _frame = new DOMFrame (_("DCP-o-matic Batch Converter"));
427 SetTopWindow (_frame);
434 JobServer* server = new JobServer (_frame);
435 new thread (boost::bind (&JobServer::run, server));
437 signal_manager = new wxSignalManager (this);
438 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
440 shared_ptr<Film> film;
441 BOOST_FOREACH (boost::filesystem::path i, films_to_load) {
442 if (boost::filesystem::is_directory (i)) {
444 film.reset (new Film (i));
445 film->read_metadata ();
447 } catch (exception& e) {
450 std_to_wx (String::compose (wx_to_std (_("Could not load film %1")), i.string())),
462 signal_manager->ui_idle ();
465 void OnInitCmdLine (wxCmdLineParser& parser)
467 parser.SetDesc (command_line_description);
468 parser.SetSwitchChars (wxT ("-"));
471 bool OnCmdLineParsed (wxCmdLineParser& parser)
473 for (size_t i = 0; i < parser.GetParamCount(); ++i) {
474 films_to_load.push_back (wx_to_std (parser.GetParam(i)));
480 void config_failed_to_load ()
482 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
485 void config_warning (string m)
487 message_dialog (_frame, std_to_wx (m));