2 Copyright (C) 2015-2021 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/>.
22 #include "wx/about_dialog.h"
23 #include "wx/dcpomatic_button.h"
24 #include "wx/editable_list.h"
25 #include "wx/file_dialog_wrapper.h"
26 #include "wx/file_picker_ctrl.h"
27 #include "wx/full_config_dialog.h"
28 #include "wx/job_view_dialog.h"
29 #include "wx/kdm_output_panel.h"
30 #include "wx/kdm_timing_panel.h"
31 #include "wx/nag_dialog.h"
32 #include "wx/new_dkdm_folder_dialog.h"
33 #include "wx/report_problem_dialog.h"
34 #include "wx/screens_panel.h"
35 #include "wx/static_text.h"
36 #include "wx/wx_signal_manager.h"
37 #include "wx/wx_util.h"
38 #include "lib/cinema.h"
39 #include "lib/compose.hpp"
40 #include "lib/config.h"
41 #include "lib/cross.h"
42 #include "lib/dkdm_wrapper.h"
43 #include "lib/exceptions.h"
44 #include "lib/job_manager.h"
45 #include "lib/kdm_with_metadata.h"
46 #include "lib/screen.h"
47 #include "lib/send_kdm_email_job.h"
49 #include <dcp/encrypted_kdm.h>
50 #include <dcp/decrypted_kdm.h>
51 #include <dcp/exceptions.h>
52 #include "lib/warnings.h"
53 DCPOMATIC_DISABLE_WARNINGS
55 #include <wx/preferences.h>
56 #include <wx/splash.h>
57 #include <wx/filepicker.h>
58 DCPOMATIC_ENABLE_WARNINGS
60 #include <ApplicationServices/ApplicationServices.h>
62 #include <boost/bind/bind.hpp>
71 using std::make_shared;
74 using std::shared_ptr;
78 using boost::optional;
80 using std::dynamic_pointer_cast;
81 #if BOOST_VERSION >= 106100
82 using namespace boost::placeholders;
84 using namespace dcpomatic;
88 ID_help_report_a_problem = 1,
92 class DOMFrame : public wxFrame
95 explicit DOMFrame (wxString const & title)
96 : wxFrame (nullptr, -1, title)
97 , _config_dialog (nullptr)
100 #if defined(DCPOMATIC_WINDOWS)
101 if (Config::instance()->win32_console ()) {
104 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
105 int hCrt = _open_osfhandle((intptr_t) handle_out, _O_TEXT);
106 FILE* hf_out = _fdopen(hCrt, "w");
107 setvbuf(hf_out, NULL, _IONBF, 1);
110 HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
111 hCrt = _open_osfhandle((intptr_t) handle_in, _O_TEXT);
112 FILE* hf_in = _fdopen(hCrt, "r");
113 setvbuf(hf_in, NULL, _IONBF, 128);
116 std::cout << "DCP-o-matic KDM creator is starting." << "\n";
120 auto bar = new wxMenuBar;
124 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
125 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
126 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
127 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
129 /* Use a panel as the only child of the Frame so that we avoid
130 the dark-grey background on Windows.
132 auto overall_panel = new wxPanel (this, wxID_ANY);
133 auto main_sizer = new wxBoxSizer (wxHORIZONTAL);
135 auto horizontal = new wxBoxSizer (wxHORIZONTAL);
136 auto left = new wxBoxSizer (wxVERTICAL);
137 auto right = new wxBoxSizer (wxVERTICAL);
139 horizontal->Add (left, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_X_GAP * 2);
140 horizontal->Add (right, 1, wxEXPAND);
142 wxFont subheading_font (*wxNORMAL_FONT);
143 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
145 auto h = new StaticText (overall_panel, _("Screens"));
146 h->SetFont (subheading_font);
147 left->Add (h, 0, wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
148 _screens = new ScreensPanel (overall_panel);
149 left->Add (_screens, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
151 /// TRANSLATORS: translate the word "Timing" here; do not include the "KDM|" prefix
152 h = new StaticText (overall_panel, S_("KDM|Timing"));
153 h->SetFont (subheading_font);
155 _timing = new KDMTimingPanel (overall_panel);
156 right->Add (_timing, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
158 h = new StaticText (overall_panel, _("DKDM"));
159 h->SetFont (subheading_font);
160 right->Add (h, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
161 auto dkdm_sizer = new wxBoxSizer (wxHORIZONTAL);
162 _dkdm = new wxTreeCtrl (
163 overall_panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT
165 dkdm_sizer->Add (_dkdm, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_Y_GAP);
166 wxBoxSizer* dkdm_buttons = new wxBoxSizer(wxVERTICAL);
167 _add_dkdm = new Button (overall_panel, _("Add..."));
168 dkdm_buttons->Add (_add_dkdm, 0, wxALL | wxEXPAND, DCPOMATIC_BUTTON_STACK_GAP);
169 _add_dkdm_folder = new Button (overall_panel, _("Add folder..."));
170 dkdm_buttons->Add (_add_dkdm_folder, 0, wxALL | wxEXPAND, DCPOMATIC_BUTTON_STACK_GAP);
171 _remove_dkdm = new Button (overall_panel, _("Remove"));
172 dkdm_buttons->Add (_remove_dkdm, 0, wxALL | wxEXPAND, DCPOMATIC_BUTTON_STACK_GAP);
173 _export_dkdm = new Button (overall_panel, _("Export..."));
174 dkdm_buttons->Add (_export_dkdm, 0, wxALL | wxEXPAND, DCPOMATIC_BUTTON_STACK_GAP);
175 dkdm_sizer->Add (dkdm_buttons, 0, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
176 right->Add (dkdm_sizer, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_Y_GAP);
178 add_dkdm_view (Config::instance()->dkdms());
180 h = new StaticText (overall_panel, _("Output"));
181 h->SetFont (subheading_font);
182 right->Add (h, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
183 /* XXX: hard-coded non-interop here */
184 _output = new KDMOutputPanel (overall_panel, false);
185 right->Add (_output, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
187 _create = new Button (overall_panel, _("Create KDMs"));
188 right->Add (_create, 0, wxALL, DCPOMATIC_SIZER_GAP);
190 main_sizer->Add (horizontal, 1, wxALL | wxEXPAND, DCPOMATIC_DIALOG_BORDER);
191 overall_panel->SetSizer (main_sizer);
193 /* Instantly save any config changes when using a DCP-o-matic GUI */
194 Config::instance()->Changed.connect (boost::bind (&Config::write, Config::instance ()));
196 _screens->ScreensChanged.connect (boost::bind (&DOMFrame::setup_sensitivity, this));
197 _create->Bind (wxEVT_BUTTON, bind (&DOMFrame::create_kdms, this));
198 _dkdm->Bind (wxEVT_TREE_SEL_CHANGED, boost::bind (&DOMFrame::setup_sensitivity, this));
199 _dkdm->Bind (wxEVT_TREE_BEGIN_DRAG, boost::bind (&DOMFrame::dkdm_begin_drag, this, _1));
200 _dkdm->Bind (wxEVT_TREE_END_DRAG, boost::bind (&DOMFrame::dkdm_end_drag, this, _1));
201 _add_dkdm->Bind (wxEVT_BUTTON, bind (&DOMFrame::add_dkdm_clicked, this));
202 _add_dkdm_folder->Bind (wxEVT_BUTTON, bind (&DOMFrame::add_dkdm_folder_clicked, this));
203 _remove_dkdm->Bind (wxEVT_BUTTON, bind (&DOMFrame::remove_dkdm_clicked, this));
204 _export_dkdm->Bind (wxEVT_BUTTON, bind (&DOMFrame::export_dkdm_clicked, this));
206 setup_sensitivity ();
212 /* false here allows the close handler to veto the close request */
216 void edit_preferences ()
218 if (!_config_dialog) {
219 _config_dialog = create_full_config_dialog ();
221 _config_dialog->Show (this);
226 auto d = new AboutDialog (this);
231 void help_report_a_problem ()
233 auto d = new ReportProblemDialog (this, shared_ptr<Film>());
234 if (d->ShowModal () == wxID_OK) {
240 void setup_menu (wxMenuBar* m)
242 auto 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())
280 /** @id if not 0 this is filled in with the wxTreeItemId of the selection */
281 shared_ptr<DKDMBase> selected_dkdm (wxTreeItemId* id = 0) const
283 wxArrayTreeItemIds selections;
284 _dkdm->GetSelections (selections);
285 if (selections.GetCount() != 1) {
289 return shared_ptr<DKDMBase> ();
296 auto i = _dkdm_id.find (selections[0]);
297 if (i == _dkdm_id.end()) {
298 return shared_ptr<DKDMBase> ();
307 auto dkdm_base = selected_dkdm ();
312 list<KDMWithMetadataPtr> kdms;
315 auto dkdm = std::dynamic_pointer_cast<DKDM>(dkdm_base);
318 /* Decrypt the DKDM */
319 dcp::DecryptedKDM decrypted (dkdm->dkdm(), Config::instance()->decryption_chain()->key().get());
320 title = decrypted.content_title_text ();
322 /* This is the signer for our new KDMs */
323 auto signer = Config::instance()->signer_chain ();
324 if (!signer->valid ()) {
325 throw InvalidSignerError ();
328 for (auto i: _screens->screens()) {
334 dcp::LocalTime begin(_timing->from(), i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute());
335 dcp::LocalTime end(_timing->until(), i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute());
337 /* Make an empty KDM */
338 dcp::DecryptedKDM kdm (
341 decrypted.annotation_text().get_value_or (""),
342 decrypted.content_title_text(),
343 dcp::LocalTime().as_string()
346 /* Add keys from the DKDM */
347 for (auto const& j: decrypted.keys()) {
351 auto const encrypted = kdm.encrypt(
352 signer, i->recipient.get(), i->trusted_device_thumbprints(), _output->formulation(),
353 !_output->forensic_mark_video(), _output->forensic_mark_audio() ? boost::optional<int>() : 0
356 dcp::NameFormat::Map name_values;
357 name_values['c'] = i->cinema->name;
358 name_values['s'] = i->name;
359 name_values['f'] = title;
360 name_values['b'] = begin.date() + " " + begin.time_of_day(true, false);
361 name_values['e'] = end.date() + " " + end.time_of_day(true, false);
362 name_values['i'] = encrypted.cpl_id ();
365 kdms.push_back (make_shared<KDMWithMetadata>(name_values, i->cinema.get(), i->cinema->emails, encrypted));
373 auto result = _output->make (
374 kdms, title, bind (&DOMFrame::confirm_overwrite, this, _1)
378 JobManager::instance()->add (result.first);
380 _job_view->Destroy ();
383 _job_view = new JobViewDialog (this, _("Send KDM emails"), result.first);
384 _job_view->ShowModal ();
387 if (result.second > 0) {
388 /* XXX: proper plural form support in wxWidgets? */
389 wxString s = result.second == 1 ? _("%d KDM written to %s") : _("%d KDMs written to %s");
392 wxString::Format (s, result.second, std_to_wx(_output->directory().string()).data())
395 } catch (dcp::NotEncryptedError& e) {
396 error_dialog (this, _("CPL's content is not encrypted."));
397 } catch (exception& e) {
398 error_dialog (this, std_to_wx(e.what()));
400 error_dialog (this, _("An unknown exception occurred."));
404 void setup_sensitivity ()
406 _screens->setup_sensitivity ();
407 _output->setup_sensitivity ();
408 wxArrayTreeItemIds sel;
409 _dkdm->GetSelections (sel);
410 _create->Enable (!_screens->screens().empty() && sel.GetCount() > 0);
411 auto group = dynamic_pointer_cast<DKDMGroup>(selected_dkdm());
412 auto dkdm = dynamic_pointer_cast<DKDM>(selected_dkdm());
413 _remove_dkdm->Enable (sel.GetCount() > 0 && (!group || group->name() != "root"));
414 _export_dkdm->Enable (sel.GetCount() > 0 && dkdm);
417 void dkdm_begin_drag (wxTreeEvent& ev)
422 void dkdm_end_drag (wxTreeEvent& ev)
424 auto from = _dkdm_id.find (_dkdm->GetSelection ());
425 auto to = _dkdm_id.find (ev.GetItem ());
426 if (from == _dkdm_id.end() || to == _dkdm_id.end() || from->first == to->first) {
430 auto group = dynamic_pointer_cast<DKDMGroup> (to->second);
432 group = to->second->parent();
435 DCPOMATIC_ASSERT (group);
436 DCPOMATIC_ASSERT (from->second->parent ());
438 from->second->parent()->remove (from->second);
439 add_dkdm_model (from->second, group, dynamic_pointer_cast<DKDM>(to->second));
441 _dkdm->Delete (from->first);
442 _dkdm_id.erase (from->first);
443 add_dkdm_view (from->second, dynamic_pointer_cast<DKDM>(to->second) ? to->first : optional<wxTreeItemId>());
446 void add_dkdm_clicked ()
448 auto d = new wxFileDialog (this, _("Select DKDM file"));
449 if (d->ShowModal() == wxID_OK) {
450 auto chain = Config::instance()->decryption_chain();
451 DCPOMATIC_ASSERT (chain->key());
454 dcp::EncryptedKDM ekdm(dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE));
455 /* Decrypt the DKDM to make sure that we can */
456 dcp::DecryptedKDM dkdm(ekdm, chain->key().get());
458 auto new_dkdm = make_shared<DKDM>(ekdm);
459 auto group = dynamic_pointer_cast<DKDMGroup> (selected_dkdm());
461 group = Config::instance()->dkdms ();
463 add_dkdm_model (new_dkdm, group);
464 add_dkdm_view (new_dkdm);
465 } catch (dcp::KDMFormatError& e) {
468 _("Could not read file as a KDM. Perhaps it is badly formatted, or not a KDM at all."),
472 } catch (dcp::KDMDecryptionError &) {
475 _("Could not decrypt the DKDM. Perhaps it was not created with the correct certificate.")
477 } catch (dcp::MiscError& e) {
480 _("Could not read file as a KDM. It is much too large. Make sure you are loading a DKDM (XML) file."),
488 void add_dkdm_folder_clicked ()
490 auto d = new NewDKDMFolderDialog (this);
491 if (d->ShowModal() == wxID_OK) {
492 auto new_dkdm = make_shared<DKDMGroup>(wx_to_std(d->get()));
493 auto parent = dynamic_pointer_cast<DKDMGroup>(selected_dkdm());
495 parent = Config::instance()->dkdms ();
497 add_dkdm_model (new_dkdm, parent);
498 add_dkdm_view (new_dkdm);
503 /** @param dkdm Thing to add.
504 * @param parent Parent group, or 0.
506 void add_dkdm_view (shared_ptr<DKDMBase> base, optional<wxTreeItemId> previous = optional<wxTreeItemId>())
508 if (!base->parent()) {
509 /* This is the root group */
510 _dkdm_id[_dkdm->AddRoot("root")] = base;
512 /* Add base to the view */
515 added = _dkdm->InsertItem(dkdm_to_id(base->parent()), *previous, std_to_wx(base->name()));
517 added = _dkdm->AppendItem(dkdm_to_id(base->parent()), std_to_wx(base->name()));
519 _dkdm_id[added] = base;
523 auto g = dynamic_pointer_cast<DKDMGroup>(base);
525 for (auto i: g->children()) {
531 /** @param group Group to add dkdm to */
532 void add_dkdm_model (shared_ptr<DKDMBase> dkdm, shared_ptr<DKDMGroup> group, shared_ptr<DKDM> previous = shared_ptr<DKDM> ())
534 group->add (dkdm, previous);
535 /* We're messing with a Config-owned object here, so tell it that something has changed.
538 Config::instance()->changed ();
541 wxTreeItemId dkdm_to_id (shared_ptr<DKDMBase> dkdm)
543 for (auto const& i: _dkdm_id) {
544 if (i.second == dkdm) {
548 DCPOMATIC_ASSERT (false);
551 void remove_dkdm_clicked ()
553 auto removed = selected_dkdm ();
558 if (NagDialog::maybe_nag (
559 this, Config::NAG_DELETE_DKDM,
560 _("You are about to remove a DKDM. This will make it impossible to decrypt the DCP that the DKDM was made for, and it cannot be undone. "
566 _dkdm->Delete (dkdm_to_id (removed));
567 auto dkdms = Config::instance()->dkdms ();
568 dkdms->remove (removed);
569 Config::instance()->changed ();
572 void export_dkdm_clicked ()
574 auto removed = selected_dkdm ();
579 auto dkdm = dynamic_pointer_cast<DKDM>(removed);
584 auto d = new wxFileDialog (
585 this, _("Select DKDM File"), wxEmptyString, wxEmptyString, wxT("XML files (*.xml)|*.xml"),
586 wxFD_SAVE | wxFD_OVERWRITE_PROMPT
589 if (d->ShowModal() == wxID_OK) {
590 dkdm->dkdm().as_xml(wx_to_std(d->GetPath()));
595 wxPreferencesEditor* _config_dialog;
596 ScreensPanel* _screens;
597 KDMTimingPanel* _timing;
599 typedef std::map<wxTreeItemId, std::shared_ptr<DKDMBase>> DKDMMap;
602 wxButton* _add_dkdm_folder;
603 wxButton* _remove_dkdm;
604 wxButton* _export_dkdm;
606 KDMOutputPanel* _output;
607 JobViewDialog* _job_view;
612 * @brief The magic App class for wxWidgets.
614 class App : public wxApp
626 wxSplashScreen* splash = nullptr;
629 wxInitAllImageHandlers ();
631 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
632 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
634 splash = maybe_show_splash ();
636 SetAppName (_("DCP-o-matic KDM Creator"));
638 if (!wxApp::OnInit()) {
642 #ifdef DCPOMATIC_LINUX
643 unsetenv ("UBUNTU_MENUPROXY");
647 make_foreground_application ();
650 dcpomatic_setup_path_encoding ();
652 /* Enable i18n; this will create a Config object
653 to look for a force-configured language. This Config
654 object will be wrong, however, because dcpomatic_setup
655 hasn't yet been called and there aren't any filters etc.
658 dcpomatic_setup_i18n ();
660 /* Set things up, including filters etc.
661 which will now be internationalised correctly.
665 /* Force the configuration to be re-loaded correctly next
670 _frame = new DOMFrame (_("DCP-o-matic KDM Creator"));
671 SetTopWindow (_frame);
679 signal_manager = new wxSignalManager (this);
680 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
687 error_dialog (0, _("DCP-o-matic could not start"), std_to_wx(e.what()));
693 /* An unhandled exception has occurred inside the main event loop */
694 bool OnExceptionInMainLoop ()
698 } catch (FileError& e) {
702 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
703 std_to_wx (e.what()),
704 std_to_wx (e.file().string().c_str ())
707 } catch (exception& e) {
711 _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
716 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
719 /* This will terminate the program */
723 void OnUnhandledException ()
725 error_dialog (nullptr, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
730 signal_manager->ui_idle ();
733 void config_failed_to_load ()
735 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
738 void config_warning (string m)
740 message_dialog (_frame, std_to_wx(m));