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