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