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