Basic ffmpeg film viewer.
[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                 
230                 film_editor->FileChanged.connect (bind (&Frame::file_changed, this, _1));
231                 if (film) {
232                         file_changed (film->directory ());
233                 } else {
234                         file_changed ("");
235                 }
236                 
237                 set_film ();
238         }
239
240         void set_film ()
241         {
242                 film_viewer->set_film (film);
243                 film_editor->set_film (film);
244                 set_menu_sensitivity ();
245         }
246
247         void file_changed (string f)
248         {
249                 stringstream s;
250                 s << "DVD-o-matic";
251                 if (!f.empty ()) {
252                         s << " - " << f;
253                 }
254                 
255                 SetTitle (std_to_wx (s.str()));
256         }
257         
258         void file_new (wxCommandEvent &)
259         {
260                 NewFilmDialog* d = new NewFilmDialog (this);
261                 int const r = d->ShowModal ();
262                 
263                 if (r == wxID_OK) {
264
265                         if (boost::filesystem::exists (d->get_path())) {
266                                 error_dialog (this, String::compose ("The directory %1 already exists.", d->get_path()));
267                                 return;
268                         }
269                         
270                         maybe_save_then_delete_film ();
271                         film.reset (new Film (d->get_path (), false));
272                         film->log()->set_level (log_level);
273 #if BOOST_FILESYSTEM_VERSION == 3               
274                         film->set_name (boost::filesystem::path (d->get_path()).filename().generic_string());
275 #else           
276                         film->set_name (boost::filesystem::path (d->get_path()).filename());
277 #endif
278                         set_film ();
279                 }
280                 
281                 d->Destroy ();
282         }
283
284         void file_open (wxCommandEvent &)
285         {
286                 wxDirDialog* c = new wxDirDialog (this, wxT ("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
287                 int const r = c->ShowModal ();
288                 
289                 if (r == wxID_OK) {
290                         maybe_save_then_delete_film ();
291                         film.reset (new Film (wx_to_std (c->GetPath ())));
292                         film->log()->set_level (log_level);
293                         set_film ();
294                 }
295
296                 c->Destroy ();
297         }
298
299         void file_save (wxCommandEvent &)
300         {
301                 film->write_metadata ();
302         }
303
304         void file_properties (wxCommandEvent &)
305         {
306                 PropertiesDialog* d = new PropertiesDialog (this, film);
307                 d->ShowModal ();
308                 d->Destroy ();
309         }
310         
311         void file_quit (wxCommandEvent &)
312         {
313                 maybe_save_then_delete_film ();
314                 Close (true);
315         }
316
317         void edit_preferences (wxCommandEvent &)
318         {
319                 ConfigDialog* d = new ConfigDialog (this);
320                 d->ShowModal ();
321                 d->Destroy ();
322                 Config::instance()->write ();
323         }
324
325         void jobs_make_dcp (wxCommandEvent &)
326         {
327                 JobWrapper::make_dcp (this, film, true);
328         }
329         
330         void jobs_make_dcp_from_existing_transcode (wxCommandEvent &)
331         {
332                 JobWrapper::make_dcp (this, film, false);
333         }
334         
335         void jobs_send_dcp_to_tms (wxCommandEvent &)
336         {
337                 film->send_dcp_to_tms ();
338         }
339         
340         void jobs_examine_content (wxCommandEvent &)
341         {
342                 film->examine_content ();
343         }
344         
345         void help_about (wxCommandEvent &)
346         {
347                 wxAboutDialogInfo info;
348                 info.SetName (_("DVD-o-matic"));
349                 info.SetVersion (std_to_wx (String::compose ("version %1 git %2", dvdomatic_version, dvdomatic_git_commit)));
350                 info.SetDescription (_("Free, open-source DCP generation from almost anything."));
351                 info.SetCopyright (_("(C) Carl Hetherington, Terrence Meiczinger, Paul Davis, Ole Laursen"));
352                 wxArrayString authors;
353                 authors.Add (wxT ("Carl Hetherington"));
354                 authors.Add (wxT ("Terrence Meiczinger"));
355                 authors.Add (wxT ("Paul Davis"));
356                 authors.Add (wxT ("Ole Laursen"));
357                 info.SetDevelopers (authors);
358                 info.SetWebSite (wxT ("http://carlh.net/software/dvdomatic"));
359                 wxAboutBox (info);
360         }
361 };
362
363 #if wxMINOR_VERSION == 9
364 static const wxCmdLineEntryDesc command_line_description[] = {
365         { wxCMD_LINE_OPTION, "l", "log", "set log level (silent, verbose or timing)", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
366         { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
367         { wxCMD_LINE_NONE }
368 };
369 #else
370 static const wxCmdLineEntryDesc command_line_description[] = {
371         { wxCMD_LINE_OPTION, wxT("l"), wxT("log"), wxT("set log level (silent, verbose or timing)"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
372         { wxCMD_LINE_PARAM, 0, 0, wxT("film to load"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
373         { wxCMD_LINE_NONE }
374 };
375 #endif
376
377 class App : public wxApp
378 {
379         bool OnInit ()
380         {
381                 if (!wxApp::OnInit()) {
382                         return false;
383                 }
384                 
385 #ifdef DVDOMATIC_POSIX          
386                 unsetenv ("UBUNTU_MENUPROXY");
387 #endif          
388                 
389                 wxInitAllImageHandlers ();
390                 
391                 dvdomatic_setup ();
392
393                 if (!film_to_load.empty() && boost::filesystem::is_directory (film_to_load)) {
394                         film.reset (new Film (film_to_load));
395                         film->log()->set_level (log_level);
396                 }
397
398                 Frame* f = new Frame (_("DVD-o-matic"));
399                 SetTopWindow (f);
400                 f->Maximize ();
401                 f->Show ();
402
403                 ui_signaller = new wxUISignaller (this);
404                 this->Connect (-1, wxEVT_IDLE, wxIdleEventHandler (App::idle));
405
406                 return true;
407         }
408
409         void OnInitCmdLine (wxCmdLineParser& parser)
410         {
411                 parser.SetDesc (command_line_description);
412                 parser.SetSwitchChars (wxT ("-"));
413         }
414
415         bool OnCmdLineParsed (wxCmdLineParser& parser)
416         {
417                 if (parser.GetParamCount() > 0) {
418                         film_to_load = wx_to_std (parser.GetParam(0));
419                 }
420
421                 wxString log;
422                 if (parser.Found(wxT("log"), &log)) {
423                         log_level = wx_to_std (log);
424                 }
425
426                 return true;
427         }
428
429         void idle (wxIdleEvent &)
430         {
431                 ui_signaller->ui_idle ();
432         }
433 };
434
435 IMPLEMENT_APP (App)