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