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