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