2 Copyright (C) 2012-2022 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 "dcpomatic_button.h"
23 #include "download_certificate_dialog.h"
24 #include "file_dialog.h"
25 #include "screen_dialog.h"
26 #include "static_text.h"
27 #include "table_dialog.h"
29 #include "lib/compose.hpp"
30 #include "lib/scope_guard.h"
32 #include <dcp/warnings.h>
33 #include <dcp/exceptions.h>
34 #include <dcp/certificate_chain.h>
35 LIBDCP_DISABLE_WARNINGS
36 #include <wx/filepicker.h>
37 #include <wx/validate.h>
38 LIBDCP_ENABLE_WARNINGS
44 using boost::optional;
45 #if BOOST_VERSION >= 106100
46 using namespace boost::placeholders;
50 class TrustedDeviceDialog : public TableDialog
53 explicit TrustedDeviceDialog (wxWindow* parent)
54 : TableDialog (parent, _("Trusted Device"), 3, 1, true)
56 add (_("Thumbprint"), true);
57 _thumbprint = add(new wxTextCtrl(this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(300, -1), wxTE_READONLY));
58 _file = add (new Button(this, _("Load certificate...")));
62 _file->Bind (wxEVT_BUTTON, bind(&TrustedDeviceDialog::load_certificate, this));
67 void load_certificate ()
69 FileDialog dialog(this, _("Trusted Device certificate"), wxEmptyString, wxFD_DEFAULT_STYLE, "SelectCertificatePath");
75 _certificate = dcp::CertificateChain(dcp::file_to_string(dialog.paths()[0])).leaf();
76 _thumbprint->SetValue (std_to_wx(_certificate->thumbprint()));
78 } catch (dcp::MiscError& e) {
79 error_dialog(this, wxString::Format(_("Could not load certificate (%s)"), std_to_wx(e.what())));
83 void set (TrustedDevice t)
85 _certificate = t.certificate ();
86 _thumbprint->SetValue (std_to_wx(t.thumbprint()));
90 optional<TrustedDevice> get ()
92 auto const t = wx_to_std (_thumbprint->GetValue());
93 if (_certificate && _certificate->thumbprint() == t) {
94 return TrustedDevice (*_certificate);
95 } else if (t.length() == 28) {
96 return TrustedDevice (t);
103 void setup_sensitivity()
105 auto ok = dynamic_cast<wxButton*>(FindWindowById(wxID_OK, this));
106 DCPOMATIC_ASSERT(ok);
107 ok->Enable(static_cast<bool>(_certificate));
110 wxTextCtrl* _thumbprint;
112 boost::optional<dcp::Certificate> _certificate;
116 ScreenDialog::ScreenDialog (
121 optional<dcp::Certificate> recipient,
122 optional<string> recipient_file,
123 vector<TrustedDevice> trusted_devices
125 : wxDialog (parent, wxID_ANY, title)
126 , _recipient (recipient)
127 , _trusted_devices (trusted_devices)
129 auto overall_sizer = new wxBoxSizer (wxVERTICAL);
130 SetSizer (overall_sizer);
132 _sizer = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
135 wxFont subheading_font(*wxNORMAL_FONT);
136 subheading_font.SetWeight(wxFONTWEIGHT_BOLD);
138 auto subheading = new StaticText(this, _("Details"));
139 subheading->SetFont(subheading_font);
140 _sizer->Add(subheading, wxGBPosition(r, 0), wxGBSpan(1, 2));
143 add_label_to_sizer(_sizer, this, _("Name"), true, wxGBPosition(r, 0), wxDefaultSpan, true);
144 _name = new wxTextCtrl (this, wxID_ANY, std_to_wx (name), wxDefaultPosition, wxSize (320, -1));
145 _sizer->Add (_name, wxGBPosition (r, 1));
148 add_label_to_sizer(_sizer, this, _("Notes"), true, wxGBPosition(r, 0), wxDefaultSpan, true);
149 _notes = new wxTextCtrl (this, wxID_ANY, std_to_wx(notes), wxDefaultPosition, wxSize(320, -1));
150 _sizer->Add (_notes, wxGBPosition(r, 1));
153 subheading = new StaticText(this, _("Recipient"));
154 subheading->SetFont(subheading_font);
155 _sizer->Add(subheading, wxGBPosition(r, 0), wxGBSpan(1, 2), wxTOP, DCPOMATIC_SUBHEADING_TOP_PAD);
158 _get_recipient_from_file = new Button (this, _("Get from file..."));
159 _download_recipient = new Button (this, _("Download..."));
160 auto s = new wxBoxSizer (wxHORIZONTAL);
161 s->Add (_get_recipient_from_file, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_X_GAP);
162 s->Add (_download_recipient, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_X_GAP);
163 _sizer->Add(s, wxGBPosition(r, 0), wxGBSpan(1, 2));
166 auto add_certificate_detail = [&r, this](wxString name, wxStaticText** value, wxSize size = wxDefaultSize) {
167 add_label_to_sizer(_sizer, this, name, true, wxGBPosition(r, 0), wxDefaultSpan, true);
168 *value = new StaticText(this, wxT (""), wxDefaultPosition, size);
169 _sizer->Add(*value, wxGBPosition(r, 1));
173 wxClientDC dc (this);
174 wxFont teletype_font = _name->GetFont();
175 teletype_font.SetFamily(wxFONTFAMILY_TELETYPE);
176 dc.SetFont(teletype_font);
177 wxSize size = dc.GetTextExtent (wxT("1234567890123456789012345678"));
180 add_certificate_detail(_("Thumbprint"), &_recipient_thumbprint, size);
181 _recipient_thumbprint->SetFont(teletype_font);
183 add_label_to_sizer(_sizer, this, _("Filename"), true, wxGBPosition(r, 0), wxDefaultSpan, true);
184 _recipient_file = new wxStaticText(this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(600, -1), wxST_ELLIPSIZE_MIDDLE | wxST_NO_AUTORESIZE);
185 set_recipient_file(recipient_file.get_value_or(""));
186 _sizer->Add (_recipient_file, wxGBPosition(r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_Y_GAP);
189 add_certificate_detail(_("Subject common name"), &_subject_common_name);
190 add_certificate_detail(_("Subject organization name"), &_subject_organization_name);
191 add_certificate_detail(_("Issuer common name"), &_issuer_common_name);
192 add_certificate_detail(_("Issuer organization name"), &_issuer_organization_name);
193 add_certificate_detail(_("Not valid before"), &_not_valid_before);
194 add_certificate_detail(_("Not valid after"), &_not_valid_after);
196 set_recipient (recipient);
199 int flags = wxALIGN_CENTER_VERTICAL | wxTOP;
201 flags |= wxALIGN_RIGHT;
202 auto m = new StaticText (this, _("Other trusted devices") + wxT(":"));
204 auto m = new StaticText (this, _("Other trusted devices"));
206 m->SetFont(subheading_font);
207 _sizer->Add(m, wxGBPosition(r, 0), wxDefaultSpan, flags, DCPOMATIC_SUBHEADING_TOP_PAD);
211 vector<EditableListColumn> columns;
212 columns.push_back (EditableListColumn(_("Thumbprint")));
213 _trusted_device_list = new EditableList<TrustedDevice, TrustedDeviceDialog> (
216 bind (&ScreenDialog::trusted_devices, this),
217 bind (&ScreenDialog::set_trusted_devices, this, _1),
218 [] (TrustedDevice const& d, int) {
219 return d.thumbprint();
221 EditableListTitle::INVISIBLE,
222 EditableListButton::NEW | EditableListButton::EDIT | EditableListButton::REMOVE
225 _sizer->Add(_trusted_device_list, wxGBPosition (r, 0), wxGBSpan (1, 3), wxEXPAND | wxLEFT, DCPOMATIC_SIZER_X_GAP);
228 _name->Bind (wxEVT_TEXT, boost::bind (&ScreenDialog::setup_sensitivity, this));
229 _get_recipient_from_file->Bind (wxEVT_BUTTON, boost::bind (&ScreenDialog::get_recipient_from_file, this));
230 _download_recipient->Bind (wxEVT_BUTTON, boost::bind (&ScreenDialog::download_recipient, this));
232 overall_sizer->Add (_sizer, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
234 auto buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
236 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
239 overall_sizer->Layout ();
240 overall_sizer->SetSizeHints (this);
242 setup_sensitivity ();
247 ScreenDialog::name () const
249 return wx_to_std (_name->GetValue());
254 ScreenDialog::notes () const
256 return wx_to_std (_notes->GetValue());
260 optional<dcp::Certificate>
261 ScreenDialog::recipient () const
268 ScreenDialog::recipient_file () const
270 auto const f = wx_to_std(_recipient_file->GetLabel());
279 ScreenDialog::load_recipient (boost::filesystem::path file)
282 /* Load this as a chain, in case it is one, and then pick the leaf certificate */
283 dcp::CertificateChain c (dcp::file_to_string(file));
284 if (c.unordered().empty()) {
285 error_dialog (this, _("Could not read certificate file."));
288 set_recipient (c.leaf ());
289 set_recipient_file(file.string());
290 } catch (dcp::MiscError& e) {
291 error_dialog (this, _("Could not read certificate file."), std_to_wx(e.what()));
297 ScreenDialog::get_recipient_from_file ()
299 FileDialog dialog(this, _("Select Certificate File"), wxEmptyString, wxFD_DEFAULT_STYLE , "SelectCertificatePath");
301 load_recipient(dialog.paths()[0]);
304 setup_sensitivity ();
309 ScreenDialog::download_recipient ()
311 DownloadCertificateDialog dialog(this);
312 if (dialog.ShowModal() == wxID_OK) {
313 set_recipient(dialog.certificate());
314 set_recipient_file(dialog.url());
316 setup_sensitivity ();
321 ScreenDialog::setup_sensitivity ()
323 auto ok = dynamic_cast<wxButton*> (FindWindowById(wxID_OK, this));
325 ok->Enable (static_cast<bool>(_recipient) && !_name->GetValue().IsEmpty());
331 ScreenDialog::set_recipient (optional<dcp::Certificate> r)
336 _recipient_thumbprint->SetLabel (std_to_wx (_recipient->thumbprint ()));
337 _subject_common_name->SetLabel(std_to_wx(_recipient->subject_common_name()));
338 _subject_organization_name->SetLabel(std_to_wx(_recipient->subject_organization_name()));
339 _issuer_common_name->SetLabel(std_to_wx(_recipient->issuer_common_name()));
340 _issuer_organization_name->SetLabel(std_to_wx(_recipient->issuer_organization_name()));
341 _not_valid_before->SetLabel(std_to_wx(_recipient->not_before().as_string()));
342 _not_valid_after->SetLabel(std_to_wx(_recipient->not_after().as_string()));
349 ScreenDialog::set_recipient_file(string file)
351 checked_set(_recipient_file, file);
352 _recipient_file->SetToolTip(std_to_wx(file));