2 Copyright (C) 2012-2018 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 "screen_dialog.h"
23 #include "file_dialog_wrapper.h"
24 #include "static_text.h"
25 #include "download_certificate_dialog.h"
26 #include "table_dialog.h"
27 #include "dcpomatic_button.h"
28 #include "lib/compose.hpp"
30 #include <dcp/exceptions.h>
31 #include <dcp/certificate_chain.h>
32 #include <wx/filepicker.h>
33 #include <wx/validate.h>
39 using boost::optional;
43 column (TrustedDevice d)
45 return d.thumbprint ();
48 class TrustedDeviceDialog : public TableDialog
51 explicit TrustedDeviceDialog (wxWindow* parent)
52 : TableDialog (parent, _("Trusted Device"), 3, 1, true)
54 add (_("Thumbprint"), true);
55 _thumbprint = add (new wxTextCtrl(this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(300, -1)));
56 _file = add (new Button(this, _("Load certificate...")));
60 _file->Bind (wxEVT_BUTTON, bind(&TrustedDeviceDialog::load_certificate, this));
63 void load_certificate ()
65 wxFileDialog* d = new wxFileDialog (this, _("Trusted Device certificate"));
68 _certificate = dcp::Certificate(dcp::file_to_string(wx_to_std(d->GetPath())));
69 _thumbprint->SetValue (std_to_wx(_certificate->thumbprint()));
70 } catch (dcp::MiscError& e) {
71 error_dialog (this, wxString::Format(_("Could not load certficate (%s)"), std_to_wx(e.what())));
75 void set (TrustedDevice t)
77 _certificate = t.certificate ();
78 _thumbprint->SetValue (std_to_wx(t.thumbprint()));
81 optional<TrustedDevice> get ()
83 string const t = wx_to_std (_thumbprint->GetValue ());
84 if (_certificate && _certificate->thumbprint() == t) {
85 return TrustedDevice (*_certificate);
86 } else if (t.length() == 28) {
87 return TrustedDevice (t);
90 return optional<TrustedDevice> ();
94 wxTextCtrl* _thumbprint;
96 boost::optional<dcp::Certificate> _certificate;
99 ScreenDialog::ScreenDialog (
100 wxWindow* parent, wxString title, string name, string notes, optional<dcp::Certificate> recipient, vector<TrustedDevice> trusted_devices
102 : wxDialog (parent, wxID_ANY, title)
103 , _recipient (recipient)
104 , _trusted_devices (trusted_devices)
106 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
107 SetSizer (overall_sizer);
109 _sizer = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
112 add_label_to_sizer (_sizer, this, _("Name"), true, wxGBPosition (r, 0));
113 _name = new wxTextCtrl (this, wxID_ANY, std_to_wx (name), wxDefaultPosition, wxSize (320, -1));
114 _sizer->Add (_name, wxGBPosition (r, 1));
117 add_label_to_sizer (_sizer, this, _("Notes"), true, wxGBPosition (r, 0));
118 _notes = new wxTextCtrl (this, wxID_ANY, std_to_wx (notes), wxDefaultPosition, wxSize (320, -1));
119 _sizer->Add (_notes, wxGBPosition (r, 1));
122 wxClientDC dc (this);
123 wxFont font = _name->GetFont ();
124 font.SetFamily (wxFONTFAMILY_TELETYPE);
126 wxSize size = dc.GetTextExtent (wxT ("1234567890123456789012345678"));
129 add_label_to_sizer (_sizer, this, _("Recipient certificate"), true, wxGBPosition (r, 0));
130 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
131 _recipient_thumbprint = new StaticText (this, wxT (""), wxDefaultPosition, size);
132 _recipient_thumbprint->SetFont (font);
133 set_recipient (recipient);
134 _get_recipient_from_file = new Button (this, _("Get from file..."));
135 _download_recipient = new Button (this, _("Download..."));
136 s->Add (_recipient_thumbprint, 1, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
137 s->Add (_get_recipient_from_file, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_X_GAP);
138 s->Add (_download_recipient, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_X_GAP);
139 _sizer->Add (s, wxGBPosition (r, 1));
142 add_label_to_sizer (_sizer, this, _("Other trusted devices"), true, wxGBPosition (r, 0));
145 vector<EditableListColumn> columns;
146 columns.push_back (EditableListColumn(wx_to_std(_("Thumbprint"))));
147 _trusted_device_list = new EditableList<TrustedDevice, TrustedDeviceDialog> (
150 bind (&ScreenDialog::trusted_devices, this),
151 bind (&ScreenDialog::set_trusted_devices, this, _1),
156 _sizer->Add (_trusted_device_list, wxGBPosition (r, 0), wxGBSpan (1, 3), wxEXPAND);
159 _name->Bind (wxEVT_TEXT, boost::bind (&ScreenDialog::setup_sensitivity, this));
160 _get_recipient_from_file->Bind (wxEVT_BUTTON, boost::bind (&ScreenDialog::get_recipient_from_file, this));
161 _download_recipient->Bind (wxEVT_BUTTON, boost::bind (&ScreenDialog::download_recipient, this));
163 overall_sizer->Add (_sizer, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
165 wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
167 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
170 overall_sizer->Layout ();
171 overall_sizer->SetSizeHints (this);
173 setup_sensitivity ();
177 ScreenDialog::name () const
179 return wx_to_std (_name->GetValue());
183 ScreenDialog::notes () const
185 return wx_to_std (_notes->GetValue());
188 optional<dcp::Certificate>
189 ScreenDialog::recipient () const
195 ScreenDialog::load_recipient (boost::filesystem::path file)
198 /* Load this as a chain, in case it is one, and then pick the leaf certificate */
199 dcp::CertificateChain c (dcp::file_to_string (file));
200 if (c.unordered().empty()) {
201 error_dialog (this, _("Could not read certificate file."));
204 set_recipient (c.leaf ());
205 } catch (dcp::MiscError& e) {
206 error_dialog (this, _("Could not read certificate file."), std_to_wx(e.what()));
211 ScreenDialog::get_recipient_from_file ()
213 wxFileDialog* d = new wxFileDialog (this, _("Select Certificate File"));
214 if (d->ShowModal () == wxID_OK) {
215 load_recipient (boost::filesystem::path (wx_to_std (d->GetPath ())));
219 setup_sensitivity ();
223 ScreenDialog::download_recipient ()
225 DownloadCertificateDialog* d = new DownloadCertificateDialog (this);
226 if (d->ShowModal() == wxID_OK) {
227 set_recipient (d->certificate ());
230 setup_sensitivity ();
234 ScreenDialog::setup_sensitivity ()
236 wxButton* ok = dynamic_cast<wxButton*> (FindWindowById (wxID_OK, this));
238 ok->Enable (static_cast<bool>(_recipient) && !_name->GetValue().IsEmpty());
243 ScreenDialog::set_recipient (optional<dcp::Certificate> r)
248 _recipient_thumbprint->SetLabel (std_to_wx (_recipient->thumbprint ()));