Merge branch 'master' into speed-up
[dcpomatic.git] / src / tools / dvdomatic.cc
1 /*
2     Copyright (C) 2012 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 <iostream>
21 #include <boost/filesystem.hpp>
22 #include <wx/aboutdlg.h>
23 #include <wx/stdpaths.h>
24 #include <wx/cmdline.h>
25 #include "wx/film_viewer.h"
26 #include "wx/film_editor.h"
27 #include "wx/job_manager_view.h"
28 #include "wx/config_dialog.h"
29 #include "wx/job_wrapper.h"
30 #include "wx/wx_util.h"
31 #include "wx/new_film_dialog.h"
32 #include "wx/properties_dialog.h"
33 #include "wx/wx_ui_signaller.h"
34 #include "lib/film.h"
35 #include "lib/format.h"
36 #include "lib/config.h"
37 #include "lib/filter.h"
38 #include "lib/util.h"
39 #include "lib/scaler.h"
40 #include "lib/exceptions.h"
41 #include "lib/version.h"
42 #include "lib/ui_signaller.h"
43 #include "lib/log.h"
44
45 using std::cout;
46 using std::string;
47 using std::stringstream;
48 using std::map;
49 using std::make_pair;
50 using boost::shared_ptr;
51
52 static FilmEditor* film_editor = 0;
53 static FilmViewer* film_viewer = 0;
54 static shared_ptr<Film> film;
55 static std::string log_level;
56 static std::string film_to_load;
57
58 static void set_menu_sensitivity ();
59
60 class FilmChangedDialog
61 {
62 public:
63         FilmChangedDialog ()
64         {
65                 stringstream s;
66                 s << "Save changes to film \"" << film->name() << "\" before closing?";
67                 _dialog = new wxMessageDialog (0, std_to_wx (s.str()), wxT ("Film changed"), wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION);
68         }
69
70         ~FilmChangedDialog ()
71         {
72                 _dialog->Destroy ();
73         }
74
75         int run ()
76         {
77                 return _dialog->ShowModal ();
78         }
79
80 private:        
81         wxMessageDialog* _dialog;
82 };
83
84
85 void
86 maybe_save_then_delete_film ()
87 {
88         if (!film) {
89                 return;
90         }
91                         
92         if (film->dirty ()) {
93                 FilmChangedDialog d;
94                 switch (d.run ()) {
95                 case wxID_NO:
96                         break;
97                 case wxID_YES:
98                         film->write_metadata ();
99                         break;
100                 }
101         }
102         
103         film.reset ();
104 }
105
106 enum Sensitivity {
107         ALWAYS,
108         NEEDS_FILM
109 };
110
111 map<wxMenuItem*, Sensitivity> menu_items;
112         
113 void
114 add_item (wxMenu* menu, std::string text, int id, Sensitivity sens)
115 {
116         wxMenuItem* item = menu->Append (id, std_to_wx (text));
117         menu_items.insert (make_pair (item, sens));
118 }
119
120 void
121 set_menu_sensitivity ()
122 {
123         for (map<wxMenuItem*, Sensitivity>::iterator i = menu_items.begin(); i != menu_items.end(); ++i) {
124                 if (i->second == NEEDS_FILM) {
125                         i->first->Enable (film != 0);
126                 } else {
127                         i->first->Enable (true);
128                 }
129         }
130 }
131
132 enum {
133         ID_file_new = 1,
134         ID_file_open,
135         ID_file_save,
136         ID_file_properties,
137         ID_file_quit,
138         ID_edit_preferences,
139         ID_jobs_make_dcp,
140         ID_jobs_send_dcp_to_tms,
141         ID_jobs_examine_content,
142         ID_jobs_make_dcp_from_existing_transcode,
143         ID_help_about
144 };
145
146 void
147 setup_menu (wxMenuBar* m)
148 {
149         wxMenu* file = new wxMenu;
150         add_item (file, "New...", ID_file_new, ALWAYS);
151         add_item (file, "&Open...", ID_file_open, ALWAYS);
152         file->AppendSeparator ();
153         add_item (file, "&Save", ID_file_save, NEEDS_FILM);
154         file->AppendSeparator ();
155         add_item (file, "&Properties...", ID_file_properties, NEEDS_FILM);
156         file->AppendSeparator ();
157         add_item (file, "&Quit", ID_file_quit, ALWAYS);
158
159         wxMenu* edit = new wxMenu;
160         add_item (edit, "&Preferences...", ID_edit_preferences, ALWAYS);
161
162         wxMenu* jobs = new wxMenu;
163         add_item (jobs, "&Make DCP", ID_jobs_make_dcp, NEEDS_FILM);
164         add_item (jobs, "&Send DCP to TMS", ID_jobs_send_dcp_to_tms, NEEDS_FILM);
165         jobs->AppendSeparator ();
166         add_item (jobs, "&Examine content", ID_jobs_examine_content, NEEDS_FILM);
167         add_item (jobs, "Make DCP from existing &transcode", ID_jobs_make_dcp_from_existing_transcode, NEEDS_FILM);
168
169         wxMenu* help = new wxMenu;
170         add_item (help, "About", ID_help_about, ALWAYS);
171
172         m->Append (file, _("&File"));
173         m->Append (edit, _("&Edit"));
174         m->Append (jobs, _("&Jobs"));
175         m->Append (help, _("&Help"));
176 }
177
178 bool
179 window_closed (wxCommandEvent &)
180 {
181         maybe_save_then_delete_film ();
182         return false;
183 }
184
185 class Frame : public wxFrame
186 {
187 public:
188         Frame (wxString const & title)
189                 : wxFrame (NULL, -1, title)
190         {
191                 wxMenuBar* bar = new wxMenuBar;
192                 setup_menu (bar);
193                 SetMenuBar (bar);
194
195                 Connect (ID_file_new, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_new));
196                 Connect (ID_file_open, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_open));
197                 Connect (ID_file_save, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_save));
198                 Connect (ID_file_properties, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_properties));
199                 Connect (ID_file_quit, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_quit));
200                 Connect (ID_edit_preferences, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::edit_preferences));
201                 Connect (ID_jobs_make_dcp, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_make_dcp));
202                 Connect (ID_jobs_send_dcp_to_tms, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_send_dcp_to_tms));
203                 Connect (ID_jobs_examine_content, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_examine_content));
204                 Connect (ID_jobs_make_dcp_from_existing_transcode, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_make_dcp_from_existing_transcode));
205                 Connect (ID_help_about, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::help_about));
206
207                 wxPanel* panel = new wxPanel (this);
208                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
209                 s->Add (panel, 1, wxEXPAND);
210                 SetSizer (s);
211
212                 film_editor = new FilmEditor (film, panel);
213                 film_viewer = new FilmViewer (film, panel);
214                 JobManagerView* job_manager_view = new JobManagerView (panel);
215
216                 wxSizer* rhs_sizer = new wxBoxSizer (wxVERTICAL);
217                 rhs_sizer->Add (film_viewer, 3, wxEXPAND | wxALL);
218                 rhs_sizer->Add (job_manager_view, 1, wxEXPAND | wxALL);
219
220                 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
221                 main_sizer->Add (film_editor, 0, wxALL, 6);
222                 main_sizer->Add (rhs_sizer, 1, wxEXPAND | wxALL, 6);
223                 panel->SetSizer (main_sizer);
224
225                 set_menu_sensitivity ();
226
227                 /* XXX: calling these here is a bit of a hack */
228                 film_editor->setup_visibility ();
229                 film_viewer->setup_visibility ();
230                 
231                 film_editor->FileChanged.connect (bind (&Frame::file_changed, this, _1));
232                 if (film) {
233                         file_changed (film->directory ());
234                 } else {
235                         file_changed ("");
236                 }
237                 
238                 set_film ();
239         }
240
241         void set_film ()
242         {
243                 film_viewer->set_film (film);
244                 film_editor->set_film (film);
245                 set_menu_sensitivity ();
246         }
247
248         void file_changed (string f)
249         {
250                 stringstream s;
251                 s << "DVD-o-matic";
252                 if (!f.empty ()) {
253                         s << " - " << f;
254                 }
255                 
256                 SetTitle (std_to_wx (s.str()));
257         }
258         
259         void file_new (wxCommandEvent &)
260         {
261                 NewFilmDialog* d = new NewFilmDialog (this);
262                 int const r = d->ShowModal ();
263                 
264                 if (r == wxID_OK) {
265
266                         if (boost::filesystem::exists (d->get_path())) {
267                                 error_dialog (this, String::compose ("The directory %1 already exists.", d->get_path()));
268                                 return;
269                         }
270                         
271                         maybe_save_then_delete_film ();
272                         film.reset (new Film (d->get_path (), false));
273                         film->log()->set_level (log_level);
274 #if BOOST_FILESYSTEM_VERSION == 3               
275                         film->set_name (boost::filesystem::path (d->get_path()).filename().generic_string());
276 #else           
277                         film->set_name (boost::filesystem::path (d->get_path()).filename());
278 #endif
279                         set_film ();
280                 }
281                 
282                 d->Destroy ();
283         }
284
285         void file_open (wxCommandEvent &)
286         {
287                 wxDirDialog* c = new wxDirDialog (this, wxT ("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
288                 int const r = c->ShowModal ();
289                 
290                 if (r == wxID_OK) {
291                         maybe_save_then_delete_film ();
292                         film.reset (new Film (wx_to_std (c->GetPath ())));
293                         film->log()->set_level (log_level);
294                         set_film ();
295                 }
296
297                 c->Destroy ();
298         }
299
300         void file_save (wxCommandEvent &)
301         {
302                 film->write_metadata ();
303         }
304
305         void file_properties (wxCommandEvent &)
306         {
307                 PropertiesDialog* d = new PropertiesDialog (this, film);
308                 d->ShowModal ();
309                 d->Destroy ();
310         }
311         
312         void file_quit (wxCommandEvent &)
313         {
314                 maybe_save_then_delete_film ();
315                 Close (true);
316         }
317
318         void edit_preferences (wxCommandEvent &)
319         {
320                 ConfigDialog* d = new ConfigDialog (this);
321                 d->ShowModal ();
322                 d->Destroy ();
323                 Config::instance()->write ();
324         }
325
326         void jobs_make_dcp (wxCommandEvent &)
327         {
328                 JobWrapper::make_dcp (this, film, true);
329         }
330         
331         void jobs_make_dcp_from_existing_transcode (wxCommandEvent &)
332         {
333                 JobWrapper::make_dcp (this, film, false);
334         }
335         
336         void jobs_send_dcp_to_tms (wxCommandEvent &)
337         {
338                 film->send_dcp_to_tms ();
339         }
340         
341         void jobs_examine_content (wxCommandEvent &)
342         {
343                 film->examine_content ();
344         }
345         
346         void help_about (wxCommandEvent &)
347         {
348                 wxAboutDialogInfo info;
349                 info.SetName (_("DVD-o-matic"));
350                 info.SetVersion (std_to_wx (String::compose ("version %1 git %2", dvdomatic_version, dvdomatic_git_commit)));
351                 info.SetDescription (_("Free, open-source DCP generation from almost anything."));
352                 info.SetCopyright (_("(C) Carl Hetherington, Terrence Meiczinger, Paul Davis, Ole Laursen"));
353                 wxArrayString authors;
354                 authors.Add (wxT ("Carl Hetherington"));
355                 authors.Add (wxT ("Terrence Meiczinger"));
356                 authors.Add (wxT ("Paul Davis"));
357                 authors.Add (wxT ("Ole Laursen"));
358                 info.SetDevelopers (authors);
359                 info.SetWebSite (wxT ("http://carlh.net/software/dvdomatic"));
360                 wxAboutBox (info);
361         }
362 };
363
364 #if wxMINOR_VERSION == 9
365 static const wxCmdLineEntryDesc command_line_description[] = {
366         { wxCMD_LINE_OPTION, "l", "log", "set log level (silent, verbose or timing)", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
367         { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
368         { wxCMD_LINE_NONE }
369 };
370 #else
371 static const wxCmdLineEntryDesc command_line_description[] = {
372         { wxCMD_LINE_OPTION, wxT("l"), wxT("log"), wxT("set log level (silent, verbose or timing)"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
373         { wxCMD_LINE_PARAM, 0, 0, wxT("film to load"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
374         { wxCMD_LINE_NONE }
375 };
376 #endif
377
378 class App : public wxApp
379 {
380         bool OnInit ()
381         {
382                 if (!wxApp::OnInit()) {
383                         return false;
384                 }
385                 
386 #ifdef DVDOMATIC_POSIX          
387                 unsetenv ("UBUNTU_MENUPROXY");
388 #endif          
389                 
390                 wxInitAllImageHandlers ();
391                 
392                 dvdomatic_setup ();
393
394                 if (!film_to_load.empty() && boost::filesystem::is_directory (film_to_load)) {
395                         film.reset (new Film (film_to_load));
396                         film->log()->set_level (log_level);
397                 }
398
399                 Frame* f = new Frame (_("DVD-o-matic"));
400                 SetTopWindow (f);
401                 f->Maximize ();
402                 f->Show ();
403
404                 ui_signaller = new wxUISignaller (this);
405                 this->Connect (-1, wxEVT_IDLE, wxIdleEventHandler (App::idle));
406
407                 return true;
408         }
409
410         void OnInitCmdLine (wxCmdLineParser& parser)
411         {
412                 parser.SetDesc (command_line_description);
413                 parser.SetSwitchChars (wxT ("-"));
414         }
415
416         bool OnCmdLineParsed (wxCmdLineParser& parser)
417         {
418                 if (parser.GetParamCount() > 0) {
419                         film_to_load = wx_to_std (parser.GetParam(0));
420                 }
421
422                 wxString log;
423                 if (parser.Found(wxT("log"), &log)) {
424                         log_level = wx_to_std (log);
425                 }
426
427                 return true;
428         }
429
430         void idle (wxIdleEvent &)
431         {
432                 ui_signaller->ui_idle ();
433         }
434 };
435
436 IMPLEMENT_APP (App)