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