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