Merge master.
[dcpomatic.git] / src / tools / dcpomatic.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 <fstream>
22 #include <boost/filesystem.hpp>
23 #ifdef __WXMSW__
24 #include <shellapi.h>
25 #endif
26 #ifdef __WXOSX__
27 #include <ApplicationServices/ApplicationServices.h>
28 #endif
29 #include <wx/aboutdlg.h>
30 #include <wx/stdpaths.h>
31 #include <wx/cmdline.h>
32 #include "wx/film_viewer.h"
33 #include "wx/film_editor.h"
34 #include "wx/job_manager_view.h"
35 #include "wx/config_dialog.h"
36 #include "wx/job_wrapper.h"
37 #include "wx/wx_util.h"
38 #include "wx/new_film_dialog.h"
39 #include "wx/properties_dialog.h"
40 #include "wx/wx_ui_signaller.h"
41 #include "lib/film.h"
42 #include "lib/config.h"
43 #include "lib/util.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 std::ofstream;
56 using boost::shared_ptr;
57
58 static FilmEditor* film_editor = 0;
59 static FilmViewer* film_viewer = 0;
60 static shared_ptr<Film> film;
61 static std::string log_level;
62 static std::string film_to_load;
63 static std::string film_to_create;
64 static wxMenu* jobs_menu = 0;
65
66 static void set_menu_sensitivity ();
67
68 class FilmChangedDialog
69 {
70 public:
71         FilmChangedDialog ()
72         {
73                 _dialog = new wxMessageDialog (
74                         0,
75                         wxString::Format (_("Save changes to film \"%s\" before closing?"), std_to_wx (film->name ()).data()),
76                         _("Film changed"),
77                         wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
78                         );
79         }
80
81         ~FilmChangedDialog ()
82         {
83                 _dialog->Destroy ();
84         }
85
86         int run ()
87         {
88                 return _dialog->ShowModal ();
89         }
90
91 private:        
92         wxMessageDialog* _dialog;
93 };
94
95
96 void
97 maybe_save_then_delete_film ()
98 {
99         if (!film) {
100                 return;
101         }
102                         
103         if (film->dirty ()) {
104                 FilmChangedDialog d;
105                 switch (d.run ()) {
106                 case wxID_NO:
107                         break;
108                 case wxID_YES:
109                         film->write_metadata ();
110                         break;
111                 }
112         }
113         
114         film.reset ();
115 }
116
117 enum Sensitivity {
118         ALWAYS,
119         NEEDS_FILM
120 };
121
122 map<wxMenuItem*, Sensitivity> menu_items;
123         
124 void
125 add_item (wxMenu* menu, wxString text, int id, Sensitivity sens)
126 {
127         wxMenuItem* item = menu->Append (id, text);
128         menu_items.insert (make_pair (item, sens));
129 }
130
131 void
132 set_menu_sensitivity ()
133 {
134         for (map<wxMenuItem*, Sensitivity>::iterator i = menu_items.begin(); i != menu_items.end(); ++i) {
135                 if (i->second == NEEDS_FILM) {
136                         i->first->Enable (film != 0);
137                 } else {
138                         i->first->Enable (true);
139                 }
140         }
141 }
142
143 enum {
144         ID_file_new = 1,
145         ID_file_open,
146         ID_file_save,
147         ID_file_properties,
148         ID_jobs_make_dcp,
149         ID_jobs_send_dcp_to_tms,
150         ID_jobs_show_dcp,
151         ID_jobs_analyse_audio,
152 };
153
154 void
155 setup_menu (wxMenuBar* m)
156 {
157         wxMenu* file = new wxMenu;
158         add_item (file, _("New..."), ID_file_new, ALWAYS);
159         add_item (file, _("&Open..."), ID_file_open, ALWAYS);
160         file->AppendSeparator ();
161         add_item (file, _("&Save"), ID_file_save, NEEDS_FILM);
162         file->AppendSeparator ();
163         add_item (file, _("&Properties..."), ID_file_properties, NEEDS_FILM);
164 #ifndef __WXOSX__       
165         file->AppendSeparator ();
166 #endif  
167         add_item (file, _("&Exit"), wxID_EXIT, ALWAYS);
168
169 #ifdef __WXOSX__        
170         add_item (file, _("&Preferences..."), wxID_PREFERENCES, ALWAYS);
171 #else
172         wxMenu* edit = new wxMenu;
173         add_item (edit, _("&Preferences..."), wxID_PREFERENCES, ALWAYS);
174 #endif  
175
176         jobs_menu = new wxMenu;
177         add_item (jobs_menu, _("&Make DCP"), ID_jobs_make_dcp, NEEDS_FILM);
178         add_item (jobs_menu, _("&Send DCP to TMS"), ID_jobs_send_dcp_to_tms, NEEDS_FILM);
179         add_item (jobs_menu, _("S&how DCP"), ID_jobs_show_dcp, NEEDS_FILM);
180         jobs_menu->AppendSeparator ();
181         add_item (jobs_menu, _("&Analyse audio"), ID_jobs_analyse_audio, NEEDS_FILM);
182
183         wxMenu* help = new wxMenu;
184 #ifdef __WXOSX__        
185         add_item (help, _("About DVD-o-matic"), wxID_ABOUT, ALWAYS);
186 #else   
187         add_item (help, _("About"), wxID_ABOUT, ALWAYS);
188 #endif  
189
190         m->Append (file, _("&File"));
191 #ifndef __WXOSX__       
192         m->Append (edit, _("&Edit"));
193 #endif  
194         m->Append (jobs_menu, _("&Jobs"));
195         m->Append (help, _("&Help"));
196 }
197
198 bool
199 window_closed (wxCommandEvent &)
200 {
201         maybe_save_then_delete_film ();
202         return false;
203 }
204
205 class Frame : public wxFrame
206 {
207 public:
208         Frame (wxString const & title)
209                 : wxFrame (NULL, -1, title)
210         {
211                 wxMenuBar* bar = new wxMenuBar;
212                 setup_menu (bar);
213                 SetMenuBar (bar);
214
215                 Connect (ID_file_new, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_new));
216                 Connect (ID_file_open, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_open));
217                 Connect (ID_file_save, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_save));
218                 Connect (ID_file_properties, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_properties));
219                 Connect (wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_exit));
220                 Connect (wxID_PREFERENCES, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::edit_preferences));
221                 Connect (ID_jobs_make_dcp, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_make_dcp));
222                 Connect (ID_jobs_send_dcp_to_tms, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_send_dcp_to_tms));
223                 Connect (ID_jobs_show_dcp, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_show_dcp));
224                 Connect (ID_jobs_analyse_audio, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_analyse_audio));
225                 Connect (wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::help_about));
226
227                 Connect (wxID_ANY, wxEVT_MENU_OPEN, wxMenuEventHandler (Frame::menu_opened));
228
229                 film_editor = new FilmEditor (film, this);
230                 film_viewer = new FilmViewer (film, this);
231                 JobManagerView* job_manager_view = new JobManagerView (this, static_cast<JobManagerView::Buttons> (0));
232
233                 wxBoxSizer* right_sizer = new wxBoxSizer (wxVERTICAL);
234                 right_sizer->Add (film_viewer, 2, wxEXPAND | wxALL, 6);
235                 right_sizer->Add (job_manager_view, 1, wxEXPAND | wxALL, 6);
236
237                 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
238                 main_sizer->Add (film_editor, 1, wxEXPAND | wxALL, 6);
239                 main_sizer->Add (right_sizer, 2, wxEXPAND | wxALL, 6);
240
241                 set_menu_sensitivity ();
242
243                 film_editor->FileChanged.connect (bind (&Frame::file_changed, this, _1));
244                 if (film) {
245                         file_changed (film->directory ());
246                 } else {
247                         file_changed ("");
248                 }
249
250                 set_film ();
251                 SetSizer (main_sizer);
252         }
253
254 private:
255
256         void menu_opened (wxMenuEvent& ev)
257         {
258                 if (ev.GetMenu() != jobs_menu) {
259                         return;
260                 }
261
262                 bool const have_dcp = film && film->have_dcp();
263                 jobs_menu->Enable (ID_jobs_send_dcp_to_tms, have_dcp);
264                 jobs_menu->Enable (ID_jobs_show_dcp, have_dcp);
265         }
266
267         void set_film ()
268         {
269                 film_viewer->set_film (film);
270                 film_editor->set_film (film);
271                 set_menu_sensitivity ();
272         }
273
274         void file_changed (string f)
275         {
276                 stringstream s;
277                 s << wx_to_std (_("DCP-o-matic"));
278                 if (!f.empty ()) {
279                         s << " - " << f;
280                 }
281                 
282                 SetTitle (std_to_wx (s.str()));
283         }
284         
285         void file_new (wxCommandEvent &)
286         {
287                 NewFilmDialog* d = new NewFilmDialog (this);
288                 int const r = d->ShowModal ();
289                 
290                 if (r == wxID_OK) {
291
292                         if (boost::filesystem::exists (d->get_path()) && !boost::filesystem::is_empty(d->get_path())) {
293                                 if (!confirm_dialog (
294                                             this,
295                                             std_to_wx (
296                                                     String::compose (wx_to_std (_("The directory %1 already exists and is not empty.  "
297                                                                                   "Are you sure you want to use it?")),
298                                                                      d->get_path().c_str())
299                                                     )
300                                             )) {
301                                         return;
302                                 }
303                         }
304                         
305                         maybe_save_then_delete_film ();
306                         film.reset (new Film (d->get_path ()));
307                         film->write_metadata ();
308                         film->log()->set_level (log_level);
309                         film->set_name (boost::filesystem::path (d->get_path()).filename().generic_string());
310                         set_film ();
311                 }
312                 
313                 d->Destroy ();
314         }
315
316         void file_open (wxCommandEvent &)
317         {
318                 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
319                 int r;
320                 while (1) {
321                         r = c->ShowModal ();
322                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
323                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
324                         } else {
325                                 break;
326                         }
327                 }
328                         
329                 if (r == wxID_OK) {
330                         maybe_save_then_delete_film ();
331                         try {
332                                 film.reset (new Film (wx_to_std (c->GetPath ())));
333                                 film->log()->set_level (log_level);
334                                 set_film ();
335                         } catch (std::exception& e) {
336                                 wxString p = c->GetPath ();
337                                 wxCharBuffer b = p.ToUTF8 ();
338                                 error_dialog (this, wxString::Format (_("Could not open film at %s (%s)"), p.data(), std_to_wx (e.what()).data()));
339                         }
340                 }
341
342                 c->Destroy ();
343         }
344
345         void file_save (wxCommandEvent &)
346         {
347                 film->write_metadata ();
348         }
349
350         void file_properties (wxCommandEvent &)
351         {
352                 PropertiesDialog* d = new PropertiesDialog (this, film);
353                 d->ShowModal ();
354                 d->Destroy ();
355         }
356         
357         void file_exit (wxCommandEvent &)
358         {
359                 maybe_save_then_delete_film ();
360                 Close (true);
361         }
362
363         void edit_preferences (wxCommandEvent &)
364         {
365                 ConfigDialog* d = new ConfigDialog (this);
366                 d->ShowModal ();
367                 d->Destroy ();
368                 Config::instance()->write ();
369         }
370
371         void jobs_make_dcp (wxCommandEvent &)
372         {
373                 JobWrapper::make_dcp (this, film);
374         }
375         
376         void jobs_send_dcp_to_tms (wxCommandEvent &)
377         {
378                 film->send_dcp_to_tms ();
379         }
380
381         void jobs_show_dcp (wxCommandEvent &)
382         {
383 #ifdef __WXMSW__
384                 string d = film->directory();
385                 wstring w;
386                 w.assign (d.begin(), d.end());
387                 ShellExecute (0, L"open", w.c_str(), 0, 0, SW_SHOWDEFAULT);
388 #else
389                 int r = system ("which nautilus");
390                 if (WEXITSTATUS (r) == 0) {
391                         system (string ("nautilus " + film->directory()).c_str ());
392                 } else {
393                         int r = system ("which konqueror");
394                         if (WEXITSTATUS (r) == 0) {
395                                 system (string ("konqueror " + film->directory()).c_str ());
396                         }
397                 }
398 #endif          
399         }
400
401         void jobs_analyse_audio (wxCommandEvent &)
402         {
403                 film->analyse_audio ();
404         }
405         
406         void help_about (wxCommandEvent &)
407         {
408                 wxAboutDialogInfo info;
409                 info.SetName (_("DCP-o-matic"));
410                 if (strcmp (dcpomatic_git_commit, "release") == 0) {
411                         info.SetVersion (std_to_wx (String::compose ("version %1", dcpomatic_version)));
412                 } else {
413                         info.SetVersion (std_to_wx (String::compose ("version %1 git %2", dcpomatic_version, dcpomatic_git_commit)));
414                 }
415                 info.SetDescription (_("Free, open-source DCP generation from almost anything."));
416                 info.SetCopyright (_("(C) 2012-2013 Carl Hetherington, Terrence Meiczinger, Paul Davis, Ole Laursen"));
417
418                 wxArrayString authors;
419                 authors.Add (wxT ("Carl Hetherington"));
420                 authors.Add (wxT ("Terrence Meiczinger"));
421                 authors.Add (wxT ("Paul Davis"));
422                 authors.Add (wxT ("Ole Laursen"));
423                 info.SetDevelopers (authors);
424
425                 wxArrayString translators;
426                 translators.Add (wxT ("Olivier Perriere"));
427                 translators.Add (wxT ("Lilian Lefranc"));
428                 translators.Add (wxT ("Thierry Journet"));
429                 translators.Add (wxT ("Massimiliano Broggi"));
430                 translators.Add (wxT ("Manuel AC"));
431                 translators.Add (wxT ("Adam Klotblixt"));
432                 info.SetTranslators (translators);
433                 
434                 info.SetWebSite (wxT ("http://carlh.net/software/dcpomatic"));
435                 wxAboutBox (info);
436         }
437 };
438
439 #if wxMINOR_VERSION == 9
440 static const wxCmdLineEntryDesc command_line_description[] = {
441         { wxCMD_LINE_OPTION, "l", "log", "set log level (silent, verbose or timing)", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
442         { wxCMD_LINE_SWITCH, "n", "new", "create new film", wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
443         { wxCMD_LINE_PARAM, 0, 0, "film to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
444         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
445 };
446 #else
447 static const wxCmdLineEntryDesc command_line_description[] = {
448         { wxCMD_LINE_OPTION, wxT("l"), wxT("log"), wxT("set log level (silent, verbose or timing)"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
449         { wxCMD_LINE_SWITCH, wxT("n"), wxT("new"), wxT("create new film"), wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
450         { wxCMD_LINE_PARAM, 0, 0, wxT("film to load or create"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
451         { wxCMD_LINE_NONE, wxT(""), wxT(""), wxT(""), wxCmdLineParamType (0), 0 }
452 };
453 #endif
454
455 class App : public wxApp
456 {
457         bool OnInit ()
458         {
459                 if (!wxApp::OnInit()) {
460                         return false;
461                 }
462                 
463 #ifdef DCPOMATIC_POSIX          
464                 unsetenv ("UBUNTU_MENUPROXY");
465 #endif
466
467 #ifdef __WXOSX__                
468                 ProcessSerialNumber serial;
469                 GetCurrentProcess (&serial);
470                 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
471 #endif          
472
473                 wxInitAllImageHandlers ();
474
475                 /* Enable i18n; this will create a Config object
476                    to look for a force-configured language.  This Config
477                    object will be wrong, however, because dcpomatic_setup
478                    hasn't yet been called and there aren't any scalers, filters etc.
479                    set up yet.
480                 */
481                 dcpomatic_setup_i18n ();
482
483                 /* Set things up, including scalers / filters etc.
484                    which will now be internationalised correctly.
485                 */
486                 dcpomatic_setup ();
487
488                 /* Force the configuration to be re-loaded correctly next
489                    time it is needed.
490                 */
491                 Config::drop ();
492
493                 if (!film_to_load.empty() && boost::filesystem::is_directory (film_to_load)) {
494                         try {
495                                 film.reset (new Film (film_to_load));
496                                 film->read_metadata ();
497                                 film->log()->set_level (log_level);
498                         } catch (exception& e) {
499                                 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load film %1 (%2)")), film_to_load, e.what())));
500                         }
501                 }
502
503                 if (!film_to_create.empty ()) {
504                         film.reset (new Film (film_to_create));
505                         film->write_metadata ();
506                         film->log()->set_level (log_level);
507                         film->set_name (boost::filesystem::path (film_to_create).filename().generic_string ());
508                 }
509
510                 Frame* f = new Frame (_("DCP-o-matic"));
511                 SetTopWindow (f);
512                 f->Maximize ();
513                 f->Show ();
514
515                 ui_signaller = new wxUISignaller (this);
516                 this->Connect (-1, wxEVT_IDLE, wxIdleEventHandler (App::idle));
517
518                 return true;
519         }
520
521         void OnInitCmdLine (wxCmdLineParser& parser)
522         {
523                 parser.SetDesc (command_line_description);
524                 parser.SetSwitchChars (wxT ("-"));
525         }
526
527         bool OnCmdLineParsed (wxCmdLineParser& parser)
528         {
529                 if (parser.GetParamCount() > 0) {
530                         if (parser.Found (wxT ("new"))) {
531                                 film_to_create = wx_to_std (parser.GetParam (0));
532                         } else {
533                                 film_to_load = wx_to_std (parser.GetParam(0));
534                         }
535                 }
536
537                 wxString log;
538                 if (parser.Found (wxT ("log"), &log)) {
539                         log_level = wx_to_std (log);
540                 }
541
542                 return true;
543         }
544
545         void idle (wxIdleEvent &)
546         {
547                 ui_signaller->ui_idle ();
548         }
549 };
550
551 IMPLEMENT_APP (App)