2 Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "wx/config_dialog.h"
22 #include "wx/about_dialog.h"
23 #include "wx/report_problem_dialog.h"
24 #include "wx/file_picker_ctrl.h"
25 #include "wx/wx_util.h"
26 #include "wx/wx_signal_manager.h"
27 #include "wx/screens_panel.h"
28 #include "wx/kdm_timing_panel.h"
29 #include "wx/kdm_output_panel.h"
30 #include "wx/job_view_dialog.h"
31 #include "wx/file_dialog_wrapper.h"
32 #include "wx/editable_list.h"
33 #include "lib/config.h"
35 #include "lib/screen.h"
36 #include "lib/job_manager.h"
37 #include "lib/screen_kdm.h"
38 #include "lib/exceptions.h"
39 #include "lib/cinema_kdms.h"
40 #include "lib/send_kdm_email_job.h"
41 #include "lib/compose.hpp"
42 #include "lib/cinema.h"
43 #include <dcp/encrypted_kdm.h>
44 #include <dcp/decrypted_kdm.h>
45 #include <dcp/exceptions.h>
47 #include <wx/preferences.h>
48 #include <wx/filepicker.h>
50 #include <ApplicationServices/ApplicationServices.h>
52 #include <boost/bind.hpp>
53 #include <boost/foreach.hpp>
63 using boost::shared_ptr;
65 using boost::optional;
68 ID_help_report_a_problem = 1,
71 class KDMFileDialogWrapper : public FileDialogWrapper<dcp::EncryptedKDM>
74 KDMFileDialogWrapper (wxWindow* parent)
75 : FileDialogWrapper<dcp::EncryptedKDM> (parent, _("Select DKDM file"))
80 optional<dcp::EncryptedKDM> get ()
83 return dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (_dialog->GetPath ()), MAX_KDM_SIZE));
84 } catch (cxml::Error& e) {
85 error_dialog (_parent, wxString::Format ("This file does not look like a KDM (%s)", std_to_wx (e.what()).data()));
88 return optional<dcp::EncryptedKDM> ();
93 column (dcp::EncryptedKDM k)
95 return String::compose ("%1 (%2)", k.content_title_text(), k.cpl_id());
98 class DOMFrame : public wxFrame
101 DOMFrame (wxString const & title)
102 : wxFrame (0, -1, title)
106 #if defined(DCPOMATIC_WINDOWS)
107 if (Config::instance()->win32_console ()) {
110 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
111 int hCrt = _open_osfhandle((intptr_t) handle_out, _O_TEXT);
112 FILE* hf_out = _fdopen(hCrt, "w");
113 setvbuf(hf_out, NULL, _IONBF, 1);
116 HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
117 hCrt = _open_osfhandle((intptr_t) handle_in, _O_TEXT);
118 FILE* hf_in = _fdopen(hCrt, "r");
119 setvbuf(hf_in, NULL, _IONBF, 128);
122 std::cout << "DCP-o-matic KDM creator is starting." << "\n";
126 wxMenuBar* bar = new wxMenuBar;
130 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
131 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
132 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
133 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
135 /* Use a panel as the only child of the Frame so that we avoid
136 the dark-grey background on Windows.
138 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
139 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
141 wxBoxSizer* horizontal = new wxBoxSizer (wxHORIZONTAL);
142 wxBoxSizer* left = new wxBoxSizer (wxVERTICAL);
143 wxBoxSizer* right = new wxBoxSizer (wxVERTICAL);
145 horizontal->Add (left, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_X_GAP * 2);
146 horizontal->Add (right, 1, wxEXPAND);
148 wxFont subheading_font (*wxNORMAL_FONT);
149 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
151 wxStaticText* h = new wxStaticText (overall_panel, wxID_ANY, _("Screens"));
152 h->SetFont (subheading_font);
153 left->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
154 _screens = new ScreensPanel (overall_panel);
155 left->Add (_screens, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
157 /// TRANSLATORS: translate the word "Timing" here; do not include the "KDM|" prefix
158 h = new wxStaticText (overall_panel, wxID_ANY, S_("KDM|Timing"));
159 h->SetFont (subheading_font);
160 right->Add (h, 0, wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_Y_GAP * 2);
161 _timing = new KDMTimingPanel (overall_panel);
162 right->Add (_timing, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
164 h = new wxStaticText (overall_panel, wxID_ANY, _("DKDM"));
165 h->SetFont (subheading_font);
166 right->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
168 vector<string> columns;
169 columns.push_back (wx_to_std (_("CPL")));
170 _dkdm = new EditableList<dcp::EncryptedKDM, KDMFileDialogWrapper> (
171 overall_panel, columns, bind (&DOMFrame::dkdms, this), bind (&DOMFrame::set_dkdms, this, _1), bind (&column, _1), false
173 right->Add (_dkdm, 0, wxEXPAND | wxALL, DCPOMATIC_SIZER_Y_GAP);
175 h = new wxStaticText (overall_panel, wxID_ANY, _("Output"));
176 h->SetFont (subheading_font);
177 right->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
178 /* XXX: hard-coded non-interop here */
179 _output = new KDMOutputPanel (overall_panel, false);
180 right->Add (_output, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
182 _create = new wxButton (overall_panel, wxID_ANY, _("Create KDMs"));
183 right->Add (_create, 0, wxALL, DCPOMATIC_SIZER_GAP);
185 main_sizer->Add (horizontal, 1, wxALL | wxEXPAND, DCPOMATIC_DIALOG_BORDER);
186 overall_panel->SetSizer (main_sizer);
188 /* Instantly save any config changes when using a DCP-o-matic GUI */
189 Config::instance()->Changed.connect (boost::bind (&Config::write, Config::instance ()));
191 _screens->ScreensChanged.connect (boost::bind (&DOMFrame::setup_sensitivity, this));
192 _create->Bind (wxEVT_BUTTON, bind (&DOMFrame::create_kdms, this));
193 _dkdm->SelectionChanged.connect (boost::bind (&DOMFrame::setup_sensitivity, this));
195 setup_sensitivity ();
199 vector<dcp::EncryptedKDM> dkdms () const
201 return Config::instance()->dkdms ();
204 void set_dkdms (vector<dcp::EncryptedKDM> dkdms)
206 Config::instance()->set_dkdms (dkdms);
211 /* false here allows the close handler to veto the close request */
215 void edit_preferences ()
217 if (!_config_dialog) {
218 _config_dialog = create_config_dialog ();
220 _config_dialog->Show (this);
225 AboutDialog* d = new AboutDialog (this);
230 void help_report_a_problem ()
232 ReportProblemDialog* d = new ReportProblemDialog (this, shared_ptr<Film> ());
233 if (d->ShowModal () == wxID_OK) {
239 void setup_menu (wxMenuBar* m)
241 wxMenu* file = new wxMenu;
244 file->Append (wxID_EXIT, _("&Exit"));
246 file->Append (wxID_EXIT, _("&Quit"));
250 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
252 wxMenu* edit = new wxMenu;
253 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
256 wxMenu* help = new wxMenu;
258 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
260 help->Append (wxID_ABOUT, _("About"));
262 help->Append (ID_help_report_a_problem, _("Report a problem..."));
264 m->Append (file, _("&File"));
266 m->Append (edit, _("&Edit"));
268 m->Append (help, _("&Help"));
274 if (!_dkdm->selection()) {
278 /* Decrypt the DKDM */
279 dcp::DecryptedKDM decrypted (_dkdm->selection().get(), Config::instance()->decryption_chain()->key().get());
281 /* This is the signer for our new KDMs */
282 shared_ptr<const dcp::CertificateChain> signer = Config::instance()->signer_chain ();
283 if (!signer->valid ()) {
284 throw InvalidSignerError ();
287 list<ScreenKDM> screen_kdms;
288 BOOST_FOREACH (shared_ptr<Screen> i, _screens->screens()) {
294 /* Make an empty KDM */
295 dcp::DecryptedKDM kdm (
296 dcp::LocalTime (_timing->from(), i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute()),
297 dcp::LocalTime (_timing->until(), i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute()),
298 decrypted.annotation_text().get_value_or (""),
299 decrypted.content_title_text(),
300 dcp::LocalTime().as_string()
303 /* Add keys from the DKDM */
304 BOOST_FOREACH (dcp::DecryptedKDMKey const & j, decrypted.keys()) {
309 screen_kdms.push_back (ScreenKDM (i, kdm.encrypt (signer, i->recipient.get(), i->trusted_devices, _output->formulation())));
312 dcp::NameFormat::Map name_values;
313 name_values['f'] = decrypted.content_title_text();
314 name_values['b'] = dcp::LocalTime(_timing->from()).date() + " " + dcp::LocalTime(_timing->from()).time_of_day();
315 name_values['e'] = dcp::LocalTime(_timing->until()).date() + " " + dcp::LocalTime(_timing->until()).time_of_day();
317 if (_output->write_to()) {
318 ScreenKDM::write_files (screen_kdms, _output->directory(), _output->name_format(), name_values);
319 /* XXX: proper plural form support in wxWidgets? */
320 wxString s = screen_kdms.size() == 1 ? _("%d KDM written to %s") : _("%d KDMs written to %s");
323 wxString::Format (s, int(screen_kdms.size()), std_to_wx(_output->directory().string()).data())
326 string film_name = decrypted.annotation_text().get_value_or ("");
327 if (film_name.empty ()) {
328 film_name = decrypted.content_title_text ();
330 shared_ptr<Job> job (new SendKDMEmailJob (
331 CinemaKDMs::collect (screen_kdms),
332 _output->name_format(),
334 decrypted.content_title_text(),
338 JobManager::instance()->add (job);
340 _job_view->Destroy ();
343 _job_view = new JobViewDialog (this, _("Send KDM emails"), job);
344 _job_view->ShowModal ();
346 } catch (dcp::NotEncryptedError& e) {
347 error_dialog (this, _("CPL's content is not encrypted."));
348 } catch (exception& e) {
349 error_dialog (this, e.what ());
351 error_dialog (this, _("An unknown exception occurred."));
355 void setup_sensitivity ()
357 _screens->setup_sensitivity ();
358 _output->setup_sensitivity ();
359 _create->Enable (!_screens->screens().empty() && _dkdm->selection());
362 wxPreferencesEditor* _config_dialog;
363 ScreensPanel* _screens;
364 KDMTimingPanel* _timing;
365 EditableList<dcp::EncryptedKDM, KDMFileDialogWrapper>* _dkdm;
367 KDMOutputPanel* _output;
368 JobViewDialog* _job_view;
372 * @brief The magic App class for wxWidgets.
374 class App : public wxApp
387 wxInitAllImageHandlers ();
389 SetAppName (_("DCP-o-matic KDM Creator"));
391 if (!wxApp::OnInit()) {
395 #ifdef DCPOMATIC_LINUX
396 unsetenv ("UBUNTU_MENUPROXY");
400 ProcessSerialNumber serial;
401 GetCurrentProcess (&serial);
402 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
405 dcpomatic_setup_path_encoding ();
407 /* Enable i18n; this will create a Config object
408 to look for a force-configured language. This Config
409 object will be wrong, however, because dcpomatic_setup
410 hasn't yet been called and there aren't any filters etc.
413 dcpomatic_setup_i18n ();
415 /* Set things up, including filters etc.
416 which will now be internationalised correctly.
420 /* Force the configuration to be re-loaded correctly next
425 _frame = new DOMFrame (_("DCP-o-matic KDM Creator"));
426 SetTopWindow (_frame);
430 signal_manager = new wxSignalManager (this);
431 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
437 error_dialog (0, wxString::Format ("DCP-o-matic could not start: %s", e.what ()));
441 /* An unhandled exception has occurred inside the main event loop */
442 bool OnExceptionInMainLoop ()
446 } catch (FileError& e) {
450 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
451 std_to_wx (e.what()),
452 std_to_wx (e.file().string().c_str ())
455 } catch (exception& e) {
459 _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
460 std_to_wx (e.what ())
464 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
467 /* This will terminate the program */
471 void OnUnhandledException ()
473 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
478 signal_manager->ui_idle ();