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