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