Try to improve new film a bit.
[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         add_item (jobs, "Copy from &DVD...", ID_jobs_copy_from_dvd, NEEDS_FILM);
156         jobs->AppendSeparator ();
157         add_item (jobs, "&Examine content", ID_jobs_examine_content, NEEDS_FILM);
158         add_item (jobs, "Make DCP from existing &transcode", ID_jobs_make_dcp_from_existing_transcode, NEEDS_FILM);
159
160         wxMenu* help = new wxMenu;
161         add_item (help, "About", ID_help_about, ALWAYS);
162
163         m->Append (file, _("&File"));
164         m->Append (edit, _("&Edit"));
165         m->Append (jobs, _("&Jobs"));
166         m->Append (help, _("&Help"));
167 }
168
169 bool
170 window_closed (wxCommandEvent &)
171 {
172         maybe_save_then_delete_film ();
173         return false;
174 }
175
176 class Frame : public wxFrame
177 {
178 public:
179         Frame (wxString const & title)
180                 : wxFrame (NULL, -1, title)
181         {
182                 wxMenuBar* bar = new wxMenuBar;
183                 setup_menu (bar);
184                 SetMenuBar (bar);
185
186                 Connect (ID_file_new, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_new));
187                 Connect (ID_file_open, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_open));
188                 Connect (ID_file_save, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_save));
189                 Connect (ID_file_quit, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_quit));
190                 Connect (ID_edit_preferences, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::edit_preferences));
191                 Connect (ID_jobs_make_dcp, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_make_dcp));
192                 Connect (ID_jobs_send_dcp_to_tms, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_send_dcp_to_tms));
193                 Connect (ID_jobs_copy_from_dvd, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_copy_from_dvd));
194                 Connect (ID_jobs_examine_content, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_examine_content));
195                 Connect (ID_jobs_make_dcp_from_existing_transcode, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_make_dcp_from_existing_transcode));
196                 Connect (ID_help_about, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::help_about));
197
198                 wxPanel* panel = new wxPanel (this);
199                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
200                 s->Add (panel, 1, wxEXPAND);
201                 SetSizer (s);
202
203                 film_editor = new FilmEditor (film, panel);
204                 film_viewer = new FilmViewer (film, panel);
205                 JobManagerView* job_manager_view = new JobManagerView (panel);
206
207                 wxSizer* rhs_sizer = new wxBoxSizer (wxVERTICAL);
208                 rhs_sizer->Add (film_viewer, 3, wxEXPAND | wxALL);
209                 rhs_sizer->Add (job_manager_view, 1, wxEXPAND | wxALL);
210
211                 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
212                 main_sizer->Add (film_editor, 0, wxALL, 6);
213                 main_sizer->Add (rhs_sizer, 1, wxEXPAND | wxALL, 6);
214                 panel->SetSizer (main_sizer);
215
216                 set_menu_sensitivity ();
217
218                 /* XXX: calling these here is a bit of a hack */
219                 film_editor->setup_visibility ();
220                 film_viewer->setup_visibility ();
221                 
222                 film_editor->FileChanged.connect (sigc::mem_fun (*this, &Frame::file_changed));
223                 if (film) {
224                         file_changed (film->directory ());
225                 } else {
226                         file_changed ("");
227                 }
228                 
229                 set_film ();
230         }
231
232         void set_film ()
233         {
234                 film_viewer->set_film (film);
235                 film_editor->set_film (film);
236                 set_menu_sensitivity ();
237         }
238
239         void file_changed (string f)
240         {
241                 stringstream s;
242                 s << "DVD-o-matic";
243                 if (!f.empty ()) {
244                         s << " - " << f;
245                 }
246                 
247                 SetTitle (std_to_wx (s.str()));
248         }
249         
250         void file_new (wxCommandEvent &)
251         {
252                 NewFilmDialog* d = new NewFilmDialog (this);
253                 int const r = d->ShowModal ();
254                 
255                 if (r == wxID_OK) {
256                         maybe_save_then_delete_film ();
257                         film = new Film (d->get_path (), false);
258 #if BOOST_FILESYSTEM_VERSION == 3               
259                         film->set_name (filesystem::path (d->get_path()).filename().generic_string());
260 #else           
261                         film->set_name (filesystem::path (d->get_path()).filename());
262 #endif
263                         set_film ();
264                 }
265                 
266                 d->Destroy ();
267         }
268
269         void file_open (wxCommandEvent &)
270         {
271                 wxDirDialog* c = new wxDirDialog (this, wxT ("Open Film"), wxStandardPaths::Get().GetDocumentsDir(), wxDD_DIR_MUST_EXIST);
272                 int const r = c->ShowModal ();
273                 c->Destroy ();
274                 
275                 if (r == wxID_OK) {
276                         maybe_save_then_delete_film ();
277                         film = new Film (wx_to_std (c->GetPath ()));
278                         set_film ();
279                 }
280         }
281
282         void file_save (wxCommandEvent &)
283         {
284                 film->write_metadata ();
285         }
286         
287         void file_quit (wxCommandEvent &)
288         {
289                 maybe_save_then_delete_film ();
290                 Close (true);
291         }
292
293         void edit_preferences (wxCommandEvent &)
294         {
295                 ConfigDialog* d = new ConfigDialog (this);
296                 d->ShowModal ();
297                 d->Destroy ();
298                 Config::instance()->write ();
299         }
300
301         void jobs_make_dcp (wxCommandEvent &)
302         {
303                 JobWrapper::make_dcp (this, film, true);
304         }
305         
306         void jobs_make_dcp_from_existing_transcode (wxCommandEvent &)
307         {
308                 JobWrapper::make_dcp (this, film, false);
309         }
310         
311         void jobs_copy_from_dvd (wxCommandEvent &)
312         {
313 //      try {
314 //              DVDTitleDialog d;
315 //              if (d.run () != Gtk::RESPONSE_OK) {
316 //                      return;
317 //              }
318 //              film->copy_from_dvd ();
319 //      } catch (DVDError& e) {
320 //              error_dialog (e.what ());
321 //      }
322         }
323         
324         void jobs_send_dcp_to_tms (wxCommandEvent &)
325         {
326                 film->send_dcp_to_tms ();
327         }
328         
329         void jobs_examine_content (wxCommandEvent &)
330         {
331                 film->examine_content ();
332         }
333         
334         void help_about (wxCommandEvent &)
335         {
336                 wxAboutDialogInfo info;
337                 info.SetName (_("DVD-o-matic"));
338                 info.SetVersion (wxT (DVDOMATIC_VERSION));
339                 info.SetDescription (_("Free, open-source DCP generation from almost anything."));
340                 info.SetCopyright (_("(C) Carl Hetherington, Terrence Meiczinger, Paul Davis"));
341                 wxArrayString authors;
342                 authors.Add (wxT ("Carl Hetherington"));
343                 authors.Add (wxT ("Terrence Meiczinger"));
344                 authors.Add (wxT ("Paul Davis"));
345                 info.SetDevelopers (authors);
346                 info.SetWebSite (wxT ("http://carlh.net/software/dvdomatic"));
347                 wxAboutBox (info);
348         }
349 };
350
351 class App : public wxApp
352 {
353         bool OnInit ()
354         {
355                 wxInitAllImageHandlers ();
356                 
357                 dvdomatic_setup ();
358
359                 if (argc == 2 && boost::filesystem::is_directory (wx_to_std (argv[1]))) {
360                         film = new Film (wx_to_std (argv[1]));
361                 }
362
363                 Frame* f = new Frame (_("DVD-o-matic"));
364                 SetTopWindow (f);
365                 f->Maximize ();
366                 f->Show ();
367                 return true;
368         }
369 };
370
371 IMPLEMENT_APP (App)