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