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