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