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>
51 using std::shared_ptr;
53 using boost::scoped_array;
54 using std::dynamic_pointer_cast;
55 #if BOOST_VERSION >= 106100
56 using namespace boost::placeholders;
59 static list<boost::filesystem::path> films_to_load;
63 ID_tools_encoding_servers,
68 setup_menu (wxMenuBar* m)
70 wxMenu* file = new wxMenu;
71 file->Append (ID_file_add_film, _("&Add Film...\tCtrl-A"));
73 file->Append (wxID_EXIT, _("&Exit"));
75 file->Append (wxID_EXIT, _("&Quit"));
79 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
81 wxMenu* edit = new wxMenu;
82 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
85 wxMenu* tools = new wxMenu;
86 tools->Append (ID_tools_encoding_servers, _("Encoding servers..."));
88 wxMenu* help = new wxMenu;
89 help->Append (ID_help_about, _("About"));
91 m->Append (file, _("&File"));
93 m->Append (edit, _("&Edit"));
95 m->Append (tools, _("&Tools"));
96 m->Append (help, _("&Help"));
99 class DOMFrame : public wxFrame
102 explicit DOMFrame (wxString const & title)
103 : wxFrame (NULL, -1, title)
104 , _sizer (new wxBoxSizer (wxVERTICAL))
106 , _servers_list_dialog (0)
108 wxMenuBar* bar = new wxMenuBar;
112 Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this, _1));
114 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_film, this), ID_file_add_film);
115 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_quit, this), wxID_EXIT);
116 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
117 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_encoding_servers, this), ID_tools_encoding_servers);
118 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), ID_help_about);
120 wxPanel* panel = new wxPanel (this);
121 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
122 s->Add (panel, 1, wxEXPAND);
125 JobManagerView* job_manager_view = new JobManagerView (panel, true);
126 _sizer->Add (job_manager_view, 1, wxALL | wxEXPAND, 6);
128 wxSizer* buttons = new wxBoxSizer (wxHORIZONTAL);
129 wxButton* add = new Button (panel, _("Add Film..."));
130 add->Bind (wxEVT_BUTTON, boost::bind (&DOMFrame::add_film, this));
131 buttons->Add (add, 1, wxALL, 6);
132 _pause = new Button (panel, _("Pause"));
133 _pause->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::pause, this));
134 buttons->Add (_pause, 1, wxALL, 6);
135 _resume = new Button (panel, _("Resume"));
136 _resume->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::resume, this));
137 buttons->Add (_resume, 1, wxALL, 6);
139 setup_sensitivity ();
141 _sizer->Add (buttons, 0, wxALL, 6);
143 panel->SetSizer (_sizer);
145 Bind (wxEVT_CLOSE_WINDOW, boost::bind (&DOMFrame::close, this, _1));
146 Bind (wxEVT_SIZE, boost::bind (&DOMFrame::sized, this, _1));
149 void setup_sensitivity ()
151 _pause->Enable (!JobManager::instance()->paused());
152 _resume->Enable (JobManager::instance()->paused());
157 JobManager::instance()->pause ();
158 setup_sensitivity ();
163 JobManager::instance()->resume ();
164 setup_sensitivity ();
167 void start_job (boost::filesystem::path path)
170 shared_ptr<Film> film (new Film (path));
171 film->read_metadata ();
173 double total_required;
177 film->should_be_enough_disk_space (total_required, available, can_hard_link);
179 set<shared_ptr<const Film> > films;
181 for (auto i: JobManager::instance()->get()) {
182 films.insert (i->film());
185 for (auto i: films) {
187 for (auto j: JobManager::instance()->get()) {
188 if (i == j->film() && dynamic_pointer_cast<TranscodeJob>(j)) {
189 progress = j->progress().get_value_or(0);
194 i->should_be_enough_disk_space (required, available, can_hard_link);
195 total_required += (1 - progress) * required;
198 if ((total_required - available) > 1) {
199 if (!confirm_dialog (
202 _("The DCPs for this film and the films already in the queue will take up about %.1f GB. The "
203 "disks that you are using only have %.1f GB available. Do you want to add this film to the queue anyway?"),
204 total_required, available))) {
210 } catch (std::exception& e) {
211 wxString p = std_to_wx (path.string ());
212 wxCharBuffer b = p.ToUTF8 ();
213 error_dialog (this, wxString::Format (_("Could not open film at %s"), p.data()), std_to_wx(e.what()));
218 void sized (wxSizeEvent& ev)
226 if (!JobManager::instance()->work_to_do ()) {
230 wxMessageDialog* d = new wxMessageDialog (
232 _("There are unfinished jobs; are you sure you want to quit?"),
233 _("Unfinished jobs"),
234 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
237 bool const r = d->ShowModal() == wxID_YES;
242 void close (wxCloseEvent& ev)
244 if (!should_close ()) {
252 void file_add_film ()
259 if (should_close ()) {
264 void edit_preferences ()
266 if (!_config_dialog) {
267 _config_dialog = create_full_config_dialog ();
269 _config_dialog->Show (this);
272 void tools_encoding_servers ()
274 if (!_servers_list_dialog) {
275 _servers_list_dialog = new ServersListDialog (this);
278 _servers_list_dialog->Show ();
283 AboutDialog* d = new AboutDialog (this);
290 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
292 c->SetPath (std_to_wx (_last_parent.get().string ()));
298 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
299 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
306 start_job (wx_to_std (c->GetPath ()));
309 _last_parent = boost::filesystem::path (wx_to_std (c->GetPath ())).parent_path ();
314 void config_changed (Config::Property what)
316 /* Instantly save any config changes when using the DCP-o-matic GUI */
317 if (what == Config::CINEMAS) {
319 Config::instance()->write_cinemas();
320 } catch (exception& e) {
324 _("Could not write to cinemas file at %s. Your changes have not been saved."),
325 std_to_wx (Config::instance()->cinemas_file().string()).data()
331 Config::instance()->write_config();
332 } catch (exception& e) {
336 _("Could not write to config file at %s. Your changes have not been saved."),
337 std_to_wx (Config::instance()->cinemas_file().string()).data()
344 boost::optional<boost::filesystem::path> _last_parent;
346 wxPreferencesEditor* _config_dialog;
347 ServersListDialog* _servers_list_dialog;
352 static const wxCmdLineEntryDesc command_line_description[] = {
353 { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
354 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
357 class JobServer : public Server
360 explicit JobServer (DOMFrame* frame)
361 : Server (BATCH_JOB_PORT)
365 void handle (shared_ptr<Socket> socket)
368 int const length = socket->read_uint32 ();
369 scoped_array<char> buffer (new char[length]);
370 socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
371 string s (buffer.get());
372 _frame->start_job (s);
373 socket->write (reinterpret_cast<uint8_t const *> ("OK"), 3);
383 class App : public wxApp
387 wxInitAllImageHandlers ();
389 SetAppName (_("DCP-o-matic Batch Converter"));
390 is_batch_converter = true;
392 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
393 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
395 wxSplashScreen* splash = maybe_show_splash ();
397 if (!wxApp::OnInit()) {
401 #ifdef DCPOMATIC_LINUX
402 unsetenv ("UBUNTU_MENUPROXY");
405 dcpomatic_setup_path_encoding ();
407 /* Enable i18n; this will create a Config object
408 to look for a force-configured language. This Config
409 object will be wrong, however, because dcpomatic_setup
410 hasn't yet been called and there aren't any filters etc.
413 dcpomatic_setup_i18n ();
415 /* Set things up, including filters etc.
416 which will now be internationalised correctly.
420 /* Force the configuration to be re-loaded correctly next
425 _frame = new DOMFrame (_("DCP-o-matic Batch Converter"));
426 SetTopWindow (_frame);
433 JobServer* server = new JobServer (_frame);
434 new thread (boost::bind (&JobServer::run, server));
436 signal_manager = new wxSignalManager (this);
437 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
439 shared_ptr<Film> film;
440 for (auto i: films_to_load) {
441 if (boost::filesystem::is_directory (i)) {
443 film.reset (new Film (i));
444 film->read_metadata ();
446 } catch (exception& e) {
449 std_to_wx (String::compose (wx_to_std (_("Could not load film %1")), i.string())),
461 signal_manager->ui_idle ();
464 void OnInitCmdLine (wxCmdLineParser& parser)
466 parser.SetDesc (command_line_description);
467 parser.SetSwitchChars (wxT ("-"));
470 bool OnCmdLineParsed (wxCmdLineParser& parser)
472 for (size_t i = 0; i < parser.GetParamCount(); ++i) {
473 films_to_load.push_back (wx_to_std (parser.GetParam(i)));
479 void config_failed_to_load ()
481 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
484 void config_warning (string m)
486 message_dialog (_frame, std_to_wx (m));