Merge branch 'master' of ssh://carlh.dyndns.org/home/carl/git/dcpomatic
[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                 int r;
172                 while (1) {
173                         r = c->ShowModal ();
174                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
175                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
176                         } else {
177                                 break;
178                         }
179                 }
180                         
181                 if (r == wxID_OK) {
182                         try {
183                                 shared_ptr<Film> film (new Film (wx_to_std (c->GetPath ())));
184                                 film->read_metadata ();
185                                 film->make_dcp ();
186                         } catch (std::exception& e) {
187                                 wxString p = c->GetPath ();
188                                 wxCharBuffer b = p.ToUTF8 ();
189                                 error_dialog (this, wxString::Format (_("Could not open film at %s (%s)"), p.data(), std_to_wx (e.what()).data()));
190                         }
191                 }
192
193                 c->Destroy ();
194         }
195 };
196
197 static const wxCmdLineEntryDesc command_line_description[] = {
198         { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
199         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
200 };
201
202 class App : public wxApp
203 {
204         bool OnInit ()
205         {
206                 if (!wxApp::OnInit()) {
207                         return false;
208                 }
209                 
210 #ifdef DCPOMATIC_LINUX          
211                 unsetenv ("UBUNTU_MENUPROXY");
212 #endif          
213
214                 /* Enable i18n; this will create a Config object
215                    to look for a force-configured language.  This Config
216                    object will be wrong, however, because dcpomatic_setup
217                    hasn't yet been called and there aren't any scalers, filters etc.
218                    set up yet.
219                 */
220                 dcpomatic_setup_i18n ();
221
222                 /* Set things up, including scalers / filters etc.
223                    which will now be internationalised correctly.
224                 */
225                 dcpomatic_setup ();
226
227                 /* Force the configuration to be re-loaded correctly next
228                    time it is needed.
229                 */
230                 Config::drop ();
231
232                 Frame* f = new Frame (_("DCP-o-matic Batch Converter"));
233                 SetTopWindow (f);
234                 f->Maximize ();
235                 f->Show ();
236
237                 ui_signaller = new wxUISignaller (this);
238                 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
239
240                 shared_ptr<Film> film;
241                 if (!film_to_load.empty() && boost::filesystem::is_directory (film_to_load)) {
242                         try {
243                                 film.reset (new Film (film_to_load));
244                                 film->read_metadata ();
245                                 film->make_dcp ();
246                         } catch (exception& e) {
247                                 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load film %1 (%2)")), film_to_load, e.what())));
248                         }
249                 }
250
251                 return true;
252         }
253
254         void idle ()
255         {
256                 ui_signaller->ui_idle ();
257         }
258
259         void OnInitCmdLine (wxCmdLineParser& parser)
260         {
261                 parser.SetDesc (command_line_description);
262                 parser.SetSwitchChars (wxT ("-"));
263         }
264
265         bool OnCmdLineParsed (wxCmdLineParser& parser)
266         {
267                 if (parser.GetParamCount() > 0) {
268                         film_to_load = wx_to_std (parser.GetParam(0));
269                 }
270
271                 return true;
272         }
273 };
274
275 IMPLEMENT_APP (App)