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