2 Copyright (C) 2013-2021 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/>.
22 #include "wx/about_dialog.h"
23 #include "wx/dcpomatic_button.h"
24 #include "wx/full_config_dialog.h"
25 #include "wx/job_manager_view.h"
26 #include "wx/servers_list_dialog.h"
27 #include "wx/wx_signal_manager.h"
28 #include "wx/wx_util.h"
29 #include "lib/compose.hpp"
30 #include "lib/config.h"
31 #include "lib/dcpomatic_socket.h"
34 #include "lib/job_manager.h"
35 #include "lib/transcode_job.h"
37 #include "lib/version.h"
38 #include <wx/aboutdlg.h>
39 #include <wx/cmdline.h>
40 #include <wx/preferences.h>
41 #include <wx/splash.h>
42 #include <wx/stdpaths.h>
49 using std::dynamic_pointer_cast;
52 using std::make_shared;
54 using std::shared_ptr;
56 using boost::scoped_array;
58 #if BOOST_VERSION >= 106100
59 using namespace boost::placeholders;
63 static list<boost::filesystem::path> films_to_load;
68 ID_tools_encoding_servers,
74 setup_menu (wxMenuBar* m)
76 auto file = new wxMenu;
77 file->Append (ID_file_add_film, _("&Add Film...\tCtrl-A"));
79 file->Append (wxID_EXIT, _("&Exit"));
81 file->Append (wxID_EXIT, _("&Quit"));
85 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
87 auto edit = new wxMenu;
88 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
91 auto tools = new wxMenu;
92 tools->Append (ID_tools_encoding_servers, _("Encoding servers..."));
94 auto help = new wxMenu;
95 help->Append (ID_help_about, _("About"));
97 m->Append (file, _("&File"));
99 m->Append (edit, _("&Edit"));
101 m->Append (tools, _("&Tools"));
102 m->Append (help, _("&Help"));
106 class DOMFrame : public wxFrame
109 explicit DOMFrame (wxString const & title)
110 : wxFrame (nullptr, -1, title)
111 , _sizer (new wxBoxSizer(wxVERTICAL))
113 auto bar = new wxMenuBar;
117 Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this, _1));
119 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_film, this), ID_file_add_film);
120 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_quit, this), wxID_EXIT);
121 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
122 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_encoding_servers, this), ID_tools_encoding_servers);
123 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), ID_help_about);
125 auto panel = new wxPanel (this);
126 auto s = new wxBoxSizer (wxHORIZONTAL);
127 s->Add (panel, 1, wxEXPAND);
130 auto job_manager_view = new JobManagerView (panel, true);
131 _sizer->Add (job_manager_view, 1, wxALL | wxEXPAND, 6);
133 auto buttons = new wxBoxSizer (wxHORIZONTAL);
134 auto add = new Button (panel, _("Add Film..."));
135 add->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::add_film, this));
136 buttons->Add (add, 1, wxALL, 6);
137 _pause = new Button (panel, _("Pause"));
138 _pause->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::pause, this));
139 buttons->Add (_pause, 1, wxALL, 6);
140 _resume = new Button (panel, _("Resume"));
141 _resume->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::resume, this));
142 buttons->Add (_resume, 1, wxALL, 6);
144 setup_sensitivity ();
146 _sizer->Add (buttons, 0, wxALL, 6);
148 panel->SetSizer (_sizer);
150 Bind (wxEVT_CLOSE_WINDOW, boost::bind(&DOMFrame::close, this, _1));
151 Bind (wxEVT_SIZE, boost::bind(&DOMFrame::sized, this, _1));
154 void setup_sensitivity ()
156 _pause->Enable (!JobManager::instance()->paused());
157 _resume->Enable (JobManager::instance()->paused());
162 JobManager::instance()->pause();
163 setup_sensitivity ();
168 JobManager::instance()->resume();
169 setup_sensitivity ();
172 void start_job (boost::filesystem::path path)
175 auto film = make_shared<Film>(path);
176 film->read_metadata ();
178 double total_required;
182 film->should_be_enough_disk_space (total_required, available, can_hard_link);
184 set<shared_ptr<const Film>> films;
186 for (auto i: JobManager::instance()->get()) {
187 films.insert (i->film());
190 for (auto i: films) {
192 for (auto j: JobManager::instance()->get()) {
193 if (i == j->film() && dynamic_pointer_cast<TranscodeJob>(j)) {
194 progress = j->progress().get_value_or(0);
199 i->should_be_enough_disk_space (required, available, can_hard_link);
200 total_required += (1 - progress) * required;
203 if ((total_required - available) > 1) {
204 if (!confirm_dialog (
207 _("The DCPs for this film and the films already in the queue will take up about %.1f GB. The "
208 "disks that you are using only have %.1f GB available. Do you want to add this film to the queue anyway?"),
209 total_required, available))) {
215 } catch (std::exception& e) {
216 auto p = std_to_wx (path.string ());
217 auto b = p.ToUTF8 ();
218 error_dialog (this, wxString::Format(_("Could not open film at %s"), p.data()), std_to_wx(e.what()));
223 void sized (wxSizeEvent& ev)
231 if (!JobManager::instance()->work_to_do()) {
235 auto d = new wxMessageDialog (
237 _("There are unfinished jobs; are you sure you want to quit?"),
238 _("Unfinished jobs"),
239 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
242 bool const r = d->ShowModal() == wxID_YES;
247 void close (wxCloseEvent& ev)
249 if (!should_close()) {
257 void file_add_film ()
264 if (should_close()) {
269 void edit_preferences ()
271 if (!_config_dialog) {
272 _config_dialog = create_full_config_dialog ();
274 _config_dialog->Show (this);
277 void tools_encoding_servers ()
279 if (!_servers_list_dialog) {
280 _servers_list_dialog = new ServersListDialog (this);
283 _servers_list_dialog->Show ();
288 auto d = new AboutDialog (this);
295 auto c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
297 c->SetPath (std_to_wx(_last_parent.get().string()));
303 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
304 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
311 start_job (wx_to_std (c->GetPath ()));
314 _last_parent = boost::filesystem::path (wx_to_std (c->GetPath ())).parent_path ();
319 void config_changed (Config::Property what)
321 /* Instantly save any config changes when using the DCP-o-matic GUI */
322 if (what == Config::CINEMAS) {
324 Config::instance()->write_cinemas();
325 } catch (exception& e) {
329 _("Could not write to cinemas file at %s. Your changes have not been saved."),
330 std_to_wx (Config::instance()->cinemas_file().string()).data()
336 Config::instance()->write_config();
337 } catch (exception& e) {
341 _("Could not write to config file at %s. Your changes have not been saved."),
342 std_to_wx (Config::instance()->cinemas_file().string()).data()
349 boost::optional<boost::filesystem::path> _last_parent;
351 wxPreferencesEditor* _config_dialog = nullptr;
352 ServersListDialog* _servers_list_dialog = nullptr;
358 static const wxCmdLineEntryDesc command_line_description[] = {
359 { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
360 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
364 class JobServer : public Server
367 explicit JobServer (DOMFrame* frame)
368 : Server (BATCH_JOB_PORT)
372 void handle (shared_ptr<Socket> socket)
375 int const length = socket->read_uint32 ();
376 scoped_array<char> buffer(new char[length]);
377 socket->read (reinterpret_cast<uint8_t*>(buffer.get()), length);
378 string s (buffer.get());
379 _frame->start_job (s);
380 socket->write (reinterpret_cast<uint8_t const *>("OK"), 3);
391 class App : public wxApp
395 wxInitAllImageHandlers ();
397 SetAppName (_("DCP-o-matic Batch Converter"));
398 is_batch_converter = true;
400 Config::FailedToLoad.connect (boost::bind(&App::config_failed_to_load, this));
401 Config::Warning.connect (boost::bind(&App::config_warning, this, _1));
403 auto splash = maybe_show_splash ();
405 if (!wxApp::OnInit()) {
409 #ifdef DCPOMATIC_LINUX
410 unsetenv ("UBUNTU_MENUPROXY");
413 dcpomatic_setup_path_encoding ();
415 /* Enable i18n; this will create a Config object
416 to look for a force-configured language. This Config
417 object will be wrong, however, because dcpomatic_setup
418 hasn't yet been called and there aren't any filters etc.
421 dcpomatic_setup_i18n ();
423 /* Set things up, including filters etc.
424 which will now be internationalised correctly.
428 /* Force the configuration to be re-loaded correctly next
433 _frame = new DOMFrame (_("DCP-o-matic Batch Converter"));
434 SetTopWindow (_frame);
441 auto server = new JobServer (_frame);
442 new thread (boost::bind (&JobServer::run, server));
444 signal_manager = new wxSignalManager (this);
445 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
447 shared_ptr<Film> film;
448 for (auto i: films_to_load) {
449 if (boost::filesystem::is_directory(i)) {
451 film = make_shared<Film>(i);
452 film->read_metadata ();
454 } catch (exception& e) {
457 std_to_wx(String::compose(wx_to_std(_("Could not load film %1")), i.string())),
469 signal_manager->ui_idle ();
472 void OnInitCmdLine (wxCmdLineParser& parser)
474 parser.SetDesc (command_line_description);
475 parser.SetSwitchChars (wxT ("-"));
478 bool OnCmdLineParsed (wxCmdLineParser& parser)
480 for (size_t i = 0; i < parser.GetParamCount(); ++i) {
481 films_to_load.push_back (wx_to_std(parser.GetParam(i)));
487 void config_failed_to_load ()
489 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
492 void config_warning (string m)
494 message_dialog (_frame, std_to_wx(m));