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