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