2 Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
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.
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.
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.
22 #include <boost/filesystem.hpp>
27 #include <ApplicationServices/ApplicationServices.h>
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"
49 #include "lib/config.h"
51 #include "lib/version.h"
52 #include "lib/ui_signaller.h"
54 #include "lib/job_manager.h"
55 #include "lib/transcode_job.h"
56 #include "lib/exceptions.h"
57 #include "lib/cinema.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"
71 using boost::shared_ptr;
72 using boost::dynamic_pointer_cast;
74 static shared_ptr<Film> film;
75 static std::string film_to_load;
76 static std::string film_to_create;
77 static std::string content_to_add;
78 static wxMenu* jobs_menu = 0;
80 // #define DCPOMATIC_WINDOWS_CONSOLE 1
82 class FilmChangedDialog
87 _dialog = new wxMessageDialog (
89 wxString::Format (_("Save changes to film \"%s\" before closing?"), std_to_wx (film->name ()).data()),
91 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
102 return _dialog->ShowModal ();
107 FilmChangedDialog (FilmChangedDialog const &);
109 wxMessageDialog* _dialog;
114 maybe_save_then_delete_film ()
120 if (film->dirty ()) {
126 film->write_metadata ();
135 check_film_state_version (int v)
140 _("This film was created with an old version of DVD-o-matic and may not load correctly "
141 "in this version. Please check the film's settings carefully.")
147 load_film (boost::filesystem::path file)
149 film.reset (new Film (file));
150 list<string> const notes = film->read_metadata ();
151 check_film_state_version (film->state_version ());
152 for (list<string>::const_iterator i = notes.begin(); i != notes.end(); ++i) {
153 error_dialog (0, std_to_wx (*i));
158 #define NEEDS_FILM 0x1
159 #define NOT_DURING_DCP_CREATION 0x2
160 #define NEEDS_CPL 0x4
161 #define NEEDS_SELECTED_VIDEO_CONTENT 0x8
163 map<wxMenuItem*, int> menu_items;
166 add_item (wxMenu* menu, wxString text, int id, int sens)
168 wxMenuItem* item = menu->Append (id, text);
169 menu_items.insert (make_pair (item, sens));
177 ID_content_scale_to_fit_width,
178 ID_content_scale_to_fit_height,
181 ID_jobs_send_dcp_to_tms,
184 ID_tools_encoding_servers,
185 ID_tools_check_for_updates
189 setup_menu (wxMenuBar* m)
191 wxMenu* file = new wxMenu;
192 add_item (file, _("New..."), ID_file_new, ALWAYS);
193 add_item (file, _("&Open..."), ID_file_open, ALWAYS);
194 file->AppendSeparator ();
195 add_item (file, _("&Save"), ID_file_save, NEEDS_FILM);
196 file->AppendSeparator ();
197 add_item (file, _("&Properties..."), ID_file_properties, NEEDS_FILM);
199 file->AppendSeparator ();
203 add_item (file, _("&Exit"), wxID_EXIT, ALWAYS);
205 add_item (file, _("&Quit"), wxID_EXIT, ALWAYS);
210 add_item (file, _("&Preferences..."), wxID_PREFERENCES, ALWAYS);
212 wxMenu* edit = new wxMenu;
213 add_item (edit, _("&Preferences..."), wxID_PREFERENCES, ALWAYS);
216 wxMenu* content = new wxMenu;
217 add_item (content, _("Scale to fit &width"), ID_content_scale_to_fit_width, NEEDS_FILM | NEEDS_SELECTED_VIDEO_CONTENT);
218 add_item (content, _("Scale to fit &height"), ID_content_scale_to_fit_height, NEEDS_FILM | NEEDS_SELECTED_VIDEO_CONTENT);
220 jobs_menu = new wxMenu;
221 add_item (jobs_menu, _("&Make DCP"), ID_jobs_make_dcp, NEEDS_FILM | NOT_DURING_DCP_CREATION);
222 add_item (jobs_menu, _("Make &KDMs..."), ID_jobs_make_kdms, NEEDS_FILM);
223 add_item (jobs_menu, _("&Send DCP to TMS"), ID_jobs_send_dcp_to_tms, NEEDS_FILM | NOT_DURING_DCP_CREATION | NEEDS_CPL);
224 add_item (jobs_menu, _("S&how DCP"), ID_jobs_show_dcp, NEEDS_FILM | NOT_DURING_DCP_CREATION | NEEDS_CPL);
226 wxMenu* tools = new wxMenu;
227 add_item (tools, _("Hints..."), ID_tools_hints, 0);
228 add_item (tools, _("Encoding servers..."), ID_tools_encoding_servers, 0);
229 add_item (tools, _("Check for updates"), ID_tools_check_for_updates, 0);
231 wxMenu* help = new wxMenu;
233 add_item (help, _("About DCP-o-matic"), wxID_ABOUT, ALWAYS);
235 add_item (help, _("About"), wxID_ABOUT, ALWAYS);
238 m->Append (file, _("&File"));
240 m->Append (edit, _("&Edit"));
242 m->Append (content, _("&Content"));
243 m->Append (jobs_menu, _("&Jobs"));
244 m->Append (tools, _("&Tools"));
245 m->Append (help, _("&Help"));
248 class Frame : public wxFrame
251 Frame (wxString const & title)
252 : wxFrame (NULL, -1, title)
254 , _servers_list_dialog (0)
257 #if defined(DCPOMATIC_WINDOWS) && defined(DCPOMATIC_WINDOWS_CONSOLE)
260 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
261 int hCrt = _open_osfhandle((intptr_t) handle_out, _O_TEXT);
262 FILE* hf_out = _fdopen(hCrt, "w");
263 setvbuf(hf_out, NULL, _IONBF, 1);
266 HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
267 hCrt = _open_osfhandle((intptr_t) handle_in, _O_TEXT);
268 FILE* hf_in = _fdopen(hCrt, "r");
269 setvbuf(hf_in, NULL, _IONBF, 128);
273 wxMenuBar* bar = new wxMenuBar;
277 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::file_new, this), ID_file_new);
278 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::file_open, this), ID_file_open);
279 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::file_save, this), ID_file_save);
280 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::file_properties, this), ID_file_properties);
281 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::file_exit, this), wxID_EXIT);
282 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::edit_preferences, this), wxID_PREFERENCES);
283 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::content_scale_to_fit_width, this), ID_content_scale_to_fit_width);
284 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::content_scale_to_fit_height, this), ID_content_scale_to_fit_height);
285 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::jobs_make_dcp, this), ID_jobs_make_dcp);
286 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::jobs_make_kdms, this), ID_jobs_make_kdms);
287 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::jobs_send_dcp_to_tms, this), ID_jobs_send_dcp_to_tms);
288 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::jobs_show_dcp, this), ID_jobs_show_dcp);
289 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::tools_hints, this), ID_tools_hints);
290 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::tools_encoding_servers, this), ID_tools_encoding_servers);
291 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::tools_check_for_updates, this), ID_tools_check_for_updates);
292 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&Frame::help_about, this), wxID_ABOUT);
294 Bind (wxEVT_CLOSE_WINDOW, boost::bind (&Frame::close, this, _1));
296 /* Use a panel as the only child of the Frame so that we avoid
297 the dark-grey background on Windows.
299 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
301 _film_editor = new FilmEditor (film, overall_panel);
302 _film_viewer = new FilmViewer (film, overall_panel);
303 JobManagerView* job_manager_view = new JobManagerView (overall_panel, static_cast<JobManagerView::Buttons> (0));
305 wxBoxSizer* right_sizer = new wxBoxSizer (wxVERTICAL);
306 right_sizer->Add (_film_viewer, 2, wxEXPAND | wxALL, 6);
307 right_sizer->Add (job_manager_view, 1, wxEXPAND | wxALL, 6);
309 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
310 main_sizer->Add (_film_editor, 1, wxEXPAND | wxALL, 6);
311 main_sizer->Add (right_sizer, 2, wxEXPAND | wxALL, 6);
313 set_menu_sensitivity ();
315 _film_editor->FileChanged.connect (bind (&Frame::file_changed, this, _1));
317 file_changed (film->directory ());
322 JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&Frame::set_menu_sensitivity, this));
325 overall_panel->SetSizer (main_sizer);
332 _film_viewer->set_film (film);
333 _film_editor->set_film (film);
334 set_menu_sensitivity ();
337 void file_changed (boost::filesystem::path f)
339 string s = wx_to_std (_("DCP-o-matic"));
341 s += " - " + f.string ();
344 SetTitle (std_to_wx (s));
349 NewFilmDialog* d = new NewFilmDialog (this);
350 int const r = d->ShowModal ();
354 if (boost::filesystem::is_directory (d->get_path()) && !boost::filesystem::is_empty(d->get_path())) {
355 if (!confirm_dialog (
358 String::compose (wx_to_std (_("The directory %1 already exists and is not empty. "
359 "Are you sure you want to use it?")),
360 d->get_path().string().c_str())
365 } else if (boost::filesystem::is_regular_file (d->get_path())) {
368 String::compose (wx_to_std (_("%1 already exists as a file, so you cannot use it for a new film.")), d->get_path().c_str())
373 maybe_save_then_delete_film ();
374 film.reset (new Film (d->get_path ()));
375 film->write_metadata ();
376 film->set_name (boost::filesystem::path (d->get_path()).filename().generic_string());
385 wxDirDialog* c = new wxDirDialog (
387 _("Select film to open"),
388 std_to_wx (Config::instance()->default_directory_or (wx_to_std (wxStandardPaths::Get().GetDocumentsDir())).string ()),
389 wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
395 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
396 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
403 maybe_save_then_delete_film ();
405 load_film (wx_to_std (c->GetPath ()));
407 } catch (std::exception& e) {
408 wxString p = c->GetPath ();
409 wxCharBuffer b = p.ToUTF8 ();
410 error_dialog (this, wxString::Format (_("Could not open film at %s (%s)"), p.data(), std_to_wx (e.what()).data()));
419 film->write_metadata ();
422 void file_properties ()
424 PropertiesDialog* d = new PropertiesDialog (this, film);
431 /* false here allows the close handler to veto the close request */
435 void edit_preferences ()
437 if (!_config_dialog) {
438 _config_dialog = create_config_dialog ();
440 _config_dialog->Show (this);
443 void jobs_make_dcp ()
448 if (!film->should_be_enough_disk_space (required, available)) {
449 if (!confirm_dialog (this, wxString::Format (_("The DCP for this film will take up about %.1f Gb, and the disk that you are using only has %.1f Gb available. Do you want to continue anyway?"), required, available))) {
454 JobWrapper::make_dcp (this, film);
457 void jobs_make_kdms ()
463 KDMDialog* d = new KDMDialog (this, film);
464 if (d->ShowModal () != wxID_OK) {
470 if (d->write_to ()) {
471 write_kdm_files (film, d->screens (), d->cpl (), d->from (), d->until (), d->formulation (), d->directory ());
473 JobManager::instance()->add (
474 shared_ptr<Job> (new SendKDMEmailJob (film, d->screens (), d->cpl (), d->from (), d->until (), d->formulation ()))
477 } catch (libdcp::NotEncryptedError& e) {
478 error_dialog (this, _("CPL's content is not encrypted."));
479 } catch (exception& e) {
480 error_dialog (this, e.what ());
482 error_dialog (this, _("An unknown exeception occurred."));
488 void content_scale_to_fit_width ()
490 VideoContentList vc = _film_editor->selected_video_content ();
491 for (VideoContentList::iterator i = vc.begin(); i != vc.end(); ++i) {
492 (*i)->scale_and_crop_to_fit_width ();
496 void content_scale_to_fit_height ()
498 VideoContentList vc = _film_editor->selected_video_content ();
499 for (VideoContentList::iterator i = vc.begin(); i != vc.end(); ++i) {
500 (*i)->scale_and_crop_to_fit_height ();
504 void jobs_send_dcp_to_tms ()
506 film->send_dcp_to_tms ();
509 void jobs_show_dcp ()
512 string d = film->directory().string ();
514 w.assign (d.begin(), d.end());
515 ShellExecute (0, L"open", w.c_str(), 0, 0, SW_SHOWDEFAULT);
517 int r = system ("which nautilus");
518 if (WEXITSTATUS (r) == 0) {
519 r = system (string ("nautilus " + film->directory().string()).c_str ());
520 if (WEXITSTATUS (r)) {
521 error_dialog (this, _("Could not show DCP (could not run nautilus)"));
524 int r = system ("which konqueror");
525 if (WEXITSTATUS (r) == 0) {
526 r = system (string ("konqueror " + film->directory().string()).c_str ());
527 if (WEXITSTATUS (r)) {
528 error_dialog (this, _("Could not show DCP (could not run konqueror)"));
537 if (!_hints_dialog) {
538 _hints_dialog = new HintsDialog (this, film);
541 _hints_dialog->Show ();
544 void tools_encoding_servers ()
546 if (!_servers_list_dialog) {
547 _servers_list_dialog = new ServersListDialog (this);
550 _servers_list_dialog->Show ();
553 void tools_check_for_updates ()
555 UpdateChecker::instance()->run ();
560 AboutDialog* d = new AboutDialog (this);
567 if (!JobManager::instance()->work_to_do ()) {
571 wxMessageDialog* d = new wxMessageDialog (
573 _("There are unfinished jobs; are you sure you want to quit?"),
574 _("Unfinished jobs"),
575 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
578 bool const r = d->ShowModal() == wxID_YES;
583 void close (wxCloseEvent& ev)
585 if (!should_close ()) {
590 maybe_save_then_delete_film ();
595 void set_menu_sensitivity ()
597 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
598 list<shared_ptr<Job> >::iterator i = jobs.begin();
599 while (i != jobs.end() && dynamic_pointer_cast<TranscodeJob> (*i) == 0) {
602 bool const dcp_creation = (i != jobs.end ()) && !(*i)->finished ();
603 bool const have_cpl = film && !film->cpls().empty ();
604 bool const have_selected_video_content = !_film_editor->selected_video_content().empty();
606 for (map<wxMenuItem*, int>::iterator j = menu_items.begin(); j != menu_items.end(); ++j) {
610 if ((j->second & NEEDS_FILM) && film == 0) {
614 if ((j->second & NOT_DURING_DCP_CREATION) && dcp_creation) {
618 if ((j->second & NEEDS_CPL) && !have_cpl) {
622 if ((j->second & NEEDS_SELECTED_VIDEO_CONTENT) && !have_selected_video_content) {
626 j->first->Enable (enabled);
630 FilmEditor* _film_editor;
631 FilmViewer* _film_viewer;
632 HintsDialog* _hints_dialog;
633 ServersListDialog* _servers_list_dialog;
634 wxPreferencesEditor* _config_dialog;
637 static const wxCmdLineEntryDesc command_line_description[] = {
638 { wxCMD_LINE_SWITCH, "n", "new", "create new film", wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
639 { wxCMD_LINE_OPTION, "c", "content", "add content file", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
640 { wxCMD_LINE_PARAM, 0, 0, "film to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
641 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
644 class App : public wxApp
649 SetAppName (_("DCP-o-matic"));
651 if (!wxApp::OnInit()) {
655 #ifdef DCPOMATIC_LINUX
656 unsetenv ("UBUNTU_MENUPROXY");
660 ProcessSerialNumber serial;
661 GetCurrentProcess (&serial);
662 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
665 wxInitAllImageHandlers ();
667 /* Enable i18n; this will create a Config object
668 to look for a force-configured language. This Config
669 object will be wrong, however, because dcpomatic_setup
670 hasn't yet been called and there aren't any scalers, filters etc.
673 dcpomatic_setup_i18n ();
675 /* Set things up, including scalers / filters etc.
676 which will now be internationalised correctly.
680 /* Force the configuration to be re-loaded correctly next
685 if (!film_to_load.empty() && boost::filesystem::is_directory (film_to_load)) {
687 load_film (film_to_load);
688 } catch (exception& e) {
689 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load film %1 (%2)")), film_to_load, e.what())));
693 if (!film_to_create.empty ()) {
694 film.reset (new Film (film_to_create));
695 film->write_metadata ();
696 film->set_name (boost::filesystem::path (film_to_create).filename().generic_string ());
699 if (!content_to_add.empty ()) {
700 film->examine_and_add_content (content_factory (film, content_to_add));
703 _frame = new Frame (_("DCP-o-matic"));
704 SetTopWindow (_frame);
708 ui_signaller = new wxUISignaller (this);
709 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
711 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
712 _timer.reset (new wxTimer (this));
713 _timer->Start (1000);
716 check_film_state_version (film->state_version ());
719 UpdateChecker::instance()->StateChanged.connect (boost::bind (&App::update_checker_state_changed, this));
720 if (Config::instance()->check_for_updates ()) {
721 UpdateChecker::instance()->run ();
728 error_dialog (0, wxString::Format ("DCP-o-matic could not start: %s", e.what ()));
732 void OnInitCmdLine (wxCmdLineParser& parser)
734 parser.SetDesc (command_line_description);
735 parser.SetSwitchChars (wxT ("-"));
738 bool OnCmdLineParsed (wxCmdLineParser& parser)
740 if (parser.GetParamCount() > 0) {
741 if (parser.Found (wxT ("new"))) {
742 film_to_create = wx_to_std (parser.GetParam (0));
744 film_to_load = wx_to_std (parser.GetParam (0));
749 if (parser.Found (wxT ("content"), &content)) {
750 content_to_add = wx_to_std (content);
756 bool OnExceptionInMainLoop ()
758 error_dialog (0, _("An unknown exception occurred. Please report this problem to the DCP-o-matic author (carl@dcpomatic.com)."));
762 void OnUnhandledException ()
764 error_dialog (0, _("An unknown exception occurred. Please report this problem to the DCP-o-matic author (carl@dcpomatic.com)."));
769 ui_signaller->ui_idle ();
775 ServerFinder::instance()->rethrow ();
776 } catch (exception& e) {
777 error_dialog (0, std_to_wx (e.what ()));
781 void update_checker_state_changed ()
783 switch (UpdateChecker::instance()->state ()) {
784 case UpdateChecker::YES:
787 if (Config::instance()->check_for_test_updates ()) {
788 test = UpdateChecker::instance()->test ();
790 UpdateDialog* dialog = new UpdateDialog (_frame, UpdateChecker::instance()->stable (), test);
791 dialog->ShowModal ();
795 case UpdateChecker::NO:
796 if (!UpdateChecker::instance()->last_emit_was_first ()) {
797 error_dialog (_frame, _("There are no new versions of DCP-o-matic available."));
800 case UpdateChecker::FAILED:
801 if (!UpdateChecker::instance()->last_emit_was_first ()) {
802 error_dialog (_frame, _("The DCP-o-matic download server could not be contacted."));
810 shared_ptr<wxTimer> _timer;