2 Copyright (C) 2012 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 <quickmail.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"
46 #include "lib/config.h"
48 #include "lib/version.h"
49 #include "lib/ui_signaller.h"
51 #include "lib/job_manager.h"
52 #include "lib/transcode_job.h"
53 #include "lib/exceptions.h"
54 #include "lib/cinema.h"
59 using std::stringstream;
65 using boost::shared_ptr;
66 using boost::dynamic_pointer_cast;
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;
76 static void set_menu_sensitivity ();
78 class FilmChangedDialog
83 _dialog = new wxMessageDialog (
85 wxString::Format (_("Save changes to film \"%s\" before closing?"), std_to_wx (film->name ()).data()),
87 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
98 return _dialog->ShowModal ();
103 FilmChangedDialog (FilmChangedDialog const &);
105 wxMessageDialog* _dialog;
110 maybe_save_then_delete_film ()
116 if (film->dirty ()) {
122 film->write_metadata ();
131 #define NEEDS_FILM 0x1
132 #define NOT_DURING_DCP_CREATION 0x2
134 map<wxMenuItem*, int> menu_items;
137 add_item (wxMenu* menu, wxString text, int id, int sens)
139 wxMenuItem* item = menu->Append (id, text);
140 menu_items.insert (make_pair (item, sens));
144 set_menu_sensitivity ()
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) {
151 bool const dcp_creation = (i != jobs.end ());
153 for (map<wxMenuItem*, int>::iterator j = menu_items.begin(); j != menu_items.end(); ++j) {
157 if ((j->second & NEEDS_FILM) && film == 0) {
161 if ((j->second & NOT_DURING_DCP_CREATION) && dcp_creation) {
165 j->first->Enable (enabled);
176 ID_jobs_send_dcp_to_tms,
181 setup_menu (wxMenuBar* m)
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);
191 file->AppendSeparator ();
195 add_item (file, _("&Exit"), wxID_EXIT, ALWAYS);
197 add_item (file, _("&Quit"), wxID_EXIT, ALWAYS);
202 add_item (file, _("&Preferences..."), wxID_PREFERENCES, ALWAYS);
204 wxMenu* edit = new wxMenu;
205 add_item (edit, _("&Preferences..."), wxID_PREFERENCES, ALWAYS);
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);
214 wxMenu* help = new wxMenu;
216 add_item (help, _("About DCP-o-matic"), wxID_ABOUT, ALWAYS);
218 add_item (help, _("About"), wxID_ABOUT, ALWAYS);
221 m->Append (file, _("&File"));
223 m->Append (edit, _("&Edit"));
225 m->Append (jobs_menu, _("&Jobs"));
226 m->Append (help, _("&Help"));
231 ScreenKDM (shared_ptr<Screen> s, libdcp::KDM k)
236 shared_ptr<Screen> screen;
240 /* Not complete but sufficient for our purposes (we're using
241 ScreenKDM in a list where all the screens will be unique).
243 bool operator== (ScreenKDM const & a, ScreenKDM const & b)
245 return a.screen == b.screen;
248 class Frame : public wxFrame
251 Frame (wxString const & title)
252 : wxFrame (NULL, -1, title)
254 wxMenuBar* bar = new wxMenuBar;
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);
270 Bind (wxEVT_MENU_OPEN, boost::bind (&Frame::menu_opened, this, _1));
271 Bind (wxEVT_CLOSE_WINDOW, boost::bind (&Frame::close, this, _1));
273 /* Use a panel as the only child of the Frame so that we avoid
274 the dark-grey background on Windows.
276 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
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));
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);
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);
290 set_menu_sensitivity ();
292 film_editor->FileChanged.connect (bind (&Frame::file_changed, this, _1));
294 file_changed (film->directory ());
299 JobManager::instance()->ActiveJobsChanged.connect (boost::bind (set_menu_sensitivity));
302 overall_panel->SetSizer (main_sizer);
307 void menu_opened (wxMenuEvent& ev)
309 if (ev.GetMenu() != jobs_menu) {
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);
320 film_viewer->set_film (film);
321 film_editor->set_film (film);
322 set_menu_sensitivity ();
325 void file_changed (string f)
328 s << wx_to_std (_("DCP-o-matic"));
333 SetTitle (std_to_wx (s.str()));
338 NewFilmDialog* d = new NewFilmDialog (this);
339 int const r = d->ShowModal ();
343 if (boost::filesystem::is_directory (d->get_path()) && !boost::filesystem::is_empty(d->get_path())) {
344 if (!confirm_dialog (
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())
354 } else if (boost::filesystem::is_regular_file (d->get_path())) {
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())
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());
375 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
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."));
387 maybe_save_then_delete_film ();
389 film.reset (new Film (wx_to_std (c->GetPath ())));
390 film->read_metadata ();
391 film->log()->set_level (log_level);
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()));
405 film->write_metadata ();
408 void file_properties ()
410 PropertiesDialog* d = new PropertiesDialog (this, film);
417 if (!should_close ()) {
421 maybe_save_then_delete_film ();
425 void edit_preferences ()
427 ConfigDialog* d = new ConfigDialog (this);
430 Config::instance()->write ();
433 void jobs_make_dcp ()
435 JobWrapper::make_dcp (this, film);
438 void jobs_make_kdms ()
444 KDMDialog* d = new KDMDialog (this);
445 if (d->ShowModal () != wxID_OK) {
451 list<shared_ptr<Screen> > screens = d->screens ();
452 list<libdcp::KDM> kdms = film->make_kdms (
458 list<ScreenKDM> screen_kdms;
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));
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";
476 while (!screen_kdms.empty ()) {
478 /* Get all the screens from a single cinema */
480 shared_ptr<Cinema> cinema;
481 list<ScreenKDM> cinema_screen_kdms;
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;
488 screen_kdms.remove (*j);
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;
495 screen_kdms.remove (*j);
501 /* Make a ZIP file of this cinema's KDMs */
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);
507 throw FileError ("could not create ZIP file", zip_file);
510 list<shared_ptr<string> > kdm_strings;
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);
516 struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
518 throw StringError ("could not create ZIP source");
521 string const name = tidy_for_filename (i->screen->cinema->name) + "_" +
522 tidy_for_filename (i->screen->name) + ".kdm.xml";
524 if (zip_add (zip, name.c_str(), source) == -1) {
525 throw StringError ("failed to add KDM to ZIP archive");
529 if (zip_close (zip) == -1) {
530 throw StringError ("failed to close ZIP archive");
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 ());
539 string body = Config::instance()->kdm_email().c_str();
540 boost::algorithm::replace_all (body, "$DCP_NAME", film->dcp_name ());
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, "", "");
546 quickmail_destroy (mail);
547 throw StringError (String::compose ("Failed to send KDM email (%1)", error));
549 quickmail_destroy (mail);
551 film->log()->log (String::compose ("Send KDM email to %1", cinema->email));
555 } catch (KDMError& e) {
556 error_dialog (this, e.what ());
562 void jobs_send_dcp_to_tms ()
564 film->send_dcp_to_tms ();
567 void jobs_show_dcp ()
570 string d = film->directory();
572 w.assign (d.begin(), d.end());
573 ShellExecute (0, L"open", w.c_str(), 0, 0, SW_SHOWDEFAULT);
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)"));
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)"));
595 AboutDialog* d = new AboutDialog (this);
602 if (!JobManager::instance()->work_to_do ()) {
606 wxMessageDialog* d = new wxMessageDialog (
608 _("There are unfinished jobs; are you sure you want to quit?"),
609 _("Unfinished jobs"),
610 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
613 bool const r = d->ShowModal() == wxID_YES;
618 void close (wxCloseEvent& ev)
620 if (!should_close ()) {
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 }
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 }
645 class App : public wxApp
650 if (!wxApp::OnInit()) {
654 #ifdef DCPOMATIC_LINUX
655 unsetenv ("UBUNTU_MENUPROXY");
659 ProcessSerialNumber serial;
660 GetCurrentProcess (&serial);
661 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
664 wxInitAllImageHandlers ();
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.
672 dcpomatic_setup_i18n ();
674 /* Set things up, including scalers / filters etc.
675 which will now be internationalised correctly.
679 /* Force the configuration to be re-loaded correctly next
684 if (!film_to_load.empty() && boost::filesystem::is_directory (film_to_load)) {
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())));
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 ());
701 Frame* f = new Frame (_("DCP-o-matic"));
706 ui_signaller = new wxUISignaller (this);
707 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
713 error_dialog (0, wxString::Format ("DCP-o-matic could not start: %s", e.what ()));
717 void OnInitCmdLine (wxCmdLineParser& parser)
719 parser.SetDesc (command_line_description);
720 parser.SetSwitchChars (wxT ("-"));
723 bool OnCmdLineParsed (wxCmdLineParser& parser)
725 if (parser.GetParamCount() > 0) {
726 if (parser.Found (wxT ("new"))) {
727 film_to_create = wx_to_std (parser.GetParam (0));
729 film_to_load = wx_to_std (parser.GetParam(0));
734 if (parser.Found (wxT ("log"), &log)) {
735 log_level = wx_to_std (log);
743 ui_signaller->ui_idle ();