535e6d74925d058b8fedb8b08ee10229cd079449
[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 "gtk/dvd_title_dialog.h"
31 #include "wx/wx_util.h"
32 #include "wx/new_film_dialog.h"
33 #include "wx/properties_dialog.h"
34 #include "wx/wx_ui_signaller.h"
35 #include "lib/film.h"
36 #include "lib/format.h"
37 #include "lib/config.h"
38 #include "lib/filter.h"
39 #include "lib/util.h"
40 #include "lib/scaler.h"
41 #include "lib/exceptions.h"
42 #include "lib/version.h"
43 #include "lib/ui_signaller.h"
44
45 using std::string;
46 using std::stringstream;
47 using std::map;
48 using std::make_pair;
49 using boost::shared_ptr;
50
51 static FilmEditor* film_editor = 0;
52 static FilmViewer* film_viewer = 0;
53
54 static shared_ptr<Film> film;
55
56 static void set_menu_sensitivity ();
57
58 class FilmChangedDialog
59 {
60 public:
61         FilmChangedDialog ()
62         {
63                 stringstream s;
64                 s << "Save changes to film \"" << film->name() << "\" before closing?";
65                 _dialog = new wxMessageDialog (0, std_to_wx (s.str()), wxT ("Film changed"), wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION);
66         }
67
68         ~FilmChangedDialog ()
69         {
70                 _dialog->Destroy ();
71         }
72
73         int run ()
74         {
75                 return _dialog->ShowModal ();
76         }
77
78 private:        
79         wxMessageDialog* _dialog;
80 };
81
82
83 void
84 maybe_save_then_delete_film ()
85 {
86         if (!film) {
87                 return;
88         }
89                         
90         if (film->dirty ()) {
91                 FilmChangedDialog d;
92                 switch (d.run ()) {
93                 case wxID_NO:
94                         break;
95                 case wxID_YES:
96                         film->write_metadata ();
97                         break;
98                 }
99         }
100         
101         film.reset ();
102 }
103
104 enum Sensitivity {
105         ALWAYS,
106         NEEDS_FILM
107 };
108
109 map<wxMenuItem*, Sensitivity> menu_items;
110         
111 void
112 add_item (wxMenu* menu, std::string text, int id, Sensitivity sens)
113 {
114         wxMenuItem* item = menu->Append (id, std_to_wx (text));
115         menu_items.insert (make_pair (item, sens));
116 }
117
118 void
119 set_menu_sensitivity ()
120 {
121         for (map<wxMenuItem*, Sensitivity>::iterator i = menu_items.begin(); i != menu_items.end(); ++i) {
122                 if (i->second == NEEDS_FILM) {
123                         i->first->Enable (film != 0);
124                 } else {
125                         i->first->Enable (true);
126                 }
127         }
128 }
129
130 enum {
131         ID_file_new = 1,
132         ID_file_open,
133         ID_file_save,
134         ID_file_properties,
135         ID_file_quit,
136         ID_edit_preferences,
137         ID_jobs_make_dcp,
138         ID_jobs_send_dcp_to_tms,
139         ID_jobs_copy_from_dvd,
140         ID_jobs_examine_content,
141         ID_jobs_make_dcp_from_existing_transcode,
142         ID_help_about
143 };
144
145 void
146 setup_menu (wxMenuBar* m)
147 {
148         wxMenu* file = new wxMenu;
149         add_item (file, "New...", ID_file_new, ALWAYS);
150         add_item (file, "&Open...", ID_file_open, ALWAYS);
151         file->AppendSeparator ();
152         add_item (file, "&Save", ID_file_save, NEEDS_FILM);
153         file->AppendSeparator ();
154         add_item (file, "&Properties...", ID_file_properties, NEEDS_FILM);
155         file->AppendSeparator ();
156         add_item (file, "&Quit", ID_file_quit, ALWAYS);
157
158         wxMenu* edit = new wxMenu;
159         add_item (edit, "&Preferences...", ID_edit_preferences, ALWAYS);
160
161         wxMenu* jobs = new wxMenu;
162         add_item (jobs, "&Make DCP", ID_jobs_make_dcp, NEEDS_FILM);
163         add_item (jobs, "&Send DCP to TMS", ID_jobs_send_dcp_to_tms, NEEDS_FILM);
164 #ifdef DVDOMATIC_POSIX  
165         add_item (jobs, "Copy from &DVD...", ID_jobs_copy_from_dvd, NEEDS_FILM);
166 #endif  
167         jobs->AppendSeparator ();
168         add_item (jobs, "&Examine content", ID_jobs_examine_content, NEEDS_FILM);
169         add_item (jobs, "Make DCP from existing &transcode", ID_jobs_make_dcp_from_existing_transcode, NEEDS_FILM);
170
171         wxMenu* help = new wxMenu;
172         add_item (help, "About", ID_help_about, ALWAYS);
173
174         m->Append (file, _("&File"));
175         m->Append (edit, _("&Edit"));
176         m->Append (jobs, _("&Jobs"));
177         m->Append (help, _("&Help"));
178 }
179
180 bool
181 window_closed (wxCommandEvent &)
182 {
183         maybe_save_then_delete_film ();
184         return false;
185 }
186
187 class Frame : public wxFrame
188 {
189 public:
190         Frame (wxString const & title)
191                 : wxFrame (NULL, -1, title)
192         {
193                 wxMenuBar* bar = new wxMenuBar;
194                 setup_menu (bar);
195                 SetMenuBar (bar);
196
197                 Connect (ID_file_new, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_new));
198                 Connect (ID_file_open, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_open));
199                 Connect (ID_file_save, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_save));
200                 Connect (ID_file_properties, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_properties));
201                 Connect (ID_file_quit, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_quit));
202                 Connect (ID_edit_preferences, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::edit_preferences));
203                 Connect (ID_jobs_make_dcp, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_make_dcp));
204                 Connect (ID_jobs_send_dcp_to_tms, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_send_dcp_to_tms));
205                 Connect (ID_jobs_copy_from_dvd, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_copy_from_dvd));
206                 Connect (ID_jobs_examine_content, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_examine_content));
207                 Connect (ID_jobs_make_dcp_from_existing_transcode, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_make_dcp_from_existing_transcode));
208                 Connect (ID_help_about, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::help_about));
209
210                 wxPanel* panel = new wxPanel (this);
211                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
212                 s->Add (panel, 1, wxEXPAND);
213                 SetSizer (s);
214
215                 film_editor = new FilmEditor (film, panel);
216                 film_viewer = new FilmViewer (film, panel);
217                 JobManagerView* job_manager_view = new JobManagerView (panel);
218
219                 wxSizer* rhs_sizer = new wxBoxSizer (wxVERTICAL);
220                 rhs_sizer->Add (film_viewer, 3, wxEXPAND | wxALL);
221                 rhs_sizer->Add (job_manager_view, 1, wxEXPAND | wxALL);
222
223                 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
224                 main_sizer->Add (film_editor, 0, wxALL, 6);
225                 main_sizer->Add (rhs_sizer, 1, wxEXPAND | wxALL, 6);
226                 panel->SetSizer (main_sizer);
227
228                 set_menu_sensitivity ();
229
230                 /* XXX: calling these here is a bit of a hack */
231                 film_editor->setup_visibility ();
232                 film_viewer->setup_visibility ();
233                 
234                 film_editor->FileChanged.connect (bind (&Frame::file_changed, this, _1));
235                 if (film) {
236                         file_changed (film->directory ());
237                 } else {
238                         file_changed ("");
239                 }
240                 
241                 set_film ();
242         }
243
244         void set_film ()
245         {
246                 film_viewer->set_film (film);
247                 film_editor->set_film (film);
248                 set_menu_sensitivity ();
249         }
250
251         void file_changed (string f)
252         {
253                 stringstream s;
254                 s << "DVD-o-matic";
255                 if (!f.empty ()) {
256                         s << " - " << f;
257                 }
258                 
259                 SetTitle (std_to_wx (s.str()));
260         }
261         
262         void file_new (wxCommandEvent &)
263         {
264                 NewFilmDialog* d = new NewFilmDialog (this);
265                 int const r = d->ShowModal ();
266                 
267                 if (r == wxID_OK) {
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_copy_from_dvd (wxCommandEvent &)
332         {
333                 try {
334
335 //              DVDTitleDialog d;
336 //              if (d.run () != Gtk::RESPONSE_OK) {
337 //                      return;
338 //              }
339                         film->copy_from_dvd ();
340                 } catch (DVDError& e) {
341                         error_dialog (this, e.what ());
342                 }
343         }
344         
345         void jobs_send_dcp_to_tms (wxCommandEvent &)
346         {
347                 film->send_dcp_to_tms ();
348         }
349         
350         void jobs_examine_content (wxCommandEvent &)
351         {
352                 film->examine_content ();
353         }
354         
355         void help_about (wxCommandEvent &)
356         {
357                 wxAboutDialogInfo info;
358                 info.SetName (_("DVD-o-matic"));
359                 info.SetVersion (std_to_wx (String::compose ("version %1 git %2", dvdomatic_version, dvdomatic_git_commit)));
360                 info.SetDescription (_("Free, open-source DCP generation from almost anything."));
361                 info.SetCopyright (_("(C) Carl Hetherington, Terrence Meiczinger, Paul Davis, Ole Laursen"));
362                 wxArrayString authors;
363                 authors.Add (wxT ("Carl Hetherington"));
364                 authors.Add (wxT ("Terrence Meiczinger"));
365                 authors.Add (wxT ("Paul Davis"));
366                 authors.Add (wxT ("Ole Laursen"));
367                 info.SetDevelopers (authors);
368                 info.SetWebSite (wxT ("http://carlh.net/software/dvdomatic"));
369                 wxAboutBox (info);
370         }
371 };
372
373 class App : public wxApp
374 {
375         bool OnInit ()
376         {
377 #ifdef DVDOMATIC_POSIX          
378                 unsetenv ("UBUNTU_MENUPROXY");
379 #endif          
380                 
381                 wxInitAllImageHandlers ();
382                 
383                 dvdomatic_setup ();
384
385                 if (argc == 2 && boost::filesystem::is_directory (wx_to_std (argv[1]))) {
386                         film.reset (new Film (wx_to_std (argv[1])));
387                 }
388
389                 Frame* f = new Frame (_("DVD-o-matic"));
390                 SetTopWindow (f);
391                 f->Maximize ();
392                 f->Show ();
393
394                 ui_signaller = new wxUISignaller (this);
395                 this->Connect (-1, wxEVT_IDLE, wxIdleEventHandler (App::idle));
396
397                 return true;
398         }
399
400         void idle (wxIdleEvent &)
401         {
402                 ui_signaller->ui_idle ();
403         }
404 };
405
406 IMPLEMENT_APP (App)