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