Fix job pausing to actually work, and always add Pause buttons to JobManagerViews...
[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                 , _sizer (new wxBoxSizer (wxVERTICAL))
66         {
67                 wxMenuBar* bar = new wxMenuBar;
68                 setup_menu (bar);
69                 SetMenuBar (bar);
70
71                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::file_add_film, this), ID_file_add_film);
72                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::file_quit, this),     ID_file_quit);
73                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::help_about, this),    ID_help_about);
74
75                 wxPanel* panel = new wxPanel (this);
76                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
77                 s->Add (panel, 1, wxEXPAND);
78                 SetSizer (s);
79
80                 JobManagerView* job_manager_view = new JobManagerView (panel);
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                 Bind (wxEVT_SIZE, boost::bind (&Frame::sized, this, _1));
94         }
95
96 private:
97         void sized (wxSizeEvent& ev)
98         {
99                 _sizer->Layout ();
100                 ev.Skip ();
101         }
102         
103         bool should_close ()
104         {
105                 if (!JobManager::instance()->work_to_do ()) {
106                         return true;
107                 }
108
109                 wxMessageDialog* d = new wxMessageDialog (
110                         0,
111                         _("There are unfinished jobs; are you sure you want to quit?"),
112                         _("Unfinished jobs"),
113                         wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
114                         );
115
116                 bool const r = d->ShowModal() == wxID_YES;
117                 d->Destroy ();
118                 return r;
119         }
120                 
121         void close (wxCloseEvent& ev)
122         {
123                 if (!should_close ()) {
124                         ev.Veto ();
125                         return;
126                 }
127
128                 ev.Skip ();
129         }
130
131         void file_add_film ()
132         {
133                 add_film ();
134         }
135         
136         void file_quit ()
137         {
138                 if (should_close ()) {
139                         Close (true);
140                 }
141         }
142
143         void help_about ()
144         {
145                 AboutDialog* d = new AboutDialog (this);
146                 d->ShowModal ();
147                 d->Destroy ();
148         }
149
150         void add_film ()
151         {
152                 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
153                 if (_last_parent) {
154                         c->SetPath (std_to_wx (_last_parent.get().string ()));
155                 }
156                 
157                 int r;
158                 while (true) {
159                         r = c->ShowModal ();
160                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
161                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
162                         } else {
163                                 break;
164                         }
165                 }
166                         
167                 if (r == wxID_OK) {
168                         try {
169                                 shared_ptr<Film> film (new Film (wx_to_std (c->GetPath ())));
170                                 film->read_metadata ();
171                                 film->make_dcp ();
172                         } catch (std::exception& e) {
173                                 wxString p = c->GetPath ();
174                                 wxCharBuffer b = p.ToUTF8 ();
175                                 error_dialog (this, wxString::Format (_("Could not open film at %s (%s)"), p.data(), std_to_wx (e.what()).data()));
176                         }
177                 }
178
179                 _last_parent = boost::filesystem::path (wx_to_std (c->GetPath ())).parent_path ();
180
181                 c->Destroy ();
182         }
183
184         boost::optional<boost::filesystem::path> _last_parent;
185         wxSizer* _sizer;
186 };
187
188 static const wxCmdLineEntryDesc command_line_description[] = {
189         { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
190         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
191 };
192
193 class App : public wxApp
194 {
195         bool OnInit ()
196         {
197                 if (!wxApp::OnInit()) {
198                         return false;
199                 }
200                 
201 #ifdef DCPOMATIC_LINUX          
202                 unsetenv ("UBUNTU_MENUPROXY");
203 #endif          
204
205                 /* Enable i18n; this will create a Config object
206                    to look for a force-configured language.  This Config
207                    object will be wrong, however, because dcpomatic_setup
208                    hasn't yet been called and there aren't any scalers, filters etc.
209                    set up yet.
210                 */
211                 dcpomatic_setup_i18n ();
212
213                 /* Set things up, including scalers / filters etc.
214                    which will now be internationalised correctly.
215                 */
216                 dcpomatic_setup ();
217
218                 /* Force the configuration to be re-loaded correctly next
219                    time it is needed.
220                 */
221                 Config::drop ();
222
223                 Frame* f = new Frame (_("DCP-o-matic Batch Converter"));
224                 SetTopWindow (f);
225                 f->Maximize ();
226                 f->Show ();
227
228                 ui_signaller = new wxUISignaller (this);
229                 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
230
231                 shared_ptr<Film> film;
232                 if (!film_to_load.empty() && boost::filesystem::is_directory (film_to_load)) {
233                         try {
234                                 film.reset (new Film (film_to_load));
235                                 film->read_metadata ();
236                                 film->make_dcp ();
237                         } catch (exception& e) {
238                                 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load film %1 (%2)")), film_to_load, e.what())));
239                         }
240                 }
241
242                 return true;
243         }
244
245         void idle ()
246         {
247                 ui_signaller->ui_idle ();
248         }
249
250         void OnInitCmdLine (wxCmdLineParser& parser)
251         {
252                 parser.SetDesc (command_line_description);
253                 parser.SetSwitchChars (wxT ("-"));
254         }
255
256         bool OnCmdLineParsed (wxCmdLineParser& parser)
257         {
258                 if (parser.GetParamCount() > 0) {
259                         film_to_load = wx_to_std (parser.GetParam(0));
260                 }
261
262                 return true;
263         }
264 };
265
266 IMPLEMENT_APP (App)