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