Check for a DCP before offering to copy it to a TMS.
[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 "wx/wx_util.h"
31 #include "wx/new_film_dialog.h"
32 #include "wx/properties_dialog.h"
33 #include "wx/wx_ui_signaller.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 #include "lib/ui_signaller.h"
43 #include "lib/log.h"
44
45 using std::cout;
46 using std::string;
47 using std::stringstream;
48 using std::map;
49 using std::make_pair;
50 using boost::shared_ptr;
51
52 static FilmEditor* film_editor = 0;
53 static FilmViewer* film_viewer = 0;
54 static shared_ptr<Film> film;
55 static std::string log_level;
56 static std::string film_to_load;
57 static wxMenu* jobs_menu = 0;
58
59 static void set_menu_sensitivity ();
60
61 class FilmChangedDialog
62 {
63 public:
64         FilmChangedDialog ()
65         {
66                 stringstream s;
67                 s << "Save changes to film \"" << film->name() << "\" before closing?";
68                 _dialog = new wxMessageDialog (0, std_to_wx (s.str()), _("Film changed"), wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION);
69         }
70
71         ~FilmChangedDialog ()
72         {
73                 _dialog->Destroy ();
74         }
75
76         int run ()
77         {
78                 return _dialog->ShowModal ();
79         }
80
81 private:        
82         wxMessageDialog* _dialog;
83 };
84
85
86 void
87 maybe_save_then_delete_film ()
88 {
89         if (!film) {
90                 return;
91         }
92                         
93         if (film->dirty ()) {
94                 FilmChangedDialog d;
95                 switch (d.run ()) {
96                 case wxID_NO:
97                         break;
98                 case wxID_YES:
99                         film->write_metadata ();
100                         break;
101                 }
102         }
103         
104         film.reset ();
105 }
106
107 enum Sensitivity {
108         ALWAYS,
109         NEEDS_FILM
110 };
111
112 map<wxMenuItem*, Sensitivity> menu_items;
113         
114 void
115 add_item (wxMenu* menu, std::string text, int id, Sensitivity sens)
116 {
117         wxMenuItem* item = menu->Append (id, std_to_wx (text));
118         menu_items.insert (make_pair (item, sens));
119 }
120
121 void
122 set_menu_sensitivity ()
123 {
124         for (map<wxMenuItem*, Sensitivity>::iterator i = menu_items.begin(); i != menu_items.end(); ++i) {
125                 if (i->second == NEEDS_FILM) {
126                         i->first->Enable (film != 0);
127                 } else {
128                         i->first->Enable (true);
129                 }
130         }
131 }
132
133 enum {
134         ID_file_new = 1,
135         ID_file_open,
136         ID_file_save,
137         ID_file_properties,
138         ID_file_quit,
139         ID_edit_preferences,
140         ID_jobs_make_dcp,
141         ID_jobs_send_dcp_to_tms,
142         ID_jobs_examine_content,
143         ID_jobs_make_dcp_from_existing_transcode,
144         ID_help_about
145 };
146
147 void
148 setup_menu (wxMenuBar* m)
149 {
150         wxMenu* file = new wxMenu;
151         add_item (file, "New...", ID_file_new, ALWAYS);
152         add_item (file, "&Open...", ID_file_open, ALWAYS);
153         file->AppendSeparator ();
154         add_item (file, "&Save", ID_file_save, NEEDS_FILM);
155         file->AppendSeparator ();
156         add_item (file, "&Properties...", ID_file_properties, NEEDS_FILM);
157         file->AppendSeparator ();
158         add_item (file, "&Quit", ID_file_quit, ALWAYS);
159
160         wxMenu* edit = new wxMenu;
161         add_item (edit, "&Preferences...", ID_edit_preferences, ALWAYS);
162
163         jobs_menu = new wxMenu;
164         add_item (jobs_menu, "&Make DCP", ID_jobs_make_dcp, NEEDS_FILM);
165         add_item (jobs_menu, "&Send DCP to TMS", ID_jobs_send_dcp_to_tms, NEEDS_FILM);
166         jobs_menu->AppendSeparator ();
167         add_item (jobs_menu, "&Examine content", ID_jobs_examine_content, NEEDS_FILM);
168         add_item (jobs_menu, "Make DCP from existing &transcode", ID_jobs_make_dcp_from_existing_transcode, NEEDS_FILM);
169
170         wxMenu* help = new wxMenu;
171         add_item (help, "About", ID_help_about, ALWAYS);
172
173         m->Append (file, _("&File"));
174         m->Append (edit, _("&Edit"));
175         m->Append (jobs_menu, _("&Jobs"));
176         m->Append (help, _("&Help"));
177 }
178
179 bool
180 window_closed (wxCommandEvent &)
181 {
182         maybe_save_then_delete_film ();
183         return false;
184 }
185
186 class Frame : public wxFrame
187 {
188 public:
189         Frame (wxString const & title)
190                 : wxFrame (NULL, -1, title)
191         {
192                 wxMenuBar* bar = new wxMenuBar;
193                 setup_menu (bar);
194                 SetMenuBar (bar);
195
196                 Connect (ID_file_new, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_new));
197                 Connect (ID_file_open, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_open));
198                 Connect (ID_file_save, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_save));
199                 Connect (ID_file_properties, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_properties));
200                 Connect (ID_file_quit, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_quit));
201                 Connect (ID_edit_preferences, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::edit_preferences));
202                 Connect (ID_jobs_make_dcp, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_make_dcp));
203                 Connect (ID_jobs_send_dcp_to_tms, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_send_dcp_to_tms));
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                 Connect (wxID_ANY, wxEVT_MENU_OPEN, wxMenuEventHandler (Frame::menu_opened));
209
210                 wxPanel* panel = new wxPanel (this);
211                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
212                 s->Add (panel, 1, wxEXPAND);
213                 SetSizer (s);
214
215                 film_editor = new FilmEditor (film, panel);
216                 film_viewer = new FilmViewer (film, panel);
217                 JobManagerView* job_manager_view = new JobManagerView (panel);
218
219                 wxSizer* rhs_sizer = new wxBoxSizer (wxVERTICAL);
220                 rhs_sizer->Add (film_viewer, 3, wxEXPAND | wxALL);
221                 rhs_sizer->Add (job_manager_view, 1, wxEXPAND | wxALL);
222
223                 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
224                 main_sizer->Add (film_editor, 0, wxALL, 6);
225                 main_sizer->Add (rhs_sizer, 1, wxEXPAND | wxALL, 6);
226                 panel->SetSizer (main_sizer);
227
228                 set_menu_sensitivity ();
229
230                 /* XXX: calling these here is a bit of a hack */
231                 film_editor->setup_visibility ();
232                 
233                 film_editor->FileChanged.connect (bind (&Frame::file_changed, this, _1));
234                 if (film) {
235                         file_changed (film->directory ());
236                 } else {
237                         file_changed ("");
238                 }
239                 
240                 set_film ();
241         }
242
243 private:
244
245         void menu_opened (wxMenuEvent& ev)
246         {
247                 if (ev.GetMenu() != jobs_menu) {
248                         return;
249                 }
250
251                 bool const have_dcp = film && film->have_dcp();
252                 jobs_menu->Enable (ID_jobs_send_dcp_to_tms, have_dcp);
253         }
254
255         void set_film ()
256         {
257                 film_viewer->set_film (film);
258                 film_editor->set_film (film);
259                 set_menu_sensitivity ();
260         }
261
262         void file_changed (string f)
263         {
264                 stringstream s;
265                 s << "DVD-o-matic";
266                 if (!f.empty ()) {
267                         s << " - " << f;
268                 }
269                 
270                 SetTitle (std_to_wx (s.str()));
271         }
272         
273         void file_new (wxCommandEvent &)
274         {
275                 NewFilmDialog* d = new NewFilmDialog (this);
276                 int const r = d->ShowModal ();
277                 
278                 if (r == wxID_OK) {
279
280                         if (boost::filesystem::exists (d->get_path())) {
281                                 error_dialog (this, wxString::Format (_("The directory %s already exists"), d->get_path().c_str()));
282                                 return;
283                         }
284                         
285                         maybe_save_then_delete_film ();
286                         film.reset (new Film (d->get_path (), false));
287                         film->log()->set_level (log_level);
288                         film->set_name (boost::filesystem::path (d->get_path()).filename().generic_string());
289                         set_film ();
290                 }
291                 
292                 d->Destroy ();
293         }
294
295         void file_open (wxCommandEvent &)
296         {
297                 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
298                 int const r = c->ShowModal ();
299                 
300                 if (r == wxID_OK) {
301                         maybe_save_then_delete_film ();
302                         try {
303                                 film.reset (new Film (wx_to_std (c->GetPath ())));
304                                 film->log()->set_level (log_level);
305                                 set_film ();
306                         } catch (std::exception& e) {
307                                 wxString p = c->GetPath ();
308                                 wxCharBuffer b = p.ToUTF8 ();
309                                 error_dialog (this, wxString::Format (_("Could not open film at %s (%s)"), p.data(), e.what()));
310                         }
311                 }
312
313                 c->Destroy ();
314         }
315
316         void file_save (wxCommandEvent &)
317         {
318                 film->write_metadata ();
319         }
320
321         void file_properties (wxCommandEvent &)
322         {
323                 PropertiesDialog* d = new PropertiesDialog (this, film);
324                 d->ShowModal ();
325                 d->Destroy ();
326         }
327         
328         void file_quit (wxCommandEvent &)
329         {
330                 maybe_save_then_delete_film ();
331                 Close (true);
332         }
333
334         void edit_preferences (wxCommandEvent &)
335         {
336                 ConfigDialog* d = new ConfigDialog (this);
337                 d->ShowModal ();
338                 d->Destroy ();
339                 Config::instance()->write ();
340         }
341
342         void jobs_make_dcp (wxCommandEvent &)
343         {
344                 JobWrapper::make_dcp (this, film, true);
345         }
346         
347         void jobs_make_dcp_from_existing_transcode (wxCommandEvent &)
348         {
349                 JobWrapper::make_dcp (this, film, false);
350         }
351         
352         void jobs_send_dcp_to_tms (wxCommandEvent &)
353         {
354                 film->send_dcp_to_tms ();
355         }
356         
357         void jobs_examine_content (wxCommandEvent &)
358         {
359                 film->examine_content ();
360         }
361         
362         void help_about (wxCommandEvent &)
363         {
364                 wxAboutDialogInfo info;
365                 info.SetName (_("DVD-o-matic"));
366                 if (strcmp (dvdomatic_git_commit, "release") == 0) {
367                         info.SetVersion (std_to_wx (String::compose ("version %1", dvdomatic_version)));
368                 } else {
369                         info.SetVersion (std_to_wx (String::compose ("version %1 git %2", dvdomatic_version, dvdomatic_git_commit)));
370                 }
371                 info.SetDescription (_("Free, open-source DCP generation from almost anything."));
372                 info.SetCopyright (_("(C) 2012-2013 Carl Hetherington, Terrence Meiczinger, Paul Davis, Ole Laursen"));
373                 wxArrayString authors;
374                 authors.Add (wxT ("Carl Hetherington"));
375                 authors.Add (wxT ("Terrence Meiczinger"));
376                 authors.Add (wxT ("Paul Davis"));
377                 authors.Add (wxT ("Ole Laursen"));
378                 info.SetDevelopers (authors);
379                 info.SetWebSite (wxT ("http://carlh.net/software/dvdomatic"));
380                 wxAboutBox (info);
381         }
382 };
383
384 #if wxMINOR_VERSION == 9
385 static const wxCmdLineEntryDesc command_line_description[] = {
386         { wxCMD_LINE_OPTION, "l", "log", "set log level (silent, verbose or timing)", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
387         { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
388         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
389 };
390 #else
391 static const wxCmdLineEntryDesc command_line_description[] = {
392         { wxCMD_LINE_OPTION, wxT("l"), wxT("log"), wxT("set log level (silent, verbose or timing)"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
393         { wxCMD_LINE_PARAM, 0, 0, wxT("film to load"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
394         { wxCMD_LINE_NONE, wxT(""), wxT(""), wxT(""), wxCmdLineParamType (0), 0 }
395 };
396 #endif
397
398 class App : public wxApp
399 {
400         bool OnInit ()
401         {
402                 if (!wxApp::OnInit()) {
403                         return false;
404                 }
405                 
406 #ifdef DVDOMATIC_POSIX          
407                 unsetenv ("UBUNTU_MENUPROXY");
408 #endif          
409                 
410                 wxInitAllImageHandlers ();
411                 
412                 dvdomatic_setup ();
413
414                 if (!film_to_load.empty() && boost::filesystem::is_directory (film_to_load)) {
415                         film.reset (new Film (film_to_load));
416                         film->log()->set_level (log_level);
417                 }
418
419                 Frame* f = new Frame (_("DVD-o-matic"));
420                 SetTopWindow (f);
421                 f->Maximize ();
422                 f->Show ();
423
424                 ui_signaller = new wxUISignaller (this);
425                 this->Connect (-1, wxEVT_IDLE, wxIdleEventHandler (App::idle));
426
427                 return true;
428         }
429
430         void OnInitCmdLine (wxCmdLineParser& parser)
431         {
432                 parser.SetDesc (command_line_description);
433                 parser.SetSwitchChars (wxT ("-"));
434         }
435
436         bool OnCmdLineParsed (wxCmdLineParser& parser)
437         {
438                 if (parser.GetParamCount() > 0) {
439                         film_to_load = wx_to_std (parser.GetParam(0));
440                 }
441
442                 wxString log;
443                 if (parser.Found(wxT("log"), &log)) {
444                         log_level = wx_to_std (log);
445                 }
446
447                 return true;
448         }
449
450         void idle (wxIdleEvent &)
451         {
452                 ui_signaller->ui_idle ();
453         }
454 };
455
456 IMPLEMENT_APP (App)