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