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