Add a load of explicit keywords.
[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                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_film, this),    ID_file_add_film);
105                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_quit, this),        wxID_EXIT);
106                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
107                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_encoding_servers, this), ID_tools_encoding_servers);
108                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this),       ID_help_about);
109
110                 wxPanel* panel = new wxPanel (this);
111                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
112                 s->Add (panel, 1, wxEXPAND);
113                 SetSizer (s);
114
115                 JobManagerView* job_manager_view = new JobManagerView (panel, true);
116                 _sizer->Add (job_manager_view, 1, wxALL | wxEXPAND, 6);
117
118                 wxSizer* buttons = new wxBoxSizer (wxHORIZONTAL);
119                 wxButton* add = new wxButton (panel, wxID_ANY, _("Add Film..."));
120                 add->Bind (wxEVT_BUTTON, boost::bind (&DOMFrame::add_film, this));
121                 buttons->Add (add, 1, wxALL, 6);
122
123                 _sizer->Add (buttons, 0, wxALL, 6);
124
125                 panel->SetSizer (_sizer);
126
127                 Bind (wxEVT_CLOSE_WINDOW, boost::bind (&DOMFrame::close, this, _1));
128                 Bind (wxEVT_SIZE, boost::bind (&DOMFrame::sized, this, _1));
129         }
130
131         void start_job (boost::filesystem::path path)
132         {
133                 try {
134                         shared_ptr<Film> film (new Film (path));
135                         film->read_metadata ();
136                         film->make_dcp ();
137                 } catch (std::exception& e) {
138                         wxString p = std_to_wx (path.string ());
139                         wxCharBuffer b = p.ToUTF8 ();
140                         error_dialog (this, wxString::Format (_("Could not open film at %s"), p.data()), std_to_wx(e.what()));
141                 }
142         }
143
144 private:
145         void sized (wxSizeEvent& ev)
146         {
147                 _sizer->Layout ();
148                 ev.Skip ();
149         }
150
151         bool should_close ()
152         {
153                 if (!JobManager::instance()->work_to_do ()) {
154                         return true;
155                 }
156
157                 wxMessageDialog* d = new wxMessageDialog (
158                         0,
159                         _("There are unfinished jobs; are you sure you want to quit?"),
160                         _("Unfinished jobs"),
161                         wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
162                         );
163
164                 bool const r = d->ShowModal() == wxID_YES;
165                 d->Destroy ();
166                 return r;
167         }
168
169         void close (wxCloseEvent& ev)
170         {
171                 if (!should_close ()) {
172                         ev.Veto ();
173                         return;
174                 }
175
176                 ev.Skip ();
177         }
178
179         void file_add_film ()
180         {
181                 add_film ();
182         }
183
184         void file_quit ()
185         {
186                 if (should_close ()) {
187                         Close (true);
188                 }
189         }
190
191         void edit_preferences ()
192         {
193                 if (!_config_dialog) {
194                         _config_dialog = create_full_config_dialog ();
195                 }
196                 _config_dialog->Show (this);
197         }
198
199         void tools_encoding_servers ()
200         {
201                 if (!_servers_list_dialog) {
202                         _servers_list_dialog = new ServersListDialog (this);
203                 }
204
205                 _servers_list_dialog->Show ();
206         }
207
208         void help_about ()
209         {
210                 AboutDialog* d = new AboutDialog (this);
211                 d->ShowModal ();
212                 d->Destroy ();
213         }
214
215         void add_film ()
216         {
217                 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
218                 if (_last_parent) {
219                         c->SetPath (std_to_wx (_last_parent.get().string ()));
220                 }
221
222                 int r;
223                 while (true) {
224                         r = c->ShowModal ();
225                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
226                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
227                         } else {
228                                 break;
229                         }
230                 }
231
232                 if (r == wxID_OK) {
233                         start_job (wx_to_std (c->GetPath ()));
234                 }
235
236                 _last_parent = boost::filesystem::path (wx_to_std (c->GetPath ())).parent_path ();
237
238                 c->Destroy ();
239         }
240
241         boost::optional<boost::filesystem::path> _last_parent;
242         wxSizer* _sizer;
243         wxPreferencesEditor* _config_dialog;
244         ServersListDialog* _servers_list_dialog;
245 };
246
247 static const wxCmdLineEntryDesc command_line_description[] = {
248         { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
249         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
250 };
251
252 class JobServer : public Server
253 {
254 public:
255         explicit JobServer (DOMFrame* frame)
256                 : Server (BATCH_JOB_PORT)
257                 , _frame (frame)
258         {}
259
260         void handle (shared_ptr<Socket> socket)
261         {
262                 try {
263                         int const length = socket->read_uint32 ();
264                         scoped_array<char> buffer (new char[length]);
265                         socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
266                         string s (buffer.get());
267                         _frame->start_job (s);
268                         socket->write (reinterpret_cast<uint8_t const *> ("OK"), 3);
269                 } catch (...) {
270
271                 }
272         }
273
274 private:
275         DOMFrame* _frame;
276 };
277
278 class App : public wxApp
279 {
280         bool OnInit ()
281         {
282                 SetAppName (_("DCP-o-matic Batch Converter"));
283                 is_batch_converter = true;
284
285                 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
286                 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
287
288                 wxSplashScreen* splash = maybe_show_splash ();
289
290                 if (!wxApp::OnInit()) {
291                         return false;
292                 }
293
294 #ifdef DCPOMATIC_LINUX
295                 unsetenv ("UBUNTU_MENUPROXY");
296 #endif
297
298                 dcpomatic_setup_path_encoding ();
299
300                 /* Enable i18n; this will create a Config object
301                    to look for a force-configured language.  This Config
302                    object will be wrong, however, because dcpomatic_setup
303                    hasn't yet been called and there aren't any filters etc.
304                    set up yet.
305                 */
306                 dcpomatic_setup_i18n ();
307
308                 /* Set things up, including filters etc.
309                    which will now be internationalised correctly.
310                 */
311                 dcpomatic_setup ();
312
313                 /* Force the configuration to be re-loaded correctly next
314                    time it is needed.
315                 */
316                 Config::drop ();
317
318                 _frame = new DOMFrame (_("DCP-o-matic Batch Converter"));
319                 SetTopWindow (_frame);
320                 _frame->Maximize ();
321                 if (splash) {
322                         splash->Destroy ();
323                 }
324                 _frame->Show ();
325
326                 JobServer* server = new JobServer (_frame);
327                 new thread (boost::bind (&JobServer::run, server));
328
329                 signal_manager = new wxSignalManager (this);
330                 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
331
332                 shared_ptr<Film> film;
333                 BOOST_FOREACH (boost::filesystem::path i, films_to_load) {
334                         if (boost::filesystem::is_directory (i)) {
335                                 try {
336                                         film.reset (new Film (i));
337                                         film->read_metadata ();
338                                         film->make_dcp ();
339                                 } catch (exception& e) {
340                                         error_dialog (
341                                                 0,
342                                                 std_to_wx (String::compose (wx_to_std (_("Could not load film %1")), i.string())),
343                                                 std_to_wx(e.what())
344                                                 );
345                                 }
346                         }
347                 }
348
349                 return true;
350         }
351
352         void idle ()
353         {
354                 signal_manager->ui_idle ();
355         }
356
357         void OnInitCmdLine (wxCmdLineParser& parser)
358         {
359                 parser.SetDesc (command_line_description);
360                 parser.SetSwitchChars (wxT ("-"));
361         }
362
363         bool OnCmdLineParsed (wxCmdLineParser& parser)
364         {
365                 for (size_t i = 0; i < parser.GetParamCount(); ++i) {
366                         films_to_load.push_back (wx_to_std (parser.GetParam(i)));
367                 }
368
369                 return true;
370         }
371
372         void config_failed_to_load ()
373         {
374                 message_dialog (_frame, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
375         }
376
377         void config_warning (string m)
378         {
379                 message_dialog (_frame, std_to_wx (m));
380         }
381
382         DOMFrame* _frame;
383 };
384
385 IMPLEMENT_APP (App)