tabs -> spaces.
[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         /* Not defined */
94         FilmChangedDialog (FilmChangedDialog const &);
95         
96         wxMessageDialog* _dialog;
97 };
98
99
100 void
101 maybe_save_then_delete_film ()
102 {
103         if (!film) {
104                 return;
105         }
106                         
107         if (film->dirty ()) {
108                 FilmChangedDialog d;
109                 switch (d.run ()) {
110                 case wxID_NO:
111                         break;
112                 case wxID_YES:
113                         film->write_metadata ();
114                         break;
115                 }
116         }
117         
118         film.reset ();
119 }
120
121 enum Sensitivity {
122         ALWAYS,
123         NEEDS_FILM
124 };
125
126 map<wxMenuItem*, Sensitivity> menu_items;
127         
128 void
129 add_item (wxMenu* menu, wxString text, int id, Sensitivity sens)
130 {
131         wxMenuItem* item = menu->Append (id, text);
132         menu_items.insert (make_pair (item, sens));
133 }
134
135 void
136 set_menu_sensitivity ()
137 {
138         for (map<wxMenuItem*, Sensitivity>::iterator i = menu_items.begin(); i != menu_items.end(); ++i) {
139                 if (i->second == NEEDS_FILM) {
140                         i->first->Enable (film != 0);
141                 } else {
142                         i->first->Enable (true);
143                 }
144         }
145 }
146
147 enum {
148         ID_file_new = 1,
149         ID_file_open,
150         ID_file_save,
151         ID_file_properties,
152         ID_jobs_make_dcp,
153         ID_jobs_send_dcp_to_tms,
154         ID_jobs_show_dcp,
155 };
156
157 void
158 setup_menu (wxMenuBar* m)
159 {
160         wxMenu* file = new wxMenu;
161         add_item (file, _("New..."), ID_file_new, ALWAYS);
162         add_item (file, _("&Open..."), ID_file_open, ALWAYS);
163         file->AppendSeparator ();
164         add_item (file, _("&Save"), ID_file_save, NEEDS_FILM);
165         file->AppendSeparator ();
166         add_item (file, _("&Properties..."), ID_file_properties, NEEDS_FILM);
167 #ifndef __WXOSX__       
168         file->AppendSeparator ();
169 #endif
170
171 #ifdef __WXOSX__        
172         add_item (file, _("&Exit"), wxID_EXIT, ALWAYS);
173 #else
174         add_item (file, _("&Quit"), wxID_EXIT, ALWAYS);
175 #endif  
176         
177
178 #ifdef __WXOSX__        
179         add_item (file, _("&Preferences..."), wxID_PREFERENCES, ALWAYS);
180 #else
181         wxMenu* edit = new wxMenu;
182         add_item (edit, _("&Preferences..."), wxID_PREFERENCES, ALWAYS);
183 #endif  
184
185         jobs_menu = new wxMenu;
186         add_item (jobs_menu, _("&Make DCP"), ID_jobs_make_dcp, NEEDS_FILM);
187         add_item (jobs_menu, _("&Send DCP to TMS"), ID_jobs_send_dcp_to_tms, NEEDS_FILM);
188         add_item (jobs_menu, _("S&how DCP"), ID_jobs_show_dcp, NEEDS_FILM);
189
190         wxMenu* help = new wxMenu;
191 #ifdef __WXOSX__        
192         add_item (help, _("About DCP-o-matic"), wxID_ABOUT, ALWAYS);
193 #else   
194         add_item (help, _("About"), wxID_ABOUT, ALWAYS);
195 #endif  
196
197         m->Append (file, _("&File"));
198 #ifndef __WXOSX__       
199         m->Append (edit, _("&Edit"));
200 #endif  
201         m->Append (jobs_menu, _("&Jobs"));
202         m->Append (help, _("&Help"));
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 (wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::help_about));
225
226                 Connect (wxID_ANY, wxEVT_MENU_OPEN, wxMenuEventHandler (Frame::menu_opened));
227
228                 film_editor = new FilmEditor (film, this);
229                 film_viewer = new FilmViewer (film, this);
230                 JobManagerView* job_manager_view = new JobManagerView (this, static_cast<JobManagerView::Buttons> (0));
231
232                 wxBoxSizer* right_sizer = new wxBoxSizer (wxVERTICAL);
233                 right_sizer->Add (film_viewer, 2, wxEXPAND | wxALL, 6);
234                 right_sizer->Add (job_manager_view, 1, wxEXPAND | wxALL, 6);
235
236                 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
237                 main_sizer->Add (film_editor, 1, wxEXPAND | wxALL, 6);
238                 main_sizer->Add (right_sizer, 2, wxEXPAND | wxALL, 6);
239
240                 set_menu_sensitivity ();
241
242                 film_editor->FileChanged.connect (bind (&Frame::file_changed, this, _1));
243                 if (film) {
244                         file_changed (film->directory ());
245                 } else {
246                         file_changed ("");
247                 }
248
249                 set_film ();
250                 SetSizer (main_sizer);
251         }
252
253 private:
254
255         void menu_opened (wxMenuEvent& ev)
256         {
257                 if (ev.GetMenu() != jobs_menu) {
258                         return;
259                 }
260
261                 bool const have_dcp = film && film->have_dcp();
262                 jobs_menu->Enable (ID_jobs_send_dcp_to_tms, have_dcp);
263                 jobs_menu->Enable (ID_jobs_show_dcp, have_dcp);
264         }
265
266         void set_film ()
267         {
268                 film_viewer->set_film (film);
269                 film_editor->set_film (film);
270                 set_menu_sensitivity ();
271         }
272
273         void file_changed (string f)
274         {
275                 stringstream s;
276                 s << wx_to_std (_("DCP-o-matic"));
277                 if (!f.empty ()) {
278                         s << " - " << f;
279                 }
280                 
281                 SetTitle (std_to_wx (s.str()));
282         }
283         
284         void file_new (wxCommandEvent &)
285         {
286                 NewFilmDialog* d = new NewFilmDialog (this);
287                 int const r = d->ShowModal ();
288                 
289                 if (r == wxID_OK) {
290
291                         if (boost::filesystem::is_directory (d->get_path()) && !boost::filesystem::is_empty(d->get_path())) {
292                                 if (!confirm_dialog (
293                                             this,
294                                             std_to_wx (
295                                                     String::compose (wx_to_std (_("The directory %1 already exists and is not empty.  "
296                                                                                   "Are you sure you want to use it?")),
297                                                                      d->get_path().c_str())
298                                                     )
299                                             )) {
300                                         return;
301                                 }
302                         } else if (boost::filesystem::is_regular_file (d->get_path())) {
303                                 error_dialog (
304                                         this,
305                                         String::compose (wx_to_std (_("%1 already exists as a file, so you cannot use it for a new film.")), d->get_path().c_str())
306                                         );
307                                 return;
308                         }
309                         
310                         maybe_save_then_delete_film ();
311                         film.reset (new Film (d->get_path ()));
312                         film->write_metadata ();
313                         film->log()->set_level (log_level);
314                         film->set_name (boost::filesystem::path (d->get_path()).filename().generic_string());
315                         set_film ();
316                 }
317                 
318                 d->Destroy ();
319         }
320
321         void file_open (wxCommandEvent &)
322         {
323                 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
324                 int r;
325                 while (1) {
326                         r = c->ShowModal ();
327                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
328                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
329                         } else {
330                                 break;
331                         }
332                 }
333                         
334                 if (r == wxID_OK) {
335                         maybe_save_then_delete_film ();
336                         try {
337                                 film.reset (new Film (wx_to_std (c->GetPath ())));
338                                 film->read_metadata ();
339                                 film->log()->set_level (log_level);
340                                 set_film ();
341                         } catch (std::exception& e) {
342                                 wxString p = c->GetPath ();
343                                 wxCharBuffer b = p.ToUTF8 ();
344                                 error_dialog (this, wxString::Format (_("Could not open film at %s (%s)"), p.data(), std_to_wx (e.what()).data()));
345                         }
346                 }
347
348                 c->Destroy ();
349         }
350
351         void file_save (wxCommandEvent &)
352         {
353                 film->write_metadata ();
354         }
355
356         void file_properties (wxCommandEvent &)
357         {
358                 PropertiesDialog* d = new PropertiesDialog (this, film);
359                 d->ShowModal ();
360                 d->Destroy ();
361         }
362         
363         void file_exit (wxCommandEvent &)
364         {
365                 maybe_save_then_delete_film ();
366                 Close (true);
367         }
368
369         void edit_preferences (wxCommandEvent &)
370         {
371                 ConfigDialog* d = new ConfigDialog (this);
372                 d->ShowModal ();
373                 d->Destroy ();
374                 Config::instance()->write ();
375         }
376
377         void jobs_make_dcp (wxCommandEvent &)
378         {
379                 JobWrapper::make_dcp (this, film);
380         }
381         
382         void jobs_send_dcp_to_tms (wxCommandEvent &)
383         {
384                 film->send_dcp_to_tms ();
385         }
386
387         void jobs_show_dcp (wxCommandEvent &)
388         {
389 #ifdef __WXMSW__
390                 string d = film->directory();
391                 wstring w;
392                 w.assign (d.begin(), d.end());
393                 ShellExecute (0, L"open", w.c_str(), 0, 0, SW_SHOWDEFAULT);
394 #else
395                 int r = system ("which nautilus");
396                 if (WEXITSTATUS (r) == 0) {
397                         system (string ("nautilus " + film->directory()).c_str ());
398                 } else {
399                         int r = system ("which konqueror");
400                         if (WEXITSTATUS (r) == 0) {
401                                 system (string ("konqueror " + film->directory()).c_str ());
402                         }
403                 }
404 #endif          
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)