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