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