2 Copyright (C) 2015-2016 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;
69 ID_help_report_a_problem = 1,
72 class KDMFileDialogWrapper : public FileDialogWrapper<dcp::EncryptedKDM>
75 KDMFileDialogWrapper (wxWindow* parent)
76 : FileDialogWrapper<dcp::EncryptedKDM> (parent, _("Select DKDM file"))
81 optional<dcp::EncryptedKDM> get ()
84 return dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (_dialog->GetPath ()), MAX_KDM_SIZE));
85 } catch (cxml::Error& e) {
86 error_dialog (_parent, wxString::Format ("This file does not look like a KDM (%s)", std_to_wx (e.what()).data()));
89 return optional<dcp::EncryptedKDM> ();
94 column (dcp::EncryptedKDM k)
96 return String::compose ("%1 (%2)", k.content_title_text(), k.cpl_id());
99 class DOMFrame : public wxFrame
102 DOMFrame (wxString const & title)
103 : wxFrame (0, -1, title)
107 #if defined(DCPOMATIC_WINDOWS)
108 if (Config::instance()->win32_console ()) {
111 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
112 int hCrt = _open_osfhandle((intptr_t) handle_out, _O_TEXT);
113 FILE* hf_out = _fdopen(hCrt, "w");
114 setvbuf(hf_out, NULL, _IONBF, 1);
117 HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
118 hCrt = _open_osfhandle((intptr_t) handle_in, _O_TEXT);
119 FILE* hf_in = _fdopen(hCrt, "r");
120 setvbuf(hf_in, NULL, _IONBF, 128);
123 std::cout << "DCP-o-matic KDM creator is starting." << "\n";
127 wxMenuBar* bar = new wxMenuBar;
131 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
132 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
133 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
134 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
136 /* Use a panel as the only child of the Frame so that we avoid
137 the dark-grey background on Windows.
139 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
140 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
142 wxBoxSizer* horizontal = new wxBoxSizer (wxHORIZONTAL);
143 wxBoxSizer* left = new wxBoxSizer (wxVERTICAL);
144 wxBoxSizer* right = new wxBoxSizer (wxVERTICAL);
146 horizontal->Add (left, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_X_GAP * 2);
147 horizontal->Add (right, 1, wxEXPAND);
149 wxFont subheading_font (*wxNORMAL_FONT);
150 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
152 wxStaticText* h = new wxStaticText (overall_panel, wxID_ANY, _("Screens"));
153 h->SetFont (subheading_font);
154 left->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
155 _screens = new ScreensPanel (overall_panel);
156 left->Add (_screens, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
158 /// TRANSLATORS: translate the word "Timing" here; do not include the "KDM|" prefix
159 h = new wxStaticText (overall_panel, wxID_ANY, S_("KDM|Timing"));
160 h->SetFont (subheading_font);
161 right->Add (h, 0, wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_Y_GAP * 2);
162 _timing = new KDMTimingPanel (overall_panel);
163 right->Add (_timing, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
165 h = new wxStaticText (overall_panel, wxID_ANY, _("DKDM"));
166 h->SetFont (subheading_font);
167 right->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
169 vector<string> columns;
170 columns.push_back (wx_to_std (_("CPL")));
171 _dkdm = new EditableList<dcp::EncryptedKDM, KDMFileDialogWrapper> (
172 overall_panel, columns, bind (&DOMFrame::dkdms, this), bind (&DOMFrame::set_dkdms, this, _1), bind (&column, _1), false
174 right->Add (_dkdm, 0, wxEXPAND | wxALL, DCPOMATIC_SIZER_Y_GAP);
176 h = new wxStaticText (overall_panel, wxID_ANY, _("Output"));
177 h->SetFont (subheading_font);
178 right->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
179 /* XXX: hard-coded non-interop here */
180 _output = new KDMOutputPanel (overall_panel, false);
181 right->Add (_output, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
183 _create = new wxButton (overall_panel, wxID_ANY, _("Create KDMs"));
184 right->Add (_create, 0, wxALL, DCPOMATIC_SIZER_GAP);
186 main_sizer->Add (horizontal, 1, wxALL | wxEXPAND, DCPOMATIC_DIALOG_BORDER);
187 overall_panel->SetSizer (main_sizer);
189 /* Instantly save any config changes when using a DCP-o-matic GUI */
190 Config::instance()->Changed.connect (boost::bind (&Config::write, Config::instance ()));
192 _screens->ScreensChanged.connect (boost::bind (&DOMFrame::setup_sensitivity, this));
193 _create->Bind (wxEVT_BUTTON, bind (&DOMFrame::create_kdms, this));
194 _dkdm->SelectionChanged.connect (boost::bind (&DOMFrame::setup_sensitivity, this));
196 setup_sensitivity ();
200 vector<dcp::EncryptedKDM> dkdms () const
202 return Config::instance()->dkdms ();
205 void set_dkdms (vector<dcp::EncryptedKDM> dkdms)
207 Config::instance()->set_dkdms (dkdms);
212 /* false here allows the close handler to veto the close request */
216 void edit_preferences ()
218 if (!_config_dialog) {
219 _config_dialog = create_config_dialog ();
221 _config_dialog->Show (this);
226 AboutDialog* d = new AboutDialog (this);
231 void help_report_a_problem ()
233 ReportProblemDialog* d = new ReportProblemDialog (this, shared_ptr<Film> ());
234 if (d->ShowModal () == wxID_OK) {
240 void setup_menu (wxMenuBar* m)
242 wxMenu* file = new wxMenu;
245 file->Append (wxID_EXIT, _("&Exit"));
247 file->Append (wxID_EXIT, _("&Quit"));
251 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
253 wxMenu* edit = new wxMenu;
254 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
257 wxMenu* help = new wxMenu;
259 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
261 help->Append (wxID_ABOUT, _("About"));
263 help->Append (ID_help_report_a_problem, _("Report a problem..."));
265 m->Append (file, _("&File"));
267 m->Append (edit, _("&Edit"));
269 m->Append (help, _("&Help"));
272 bool confirm_overwrite (boost::filesystem::path path)
274 return confirm_dialog (
276 wxString::Format (_("File %s already exists. Do you want to overwrite it?"), std_to_wx(path.string()).data())
283 if (!_dkdm->selection()) {
287 /* Decrypt the DKDM */
288 dcp::DecryptedKDM decrypted (_dkdm->selection().get(), Config::instance()->decryption_chain()->key().get());
290 /* This is the signer for our new KDMs */
291 shared_ptr<const dcp::CertificateChain> signer = Config::instance()->signer_chain ();
292 if (!signer->valid ()) {
293 throw InvalidSignerError ();
296 list<ScreenKDM> screen_kdms;
297 BOOST_FOREACH (shared_ptr<Screen> i, _screens->screens()) {
303 /* Make an empty KDM */
304 dcp::DecryptedKDM kdm (
305 dcp::LocalTime (_timing->from(), i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute()),
306 dcp::LocalTime (_timing->until(), i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute()),
307 decrypted.annotation_text().get_value_or (""),
308 decrypted.content_title_text(),
309 dcp::LocalTime().as_string()
312 /* Add keys from the DKDM */
313 BOOST_FOREACH (dcp::DecryptedKDMKey const & j, decrypted.keys()) {
318 screen_kdms.push_back (ScreenKDM (i, kdm.encrypt (signer, i->recipient.get(), i->trusted_devices, _output->formulation())));
321 dcp::NameFormat::Map name_values;
322 name_values['f'] = decrypted.content_title_text();
323 name_values['b'] = dcp::LocalTime(_timing->from()).date() + " " + dcp::LocalTime(_timing->from()).time_of_day();
324 name_values['e'] = dcp::LocalTime(_timing->until()).date() + " " + dcp::LocalTime(_timing->until()).time_of_day();
326 if (_output->write_to()) {
327 int written = ScreenKDM::write_files (
328 screen_kdms, _output->directory(), _output->name_format(), name_values,
329 bind (&DOMFrame::confirm_overwrite, this, _1)
333 /* XXX: proper plural form support in wxWidgets? */
334 wxString s = written == 1 ? _("%d KDM written to %s") : _("%d KDMs written to %s");
337 wxString::Format (s, written, std_to_wx(_output->directory().string()).data())
341 string film_name = decrypted.annotation_text().get_value_or ("");
342 if (film_name.empty ()) {
343 film_name = decrypted.content_title_text ();
345 shared_ptr<Job> job (new SendKDMEmailJob (
346 CinemaKDMs::collect (screen_kdms),
347 _output->name_format(),
349 decrypted.content_title_text(),
353 JobManager::instance()->add (job);
355 _job_view->Destroy ();
358 _job_view = new JobViewDialog (this, _("Send KDM emails"), job);
359 _job_view->ShowModal ();
361 } catch (dcp::NotEncryptedError& e) {
362 error_dialog (this, _("CPL's content is not encrypted."));
363 } catch (exception& e) {
364 error_dialog (this, e.what ());
366 error_dialog (this, _("An unknown exception occurred."));
370 void setup_sensitivity ()
372 _screens->setup_sensitivity ();
373 _output->setup_sensitivity ();
374 _create->Enable (!_screens->screens().empty() && _dkdm->selection());
377 wxPreferencesEditor* _config_dialog;
378 ScreensPanel* _screens;
379 KDMTimingPanel* _timing;
380 EditableList<dcp::EncryptedKDM, KDMFileDialogWrapper>* _dkdm;
382 KDMOutputPanel* _output;
383 JobViewDialog* _job_view;
387 * @brief The magic App class for wxWidgets.
389 class App : public wxApp
402 wxInitAllImageHandlers ();
404 SetAppName (_("DCP-o-matic KDM Creator"));
406 if (!wxApp::OnInit()) {
410 #ifdef DCPOMATIC_LINUX
411 unsetenv ("UBUNTU_MENUPROXY");
415 ProcessSerialNumber serial;
416 GetCurrentProcess (&serial);
417 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
420 dcpomatic_setup_path_encoding ();
422 /* Enable i18n; this will create a Config object
423 to look for a force-configured language. This Config
424 object will be wrong, however, because dcpomatic_setup
425 hasn't yet been called and there aren't any filters etc.
428 dcpomatic_setup_i18n ();
430 /* Set things up, including filters etc.
431 which will now be internationalised correctly.
435 /* Force the configuration to be re-loaded correctly next
440 _frame = new DOMFrame (_("DCP-o-matic KDM Creator"));
441 SetTopWindow (_frame);
445 signal_manager = new wxSignalManager (this);
446 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
452 error_dialog (0, wxString::Format ("DCP-o-matic could not start: %s", e.what ()));
456 /* An unhandled exception has occurred inside the main event loop */
457 bool OnExceptionInMainLoop ()
461 } catch (FileError& e) {
465 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
466 std_to_wx (e.what()),
467 std_to_wx (e.file().string().c_str ())
470 } catch (exception& e) {
474 _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
475 std_to_wx (e.what ())
479 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
482 /* This will terminate the program */
486 void OnUnhandledException ()
488 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
493 signal_manager->ui_idle ();