Remove some vestigial stuff.
[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                 wxSizer* 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
248 private:
249
250         void menu_opened (wxMenuEvent& ev)
251         {
252                 if (ev.GetMenu() != jobs_menu) {
253                         return;
254                 }
255
256                 bool const have_dcp = film && film->have_dcp();
257                 jobs_menu->Enable (ID_jobs_send_dcp_to_tms, have_dcp);
258                 jobs_menu->Enable (ID_jobs_show_dcp, have_dcp);
259         }
260
261         void set_film ()
262         {
263                 film_viewer->set_film (film);
264                 film_editor->set_film (film);
265                 set_menu_sensitivity ();
266         }
267
268         void file_changed (string f)
269         {
270                 stringstream s;
271                 s << "DVD-o-matic";
272                 if (!f.empty ()) {
273                         s << " - " << f;
274                 }
275                 
276                 SetTitle (std_to_wx (s.str()));
277         }
278         
279         void file_new (wxCommandEvent &)
280         {
281                 NewFilmDialog* d = new NewFilmDialog (this);
282                 int const r = d->ShowModal ();
283                 
284                 if (r == wxID_OK) {
285
286                         if (boost::filesystem::exists (d->get_path())) {
287                                 error_dialog (this, wxString::Format (_("The directory %s already exists."), d->get_path().c_str()));
288                                 return;
289                         }
290                         
291                         maybe_save_then_delete_film ();
292                         film.reset (new Film (d->get_path (), false));
293                         film->log()->set_level (log_level);
294                         film->set_name (boost::filesystem::path (d->get_path()).filename().generic_string());
295                         set_film ();
296                 }
297                 
298                 d->Destroy ();
299         }
300
301         void file_open (wxCommandEvent &)
302         {
303                 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
304                 int const r = c->ShowModal ();
305                 
306                 if (r == wxID_OK) {
307                         maybe_save_then_delete_film ();
308                         try {
309                                 film.reset (new Film (wx_to_std (c->GetPath ())));
310                                 film->log()->set_level (log_level);
311                                 set_film ();
312                         } catch (std::exception& e) {
313                                 wxString p = c->GetPath ();
314                                 wxCharBuffer b = p.ToUTF8 ();
315                                 error_dialog (this, wxString::Format (_("Could not open film at %s (%s)"), p.data(), e.what()));
316                         }
317                 }
318
319                 c->Destroy ();
320         }
321
322         void file_save (wxCommandEvent &)
323         {
324                 film->write_metadata ();
325         }
326
327         void file_properties (wxCommandEvent &)
328         {
329                 PropertiesDialog* d = new PropertiesDialog (this, film);
330                 d->ShowModal ();
331                 d->Destroy ();
332         }
333         
334         void file_quit (wxCommandEvent &)
335         {
336                 maybe_save_then_delete_film ();
337                 Close (true);
338         }
339
340         void edit_preferences (wxCommandEvent &)
341         {
342                 ConfigDialog* d = new ConfigDialog (this);
343                 d->ShowModal ();
344                 d->Destroy ();
345                 Config::instance()->write ();
346         }
347
348         void jobs_make_dcp (wxCommandEvent &)
349         {
350                 JobWrapper::make_dcp (this, film);
351         }
352         
353         void jobs_send_dcp_to_tms (wxCommandEvent &)
354         {
355                 film->send_dcp_to_tms ();
356         }
357
358         void jobs_show_dcp (wxCommandEvent &)
359         {
360 #ifdef __WXMSW__
361                 string d = film->directory();
362                 wstring w;
363                 w.assign (d.begin(), d.end());
364                 ShellExecute (0, L"open", w.c_str(), 0, 0, SW_SHOWDEFAULT);
365 #else
366                 int r = system ("which nautilus");
367                 if (WEXITSTATUS (r) == 0) {
368                         system (string ("nautilus " + film->directory()).c_str ());
369                 } else {
370                         int r = system ("which konqueror");
371                         if (WEXITSTATUS (r) == 0) {
372                                 system (string ("konqueror " + film->directory()).c_str ());
373                         }
374                 }
375 #endif          
376         }
377         
378         void jobs_examine_content (wxCommandEvent &)
379         {
380                 film->examine_content ();
381         }
382         
383         void help_about (wxCommandEvent &)
384         {
385                 wxAboutDialogInfo info;
386                 info.SetName (_("DVD-o-matic"));
387                 if (strcmp (dvdomatic_git_commit, "release") == 0) {
388                         info.SetVersion (std_to_wx (String::compose ("version %1", dvdomatic_version)));
389                 } else {
390                         info.SetVersion (std_to_wx (String::compose ("version %1 git %2", dvdomatic_version, dvdomatic_git_commit)));
391                 }
392                 info.SetDescription (_("Free, open-source DCP generation from almost anything."));
393                 info.SetCopyright (_("(C) 2012-2013 Carl Hetherington, Terrence Meiczinger, Paul Davis, Ole Laursen"));
394                 wxArrayString authors;
395                 authors.Add (wxT ("Carl Hetherington"));
396                 authors.Add (wxT ("Terrence Meiczinger"));
397                 authors.Add (wxT ("Paul Davis"));
398                 authors.Add (wxT ("Ole Laursen"));
399                 info.SetDevelopers (authors);
400                 info.SetWebSite (wxT ("http://carlh.net/software/dvdomatic"));
401                 wxAboutBox (info);
402         }
403 };
404
405 #if wxMINOR_VERSION == 9
406 static const wxCmdLineEntryDesc command_line_description[] = {
407         { wxCMD_LINE_OPTION, "l", "log", "set log level (silent, verbose or timing)", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
408         { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
409         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
410 };
411 #else
412 static const wxCmdLineEntryDesc command_line_description[] = {
413         { wxCMD_LINE_OPTION, wxT("l"), wxT("log"), wxT("set log level (silent, verbose or timing)"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
414         { wxCMD_LINE_PARAM, 0, 0, wxT("film to load"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
415         { wxCMD_LINE_NONE, wxT(""), wxT(""), wxT(""), wxCmdLineParamType (0), 0 }
416 };
417 #endif
418
419 class App : public wxApp
420 {
421         bool OnInit ()
422         {
423                 if (!wxApp::OnInit()) {
424                         return false;
425                 }
426                 
427 #ifdef DVDOMATIC_POSIX          
428                 unsetenv ("UBUNTU_MENUPROXY");
429 #endif          
430                 
431                 wxInitAllImageHandlers ();
432                 
433                 dvdomatic_setup ();
434
435                 if (!film_to_load.empty() && boost::filesystem::is_directory (film_to_load)) {
436                         try {
437                                 film.reset (new Film (film_to_load));
438                                 film->log()->set_level (log_level);
439                         } catch (exception& e) {
440                                 error_dialog (0, std_to_wx (String::compose ("Could not load film %1 (%2)", film_to_load, e.what())));
441                         }
442                 }
443
444                 Frame* f = new Frame (_("DVD-o-matic"));
445                 SetTopWindow (f);
446                 f->Maximize ();
447                 f->Show ();
448
449                 ui_signaller = new wxUISignaller (this);
450                 this->Connect (-1, wxEVT_IDLE, wxIdleEventHandler (App::idle));
451
452                 return true;
453         }
454
455         void OnInitCmdLine (wxCmdLineParser& parser)
456         {
457                 parser.SetDesc (command_line_description);
458                 parser.SetSwitchChars (wxT ("-"));
459         }
460
461         bool OnCmdLineParsed (wxCmdLineParser& parser)
462         {
463                 if (parser.GetParamCount() > 0) {
464                         film_to_load = wx_to_std (parser.GetParam(0));
465                 }
466
467                 wxString log;
468                 if (parser.Found(wxT("log"), &log)) {
469                         log_level = wx_to_std (log);
470                 }
471
472                 return true;
473         }
474
475         void idle (wxIdleEvent &)
476         {
477                 ui_signaller->ui_idle ();
478         }
479 };
480
481 IMPLEMENT_APP (App)