Basic email of KDMs works.
[dcpomatic.git] / src / tools / dcpomatic.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <iostream>
21 #include <fstream>
22 #include <boost/filesystem.hpp>
23 #ifdef __WXMSW__
24 #include <shellapi.h>
25 #endif
26 #ifdef __WXOSX__
27 #include <ApplicationServices/ApplicationServices.h>
28 #endif
29 #include <wx/generic/aboutdlgg.h>
30 #include <wx/stdpaths.h>
31 #include <wx/cmdline.h>
32 #include <quickmail.h>
33 #include <zip.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 "lib/film.h"
46 #include "lib/config.h"
47 #include "lib/util.h"
48 #include "lib/version.h"
49 #include "lib/ui_signaller.h"
50 #include "lib/log.h"
51 #include "lib/job_manager.h"
52 #include "lib/transcode_job.h"
53 #include "lib/exceptions.h"
54 #include "lib/cinema.h"
55
56 using std::cout;
57 using std::string;
58 using std::wstring;
59 using std::stringstream;
60 using std::map;
61 using std::make_pair;
62 using std::list;
63 using std::exception;
64 using std::ofstream;
65 using boost::shared_ptr;
66 using boost::dynamic_pointer_cast;
67
68 static FilmEditor* film_editor = 0;
69 static FilmViewer* film_viewer = 0;
70 static shared_ptr<Film> film;
71 static std::string log_level;
72 static std::string film_to_load;
73 static std::string film_to_create;
74 static wxMenu* jobs_menu = 0;
75
76 static void set_menu_sensitivity ();
77
78 class FilmChangedDialog
79 {
80 public:
81         FilmChangedDialog ()
82         {
83                 _dialog = new wxMessageDialog (
84                         0,
85                         wxString::Format (_("Save changes to film \"%s\" before closing?"), std_to_wx (film->name ()).data()),
86                         _("Film changed"),
87                         wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
88                         );
89         }
90
91         ~FilmChangedDialog ()
92         {
93                 _dialog->Destroy ();
94         }
95
96         int run ()
97         {
98                 return _dialog->ShowModal ();
99         }
100
101 private:
102         /* Not defined */
103         FilmChangedDialog (FilmChangedDialog const &);
104         
105         wxMessageDialog* _dialog;
106 };
107
108
109 void
110 maybe_save_then_delete_film ()
111 {
112         if (!film) {
113                 return;
114         }
115                         
116         if (film->dirty ()) {
117                 FilmChangedDialog d;
118                 switch (d.run ()) {
119                 case wxID_NO:
120                         break;
121                 case wxID_YES:
122                         film->write_metadata ();
123                         break;
124                 }
125         }
126         
127         film.reset ();
128 }
129
130 #define ALWAYS                  0x0
131 #define NEEDS_FILM              0x1
132 #define NOT_DURING_DCP_CREATION 0x2
133
134 map<wxMenuItem*, int> menu_items;
135         
136 void
137 add_item (wxMenu* menu, wxString text, int id, int sens)
138 {
139         wxMenuItem* item = menu->Append (id, text);
140         menu_items.insert (make_pair (item, sens));
141 }
142
143 void
144 set_menu_sensitivity ()
145 {
146         list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
147         list<shared_ptr<Job> >::iterator i = jobs.begin();
148         while (i != jobs.end() && dynamic_pointer_cast<TranscodeJob> (*i) == 0) {
149                 ++i;
150         }
151         bool const dcp_creation = (i != jobs.end ());
152
153         for (map<wxMenuItem*, int>::iterator j = menu_items.begin(); j != menu_items.end(); ++j) {
154
155                 bool enabled = true;
156
157                 if ((j->second & NEEDS_FILM) && film == 0) {
158                         enabled = false;
159                 }
160
161                 if ((j->second & NOT_DURING_DCP_CREATION) && dcp_creation) {
162                         enabled = false;
163                 }
164                 
165                 j->first->Enable (enabled);
166         }
167 }
168
169 enum {
170         ID_file_new = 1,
171         ID_file_open,
172         ID_file_save,
173         ID_file_properties,
174         ID_jobs_make_dcp,
175         ID_jobs_make_kdms,
176         ID_jobs_send_dcp_to_tms,
177         ID_jobs_show_dcp,
178 };
179
180 void
181 setup_menu (wxMenuBar* m)
182 {
183         wxMenu* file = new wxMenu;
184         add_item (file, _("New..."), ID_file_new, ALWAYS);
185         add_item (file, _("&Open..."), ID_file_open, ALWAYS);
186         file->AppendSeparator ();
187         add_item (file, _("&Save"), ID_file_save, NEEDS_FILM);
188         file->AppendSeparator ();
189         add_item (file, _("&Properties..."), ID_file_properties, NEEDS_FILM);
190 #ifndef __WXOSX__       
191         file->AppendSeparator ();
192 #endif
193
194 #ifdef __WXOSX__        
195         add_item (file, _("&Exit"), wxID_EXIT, ALWAYS);
196 #else
197         add_item (file, _("&Quit"), wxID_EXIT, ALWAYS);
198 #endif  
199         
200
201 #ifdef __WXOSX__        
202         add_item (file, _("&Preferences..."), wxID_PREFERENCES, ALWAYS);
203 #else
204         wxMenu* edit = new wxMenu;
205         add_item (edit, _("&Preferences..."), wxID_PREFERENCES, ALWAYS);
206 #endif  
207
208         jobs_menu = new wxMenu;
209         add_item (jobs_menu, _("&Make DCP"), ID_jobs_make_dcp, NEEDS_FILM | NOT_DURING_DCP_CREATION);
210         add_item (jobs_menu, _("Make &KDMs..."), ID_jobs_make_kdms, NEEDS_FILM);
211         add_item (jobs_menu, _("&Send DCP to TMS"), ID_jobs_send_dcp_to_tms, NEEDS_FILM | NOT_DURING_DCP_CREATION);
212         add_item (jobs_menu, _("S&how DCP"), ID_jobs_show_dcp, NEEDS_FILM | NOT_DURING_DCP_CREATION);
213
214         wxMenu* help = new wxMenu;
215 #ifdef __WXOSX__        
216         add_item (help, _("About DCP-o-matic"), wxID_ABOUT, ALWAYS);
217 #else   
218         add_item (help, _("About"), wxID_ABOUT, ALWAYS);
219 #endif  
220
221         m->Append (file, _("&File"));
222 #ifndef __WXOSX__       
223         m->Append (edit, _("&Edit"));
224 #endif  
225         m->Append (jobs_menu, _("&Jobs"));
226         m->Append (help, _("&Help"));
227 }
228
229 struct ScreenKDM
230 {
231         ScreenKDM (shared_ptr<Screen> s, libdcp::KDM k)
232                 : screen (s)
233                 , kdm (k)
234         {}
235         
236         shared_ptr<Screen> screen;
237         libdcp::KDM kdm;
238 };
239
240 /* Not complete but sufficient for our purposes (we're using
241    ScreenKDM in a list where all the screens will be unique).
242 */
243 bool operator== (ScreenKDM const & a, ScreenKDM const & b)
244 {
245         return a.screen == b.screen;
246 }
247
248 class Frame : public wxFrame
249 {
250 public:
251         Frame (wxString const & title)
252                 : wxFrame (NULL, -1, title)
253         {
254                 wxMenuBar* bar = new wxMenuBar;
255                 setup_menu (bar);
256                 SetMenuBar (bar);
257
258                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::file_new, this),             ID_file_new);
259                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::file_open, this),            ID_file_open);
260                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::file_save, this),            ID_file_save);
261                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::file_properties, this),      ID_file_properties);
262                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::file_exit, this),            wxID_EXIT);
263                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::edit_preferences, this),     wxID_PREFERENCES);
264                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::jobs_make_dcp, this),        ID_jobs_make_dcp);
265                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::jobs_make_kdms, this),       ID_jobs_make_kdms);
266                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::jobs_send_dcp_to_tms, this), ID_jobs_send_dcp_to_tms);
267                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::jobs_show_dcp, this),        ID_jobs_show_dcp);
268                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::help_about, this),           wxID_ABOUT);
269
270                 Bind (wxEVT_MENU_OPEN, boost::bind (&Frame::menu_opened, this, _1));
271                 Bind (wxEVT_CLOSE_WINDOW, boost::bind (&Frame::close, this, _1));
272
273                 /* Use a panel as the only child of the Frame so that we avoid
274                    the dark-grey background on Windows.
275                 */
276                 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
277
278                 film_editor = new FilmEditor (film, overall_panel);
279                 film_viewer = new FilmViewer (film, overall_panel);
280                 JobManagerView* job_manager_view = new JobManagerView (overall_panel, static_cast<JobManagerView::Buttons> (0));
281
282                 wxBoxSizer* right_sizer = new wxBoxSizer (wxVERTICAL);
283                 right_sizer->Add (film_viewer, 2, wxEXPAND | wxALL, 6);
284                 right_sizer->Add (job_manager_view, 1, wxEXPAND | wxALL, 6);
285
286                 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
287                 main_sizer->Add (film_editor, 1, wxEXPAND | wxALL, 6);
288                 main_sizer->Add (right_sizer, 2, wxEXPAND | wxALL, 6);
289
290                 set_menu_sensitivity ();
291
292                 film_editor->FileChanged.connect (bind (&Frame::file_changed, this, _1));
293                 if (film) {
294                         file_changed (film->directory ());
295                 } else {
296                         file_changed ("");
297                 }
298
299                 JobManager::instance()->ActiveJobsChanged.connect (boost::bind (set_menu_sensitivity));
300
301                 set_film ();
302                 overall_panel->SetSizer (main_sizer);
303         }
304
305 private:
306
307         void menu_opened (wxMenuEvent& ev)
308         {
309                 if (ev.GetMenu() != jobs_menu) {
310                         return;
311                 }
312
313                 bool const have_dcp = false;//film && film->have_dcp();
314                 jobs_menu->Enable (ID_jobs_send_dcp_to_tms, have_dcp);
315                 jobs_menu->Enable (ID_jobs_show_dcp, have_dcp);
316         }
317
318         void set_film ()
319         {
320                 film_viewer->set_film (film);
321                 film_editor->set_film (film);
322                 set_menu_sensitivity ();
323         }
324
325         void file_changed (string f)
326         {
327                 stringstream s;
328                 s << wx_to_std (_("DCP-o-matic"));
329                 if (!f.empty ()) {
330                         s << " - " << f;
331                 }
332                 
333                 SetTitle (std_to_wx (s.str()));
334         }
335         
336         void file_new ()
337         {
338                 NewFilmDialog* d = new NewFilmDialog (this);
339                 int const r = d->ShowModal ();
340                 
341                 if (r == wxID_OK) {
342
343                         if (boost::filesystem::is_directory (d->get_path()) && !boost::filesystem::is_empty(d->get_path())) {
344                                 if (!confirm_dialog (
345                                             this,
346                                             std_to_wx (
347                                                     String::compose (wx_to_std (_("The directory %1 already exists and is not empty.  "
348                                                                                   "Are you sure you want to use it?")),
349                                                                      d->get_path().c_str())
350                                                     )
351                                             )) {
352                                         return;
353                                 }
354                         } else if (boost::filesystem::is_regular_file (d->get_path())) {
355                                 error_dialog (
356                                         this,
357                                         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())
358                                         );
359                                 return;
360                         }
361                         
362                         maybe_save_then_delete_film ();
363                         film.reset (new Film (d->get_path ()));
364                         film->write_metadata ();
365                         film->log()->set_level (log_level);
366                         film->set_name (boost::filesystem::path (d->get_path()).filename().generic_string());
367                         set_film ();
368                 }
369                 
370                 d->Destroy ();
371         }
372
373         void file_open ()
374         {
375                 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
376                 int r;
377                 while (1) {
378                         r = c->ShowModal ();
379                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
380                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
381                         } else {
382                                 break;
383                         }
384                 }
385                         
386                 if (r == wxID_OK) {
387                         maybe_save_then_delete_film ();
388                         try {
389                                 film.reset (new Film (wx_to_std (c->GetPath ())));
390                                 film->read_metadata ();
391                                 film->log()->set_level (log_level);
392                                 set_film ();
393                         } catch (std::exception& e) {
394                                 wxString p = c->GetPath ();
395                                 wxCharBuffer b = p.ToUTF8 ();
396                                 error_dialog (this, wxString::Format (_("Could not open film at %s (%s)"), p.data(), std_to_wx (e.what()).data()));
397                         }
398                 }
399
400                 c->Destroy ();
401         }
402
403         void file_save ()
404         {
405                 film->write_metadata ();
406         }
407
408         void file_properties ()
409         {
410                 PropertiesDialog* d = new PropertiesDialog (this, film);
411                 d->ShowModal ();
412                 d->Destroy ();
413         }
414         
415         void file_exit ()
416         {
417                 if (!should_close ()) {
418                         return;
419                 }
420                 
421                 maybe_save_then_delete_film ();
422                 Close (true);
423         }
424
425         void edit_preferences ()
426         {
427                 ConfigDialog* d = new ConfigDialog (this);
428                 d->ShowModal ();
429                 d->Destroy ();
430                 Config::instance()->write ();
431         }
432
433         void jobs_make_dcp ()
434         {
435                 JobWrapper::make_dcp (this, film);
436         }
437
438         void jobs_make_kdms ()
439         {
440                 if (!film) {
441                         return;
442                 }
443                 
444                 KDMDialog* d = new KDMDialog (this);
445                 if (d->ShowModal () != wxID_OK) {
446                         d->Destroy ();
447                         return;
448                 }
449                 
450                 try {
451                         list<shared_ptr<Screen> > screens = d->screens ();
452                         list<libdcp::KDM> kdms = film->make_kdms (
453                                 screens,
454                                 d->from (),
455                                 d->until ()
456                                 );
457
458                         list<ScreenKDM> screen_kdms;
459                         
460                         list<shared_ptr<Screen> >::iterator i = screens.begin ();
461                         list<libdcp::KDM>::iterator j = kdms.begin ();
462                         while (i != screens.end() && j != kdms.end ()) {
463                                 screen_kdms.push_back (ScreenKDM (*i, *j));
464                                 ++i;
465                                 ++j;
466                         }
467                         
468                         if (d->write_to ()) {
469                                 /* Write KDMs to the specified directory */
470                                 for (list<ScreenKDM>::iterator i = screen_kdms.begin(); i != screen_kdms.end(); ++i) {
471                                         boost::filesystem::path out = d->directory ();
472                                         out /= tidy_for_filename (i->screen->cinema->name) + "_" + tidy_for_filename (i->screen->name) + ".kdm.xml";
473                                         i->kdm.as_xml (out);
474                                 }
475                         } else {
476                                 while (!screen_kdms.empty ()) {
477
478                                         /* Get all the screens from a single cinema */
479
480                                         shared_ptr<Cinema> cinema;
481                                         list<ScreenKDM> cinema_screen_kdms;
482
483                                         list<ScreenKDM>::iterator i = screen_kdms.begin ();
484                                         cinema = i->screen->cinema;
485                                         cinema_screen_kdms.push_back (*i);
486                                         list<ScreenKDM>::iterator j = i;
487                                         ++i;
488                                         screen_kdms.remove (*j);
489
490                                         while (i != screen_kdms.end ()) {
491                                                 if (i->screen->cinema == cinema) {
492                                                         cinema_screen_kdms.push_back (*i);
493                                                         list<ScreenKDM>::iterator j = i;
494                                                         ++i;
495                                                         screen_kdms.remove (*j);
496                                                 } else {
497                                                         ++i;
498                                                 }
499                                         }
500
501                                         /* Make a ZIP file of this cinema's KDMs */
502                                         
503                                         boost::filesystem::path zip_file = boost::filesystem::temp_directory_path ();
504                                         zip_file /= boost::filesystem::unique_path().string() + ".zip";
505                                         struct zip* zip = zip_open (zip_file.c_str(), ZIP_CREATE | ZIP_EXCL, 0);
506                                         if (!zip) {
507                                                 throw FileError ("could not create ZIP file", zip_file);
508                                         }
509
510                                         list<shared_ptr<string> > kdm_strings;
511
512                                         for (list<ScreenKDM>::const_iterator i = cinema_screen_kdms.begin(); i != cinema_screen_kdms.end(); ++i) {
513                                                 shared_ptr<string> kdm (new string (i->kdm.as_xml ()));
514                                                 kdm_strings.push_back (kdm);
515                                                 
516                                                 struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
517                                                 if (!source) {
518                                                         throw StringError ("could not create ZIP source");
519                                                 }
520                                                 
521                                                 string const name = tidy_for_filename (i->screen->cinema->name) + "_" +
522                                                         tidy_for_filename (i->screen->name) + ".kdm.xml";
523                                                 
524                                                 if (zip_add (zip, name.c_str(), source) == -1) {
525                                                         throw StringError ("failed to add KDM to ZIP archive");
526                                                 }
527                                         }
528
529                                         if (zip_close (zip) == -1) {
530                                                 throw StringError ("failed to close ZIP archive");
531                                         }
532
533                                         /* Send email */
534
535                                         quickmail_initialize ();
536                                         quickmail mail = quickmail_create (Config::instance()->kdm_from().c_str(), "KDM delivery");
537                                         quickmail_add_to (mail, cinema->email.c_str ());
538
539                                         string body = Config::instance()->kdm_email().c_str();
540                                         boost::algorithm::replace_all (body, "$DCP_NAME", film->dcp_name ());
541                                         
542                                         quickmail_set_body (mail, body.c_str());
543                                         quickmail_add_attachment_file (mail, zip_file.c_str());
544                                         char const* error = quickmail_send (mail, Config::instance()->mail_server().c_str(), 25, "", "");
545                                         if (error) {
546                                                 quickmail_destroy (mail);
547                                                 throw StringError (String::compose ("Failed to send KDM email (%1)", error));
548                                         }
549                                         quickmail_destroy (mail);
550
551                                         film->log()->log (String::compose ("Send KDM email to %1", cinema->email));
552                                 }
553                         }
554                         
555                 } catch (KDMError& e) {
556                         error_dialog (this, e.what ());
557                 }
558         
559                 d->Destroy ();
560         }
561         
562         void jobs_send_dcp_to_tms ()
563         {
564                 film->send_dcp_to_tms ();
565         }
566
567         void jobs_show_dcp ()
568         {
569 #ifdef __WXMSW__
570                 string d = film->directory();
571                 wstring w;
572                 w.assign (d.begin(), d.end());
573                 ShellExecute (0, L"open", w.c_str(), 0, 0, SW_SHOWDEFAULT);
574 #else
575                 int r = system ("which nautilus");
576                 if (WEXITSTATUS (r) == 0) {
577                         r = system (string ("nautilus " + film->directory()).c_str ());
578                         if (WEXITSTATUS (r)) {
579                                 error_dialog (this, _("Could not show DCP (could not run nautilus)"));
580                         }
581                 } else {
582                         int r = system ("which konqueror");
583                         if (WEXITSTATUS (r) == 0) {
584                                 r = system (string ("konqueror " + film->directory()).c_str ());
585                                 if (WEXITSTATUS (r)) {
586                                         error_dialog (this, _("Could not show DCP (could not run konqueror)"));
587                                 }
588                         }
589                 }
590 #endif          
591         }
592
593         void help_about ()
594         {
595                 AboutDialog* d = new AboutDialog (this);
596                 d->ShowModal ();
597                 d->Destroy ();
598         }
599
600         bool should_close ()
601         {
602                 if (!JobManager::instance()->work_to_do ()) {
603                         return true;
604                 }
605
606                 wxMessageDialog* d = new wxMessageDialog (
607                         0,
608                         _("There are unfinished jobs; are you sure you want to quit?"),
609                         _("Unfinished jobs"),
610                         wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
611                         );
612
613                 bool const r = d->ShowModal() == wxID_YES;
614                 d->Destroy ();
615                 return r;
616         }
617                 
618         void close (wxCloseEvent& ev)
619         {
620                 if (!should_close ()) {
621                         ev.Veto ();
622                         return;
623                 }
624
625                 ev.Skip ();
626         }       
627 };
628
629 #if wxMINOR_VERSION == 9
630 static const wxCmdLineEntryDesc command_line_description[] = {
631         { wxCMD_LINE_OPTION, "l", "log", "set log level (silent, verbose or timing)", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
632         { wxCMD_LINE_SWITCH, "n", "new", "create new film", wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
633         { wxCMD_LINE_PARAM, 0, 0, "film to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
634         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
635 };
636 #else
637 static const wxCmdLineEntryDesc command_line_description[] = {
638         { wxCMD_LINE_OPTION, wxT("l"), wxT("log"), wxT("set log level (silent, verbose or timing)"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
639         { wxCMD_LINE_SWITCH, wxT("n"), wxT("new"), wxT("create new film"), wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
640         { wxCMD_LINE_PARAM, 0, 0, wxT("film to load or create"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
641         { wxCMD_LINE_NONE, wxT(""), wxT(""), wxT(""), wxCmdLineParamType (0), 0 }
642 };
643 #endif
644
645 class App : public wxApp
646 {
647         bool OnInit ()
648         try
649         {
650                 if (!wxApp::OnInit()) {
651                         return false;
652                 }
653                 
654 #ifdef DCPOMATIC_LINUX  
655                 unsetenv ("UBUNTU_MENUPROXY");
656 #endif
657
658 #ifdef __WXOSX__                
659                 ProcessSerialNumber serial;
660                 GetCurrentProcess (&serial);
661                 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
662 #endif          
663
664                 wxInitAllImageHandlers ();
665
666                 /* Enable i18n; this will create a Config object
667                    to look for a force-configured language.  This Config
668                    object will be wrong, however, because dcpomatic_setup
669                    hasn't yet been called and there aren't any scalers, filters etc.
670                    set up yet.
671                 */
672                 dcpomatic_setup_i18n ();
673
674                 /* Set things up, including scalers / filters etc.
675                    which will now be internationalised correctly.
676                 */
677                 dcpomatic_setup ();
678
679                 /* Force the configuration to be re-loaded correctly next
680                    time it is needed.
681                 */
682                 Config::drop ();
683
684                 if (!film_to_load.empty() && boost::filesystem::is_directory (film_to_load)) {
685                         try {
686                                 film.reset (new Film (film_to_load));
687                                 film->read_metadata ();
688                                 film->log()->set_level (log_level);
689                         } catch (exception& e) {
690                                 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load film %1 (%2)")), film_to_load, e.what())));
691                         }
692                 }
693
694                 if (!film_to_create.empty ()) {
695                         film.reset (new Film (film_to_create));
696                         film->write_metadata ();
697                         film->log()->set_level (log_level);
698                         film->set_name (boost::filesystem::path (film_to_create).filename().generic_string ());
699                 }
700
701                 Frame* f = new Frame (_("DCP-o-matic"));
702                 SetTopWindow (f);
703                 f->Maximize ();
704                 f->Show ();
705
706                 ui_signaller = new wxUISignaller (this);
707                 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
708
709                 return true;
710         }
711         catch (exception& e)
712         {
713                 error_dialog (0, wxString::Format ("DCP-o-matic could not start: %s", e.what ()));
714                 return true;
715         }
716
717         void OnInitCmdLine (wxCmdLineParser& parser)
718         {
719                 parser.SetDesc (command_line_description);
720                 parser.SetSwitchChars (wxT ("-"));
721         }
722
723         bool OnCmdLineParsed (wxCmdLineParser& parser)
724         {
725                 if (parser.GetParamCount() > 0) {
726                         if (parser.Found (wxT ("new"))) {
727                                 film_to_create = wx_to_std (parser.GetParam (0));
728                         } else {
729                                 film_to_load = wx_to_std (parser.GetParam(0));
730                         }
731                 }
732
733                 wxString log;
734                 if (parser.Found (wxT ("log"), &log)) {
735                         log_level = wx_to_std (log);
736                 }
737
738                 return true;
739         }
740
741         void idle ()
742         {
743                 ui_signaller->ui_idle ();
744         }
745 };
746
747 IMPLEMENT_APP (App)