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