Tidy.
[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_jobs_make_dcp_from_existing_transcode,
150         ID_help_about
151 };
152
153 void
154 setup_menu (wxMenuBar* m)
155 {
156         wxMenu* file = new wxMenu;
157         add_item (file, "New...", ID_file_new, ALWAYS);
158         add_item (file, "&Open...", ID_file_open, ALWAYS);
159         file->AppendSeparator ();
160         add_item (file, "&Save", ID_file_save, NEEDS_FILM);
161         file->AppendSeparator ();
162         add_item (file, "&Properties...", ID_file_properties, NEEDS_FILM);
163         file->AppendSeparator ();
164         add_item (file, "&Quit", ID_file_quit, ALWAYS);
165
166         wxMenu* edit = new wxMenu;
167         add_item (edit, "&Preferences...", ID_edit_preferences, ALWAYS);
168
169         jobs_menu = new wxMenu;
170         add_item (jobs_menu, "&Make DCP", ID_jobs_make_dcp, NEEDS_FILM);
171         add_item (jobs_menu, "&Send DCP to TMS", ID_jobs_send_dcp_to_tms, NEEDS_FILM);
172         add_item (jobs_menu, "S&how DCP", ID_jobs_show_dcp, NEEDS_FILM);
173         jobs_menu->AppendSeparator ();
174         add_item (jobs_menu, "&Examine content", ID_jobs_examine_content, NEEDS_FILM);
175         add_item (jobs_menu, "Make DCP from existing &transcode", ID_jobs_make_dcp_from_existing_transcode, NEEDS_FILM);
176
177         wxMenu* help = new wxMenu;
178         add_item (help, "About", ID_help_about, ALWAYS);
179
180         m->Append (file, _("&File"));
181         m->Append (edit, _("&Edit"));
182         m->Append (jobs_menu, _("&Jobs"));
183         m->Append (help, _("&Help"));
184 }
185
186 bool
187 window_closed (wxCommandEvent &)
188 {
189         maybe_save_then_delete_film ();
190         return false;
191 }
192
193 class Frame : public wxFrame
194 {
195 public:
196         Frame (wxString const & title)
197                 : wxFrame (NULL, -1, title)
198         {
199                 wxMenuBar* bar = new wxMenuBar;
200                 setup_menu (bar);
201                 SetMenuBar (bar);
202
203                 Connect (ID_file_new, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_new));
204                 Connect (ID_file_open, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_open));
205                 Connect (ID_file_save, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_save));
206                 Connect (ID_file_properties, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_properties));
207                 Connect (ID_file_quit, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_quit));
208                 Connect (ID_edit_preferences, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::edit_preferences));
209                 Connect (ID_jobs_make_dcp, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_make_dcp));
210                 Connect (ID_jobs_send_dcp_to_tms, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_send_dcp_to_tms));
211                 Connect (ID_jobs_show_dcp, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_show_dcp));
212                 Connect (ID_jobs_examine_content, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_examine_content));
213                 Connect (ID_jobs_make_dcp_from_existing_transcode, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_make_dcp_from_existing_transcode));
214                 Connect (ID_help_about, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::help_about));
215
216                 Connect (wxID_ANY, wxEVT_MENU_OPEN, wxMenuEventHandler (Frame::menu_opened));
217
218                 wxPanel* panel = new wxPanel (this);
219                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
220                 s->Add (panel, 1, wxEXPAND);
221                 SetSizer (s);
222
223                 film_editor = new FilmEditor (film, panel);
224                 film_viewer = new FilmViewer (film, panel);
225                 JobManagerView* job_manager_view = new JobManagerView (panel);
226
227                 wxSizer* top_sizer = new wxBoxSizer (wxHORIZONTAL);
228                 top_sizer->Add (film_editor, 0, wxALL, 6);
229                 top_sizer->Add (film_viewer, 1, wxEXPAND | wxALL, 6);
230
231                 wxBoxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
232                 main_sizer->Add (top_sizer, 2, wxEXPAND | wxALL, 6);
233                 main_sizer->Add (job_manager_view, 1, wxEXPAND | wxALL, 6);
234                 panel->SetSizer (main_sizer);
235
236                 set_menu_sensitivity ();
237
238                 /* XXX: calling these here is a bit of a hack */
239                 film_editor->setup_visibility ();
240                 
241                 film_editor->FileChanged.connect (bind (&Frame::file_changed, this, _1));
242                 if (film) {
243                         file_changed (film->directory ());
244                 } else {
245                         file_changed ("");
246                 }
247                 
248                 set_film ();
249         }
250
251 private:
252
253         void menu_opened (wxMenuEvent& ev)
254         {
255                 if (ev.GetMenu() != jobs_menu) {
256                         return;
257                 }
258
259                 bool const have_dcp = film && film->have_dcp();
260                 jobs_menu->Enable (ID_jobs_send_dcp_to_tms, have_dcp);
261                 jobs_menu->Enable (ID_jobs_show_dcp, have_dcp);
262         }
263
264         void set_film ()
265         {
266                 film_viewer->set_film (film);
267                 film_editor->set_film (film);
268                 set_menu_sensitivity ();
269         }
270
271         void file_changed (string f)
272         {
273                 stringstream s;
274                 s << "DVD-o-matic";
275                 if (!f.empty ()) {
276                         s << " - " << f;
277                 }
278                 
279                 SetTitle (std_to_wx (s.str()));
280         }
281         
282         void file_new (wxCommandEvent &)
283         {
284                 NewFilmDialog* d = new NewFilmDialog (this);
285                 int const r = d->ShowModal ();
286                 
287                 if (r == wxID_OK) {
288
289                         if (boost::filesystem::exists (d->get_path())) {
290                                 error_dialog (this, wxString::Format (_("The directory %s already exists."), d->get_path().c_str()));
291                                 return;
292                         }
293                         
294                         maybe_save_then_delete_film ();
295                         film.reset (new Film (d->get_path (), false));
296                         film->log()->set_level (log_level);
297                         film->set_name (boost::filesystem::path (d->get_path()).filename().generic_string());
298                         set_film ();
299                 }
300                 
301                 d->Destroy ();
302         }
303
304         void file_open (wxCommandEvent &)
305         {
306                 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
307                 int const r = c->ShowModal ();
308                 
309                 if (r == wxID_OK) {
310                         maybe_save_then_delete_film ();
311                         try {
312                                 film.reset (new Film (wx_to_std (c->GetPath ())));
313                                 film->log()->set_level (log_level);
314                                 set_film ();
315                         } catch (std::exception& e) {
316                                 wxString p = c->GetPath ();
317                                 wxCharBuffer b = p.ToUTF8 ();
318                                 error_dialog (this, wxString::Format (_("Could not open film at %s (%s)"), p.data(), e.what()));
319                         }
320                 }
321
322                 c->Destroy ();
323         }
324
325         void file_save (wxCommandEvent &)
326         {
327                 film->write_metadata ();
328         }
329
330         void file_properties (wxCommandEvent &)
331         {
332                 PropertiesDialog* d = new PropertiesDialog (this, film);
333                 d->ShowModal ();
334                 d->Destroy ();
335         }
336         
337         void file_quit (wxCommandEvent &)
338         {
339                 maybe_save_then_delete_film ();
340                 Close (true);
341         }
342
343         void edit_preferences (wxCommandEvent &)
344         {
345                 ConfigDialog* d = new ConfigDialog (this);
346                 d->ShowModal ();
347                 d->Destroy ();
348                 Config::instance()->write ();
349         }
350
351         void jobs_make_dcp (wxCommandEvent &)
352         {
353                 JobWrapper::make_dcp (this, film, true);
354         }
355         
356         void jobs_make_dcp_from_existing_transcode (wxCommandEvent &)
357         {
358                 JobWrapper::make_dcp (this, film, false);
359         }
360         
361         void jobs_send_dcp_to_tms (wxCommandEvent &)
362         {
363                 film->send_dcp_to_tms ();
364         }
365
366         void jobs_show_dcp (wxCommandEvent &)
367         {
368 #ifdef __WXMSW__
369                 string d = film->directory();
370                 wstring w;
371                 w.assign (d.begin(), d.end());
372                 ShellExecute (0, L"open", w.c_str(), 0, 0, SW_SHOWDEFAULT);
373 #else
374                 int r = system ("which nautilus");
375                 if (WEXITSTATUS (r) == 0) {
376                         system (string ("nautilus " + film->directory()).c_str ());
377                 } else {
378                         int r = system ("which konqueror");
379                         if (WEXITSTATUS (r) == 0) {
380                                 system (string ("konqueror " + film->directory()).c_str ());
381                         }
382                 }
383 #endif          
384         }
385         
386         void jobs_examine_content (wxCommandEvent &)
387         {
388                 film->examine_content ();
389         }
390         
391         void help_about (wxCommandEvent &)
392         {
393                 wxAboutDialogInfo info;
394                 info.SetName (_("DVD-o-matic"));
395                 if (strcmp (dvdomatic_git_commit, "release") == 0) {
396                         info.SetVersion (std_to_wx (String::compose ("version %1", dvdomatic_version)));
397                 } else {
398                         info.SetVersion (std_to_wx (String::compose ("version %1 git %2", dvdomatic_version, dvdomatic_git_commit)));
399                 }
400                 info.SetDescription (_("Free, open-source DCP generation from almost anything."));
401                 info.SetCopyright (_("(C) 2012-2013 Carl Hetherington, Terrence Meiczinger, Paul Davis, Ole Laursen"));
402                 wxArrayString authors;
403                 authors.Add (wxT ("Carl Hetherington"));
404                 authors.Add (wxT ("Terrence Meiczinger"));
405                 authors.Add (wxT ("Paul Davis"));
406                 authors.Add (wxT ("Ole Laursen"));
407                 info.SetDevelopers (authors);
408                 info.SetWebSite (wxT ("http://carlh.net/software/dvdomatic"));
409                 wxAboutBox (info);
410         }
411 };
412
413 #if wxMINOR_VERSION == 9
414 static const wxCmdLineEntryDesc command_line_description[] = {
415         { wxCMD_LINE_OPTION, "l", "log", "set log level (silent, verbose or timing)", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
416         { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
417         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
418 };
419 #else
420 static const wxCmdLineEntryDesc command_line_description[] = {
421         { wxCMD_LINE_OPTION, wxT("l"), wxT("log"), wxT("set log level (silent, verbose or timing)"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
422         { wxCMD_LINE_PARAM, 0, 0, wxT("film to load"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
423         { wxCMD_LINE_NONE, wxT(""), wxT(""), wxT(""), wxCmdLineParamType (0), 0 }
424 };
425 #endif
426
427 class App : public wxApp
428 {
429         bool OnInit ()
430         {
431                 if (!wxApp::OnInit()) {
432                         return false;
433                 }
434                 
435 #ifdef DVDOMATIC_POSIX          
436                 unsetenv ("UBUNTU_MENUPROXY");
437 #endif          
438                 
439                 wxInitAllImageHandlers ();
440                 
441                 dvdomatic_setup ();
442
443                 if (!film_to_load.empty() && boost::filesystem::is_directory (film_to_load)) {
444                         try {
445                                 film.reset (new Film (film_to_load));
446                                 film->log()->set_level (log_level);
447                         } catch (exception& e) {
448                                 error_dialog (0, std_to_wx (String::compose ("Could not load film %1 (%2)", film_to_load, e.what())));
449                         }
450                 }
451
452                 Frame* f = new Frame (_("DVD-o-matic"));
453                 SetTopWindow (f);
454                 f->Maximize ();
455                 f->Show ();
456
457                 ui_signaller = new wxUISignaller (this);
458                 this->Connect (-1, wxEVT_IDLE, wxIdleEventHandler (App::idle));
459
460                 return true;
461         }
462
463         void OnInitCmdLine (wxCmdLineParser& parser)
464         {
465                 parser.SetDesc (command_line_description);
466                 parser.SetSwitchChars (wxT ("-"));
467         }
468
469         bool OnCmdLineParsed (wxCmdLineParser& parser)
470         {
471                 if (parser.GetParamCount() > 0) {
472                         film_to_load = wx_to_std (parser.GetParam(0));
473                 }
474
475                 wxString log;
476                 if (parser.Found(wxT("log"), &log)) {
477                         log_level = wx_to_std (log);
478                 }
479
480                 return true;
481         }
482
483         void idle (wxIdleEvent &)
484         {
485                 ui_signaller->ui_idle ();
486         }
487 };
488
489 IMPLEMENT_APP (App)