Merge branch 'master' into 2.0--libdcp-1.0
[dcpomatic.git] / src / tools / dcpomatic_batch.cc
1 /*
2     Copyright (C) 2013 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/aboutdlg.h>
21 #include <wx/stdpaths.h>
22 #include <wx/cmdline.h>
23 #include <wx/wx.h>
24 #include "lib/version.h"
25 #include "lib/compose.hpp"
26 #include "lib/config.h"
27 #include "lib/util.h"
28 #include "lib/film.h"
29 #include "lib/job_manager.h"
30 #include "wx/wx_util.h"
31 #include "wx/wx_ui_signaller.h"
32 #include "wx/job_manager_view.h"
33
34 using std::exception;
35 using boost::shared_ptr;
36
37 static std::string film_to_load;
38
39 enum {
40         ID_file_add_film = 1,
41         ID_file_quit,
42         ID_help_about
43 };
44
45 void
46 setup_menu (wxMenuBar* m)
47 {
48         wxMenu* file = new wxMenu;
49         file->Append (ID_file_add_film, _("&Add Film..."));
50         file->Append (ID_file_quit, _("&Quit"));
51
52         wxMenu* help = new wxMenu;
53         help->Append (ID_help_about, _("About"));
54
55         m->Append (file, _("&File"));
56         m->Append (help, _("&Help"));
57 }
58
59 class Frame : public wxFrame
60 {
61 public:
62         Frame (wxString const & title)
63                 : wxFrame (NULL, -1, title)
64         {
65                 wxMenuBar* bar = new wxMenuBar;
66                 setup_menu (bar);
67                 SetMenuBar (bar);
68
69                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::file_add_film, this), ID_file_add_film);
70                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::file_quit, this),     ID_file_quit);
71                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::help_about, this),    ID_help_about);
72
73                 wxPanel* panel = new wxPanel (this);
74                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
75                 s->Add (panel, 1, wxEXPAND);
76                 SetSizer (s);
77
78                 wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
79
80                 JobManagerView* job_manager_view = new JobManagerView (panel, JobManagerView::PAUSE);
81                 sizer->Add (job_manager_view, 1, wxALL | wxEXPAND, 6);
82
83                 wxSizer* buttons = new wxBoxSizer (wxHORIZONTAL);
84                 wxButton* add = new wxButton (panel, wxID_ANY, _("Add Film..."));
85                 add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&Frame::add_film, this));
86                 buttons->Add (add, 1, wxALL, 6);
87
88                 sizer->Add (buttons, 0, wxALL, 6);
89
90                 panel->SetSizer (sizer);
91
92                 Bind (wxEVT_CLOSE_WINDOW, boost::bind (&Frame::close, this, _1));
93         }
94
95 private:
96         bool should_close ()
97         {
98                 if (!JobManager::instance()->work_to_do ()) {
99                         return true;
100                 }
101
102                 wxMessageDialog* d = new wxMessageDialog (
103                         0,
104                         _("There are unfinished jobs; are you sure you want to quit?"),
105                         _("Unfinished jobs"),
106                         wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
107                         );
108
109                 bool const r = d->ShowModal() == wxID_YES;
110                 d->Destroy ();
111                 return r;
112         }
113                 
114         void close (wxCloseEvent& ev)
115         {
116                 if (!should_close ()) {
117                         ev.Veto ();
118                         return;
119                 }
120
121                 ev.Skip ();
122         }
123
124         void file_add_film ()
125         {
126                 add_film ();
127         }
128         
129         void file_quit ()
130         {
131                 if (should_close ()) {
132                         Close (true);
133                 }
134         }
135
136         void help_about ()
137         {
138                 wxAboutDialogInfo info;
139                 info.SetName (_("DCP-o-matic Batch Converter"));
140                 if (strcmp (dcpomatic_git_commit, "release") == 0) {
141                         info.SetVersion (std_to_wx (String::compose ("version %1", dcpomatic_version)));
142                 } else {
143                         info.SetVersion (std_to_wx (String::compose ("version %1 git %2", dcpomatic_version, dcpomatic_git_commit)));
144                 }
145                 info.SetDescription (_("Free, open-source DCP generation from almost anything."));
146                 info.SetCopyright (_("(C) 2012-2013 Carl Hetherington, Terrence Meiczinger, Paul Davis, Ole Laursen"));
147
148                 wxArrayString authors;
149                 authors.Add (wxT ("Carl Hetherington"));
150                 authors.Add (wxT ("Terrence Meiczinger"));
151                 authors.Add (wxT ("Paul Davis"));
152                 authors.Add (wxT ("Ole Laursen"));
153                 info.SetDevelopers (authors);
154
155                 wxArrayString translators;
156                 translators.Add (wxT ("Olivier Perriere"));
157                 translators.Add (wxT ("Lilian Lefranc"));
158                 translators.Add (wxT ("Thierry Journet"));
159                 translators.Add (wxT ("Massimiliano Broggi"));
160                 translators.Add (wxT ("Manuel AC"));
161                 translators.Add (wxT ("Adam Klotblixt"));
162                 info.SetTranslators (translators);
163                 
164                 info.SetWebSite (wxT ("http://carlh.net/software/dcpomatic"));
165                 wxAboutBox (info);
166         }
167
168         void add_film ()
169         {
170                 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
171                 if (_last_parent) {
172                         c->SetPath (std_to_wx (_last_parent.get().string ()));
173                 }
174                 
175                 int r;
176                 while (1) {
177                         r = c->ShowModal ();
178                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
179                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
180                         } else {
181                                 break;
182                         }
183                 }
184                         
185                 if (r == wxID_OK) {
186                         try {
187                                 shared_ptr<Film> film (new Film (wx_to_std (c->GetPath ())));
188                                 film->read_metadata ();
189                                 film->make_dcp ();
190                         } catch (std::exception& e) {
191                                 wxString p = c->GetPath ();
192                                 wxCharBuffer b = p.ToUTF8 ();
193                                 error_dialog (this, wxString::Format (_("Could not open film at %s (%s)"), p.data(), std_to_wx (e.what()).data()));
194                         }
195                 }
196
197                 _last_parent = boost::filesystem::path (wx_to_std (c->GetPath ())).parent_path ();
198
199                 c->Destroy ();
200         }
201
202         boost::optional<boost::filesystem::path> _last_parent;
203 };
204
205 static const wxCmdLineEntryDesc command_line_description[] = {
206         { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
207         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
208 };
209
210 class App : public wxApp
211 {
212         bool OnInit ()
213         {
214                 if (!wxApp::OnInit()) {
215                         return false;
216                 }
217                 
218 #ifdef DCPOMATIC_LINUX          
219                 unsetenv ("UBUNTU_MENUPROXY");
220 #endif          
221
222                 /* Enable i18n; this will create a Config object
223                    to look for a force-configured language.  This Config
224                    object will be wrong, however, because dcpomatic_setup
225                    hasn't yet been called and there aren't any scalers, filters etc.
226                    set up yet.
227                 */
228                 dcpomatic_setup_i18n ();
229
230                 /* Set things up, including scalers / filters etc.
231                    which will now be internationalised correctly.
232                 */
233                 dcpomatic_setup ();
234
235                 /* Force the configuration to be re-loaded correctly next
236                    time it is needed.
237                 */
238                 Config::drop ();
239
240                 Frame* f = new Frame (_("DCP-o-matic Batch Converter"));
241                 SetTopWindow (f);
242                 f->Maximize ();
243                 f->Show ();
244
245                 ui_signaller = new wxUISignaller (this);
246                 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
247
248                 shared_ptr<Film> film;
249                 if (!film_to_load.empty() && boost::filesystem::is_directory (film_to_load)) {
250                         try {
251                                 film.reset (new Film (film_to_load));
252                                 film->read_metadata ();
253                                 film->make_dcp ();
254                         } catch (exception& e) {
255                                 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load film %1 (%2)")), film_to_load, e.what())));
256                         }
257                 }
258
259                 return true;
260         }
261
262         void idle ()
263         {
264                 ui_signaller->ui_idle ();
265         }
266
267         void OnInitCmdLine (wxCmdLineParser& parser)
268         {
269                 parser.SetDesc (command_line_description);
270                 parser.SetSwitchChars (wxT ("-"));
271         }
272
273         bool OnCmdLineParsed (wxCmdLineParser& parser)
274         {
275                 if (parser.GetParamCount() > 0) {
276                         film_to_load = wx_to_std (parser.GetParam(0));
277                 }
278
279                 return true;
280         }
281 };
282
283 IMPLEMENT_APP (App)