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;
57 static list<boost::filesystem::path> films_to_load;
61 ID_tools_encoding_servers,
66 setup_menu (wxMenuBar* m)
68 wxMenu* file = new wxMenu;
69 file->Append (ID_file_add_film, _("&Add Film...\tCtrl-A"));
71 file->Append (wxID_EXIT, _("&Exit"));
73 file->Append (wxID_EXIT, _("&Quit"));
77 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
79 wxMenu* edit = new wxMenu;
80 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
83 wxMenu* tools = new wxMenu;
84 tools->Append (ID_tools_encoding_servers, _("Encoding servers..."));
86 wxMenu* help = new wxMenu;
87 help->Append (ID_help_about, _("About"));
89 m->Append (file, _("&File"));
91 m->Append (edit, _("&Edit"));
93 m->Append (tools, _("&Tools"));
94 m->Append (help, _("&Help"));
97 class DOMFrame : public wxFrame
100 explicit DOMFrame (wxString const & title)
101 : wxFrame (NULL, -1, title)
102 , _sizer (new wxBoxSizer (wxVERTICAL))
104 , _servers_list_dialog (0)
106 wxMenuBar* bar = new wxMenuBar;
110 Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this, _1));
112 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_film, this), ID_file_add_film);
113 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_quit, this), wxID_EXIT);
114 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
115 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_encoding_servers, this), ID_tools_encoding_servers);
116 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), ID_help_about);
118 wxPanel* panel = new wxPanel (this);
119 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
120 s->Add (panel, 1, wxEXPAND);
123 JobManagerView* job_manager_view = new JobManagerView (panel, true);
124 _sizer->Add (job_manager_view, 1, wxALL | wxEXPAND, 6);
126 wxSizer* buttons = new wxBoxSizer (wxHORIZONTAL);
127 wxButton* add = new Button (panel, _("Add Film..."));
128 add->Bind (wxEVT_BUTTON, boost::bind (&DOMFrame::add_film, this));
129 buttons->Add (add, 1, wxALL, 6);
130 _pause = new Button (panel, _("Pause"));
131 _pause->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::pause, this));
132 buttons->Add (_pause, 1, wxALL, 6);
133 _resume = new Button (panel, _("Resume"));
134 _resume->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::resume, this));
135 buttons->Add (_resume, 1, wxALL, 6);
137 setup_sensitivity ();
139 _sizer->Add (buttons, 0, wxALL, 6);
141 panel->SetSizer (_sizer);
143 Bind (wxEVT_CLOSE_WINDOW, boost::bind (&DOMFrame::close, this, _1));
144 Bind (wxEVT_SIZE, boost::bind (&DOMFrame::sized, this, _1));
147 void setup_sensitivity ()
149 _pause->Enable (!JobManager::instance()->paused());
150 _resume->Enable (JobManager::instance()->paused());
155 JobManager::instance()->pause ();
156 setup_sensitivity ();
161 JobManager::instance()->resume ();
162 setup_sensitivity ();
165 void start_job (boost::filesystem::path path)
168 shared_ptr<Film> film (new Film (path));
169 film->read_metadata ();
171 double total_required;
175 film->should_be_enough_disk_space (total_required, available, can_hard_link);
177 set<shared_ptr<const Film> > films;
179 BOOST_FOREACH (shared_ptr<Job> i, JobManager::instance()->get()) {
180 films.insert (i->film());
183 BOOST_FOREACH (shared_ptr<const Film> i, films) {
185 BOOST_FOREACH (shared_ptr<Job> j, JobManager::instance()->get()) {
186 if (i == j->film() && dynamic_pointer_cast<TranscodeJob>(j)) {
187 progress = j->progress().get_value_or(0);
192 i->should_be_enough_disk_space (required, available, can_hard_link);
193 total_required += (1 - progress) * required;
196 if ((total_required - available) > 1) {
197 if (!confirm_dialog (
200 _("The DCPs for this film and the films already in the queue will take up about %.1f GB. The "
201 "disks that you are using only have %.1f GB available. Do you want to add this film to the queue anyway?"),
202 total_required, available))) {
208 } catch (std::exception& e) {
209 wxString p = std_to_wx (path.string ());
210 wxCharBuffer b = p.ToUTF8 ();
211 error_dialog (this, wxString::Format (_("Could not open film at %s"), p.data()), std_to_wx(e.what()));
216 void sized (wxSizeEvent& ev)
224 if (!JobManager::instance()->work_to_do ()) {
228 wxMessageDialog* d = new wxMessageDialog (
230 _("There are unfinished jobs; are you sure you want to quit?"),
231 _("Unfinished jobs"),
232 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
235 bool const r = d->ShowModal() == wxID_YES;
240 void close (wxCloseEvent& ev)
242 if (!should_close ()) {
250 void file_add_film ()
257 if (should_close ()) {
262 void edit_preferences ()
264 if (!_config_dialog) {
265 _config_dialog = create_full_config_dialog ();
267 _config_dialog->Show (this);
270 void tools_encoding_servers ()
272 if (!_servers_list_dialog) {
273 _servers_list_dialog = new ServersListDialog (this);
276 _servers_list_dialog->Show ();
281 AboutDialog* d = new AboutDialog (this);
288 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
290 c->SetPath (std_to_wx (_last_parent.get().string ()));
296 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
297 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
304 start_job (wx_to_std (c->GetPath ()));
307 _last_parent = boost::filesystem::path (wx_to_std (c->GetPath ())).parent_path ();
312 void config_changed (Config::Property what)
314 /* Instantly save any config changes when using the DCP-o-matic GUI */
315 if (what == Config::CINEMAS) {
317 Config::instance()->write_cinemas();
318 } catch (exception& e) {
322 _("Could not write to cinemas file at %s. Your changes have not been saved."),
323 std_to_wx (Config::instance()->cinemas_file().string()).data()
329 Config::instance()->write_config();
330 } catch (exception& e) {
334 _("Could not write to config file at %s. Your changes have not been saved."),
335 std_to_wx (Config::instance()->cinemas_file().string()).data()
342 boost::optional<boost::filesystem::path> _last_parent;
344 wxPreferencesEditor* _config_dialog;
345 ServersListDialog* _servers_list_dialog;
350 static const wxCmdLineEntryDesc command_line_description[] = {
351 { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
352 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
355 class JobServer : public Server
358 explicit JobServer (DOMFrame* frame)
359 : Server (BATCH_JOB_PORT)
363 void handle (shared_ptr<Socket> socket)
366 int const length = socket->read_uint32 ();
367 scoped_array<char> buffer (new char[length]);
368 socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
369 string s (buffer.get());
370 _frame->start_job (s);
371 socket->write (reinterpret_cast<uint8_t const *> ("OK"), 3);
381 class App : public wxApp
385 wxInitAllImageHandlers ();
387 SetAppName (_("DCP-o-matic Batch Converter"));
388 is_batch_converter = true;
390 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
391 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
393 wxSplashScreen* splash = maybe_show_splash ();
395 if (!wxApp::OnInit()) {
399 #ifdef DCPOMATIC_LINUX
400 unsetenv ("UBUNTU_MENUPROXY");
403 dcpomatic_setup_path_encoding ();
405 /* Enable i18n; this will create a Config object
406 to look for a force-configured language. This Config
407 object will be wrong, however, because dcpomatic_setup
408 hasn't yet been called and there aren't any filters etc.
411 dcpomatic_setup_i18n ();
413 /* Set things up, including filters etc.
414 which will now be internationalised correctly.
418 /* Force the configuration to be re-loaded correctly next
423 _frame = new DOMFrame (_("DCP-o-matic Batch Converter"));
424 SetTopWindow (_frame);
431 JobServer* server = new JobServer (_frame);
432 new thread (boost::bind (&JobServer::run, server));
434 signal_manager = new wxSignalManager (this);
435 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
437 shared_ptr<Film> film;
438 BOOST_FOREACH (boost::filesystem::path i, films_to_load) {
439 if (boost::filesystem::is_directory (i)) {
441 film.reset (new Film (i));
442 film->read_metadata ();
444 } catch (exception& e) {
447 std_to_wx (String::compose (wx_to_std (_("Could not load film %1")), i.string())),
459 signal_manager->ui_idle ();
462 void OnInitCmdLine (wxCmdLineParser& parser)
464 parser.SetDesc (command_line_description);
465 parser.SetSwitchChars (wxT ("-"));
468 bool OnCmdLineParsed (wxCmdLineParser& parser)
470 for (size_t i = 0; i < parser.GetParamCount(); ++i) {
471 films_to_load.push_back (wx_to_std (parser.GetParam(i)));
477 void config_failed_to_load ()
479 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
482 void config_warning (string m)
484 message_dialog (_frame, std_to_wx (m));