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