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