C++11 tidying.
[dcpomatic.git] / src / tools / dcpomatic_batch.cc
1 /*
2     Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
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"
32 #include "lib/film.h"
33 #include "lib/job.h"
34 #include "lib/job_manager.h"
35 #include "lib/transcode_job.h"
36 #include "lib/util.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>
43 #include <wx/wx.h>
44 #include <iostream>
45 #include <set>
46
47
48 using std::cout;
49 using std::dynamic_pointer_cast;
50 using std::exception;
51 using std::list;
52 using std::make_shared;
53 using std::set;
54 using std::shared_ptr;
55 using std::string;
56 using boost::scoped_array;
57 using boost::thread;
58 #if BOOST_VERSION >= 106100
59 using namespace boost::placeholders;
60 #endif
61
62
63 static list<boost::filesystem::path> films_to_load;
64
65
66 enum {
67         ID_file_add_film = 1,
68         ID_tools_encoding_servers,
69         ID_help_about
70 };
71
72
73 void
74 setup_menu (wxMenuBar* m)
75 {
76         auto file = new wxMenu;
77         file->Append (ID_file_add_film, _("&Add Film...\tCtrl-A"));
78 #ifdef DCPOMATIC_OSX
79         file->Append (wxID_EXIT, _("&Exit"));
80 #else
81         file->Append (wxID_EXIT, _("&Quit"));
82 #endif
83
84 #ifdef DCPOMATIC_OSX
85         file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
86 #else
87         auto edit = new wxMenu;
88         edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
89 #endif
90
91         auto tools = new wxMenu;
92         tools->Append (ID_tools_encoding_servers, _("Encoding servers..."));
93
94         auto help = new wxMenu;
95         help->Append (ID_help_about, _("About"));
96
97         m->Append (file, _("&File"));
98 #ifndef DCPOMATIC_OSX
99         m->Append (edit, _("&Edit"));
100 #endif
101         m->Append (tools, _("&Tools"));
102         m->Append (help, _("&Help"));
103 }
104
105
106 class DOMFrame : public wxFrame
107 {
108 public:
109         explicit DOMFrame (wxString const & title)
110                 : wxFrame (nullptr, -1, title)
111                 , _sizer (new wxBoxSizer(wxVERTICAL))
112         {
113                 auto bar = new wxMenuBar;
114                 setup_menu (bar);
115                 SetMenuBar (bar);
116
117                 Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this, _1));
118
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);
124
125                 auto panel = new wxPanel (this);
126                 auto s = new wxBoxSizer (wxHORIZONTAL);
127                 s->Add (panel, 1, wxEXPAND);
128                 SetSizer (s);
129
130                 auto job_manager_view = new JobManagerView (panel, true);
131                 _sizer->Add (job_manager_view, 1, wxALL | wxEXPAND, 6);
132
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);
143
144                 setup_sensitivity ();
145
146                 _sizer->Add (buttons, 0, wxALL, 6);
147
148                 panel->SetSizer (_sizer);
149
150                 Bind (wxEVT_CLOSE_WINDOW, boost::bind(&DOMFrame::close, this, _1));
151                 Bind (wxEVT_SIZE, boost::bind(&DOMFrame::sized, this, _1));
152         }
153
154         void setup_sensitivity ()
155         {
156                 _pause->Enable (!JobManager::instance()->paused());
157                 _resume->Enable (JobManager::instance()->paused());
158         }
159
160         void pause ()
161         {
162                 JobManager::instance()->pause();
163                 setup_sensitivity ();
164         }
165
166         void resume ()
167         {
168                 JobManager::instance()->resume();
169                 setup_sensitivity ();
170         }
171
172         void start_job (boost::filesystem::path path)
173         {
174                 try {
175                         auto film = make_shared<Film>(path);
176                         film->read_metadata ();
177
178                         double total_required;
179                         double available;
180                         bool can_hard_link;
181
182                         film->should_be_enough_disk_space (total_required, available, can_hard_link);
183
184                         set<shared_ptr<const Film>> films;
185
186                         for (auto i: JobManager::instance()->get()) {
187                                 films.insert (i->film());
188                         }
189
190                         for (auto i: films) {
191                                 double progress = 0;
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);
195                                         }
196                                 }
197
198                                 double required;
199                                 i->should_be_enough_disk_space (required, available, can_hard_link);
200                                 total_required += (1 - progress) * required;
201                         }
202
203                         if ((total_required - available) > 1) {
204                                 if (!confirm_dialog (
205                                             this,
206                                             wxString::Format(
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))) {
210                                         return;
211                                 }
212                         }
213
214                         film->make_dcp ();
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()));
219                 }
220         }
221
222 private:
223         void sized (wxSizeEvent& ev)
224         {
225                 _sizer->Layout ();
226                 ev.Skip ();
227         }
228
229         bool should_close ()
230         {
231                 if (!JobManager::instance()->work_to_do()) {
232                         return true;
233                 }
234
235                 auto d = new wxMessageDialog (
236                         0,
237                         _("There are unfinished jobs; are you sure you want to quit?"),
238                         _("Unfinished jobs"),
239                         wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
240                         );
241
242                 bool const r = d->ShowModal() == wxID_YES;
243                 d->Destroy ();
244                 return r;
245         }
246
247         void close (wxCloseEvent& ev)
248         {
249                 if (!should_close()) {
250                         ev.Veto ();
251                         return;
252                 }
253
254                 ev.Skip ();
255         }
256
257         void file_add_film ()
258         {
259                 add_film ();
260         }
261
262         void file_quit ()
263         {
264                 if (should_close()) {
265                         Close (true);
266                 }
267         }
268
269         void edit_preferences ()
270         {
271                 if (!_config_dialog) {
272                         _config_dialog = create_full_config_dialog ();
273                 }
274                 _config_dialog->Show (this);
275         }
276
277         void tools_encoding_servers ()
278         {
279                 if (!_servers_list_dialog) {
280                         _servers_list_dialog = new ServersListDialog (this);
281                 }
282
283                 _servers_list_dialog->Show ();
284         }
285
286         void help_about ()
287         {
288                 auto d = new AboutDialog (this);
289                 d->ShowModal ();
290                 d->Destroy ();
291         }
292
293         void add_film ()
294         {
295                 auto c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
296                 if (_last_parent) {
297                         c->SetPath (std_to_wx(_last_parent.get().string()));
298                 }
299
300                 int r;
301                 while (true) {
302                         r = c->ShowModal ();
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."));
305                         } else {
306                                 break;
307                         }
308                 }
309
310                 if (r == wxID_OK) {
311                         start_job (wx_to_std (c->GetPath ()));
312                 }
313
314                 _last_parent = boost::filesystem::path (wx_to_std (c->GetPath ())).parent_path ();
315
316                 c->Destroy ();
317         }
318
319         void config_changed (Config::Property what)
320         {
321                 /* Instantly save any config changes when using the DCP-o-matic GUI */
322                 if (what == Config::CINEMAS) {
323                         try {
324                                 Config::instance()->write_cinemas();
325                         } catch (exception& e) {
326                                 error_dialog (
327                                         this,
328                                         wxString::Format(
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()
331                                                 )
332                                         );
333                         }
334                 } else {
335                         try {
336                                 Config::instance()->write_config();
337                         } catch (exception& e) {
338                                 error_dialog (
339                                         this,
340                                         wxString::Format(
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()
343                                                 )
344                                         );
345                         }
346                 }
347         }
348
349         boost::optional<boost::filesystem::path> _last_parent;
350         wxSizer* _sizer;
351         wxPreferencesEditor* _config_dialog = nullptr;
352         ServersListDialog* _servers_list_dialog = nullptr;
353         wxButton* _pause;
354         wxButton* _resume;
355 };
356
357
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 }
361 };
362
363
364 class JobServer : public Server
365 {
366 public:
367         explicit JobServer (DOMFrame* frame)
368                 : Server (BATCH_JOB_PORT)
369                 , _frame (frame)
370         {}
371
372         void handle (shared_ptr<Socket> socket)
373         {
374                 try {
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);
381                 } catch (...) {
382
383                 }
384         }
385
386 private:
387         DOMFrame* _frame;
388 };
389
390
391 class App : public wxApp
392 {
393         bool OnInit ()
394         {
395                 wxInitAllImageHandlers ();
396
397                 SetAppName (_("DCP-o-matic Batch Converter"));
398                 is_batch_converter = true;
399
400                 Config::FailedToLoad.connect (boost::bind(&App::config_failed_to_load, this));
401                 Config::Warning.connect (boost::bind(&App::config_warning, this, _1));
402
403                 auto splash = maybe_show_splash ();
404
405                 if (!wxApp::OnInit()) {
406                         return false;
407                 }
408
409 #ifdef DCPOMATIC_LINUX
410                 unsetenv ("UBUNTU_MENUPROXY");
411 #endif
412
413                 dcpomatic_setup_path_encoding ();
414
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.
419                    set up yet.
420                 */
421                 dcpomatic_setup_i18n ();
422
423                 /* Set things up, including filters etc.
424                    which will now be internationalised correctly.
425                 */
426                 dcpomatic_setup ();
427
428                 /* Force the configuration to be re-loaded correctly next
429                    time it is needed.
430                 */
431                 Config::drop ();
432
433                 _frame = new DOMFrame (_("DCP-o-matic Batch Converter"));
434                 SetTopWindow (_frame);
435                 _frame->Maximize ();
436                 if (splash) {
437                         splash->Destroy ();
438                 }
439                 _frame->Show ();
440
441                 auto server = new JobServer (_frame);
442                 new thread (boost::bind (&JobServer::run, server));
443
444                 signal_manager = new wxSignalManager (this);
445                 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
446
447                 shared_ptr<Film> film;
448                 for (auto i: films_to_load) {
449                         if (boost::filesystem::is_directory(i)) {
450                                 try {
451                                         film = make_shared<Film>(i);
452                                         film->read_metadata ();
453                                         film->make_dcp ();
454                                 } catch (exception& e) {
455                                         error_dialog (
456                                                 0,
457                                                 std_to_wx(String::compose(wx_to_std(_("Could not load film %1")), i.string())),
458                                                 std_to_wx(e.what())
459                                                 );
460                                 }
461                         }
462                 }
463
464                 return true;
465         }
466
467         void idle ()
468         {
469                 signal_manager->ui_idle ();
470         }
471
472         void OnInitCmdLine (wxCmdLineParser& parser)
473         {
474                 parser.SetDesc (command_line_description);
475                 parser.SetSwitchChars (wxT ("-"));
476         }
477
478         bool OnCmdLineParsed (wxCmdLineParser& parser)
479         {
480                 for (size_t i = 0; i < parser.GetParamCount(); ++i) {
481                         films_to_load.push_back (wx_to_std(parser.GetParam(i)));
482                 }
483
484                 return true;
485         }
486
487         void config_failed_to_load ()
488         {
489                 message_dialog (_frame, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
490         }
491
492         void config_warning (string m)
493         {
494                 message_dialog (_frame, std_to_wx(m));
495         }
496
497         DOMFrame* _frame;
498 };
499
500
501 IMPLEMENT_APP (App)