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