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