Bump version
[dcpomatic.git] / src / tools / dcpomatic.cc
1 /*
2     Copyright (C) 2012-2014 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/preferences.h>
33 #include <libdcp/exceptions.h>
34 #include "wx/film_viewer.h"
35 #include "wx/film_editor.h"
36 #include "wx/job_manager_view.h"
37 #include "wx/config_dialog.h"
38 #include "wx/job_wrapper.h"
39 #include "wx/wx_util.h"
40 #include "wx/new_film_dialog.h"
41 #include "wx/properties_dialog.h"
42 #include "wx/wx_ui_signaller.h"
43 #include "wx/about_dialog.h"
44 #include "wx/kdm_dialog.h"
45 #include "wx/servers_list_dialog.h"
46 #include "wx/hints_dialog.h"
47 #include "wx/update_dialog.h"
48 #include "lib/film.h"
49 #include "lib/config.h"
50 #include "lib/util.h"
51 #include "lib/version.h"
52 #include "lib/ui_signaller.h"
53 #include "lib/log.h"
54 #include "lib/job_manager.h"
55 #include "lib/transcode_job.h"
56 #include "lib/exceptions.h"
57 #include "lib/cinema.h"
58 #include "lib/kdm.h"
59 #include "lib/send_kdm_email_job.h"
60 #include "lib/server_finder.h"
61 #include "lib/update.h"
62 #include "lib/content_factory.h"
63
64 using std::cout;
65 using std::string;
66 using std::wstring;
67 using std::map;
68 using std::make_pair;
69 using std::list;
70 using std::exception;
71 using boost::shared_ptr;
72 using boost::dynamic_pointer_cast;
73
74 static shared_ptr<Film> film;
75 static std::string film_to_load;
76 static std::string film_to_create;
77 static std::string content_to_add;
78 static wxMenu* jobs_menu = 0;
79
80 // #define DCPOMATIC_WINDOWS_CONSOLE 1
81
82 class FilmChangedDialog
83 {
84 public:
85         FilmChangedDialog ()
86         {
87                 _dialog = new wxMessageDialog (
88                         0,
89                         wxString::Format (_("Save changes to film \"%s\" before closing?"), std_to_wx (film->name ()).data()),
90                         _("Film changed"),
91                         wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
92                         );
93         }
94
95         ~FilmChangedDialog ()
96         {
97                 _dialog->Destroy ();
98         }
99
100         int run ()
101         {
102                 return _dialog->ShowModal ();
103         }
104
105 private:
106         /* Not defined */
107         FilmChangedDialog (FilmChangedDialog const &);
108         
109         wxMessageDialog* _dialog;
110 };
111
112
113 static void
114 maybe_save_then_delete_film ()
115 {
116         if (!film) {
117                 return;
118         }
119                         
120         if (film->dirty ()) {
121                 FilmChangedDialog d;
122                 switch (d.run ()) {
123                 case wxID_NO:
124                         break;
125                 case wxID_YES:
126                         film->write_metadata ();
127                         break;
128                 }
129         }
130         
131         film.reset ();
132 }
133
134 static void
135 check_film_state_version (int v)
136 {
137         if (v == 4) {
138                 error_dialog (
139                         0,
140                         _("This film was created with an old version of DVD-o-matic and may not load correctly "
141                           "in this version.  Please check the film's settings carefully.")
142                         );
143         }
144 }
145
146 static void
147 load_film (boost::filesystem::path file)
148 {
149         film.reset (new Film (file));
150         list<string> const notes = film->read_metadata ();
151         check_film_state_version (film->state_version ());
152         for (list<string>::const_iterator i = notes.begin(); i != notes.end(); ++i) {
153                 error_dialog (0, std_to_wx (*i));
154         }
155 }
156
157 #define ALWAYS                       0x0
158 #define NEEDS_FILM                   0x1
159 #define NOT_DURING_DCP_CREATION      0x2
160 #define NEEDS_CPL                    0x4
161 #define NEEDS_SELECTED_VIDEO_CONTENT 0x8
162
163 map<wxMenuItem*, int> menu_items;
164         
165 static void
166 add_item (wxMenu* menu, wxString text, int id, int sens)
167 {
168         wxMenuItem* item = menu->Append (id, text);
169         menu_items.insert (make_pair (item, sens));
170 }
171
172 enum {
173         ID_file_new = 1,
174         ID_file_open,
175         ID_file_save,
176         ID_file_properties,
177         ID_content_scale_to_fit_width,
178         ID_content_scale_to_fit_height,
179         ID_jobs_make_dcp,
180         ID_jobs_make_kdms,
181         ID_jobs_send_dcp_to_tms,
182         ID_jobs_show_dcp,
183         ID_tools_hints,
184         ID_tools_encoding_servers,
185         ID_tools_check_for_updates
186 };
187
188 static void
189 setup_menu (wxMenuBar* m)
190 {
191         wxMenu* file = new wxMenu;
192         add_item (file, _("New..."), ID_file_new, ALWAYS);
193         add_item (file, _("&Open..."), ID_file_open, ALWAYS);
194         file->AppendSeparator ();
195         add_item (file, _("&Save"), ID_file_save, NEEDS_FILM);
196         file->AppendSeparator ();
197         add_item (file, _("&Properties..."), ID_file_properties, NEEDS_FILM);
198 #ifndef __WXOSX__       
199         file->AppendSeparator ();
200 #endif
201
202 #ifdef __WXOSX__        
203         add_item (file, _("&Exit"), wxID_EXIT, ALWAYS);
204 #else
205         add_item (file, _("&Quit"), wxID_EXIT, ALWAYS);
206 #endif  
207         
208
209 #ifdef __WXOSX__        
210         add_item (file, _("&Preferences..."), wxID_PREFERENCES, ALWAYS);
211 #else
212         wxMenu* edit = new wxMenu;
213         add_item (edit, _("&Preferences..."), wxID_PREFERENCES, ALWAYS);
214 #endif
215
216         wxMenu* content = new wxMenu;
217         add_item (content, _("Scale to fit &width"), ID_content_scale_to_fit_width, NEEDS_FILM | NEEDS_SELECTED_VIDEO_CONTENT);
218         add_item (content, _("Scale to fit &height"), ID_content_scale_to_fit_height, NEEDS_FILM | NEEDS_SELECTED_VIDEO_CONTENT);
219
220         jobs_menu = new wxMenu;
221         add_item (jobs_menu, _("&Make DCP"), ID_jobs_make_dcp, NEEDS_FILM | NOT_DURING_DCP_CREATION);
222         add_item (jobs_menu, _("Make &KDMs..."), ID_jobs_make_kdms, NEEDS_FILM);
223         add_item (jobs_menu, _("&Send DCP to TMS"), ID_jobs_send_dcp_to_tms, NEEDS_FILM | NOT_DURING_DCP_CREATION | NEEDS_CPL);
224         add_item (jobs_menu, _("S&how DCP"), ID_jobs_show_dcp, NEEDS_FILM | NOT_DURING_DCP_CREATION | NEEDS_CPL);
225
226         wxMenu* tools = new wxMenu;
227         add_item (tools, _("Hints..."), ID_tools_hints, 0);
228         add_item (tools, _("Encoding servers..."), ID_tools_encoding_servers, 0);
229         add_item (tools, _("Check for updates"), ID_tools_check_for_updates, 0);
230
231         wxMenu* help = new wxMenu;
232 #ifdef __WXOSX__        
233         add_item (help, _("About DCP-o-matic"), wxID_ABOUT, ALWAYS);
234 #else   
235         add_item (help, _("About"), wxID_ABOUT, ALWAYS);
236 #endif  
237
238         m->Append (file, _("&File"));
239 #ifndef __WXOSX__       
240         m->Append (edit, _("&Edit"));
241 #endif
242         m->Append (content, _("&Content"));
243         m->Append (jobs_menu, _("&Jobs"));
244         m->Append (tools, _("&Tools"));
245         m->Append (help, _("&Help"));
246 }
247
248 class Frame : public wxFrame
249 {
250 public:
251         Frame (wxString const & title)
252                 : wxFrame (NULL, -1, title)
253                 , _hints_dialog (0)
254                 , _servers_list_dialog (0)
255                 , _config_dialog (0)
256         {
257 #if defined(DCPOMATIC_WINDOWS) && defined(DCPOMATIC_WINDOWS_CONSOLE)
258                 AllocConsole();
259                 
260                 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
261                 int hCrt = _open_osfhandle((intptr_t) handle_out, _O_TEXT);
262                 FILE* hf_out = _fdopen(hCrt, "w");
263                 setvbuf(hf_out, NULL, _IONBF, 1);
264                 *stdout = *hf_out;
265                 
266                 HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
267                 hCrt = _open_osfhandle((intptr_t) handle_in, _O_TEXT);
268                 FILE* hf_in = _fdopen(hCrt, "r");
269                 setvbuf(hf_in, NULL, _IONBF, 128);
270                 *stdin = *hf_in;
271 #endif
272
273                 wxMenuBar* bar = new wxMenuBar;
274                 setup_menu (bar);
275                 SetMenuBar (bar);
276
277                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::file_new, this),                ID_file_new);
278                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::file_open, this),               ID_file_open);
279                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::file_save, this),               ID_file_save);
280                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::file_properties, this),         ID_file_properties);
281                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::file_exit, this),               wxID_EXIT);
282                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::edit_preferences, this),        wxID_PREFERENCES);
283                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::content_scale_to_fit_width, this), ID_content_scale_to_fit_width);
284                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::content_scale_to_fit_height, this), ID_content_scale_to_fit_height);
285                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::jobs_make_dcp, this),           ID_jobs_make_dcp);
286                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::jobs_make_kdms, this),          ID_jobs_make_kdms);
287                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::jobs_send_dcp_to_tms, this),    ID_jobs_send_dcp_to_tms);
288                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::jobs_show_dcp, this),           ID_jobs_show_dcp);
289                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::tools_hints, this),             ID_tools_hints);
290                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::tools_encoding_servers, this),  ID_tools_encoding_servers);
291                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::tools_check_for_updates, this), ID_tools_check_for_updates);
292                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::help_about, this),              wxID_ABOUT);
293
294                 Bind (wxEVT_CLOSE_WINDOW, boost::bind (&Frame::close, this, _1));
295
296                 /* Use a panel as the only child of the Frame so that we avoid
297                    the dark-grey background on Windows.
298                 */
299                 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
300
301                 _film_editor = new FilmEditor (film, overall_panel);
302                 _film_viewer = new FilmViewer (film, overall_panel);
303                 JobManagerView* job_manager_view = new JobManagerView (overall_panel, static_cast<JobManagerView::Buttons> (0));
304
305                 wxBoxSizer* right_sizer = new wxBoxSizer (wxVERTICAL);
306                 right_sizer->Add (_film_viewer, 2, wxEXPAND | wxALL, 6);
307                 right_sizer->Add (job_manager_view, 1, wxEXPAND | wxALL, 6);
308
309                 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
310                 main_sizer->Add (_film_editor, 1, wxEXPAND | wxALL, 6);
311                 main_sizer->Add (right_sizer, 2, wxEXPAND | wxALL, 6);
312
313                 set_menu_sensitivity ();
314
315                 _film_editor->FileChanged.connect (bind (&Frame::file_changed, this, _1));
316                 if (film) {
317                         file_changed (film->directory ());
318                 } else {
319                         file_changed ("");
320                 }
321
322                 JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&Frame::set_menu_sensitivity, this));
323
324                 set_film ();
325                 overall_panel->SetSizer (main_sizer);
326         }
327
328 private:
329
330         void set_film ()
331         {
332                 _film_viewer->set_film (film);
333                 _film_editor->set_film (film);
334                 set_menu_sensitivity ();
335         }
336
337         void file_changed (boost::filesystem::path f)
338         {
339                 string s = wx_to_std (_("DCP-o-matic"));
340                 if (!f.empty ()) {
341                         s += " - " + f.string ();
342                 }
343                 
344                 SetTitle (std_to_wx (s));
345         }
346         
347         void file_new ()
348         {
349                 NewFilmDialog* d = new NewFilmDialog (this);
350                 int const r = d->ShowModal ();
351                 
352                 if (r == wxID_OK) {
353
354                         if (boost::filesystem::is_directory (d->get_path()) && !boost::filesystem::is_empty(d->get_path())) {
355                                 if (!confirm_dialog (
356                                             this,
357                                             std_to_wx (
358                                                     String::compose (wx_to_std (_("The directory %1 already exists and is not empty.  "
359                                                                                   "Are you sure you want to use it?")),
360                                                                      d->get_path().string().c_str())
361                                                     )
362                                             )) {
363                                         return;
364                                 }
365                         } else if (boost::filesystem::is_regular_file (d->get_path())) {
366                                 error_dialog (
367                                         this,
368                                         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())
369                                         );
370                                 return;
371                         }
372                         
373                         maybe_save_then_delete_film ();
374                         film.reset (new Film (d->get_path ()));
375                         film->write_metadata ();
376                         film->set_name (boost::filesystem::path (d->get_path()).filename().generic_string());
377                         set_film ();
378                 }
379                 
380                 d->Destroy ();
381         }
382
383         void file_open ()
384         {
385                 wxDirDialog* c = new wxDirDialog (
386                         this,
387                         _("Select film to open"),
388                         std_to_wx (Config::instance()->default_directory_or (wx_to_std (wxStandardPaths::Get().GetDocumentsDir())).string ()),
389                         wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
390                         );
391                 
392                 int r;
393                 while (true) {
394                         r = c->ShowModal ();
395                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
396                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
397                         } else {
398                                 break;
399                         }
400                 }
401                         
402                 if (r == wxID_OK) {
403                         maybe_save_then_delete_film ();
404                         try {
405                                 load_film (wx_to_std (c->GetPath ()));
406                                 set_film ();
407                         } catch (std::exception& e) {
408                                 wxString p = c->GetPath ();
409                                 wxCharBuffer b = p.ToUTF8 ();
410                                 error_dialog (this, wxString::Format (_("Could not open film at %s (%s)"), p.data(), std_to_wx (e.what()).data()));
411                         }
412                 }
413
414                 c->Destroy ();
415         }
416
417         void file_save ()
418         {
419                 film->write_metadata ();
420         }
421
422         void file_properties ()
423         {
424                 PropertiesDialog* d = new PropertiesDialog (this, film);
425                 d->ShowModal ();
426                 d->Destroy ();
427         }
428         
429         void file_exit ()
430         {
431                 /* false here allows the close handler to veto the close request */
432                 Close (false);
433         }
434
435         void edit_preferences ()
436         {
437                 if (!_config_dialog) {
438                         _config_dialog = create_config_dialog ();
439                 }
440                 _config_dialog->Show (this);
441         }
442
443         void jobs_make_dcp ()
444         {
445                 double required;
446                 double available;
447
448                 if (!film->should_be_enough_disk_space (required, available)) {
449                         if (!confirm_dialog (this, wxString::Format (_("The DCP for this film will take up about %.1f Gb, and the disk that you are using only has %.1f Gb available.  Do you want to continue anyway?"), required, available))) {
450                                 return;
451                         }
452                 }
453                 
454                 JobWrapper::make_dcp (this, film);
455         }
456
457         void jobs_make_kdms ()
458         {
459                 if (!film) {
460                         return;
461                 }
462                 
463                 KDMDialog* d = new KDMDialog (this, film);
464                 if (d->ShowModal () != wxID_OK) {
465                         d->Destroy ();
466                         return;
467                 }
468
469                 try {
470                         if (d->write_to ()) {
471                                 write_kdm_files (film, d->screens (), d->cpl (), d->from (), d->until (), d->formulation (), d->directory ());
472                         } else {
473                                 JobManager::instance()->add (
474                                         shared_ptr<Job> (new SendKDMEmailJob (film, d->screens (), d->cpl (), d->from (), d->until (), d->formulation ()))
475                                         );
476                         }
477                 } catch (libdcp::NotEncryptedError& e) {
478                         error_dialog (this, _("CPL's content is not encrypted."));
479                 } catch (exception& e) {
480                         error_dialog (this, e.what ());
481                 } catch (...) {
482                         error_dialog (this, _("An unknown exeception occurred."));
483                 }
484         
485                 d->Destroy ();
486         }
487
488         void content_scale_to_fit_width ()
489         {
490                 VideoContentList vc = _film_editor->selected_video_content ();
491                 for (VideoContentList::iterator i = vc.begin(); i != vc.end(); ++i) {
492                         (*i)->scale_and_crop_to_fit_width ();
493                 }
494         }
495
496         void content_scale_to_fit_height ()
497         {
498                 VideoContentList vc = _film_editor->selected_video_content ();
499                 for (VideoContentList::iterator i = vc.begin(); i != vc.end(); ++i) {
500                         (*i)->scale_and_crop_to_fit_height ();
501                 }
502         }
503         
504         void jobs_send_dcp_to_tms ()
505         {
506                 film->send_dcp_to_tms ();
507         }
508
509         void jobs_show_dcp ()
510         {
511 #ifdef __WXMSW__
512                 string d = film->directory().string ();
513                 wstring w;
514                 w.assign (d.begin(), d.end());
515                 ShellExecute (0, L"open", w.c_str(), 0, 0, SW_SHOWDEFAULT);
516 #else
517                 int r = system ("which nautilus");
518                 if (WEXITSTATUS (r) == 0) {
519                         r = system (string ("nautilus " + film->directory().string()).c_str ());
520                         if (WEXITSTATUS (r)) {
521                                 error_dialog (this, _("Could not show DCP (could not run nautilus)"));
522                         }
523                 } else {
524                         int r = system ("which konqueror");
525                         if (WEXITSTATUS (r) == 0) {
526                                 r = system (string ("konqueror " + film->directory().string()).c_str ());
527                                 if (WEXITSTATUS (r)) {
528                                         error_dialog (this, _("Could not show DCP (could not run konqueror)"));
529                                 }
530                         }
531                 }
532 #endif          
533         }
534
535         void tools_hints ()
536         {
537                 if (!_hints_dialog) {
538                         _hints_dialog = new HintsDialog (this, film);
539                 }
540
541                 _hints_dialog->Show ();
542         }
543
544         void tools_encoding_servers ()
545         {
546                 if (!_servers_list_dialog) {
547                         _servers_list_dialog = new ServersListDialog (this);
548                 }
549
550                 _servers_list_dialog->Show ();
551         }
552
553         void tools_check_for_updates ()
554         {
555                 UpdateChecker::instance()->run ();
556         }
557
558         void help_about ()
559         {
560                 AboutDialog* d = new AboutDialog (this);
561                 d->ShowModal ();
562                 d->Destroy ();
563         }
564
565         bool should_close ()
566         {
567                 if (!JobManager::instance()->work_to_do ()) {
568                         return true;
569                 }
570
571                 wxMessageDialog* d = new wxMessageDialog (
572                         0,
573                         _("There are unfinished jobs; are you sure you want to quit?"),
574                         _("Unfinished jobs"),
575                         wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
576                         );
577
578                 bool const r = d->ShowModal() == wxID_YES;
579                 d->Destroy ();
580                 return r;
581         }
582                 
583         void close (wxCloseEvent& ev)
584         {
585                 if (!should_close ()) {
586                         ev.Veto ();
587                         return;
588                 }
589
590                 maybe_save_then_delete_film ();
591
592                 ev.Skip ();
593         }
594
595         void set_menu_sensitivity ()
596         {
597                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
598                 list<shared_ptr<Job> >::iterator i = jobs.begin();
599                 while (i != jobs.end() && dynamic_pointer_cast<TranscodeJob> (*i) == 0) {
600                         ++i;
601                 }
602                 bool const dcp_creation = (i != jobs.end ()) && !(*i)->finished ();
603                 bool const have_cpl = film && !film->cpls().empty ();
604                 bool const have_selected_video_content = !_film_editor->selected_video_content().empty();
605                 
606                 for (map<wxMenuItem*, int>::iterator j = menu_items.begin(); j != menu_items.end(); ++j) {
607                         
608                         bool enabled = true;
609                         
610                         if ((j->second & NEEDS_FILM) && film == 0) {
611                                 enabled = false;
612                         }
613                         
614                         if ((j->second & NOT_DURING_DCP_CREATION) && dcp_creation) {
615                                 enabled = false;
616                         }
617                         
618                         if ((j->second & NEEDS_CPL) && !have_cpl) {
619                                 enabled = false;
620                         }
621                         
622                         if ((j->second & NEEDS_SELECTED_VIDEO_CONTENT) && !have_selected_video_content) {
623                                 enabled = false;
624                         }
625                         
626                         j->first->Enable (enabled);
627                 }
628         }
629         
630         FilmEditor* _film_editor;
631         FilmViewer* _film_viewer;
632         HintsDialog* _hints_dialog;
633         ServersListDialog* _servers_list_dialog;
634         wxPreferencesEditor* _config_dialog;
635 };
636
637 static const wxCmdLineEntryDesc command_line_description[] = {
638         { wxCMD_LINE_SWITCH, "n", "new", "create new film", wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
639         { wxCMD_LINE_OPTION, "c", "content", "add content file", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
640         { wxCMD_LINE_PARAM, 0, 0, "film to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
641         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
642 };
643
644 class App : public wxApp
645 {
646         bool OnInit ()
647         try
648         {
649                 SetAppName (_("DCP-o-matic"));
650                 
651                 if (!wxApp::OnInit()) {
652                         return false;
653                 }
654                 
655 #ifdef DCPOMATIC_LINUX  
656                 unsetenv ("UBUNTU_MENUPROXY");
657 #endif
658
659 #ifdef __WXOSX__                
660                 ProcessSerialNumber serial;
661                 GetCurrentProcess (&serial);
662                 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
663 #endif          
664
665                 wxInitAllImageHandlers ();
666
667                 /* Enable i18n; this will create a Config object
668                    to look for a force-configured language.  This Config
669                    object will be wrong, however, because dcpomatic_setup
670                    hasn't yet been called and there aren't any scalers, filters etc.
671                    set up yet.
672                 */
673                 dcpomatic_setup_i18n ();
674
675                 /* Set things up, including scalers / filters etc.
676                    which will now be internationalised correctly.
677                 */
678                 dcpomatic_setup ();
679
680                 /* Force the configuration to be re-loaded correctly next
681                    time it is needed.
682                 */
683                 Config::drop ();
684
685                 if (!film_to_load.empty() && boost::filesystem::is_directory (film_to_load)) {
686                         try {
687                                 load_film (film_to_load);
688                         } catch (exception& e) {
689                                 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load film %1 (%2)")), film_to_load, e.what())));
690                         }
691                 }
692
693                 if (!film_to_create.empty ()) {
694                         film.reset (new Film (film_to_create));
695                         film->write_metadata ();
696                         film->set_name (boost::filesystem::path (film_to_create).filename().generic_string ());
697                 }
698
699                 if (!content_to_add.empty ()) {
700                         film->examine_and_add_content (content_factory (film, content_to_add));
701                 }
702
703                 _frame = new Frame (_("DCP-o-matic"));
704                 SetTopWindow (_frame);
705                 _frame->Maximize ();
706                 _frame->Show ();
707
708                 ui_signaller = new wxUISignaller (this);
709                 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
710
711                 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
712                 _timer.reset (new wxTimer (this));
713                 _timer->Start (1000);
714
715                 if (film) {
716                         check_film_state_version (film->state_version ());
717                 }
718
719                 UpdateChecker::instance()->StateChanged.connect (boost::bind (&App::update_checker_state_changed, this));
720                 if (Config::instance()->check_for_updates ()) {
721                         UpdateChecker::instance()->run ();
722                 }
723
724                 return true;
725         }
726         catch (exception& e)
727         {
728                 error_dialog (0, wxString::Format ("DCP-o-matic could not start: %s", e.what ()));
729                 return true;
730         }
731
732         void OnInitCmdLine (wxCmdLineParser& parser)
733         {
734                 parser.SetDesc (command_line_description);
735                 parser.SetSwitchChars (wxT ("-"));
736         }
737
738         bool OnCmdLineParsed (wxCmdLineParser& parser)
739         {
740                 if (parser.GetParamCount() > 0) {
741                         if (parser.Found (wxT ("new"))) {
742                                 film_to_create = wx_to_std (parser.GetParam (0));
743                         } else {
744                                 film_to_load = wx_to_std (parser.GetParam (0));
745                         }
746                 }
747
748                 wxString content;
749                 if (parser.Found (wxT ("content"), &content)) {
750                         content_to_add = wx_to_std (content);
751                 }
752
753                 return true;
754         }
755
756         bool OnExceptionInMainLoop ()
757         {
758                 error_dialog (0, _("An unknown exception occurred.  Please report this problem to the DCP-o-matic author (carl@dcpomatic.com)."));
759                 return false;
760         }
761                 
762         void OnUnhandledException ()
763         {
764                 error_dialog (0, _("An unknown exception occurred.  Please report this problem to the DCP-o-matic author (carl@dcpomatic.com)."));
765         }
766
767         void idle ()
768         {
769                 ui_signaller->ui_idle ();
770         }
771
772         void check ()
773         {
774                 try {
775                         ServerFinder::instance()->rethrow ();
776                 } catch (exception& e) {
777                         error_dialog (0, std_to_wx (e.what ()));
778                 }
779         }
780
781         void update_checker_state_changed ()
782         {
783                 switch (UpdateChecker::instance()->state ()) {
784                 case UpdateChecker::YES:
785                 {
786                         string test;
787                         if (Config::instance()->check_for_test_updates ()) {
788                                 test = UpdateChecker::instance()->test ();
789                         }
790                         UpdateDialog* dialog = new UpdateDialog (_frame, UpdateChecker::instance()->stable (), test);
791                         dialog->ShowModal ();
792                         dialog->Destroy ();
793                         break;
794                 }
795                 case UpdateChecker::NO:
796                         if (!UpdateChecker::instance()->last_emit_was_first ()) {
797                                 error_dialog (_frame, _("There are no new versions of DCP-o-matic available."));
798                         }
799                         break;
800                 case UpdateChecker::FAILED:
801                         if (!UpdateChecker::instance()->last_emit_was_first ()) {
802                                 error_dialog (_frame, _("The DCP-o-matic download server could not be contacted."));
803                         }
804                 default:
805                         break;
806                 }
807         }
808
809         Frame* _frame;
810         shared_ptr<wxTimer> _timer;
811 };
812
813 IMPLEMENT_APP (App)