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