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