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