Merge.
[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/wx.h>
23 #include "lib/version.h"
24 #include "lib/compose.hpp"
25 #include "lib/config.h"
26 #include "lib/util.h"
27 #include "lib/film.h"
28 #include "lib/job_manager.h"
29 #include "wx/wx_util.h"
30 #include "wx/wx_ui_signaller.h"
31 #include "wx/job_manager_view.h"
32
33 using boost::shared_ptr;
34
35 enum {
36         ID_file_add_film = 1,
37         ID_file_quit,
38         ID_help_about
39 };
40
41 void
42 setup_menu (wxMenuBar* m)
43 {
44         wxMenu* file = new wxMenu;
45         file->Append (ID_file_add_film, _("&Add Film..."));
46         file->Append (ID_file_quit, _("&Quit"));
47
48         wxMenu* help = new wxMenu;
49         help->Append (ID_help_about, _("About"));
50
51         m->Append (file, _("&File"));
52         m->Append (help, _("&Help"));
53 }
54
55 class Frame : public wxFrame
56 {
57 public:
58         Frame (wxString const & title)
59                 : wxFrame (NULL, -1, title)
60         {
61                 wxMenuBar* bar = new wxMenuBar;
62                 setup_menu (bar);
63                 SetMenuBar (bar);
64
65                 Connect (ID_file_add_film, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_add_film));
66                 Connect (ID_file_quit, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_quit));
67                 Connect (ID_help_about, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::help_about));
68
69                 wxPanel* panel = new wxPanel (this);
70                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
71                 s->Add (panel, 1, wxEXPAND);
72                 SetSizer (s);
73
74                 wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
75
76                 JobManagerView* job_manager_view = new JobManagerView (panel, JobManagerView::PAUSE);
77                 sizer->Add (job_manager_view, 1, wxALL | wxEXPAND, 6);
78
79                 wxSizer* buttons = new wxBoxSizer (wxHORIZONTAL);
80                 wxButton* add = new wxButton (panel, wxID_ANY, _("Add Film..."));
81                 add->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (Frame::add_film));
82                 buttons->Add (add, 1, wxALL, 6);
83
84                 sizer->Add (buttons, 0, wxALL, 6);
85
86                 panel->SetSizer (sizer);
87
88                 Connect (wxID_ANY, wxEVT_CLOSE_WINDOW, wxCloseEventHandler (Frame::close));
89         }
90
91 private:
92         bool should_close ()
93         {
94                 if (!JobManager::instance()->work_to_do ()) {
95                         return true;
96                 }
97
98                 wxMessageDialog* d = new wxMessageDialog (
99                         0,
100                         _("There are unfinished jobs; are you sure you want to quit?"),
101                         _("Unfinished jobs"),
102                         wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
103                         );
104
105                 bool const r = d->ShowModal() == wxID_YES;
106                 d->Destroy ();
107                 return r;
108         }
109                 
110         void close (wxCloseEvent& ev)
111         {
112                 if (!should_close ()) {
113                         ev.Veto ();
114                         return;
115                 }
116
117                 ev.Skip ();
118         }
119
120         void file_add_film (wxCommandEvent& ev)
121         {
122                 add_film (ev);
123         }
124         
125         void file_quit (wxCommandEvent &)
126         {
127                 if (should_close ()) {
128                         Close (true);
129                 }
130         }
131
132         void help_about (wxCommandEvent &)
133         {
134                 wxAboutDialogInfo info;
135                 info.SetName (_("DCP-o-matic Batch Converter"));
136                 if (strcmp (dcpomatic_git_commit, "release") == 0) {
137                         info.SetVersion (std_to_wx (String::compose ("version %1", dcpomatic_version)));
138                 } else {
139                         info.SetVersion (std_to_wx (String::compose ("version %1 git %2", dcpomatic_version, dcpomatic_git_commit)));
140                 }
141                 info.SetDescription (_("Free, open-source DCP generation from almost anything."));
142                 info.SetCopyright (_("(C) 2012-2013 Carl Hetherington, Terrence Meiczinger, Paul Davis, Ole Laursen"));
143
144                 wxArrayString authors;
145                 authors.Add (wxT ("Carl Hetherington"));
146                 authors.Add (wxT ("Terrence Meiczinger"));
147                 authors.Add (wxT ("Paul Davis"));
148                 authors.Add (wxT ("Ole Laursen"));
149                 info.SetDevelopers (authors);
150
151                 wxArrayString translators;
152                 translators.Add (wxT ("Olivier Perriere"));
153                 translators.Add (wxT ("Lilian Lefranc"));
154                 translators.Add (wxT ("Thierry Journet"));
155                 translators.Add (wxT ("Massimiliano Broggi"));
156                 translators.Add (wxT ("Manuel AC"));
157                 translators.Add (wxT ("Adam Klotblixt"));
158                 info.SetTranslators (translators);
159                 
160                 info.SetWebSite (wxT ("http://carlh.net/software/dcpomatic"));
161                 wxAboutBox (info);
162         }
163
164         void add_film (wxCommandEvent &)
165         {
166                 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
167                 int r;
168                 while (1) {
169                         r = c->ShowModal ();
170                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
171                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
172                         } else {
173                                 break;
174                         }
175                 }
176                         
177                 if (r == wxID_OK) {
178                         try {
179                                 shared_ptr<Film> film (new Film (wx_to_std (c->GetPath ())));
180                                 film->read_metadata ();
181                                 film->make_dcp ();
182                         } catch (std::exception& e) {
183                                 wxString p = c->GetPath ();
184                                 wxCharBuffer b = p.ToUTF8 ();
185                                 error_dialog (this, wxString::Format (_("Could not open film at %s (%s)"), p.data(), std_to_wx (e.what()).data()));
186                         }
187                 }
188
189                 c->Destroy ();
190         }
191 };
192
193 class App : public wxApp
194 {
195         bool OnInit ()
196         {
197                 if (!wxApp::OnInit()) {
198                         return false;
199                 }
200                 
201 #ifdef DCPOMATIC_POSIX          
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->Connect (-1, wxEVT_IDLE, wxIdleEventHandler (App::idle));
230
231                 return true;
232         }
233
234         void idle (wxIdleEvent &)
235         {
236                 ui_signaller->ui_idle ();
237         }
238 };
239
240 IMPLEMENT_APP (App)