Various hacks.
[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/film_viewer.h"
23 #include "wx/film_editor.h"
24 #ifndef DVDOMATIC_DISABLE_PLAYER
25 #include "wx/film_player.h"
26 #endif
27 //#include "gtk/job_manager_view.h"
28 //#include "gtk/config_dialog.h"
29 //#include "gtk/gpl.h"
30 //#include "gtk/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 wxFrame* frame = 0;
45 static FilmEditor* film_editor = 0;
46 static FilmViewer* film_viewer = 0;
47
48 #ifndef DVDOMATIC_DISABLE_PLAYER
49 static FilmPlayer* film_player = 0;
50 #endif
51 static Film* film = 0;
52
53 static void set_menu_sensitivity ();
54
55 class FilmChangedDialog
56 {
57 public:
58         FilmChangedDialog ()
59         {
60                 stringstream s;
61                 s << "Save changes to film \"" << film->name() << "\" before closing?";
62                 _dialog = new wxMessageDialog (frame, std_to_wx (s.str()), wxT ("Film changed"), wxYES_DEFAULT | wxICON_QUESTION);
63         }
64
65         ~FilmChangedDialog ()
66         {
67                 _dialog->Destroy ();
68         }
69
70         int run ()
71         {
72                 return _dialog->ShowModal ();
73         }
74
75 private:        
76         wxMessageDialog* _dialog;
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
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_quit,
134         ID_edit_preferences,
135         ID_jobs_make_dcp,
136         ID_jobs_send_dcp_to_tms,
137         ID_jobs_copy_from_dvd,
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, "&Quit", ID_file_quit, ALWAYS);
153
154         wxMenu* edit = new wxMenu;
155         add_item (edit, "&Preferences...", ID_edit_preferences, ALWAYS);
156
157         wxMenu* jobs = new wxMenu;
158         add_item (jobs, "&Make DCP", ID_jobs_make_dcp, NEEDS_FILM);
159         add_item (jobs, "&Send DCP to TMS", ID_jobs_send_dcp_to_tms, NEEDS_FILM);
160         add_item (jobs, "Copy from &DVD...", ID_jobs_copy_from_dvd, NEEDS_FILM);
161         jobs->AppendSeparator ();
162         add_item (jobs, "&Examine content", ID_jobs_examine_content, NEEDS_FILM);
163         add_item (jobs, "Make DCP from existing &transcode", ID_jobs_make_dcp_from_existing_transcode, NEEDS_FILM);
164
165         wxMenu* help = new wxMenu;
166         add_item (help, "About", ID_help_about, ALWAYS);
167
168         m->Append (file, _("&File"));
169         m->Append (edit, _("&Edit"));
170         m->Append (jobs, _("&Jobs"));
171         m->Append (help, _("&Help"));
172 }
173
174 bool
175 window_closed (wxCommandEvent &)
176 {
177         maybe_save_then_delete_film ();
178         return false;
179 }
180
181 void
182 file_changed (string f)
183 {
184         stringstream s;
185         s << "DVD-o-matic";
186         if (!f.empty ()) {
187                 s << " — " << f;
188         }
189         
190         frame->SetTitle (std_to_wx (s.str()));
191 }
192
193 class Frame : public wxFrame
194 {
195 public:
196         Frame (wxString const & title)
197                 : wxFrame (NULL, -1, title)
198         {
199                 wxMenuBar* bar = new wxMenuBar;
200                 setup_menu (bar);
201                 SetMenuBar (bar);
202         }
203
204         void file_new (wxCommandEvent &)
205         {
206                 wxDirDialog* c = new wxDirDialog (frame, wxT ("New Film"));
207                 int const r = c->ShowModal ();
208                 c->Destroy ();
209                 
210                 if (r == wxID_OK) {
211                         maybe_save_then_delete_film ();
212                         film = new Film (wx_to_std (c->GetPath ()));
213 #if BOOST_FILESYSTEM_VERSION == 3               
214                         film->set_name (filesystem::path (wx_to_std (c->GetPath())).filename().generic_string());
215 #else           
216                         film->set_name (filesystem::path (wx_to_std (c->GetPath())).filename());
217 #endif          
218                         film_viewer->set_film (film);
219                         film_editor->set_film (film);
220                         set_menu_sensitivity ();
221                 }
222         }
223
224         void file_open (wxCommandEvent &)
225         {
226                 wxDirDialog* c = new wxDirDialog (frame, wxT ("Open Film"), wxT (""), wxDD_DIR_MUST_EXIST);
227                 int const r = c->ShowModal ();
228                 c->Destroy ();
229                 
230                 if (r == wxID_OK) {
231                         maybe_save_then_delete_film ();
232                         film = new Film (wx_to_std (c->GetPath ()));
233                         film_viewer->set_film (film);
234                         film_editor->set_film (film);
235                         set_menu_sensitivity ();
236                 }
237         }
238
239         void file_save (wxCommandEvent &)
240         {
241                 film->write_metadata ();
242         }
243         
244         void file_quit (wxCommandEvent &)
245         {
246                 maybe_save_then_delete_film ();
247                 frame->Close (true);
248         }
249
250         void edit_preferences (wxCommandEvent &)
251         {
252 //      ConfigDialog d;
253 //      d.run ();
254 //      Config::instance()->write ();
255         }
256
257         void jobs_make_dcp (wxCommandEvent &)
258         {
259 //      JobWrapper::make_dcp (film, true);
260         }
261         
262         void jobs_make_dcp_from_existing_transcode (wxCommandEvent &)
263         {
264 //      JobWrapper::make_dcp (film, false);
265         }
266         
267         void jobs_copy_from_dvd (wxCommandEvent &)
268         {
269 //      try {
270 //              DVDTitleDialog d;
271 //              if (d.run () != Gtk::RESPONSE_OK) {
272 //                      return;
273 //              }
274 //              film->copy_from_dvd ();
275 //      } catch (DVDError& e) {
276 //              error_dialog (e.what ());
277 //      }
278         }
279         
280         void jobs_send_dcp_to_tms (wxCommandEvent &)
281         {
282                 film->send_dcp_to_tms ();
283         }
284         
285         void jobs_examine_content (wxCommandEvent &)
286         {
287                 film->examine_content ();
288         }
289         
290         void help_about (wxCommandEvent &)
291         {
292 //      Gtk::AboutDialog d;
293 //      d.set_name ("DVD-o-matic");
294 //      d.set_version (DVDOMATIC_VERSION);
295                 
296 //      stringstream s;
297 //      s << "DCP generation from arbitrary formats\n\n"
298 //        << "Using " << dependency_version_summary() << "\n";
299 //      d.set_comments (s.str ());
300                 
301 //      vector<string> authors;
302 //      authors.push_back ("Carl Hetherington");
303 //      authors.push_back ("Terrence Meiczinger");
304 //      authors.push_back ("Paul Davis");
305 //      d.set_authors (authors);
306
307 //      d.set_website ("http://carlh.net/software/dvdomatic");
308 //      d.set_license (gpl);
309         
310 //      d.run ();
311 }
312 };
313
314 class App : public wxApp
315 {
316         bool OnInit ()
317         {
318                 if (!wxApp::OnInit ()) {
319                         return false;
320                 }
321
322                 wxInitAllImageHandlers ();
323                 
324                 dvdomatic_setup ();
325
326 //              if (argc == 2 && boost::filesystem::is_directory (argv[1])) {
327 //                      film = new Film (argv[1]);
328 //              }
329                 
330                 film = new Film ("/home/carl/DCP/BitHarvest");
331                 
332                 frame = new Frame (_("DVD-o-matic"));
333                 frame->Show (true);
334                 
335                 frame->Connect (ID_file_new, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_new));
336                 frame->Connect (ID_file_open, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_open));
337                 frame->Connect (ID_file_save, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_save));
338                 frame->Connect (ID_file_quit, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_quit));
339                 frame->Connect (ID_edit_preferences, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::edit_preferences));
340                 frame->Connect (ID_jobs_make_dcp, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_make_dcp));
341                 frame->Connect (ID_jobs_send_dcp_to_tms, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_send_dcp_to_tms));
342                 frame->Connect (ID_jobs_copy_from_dvd, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_copy_from_dvd));
343                 frame->Connect (ID_jobs_examine_content, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_examine_content));
344                 frame->Connect (ID_jobs_make_dcp_from_existing_transcode, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_make_dcp_from_existing_transcode));
345                 frame->Connect (ID_help_about, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::help_about));
346
347                 film_editor = new FilmEditor (film, frame);
348                 film_editor->Show (true);
349                 film_viewer = new FilmViewer (film, frame);
350                 film_viewer->Show (true);
351 #ifndef DVDOMATIC_DISABLE_PLAYER
352                 film_player = new FilmPlayer (film, frame);
353 #endif          
354
355                 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
356                 main_sizer->Add (film_editor, 0, wxALL, 6);
357                 main_sizer->Add (film_viewer, 1, wxEXPAND | wxALL, 6);
358                 frame->SetSizer (main_sizer);
359
360                 SetTopWindow (frame);
361                 frame->Maximize ();
362
363 #if 0
364                 /* XXX: calling these here is a bit of a hack */
365                 film_editor->setup_visibility ();
366 #ifndef DVDOMATIC_DISABLE_PLAYER        
367                 film_player->setup_visibility ();
368 #endif  
369                 film_viewer->setup_visibility ();
370                 
371                 film_editor->FileChanged.connect (ptr_fun (file_changed));
372                 if (film) {
373                         file_changed (film->directory ());
374                 } else {
375                         file_changed ("");
376                 }
377 #endif          
378                 
379                 /* XXX this should be in JobManagerView, shouldn't it? */
380 //XXX           Glib::signal_timeout().connect (sigc::bind_return (sigc::mem_fun (jobs_view, &JobManagerView::update), true), 1000);
381                 
382                 return true;
383         }
384 };
385
386 IMPLEMENT_APP (App)