2 Copyright (C) 2015 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.
20 #include "wx/config_dialog.h"
21 #include "wx/about_dialog.h"
22 #include "wx/report_problem_dialog.h"
23 #include "wx/file_picker_ctrl.h"
24 #include "wx/wx_util.h"
25 #include "wx/wx_signal_manager.h"
26 #include "wx/screens_panel.h"
27 #include "wx/kdm_timing_panel.h"
28 #include "wx/kdm_output_panel.h"
29 #include "wx/job_view_dialog.h"
30 #include "lib/config.h"
32 #include "lib/screen.h"
33 #include "lib/job_manager.h"
34 #include "lib/screen_kdm.h"
35 #include "lib/exceptions.h"
36 #include "lib/cinema_kdms.h"
37 #include "lib/send_kdm_email_job.h"
38 #include <dcp/encrypted_kdm.h>
39 #include <dcp/decrypted_kdm.h>
40 #include <dcp/exceptions.h>
42 #include <wx/preferences.h>
43 #include <wx/filepicker.h>
44 #include <boost/bind.hpp>
45 #include <boost/foreach.hpp>
54 using boost::shared_ptr;
58 ID_help_report_a_problem,
61 class DOMFrame : public wxFrame
64 DOMFrame (wxString const & title)
65 : wxFrame (NULL, -1, title)
69 wxMenuBar* bar = new wxMenuBar;
73 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
74 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
75 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
76 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
78 /* Use a panel as the only child of the Frame so that we avoid
79 the dark-grey background on Windows.
81 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
82 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
84 wxBoxSizer* vertical = new wxBoxSizer (wxVERTICAL);
86 wxFont subheading_font (*wxNORMAL_FONT);
87 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
89 wxStaticText* h = new wxStaticText (overall_panel, wxID_ANY, _("Screens"));
90 h->SetFont (subheading_font);
91 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL);
92 _screens = new ScreensPanel (overall_panel);
93 vertical->Add (_screens, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_Y_GAP);
95 h = new wxStaticText (overall_panel, wxID_ANY, S_("KDM|Timing"));
96 h->SetFont (subheading_font);
97 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
98 _timing = new KDMTimingPanel (overall_panel);
99 vertical->Add (_timing, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
101 h = new wxStaticText (overall_panel, wxID_ANY, _("DKDM"));
102 h->SetFont (subheading_font);
103 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
104 wxSizer* dkdm = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
105 add_label_to_sizer (dkdm, overall_panel, _("DKDM file"), true);
106 _dkdm = new FilePickerCtrl (overall_panel, _("Select a DKDM XML file..."), "*.xml");
107 dkdm->Add (_dkdm, 1, wxEXPAND);
108 add_label_to_sizer (dkdm, overall_panel, _("Annotation"), true);
109 _annotation_text = new wxStaticText (overall_panel, wxID_ANY, wxT(""));
110 dkdm->Add (_annotation_text, 1, wxEXPAND);
111 add_label_to_sizer (dkdm, overall_panel, _("Content title"), true);
112 _content_title_text = new wxStaticText (overall_panel, wxID_ANY, wxT(""));
113 dkdm->Add (_content_title_text, 1, wxEXPAND);
114 add_label_to_sizer (dkdm, overall_panel, _("Issue date"), true);
115 _issue_date = new wxStaticText (overall_panel, wxID_ANY, wxT(""));
116 dkdm->Add (_issue_date, 1, wxEXPAND);
117 vertical->Add (dkdm, 0, wxALL, DCPOMATIC_SIZER_X_GAP);
119 h = new wxStaticText (overall_panel, wxID_ANY, _("Output"));
120 h->SetFont (subheading_font);
121 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
122 /* XXX: hard-coded non-interop here */
123 _output = new KDMOutputPanel (overall_panel, false);
124 vertical->Add (_output, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
126 _create = new wxButton (overall_panel, wxID_ANY, _("Create KDMs"));
127 vertical->Add (_create, 0, wxALL, DCPOMATIC_SIZER_GAP);
129 main_sizer->Add (vertical, 1, wxALL, DCPOMATIC_DIALOG_BORDER);
130 overall_panel->SetSizer (main_sizer);
132 /* Instantly save any config changes when using a DCP-o-matic GUI */
133 Config::instance()->Changed.connect (boost::bind (&Config::write, Config::instance ()));
135 _screens->ScreensChanged.connect (boost::bind (&DOMFrame::setup_sensitivity, this));
136 _dkdm->Bind (wxEVT_COMMAND_FILEPICKER_CHANGED, bind (&DOMFrame::dkdm_changed, this));
137 _create->Bind (wxEVT_COMMAND_BUTTON_CLICKED, bind (&DOMFrame::create_kdms, this));
139 setup_sensitivity ();
145 /* false here allows the close handler to veto the close request */
149 void edit_preferences ()
151 if (!_config_dialog) {
152 _config_dialog = create_config_dialog ();
154 _config_dialog->Show (this);
159 AboutDialog* d = new AboutDialog (this);
164 void help_report_a_problem ()
166 ReportProblemDialog* d = new ReportProblemDialog (this, shared_ptr<Film> ());
167 if (d->ShowModal () == wxID_OK) {
173 void setup_menu (wxMenuBar* m)
175 wxMenu* file = new wxMenu;
178 file->Append (wxID_EXIT, _("&Exit"));
180 file->Append (wxID_EXIT, _("&Quit"));
184 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
186 wxMenu* edit = new wxMenu;
187 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
190 wxMenu* help = new wxMenu;
192 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
194 help->Append (wxID_ABOUT, _("About"));
196 help->Append (ID_help_report_a_problem, _("Report a problem..."));
198 m->Append (file, _("&File"));
200 m->Append (edit, _("&Edit"));
202 m->Append (help, _("&Help"));
207 if (_dkdm->GetPath().IsEmpty()) {
212 dcp::EncryptedKDM encrypted (dcp::file_to_string (wx_to_std (_dkdm->GetPath())));
213 dcp::DecryptedKDM decrypted (encrypted, Config::instance()->decryption_chain()->key().get());
214 _annotation_text->Enable (true);
215 _annotation_text->SetLabel (std_to_wx (decrypted.annotation_text ()));
216 _content_title_text->Enable (true);
217 _content_title_text->SetLabel (std_to_wx (decrypted.content_title_text ()));
218 _issue_date->Enable (true);
219 _issue_date->SetLabel (std_to_wx (decrypted.issue_date ()));
220 } catch (exception& e) {
221 error_dialog (this, wxString::Format (_("Could not load DKDM (%s)"), std_to_wx (e.what()).data()));
222 _dkdm->SetPath (wxT(""));
223 _annotation_text->SetLabel (wxT(""));
224 _annotation_text->Enable (false);
225 _content_title_text->SetLabel (wxT(""));
226 _content_title_text->Enable (false);
227 _issue_date->SetLabel (wxT(""));
228 _issue_date->Enable (false);
231 setup_sensitivity ();
237 /* Decrypt the DKDM */
238 dcp::EncryptedKDM encrypted (dcp::file_to_string (wx_to_std (_dkdm->GetPath())));
239 dcp::DecryptedKDM decrypted (encrypted, Config::instance()->decryption_chain()->key().get());
241 /* This is the signer for our new KDMs */
242 shared_ptr<const dcp::CertificateChain> signer = Config::instance()->signer_chain ();
243 if (!signer->valid ()) {
244 throw InvalidSignerError ();
247 list<ScreenKDM> screen_kdms;
248 BOOST_FOREACH (shared_ptr<Screen> i, _screens->screens()) {
250 if (!i->certificate) {
254 /* Make an empty KDM */
255 dcp::DecryptedKDM kdm (
256 _timing->from(), _timing->until(), decrypted.annotation_text(), decrypted.content_title_text(), dcp::LocalTime().as_string()
259 /* Add keys from the DKDM */
260 BOOST_FOREACH (dcp::DecryptedKDMKey const & j, decrypted.keys()) {
265 screen_kdms.push_back (ScreenKDM (i, kdm.encrypt (signer, i->certificate.get(), _output->formulation())));
268 if (_output->write_to()) {
269 ScreenKDM::write_files (decrypted.content_title_text(), screen_kdms, _output->directory());
270 /* XXX: proper plural form support in wxWidgets? */
271 wxString s = screen_kdms.size() == 1 ? _("%d KDM written to %s") : _("%d KDMs written to %s");
274 wxString::Format (s, int(screen_kdms.size()), std_to_wx(_output->directory().string()).data())
277 string film_name = decrypted.annotation_text ();
278 if (film_name.empty ()) {
279 film_name = decrypted.content_title_text ();
281 shared_ptr<Job> job (new SendKDMEmailJob (
283 decrypted.content_title_text(),
284 _timing->from(), _timing->until(),
285 CinemaKDMs::collect (screen_kdms)
288 JobManager::instance()->add (job);
290 _job_view->Destroy ();
293 _job_view = new JobViewDialog (this, _("Send KDM emails"), job);
294 _job_view->ShowModal ();
296 } catch (dcp::NotEncryptedError& e) {
297 error_dialog (this, _("CPL's content is not encrypted."));
298 } catch (exception& e) {
299 error_dialog (this, e.what ());
301 error_dialog (this, _("An unknown exception occurred."));
305 void setup_sensitivity ()
307 _screens->setup_sensitivity ();
308 _output->setup_sensitivity ();
309 _create->Enable (!_screens->screens().empty() && !_dkdm->GetPath().IsEmpty());
312 wxPreferencesEditor* _config_dialog;
313 ScreensPanel* _screens;
314 KDMTimingPanel* _timing;
315 /* I can't seem to clear the value in a wxFilePickerCtrl, so use our own */
316 FilePickerCtrl* _dkdm;
317 wxStaticText* _annotation_text;
318 wxStaticText* _content_title_text;
319 wxStaticText* _issue_date;
321 KDMOutputPanel* _output;
322 JobViewDialog* _job_view;
326 * @brief The magic App class for wxWidgets.
328 class App : public wxApp
341 wxInitAllImageHandlers ();
343 SetAppName (_("DCP-o-matic KDM creator"));
345 if (!wxApp::OnInit()) {
349 #ifdef DCPOMATIC_LINUX
350 unsetenv ("UBUNTU_MENUPROXY");
354 ProcessSerialNumber serial;
355 GetCurrentProcess (&serial);
356 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
359 dcpomatic_setup_path_encoding ();
361 /* Enable i18n; this will create a Config object
362 to look for a force-configured language. This Config
363 object will be wrong, however, because dcpomatic_setup
364 hasn't yet been called and there aren't any filters etc.
367 dcpomatic_setup_i18n ();
369 /* Set things up, including filters etc.
370 which will now be internationalised correctly.
374 /* Force the configuration to be re-loaded correctly next
379 _frame = new DOMFrame (_("DCP-o-matic KDM creator"));
380 SetTopWindow (_frame);
384 signal_manager = new wxSignalManager (this);
385 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
391 error_dialog (0, wxString::Format ("DCP-o-matic could not start: %s", e.what ()));
395 /* An unhandled exception has occurred inside the main event loop */
396 bool OnExceptionInMainLoop ()
400 } catch (FileError& e) {
404 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
405 std_to_wx (e.what()),
406 std_to_wx (e.file().string().c_str ())
409 } catch (exception& e) {
413 _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
414 std_to_wx (e.what ())
418 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
421 /* This will terminate the program */
425 void OnUnhandledException ()
427 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
432 signal_manager->ui_idle ();