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