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