Add subject/issuer details to the screen dialog (#2422).
[dcpomatic.git] / src / wx / screen_dialog.cc
1 /*
2     Copyright (C) 2012-2022 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
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"
28 #include "wx_ptr.h"
29 #include "wx_util.h"
30 #include "lib/compose.hpp"
31 #include "lib/scope_guard.h"
32 #include "lib/util.h"
33 #include <dcp/warnings.h>
34 #include <dcp/exceptions.h>
35 #include <dcp/certificate_chain.h>
36 LIBDCP_DISABLE_WARNINGS
37 #include <wx/filepicker.h>
38 #include <wx/validate.h>
39 LIBDCP_ENABLE_WARNINGS
40
41
42 using std::string;
43 using std::vector;
44 using boost::bind;
45 using boost::optional;
46 #if BOOST_VERSION >= 106100
47 using namespace boost::placeholders;
48 #endif
49
50
51 class TrustedDeviceDialog : public TableDialog
52 {
53 public:
54         explicit TrustedDeviceDialog (wxWindow* parent)
55                 : TableDialog (parent, _("Trusted Device"), 3, 1, true)
56         {
57                 add (_("Thumbprint"), true);
58                 _thumbprint = add (new wxTextCtrl(this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(300, -1)));
59                 _file = add (new Button(this, _("Load certificate...")));
60
61                 layout ();
62
63                 _file->Bind (wxEVT_BUTTON, bind(&TrustedDeviceDialog::load_certificate, this));
64         }
65
66         void load_certificate ()
67         {
68                 auto dialog = make_wx<FileDialog>(this, _("Trusted Device certificate"), wxEmptyString, wxFD_DEFAULT_STYLE, "SelectCertificatePath");
69                 if (!dialog->show()) {
70                         return;
71                 }
72
73                 try {
74                         _certificate = dcp::Certificate(dcp::file_to_string(dialog->paths()[0]));
75                         _thumbprint->SetValue (std_to_wx(_certificate->thumbprint()));
76                 } catch (dcp::MiscError& e) {
77                         error_dialog(this, wxString::Format(_("Could not load certificate (%s)"), std_to_wx(e.what())));
78                 }
79         }
80
81         void set (TrustedDevice t)
82         {
83                 _certificate = t.certificate ();
84                 _thumbprint->SetValue (std_to_wx(t.thumbprint()));
85         }
86
87         optional<TrustedDevice> get ()
88         {
89                 auto const t = wx_to_std (_thumbprint->GetValue());
90                 if (_certificate && _certificate->thumbprint() == t) {
91                         return TrustedDevice (*_certificate);
92                 } else if (t.length() == 28) {
93                         return TrustedDevice (t);
94                 }
95
96                 return {};
97         }
98
99 private:
100         wxTextCtrl* _thumbprint;
101         wxButton* _file;
102         boost::optional<dcp::Certificate> _certificate;
103 };
104
105
106 ScreenDialog::ScreenDialog (
107         wxWindow* parent,
108         wxString title,
109         string name,
110         string notes,
111         optional<dcp::Certificate> recipient,
112         optional<string> recipient_file,
113         vector<TrustedDevice> trusted_devices
114         )
115         : wxDialog (parent, wxID_ANY, title)
116         , _recipient (recipient)
117         , _trusted_devices (trusted_devices)
118 {
119         auto overall_sizer = new wxBoxSizer (wxVERTICAL);
120         SetSizer (overall_sizer);
121
122         _sizer = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
123         int r = 0;
124
125         add_label_to_sizer (_sizer, this, _("Name"), true, wxGBPosition(r, 0));
126         _name = new wxTextCtrl (this, wxID_ANY, std_to_wx (name), wxDefaultPosition, wxSize (320, -1));
127         _sizer->Add (_name, wxGBPosition (r, 1));
128         ++r;
129
130         add_label_to_sizer (_sizer, this, _("Notes"), true, wxGBPosition(r, 0));
131         _notes = new wxTextCtrl (this, wxID_ANY, std_to_wx(notes), wxDefaultPosition, wxSize(320, -1));
132         _sizer->Add (_notes, wxGBPosition(r, 1));
133         ++r;
134
135         wxClientDC dc (this);
136         wxFont font = _name->GetFont ();
137         font.SetFamily (wxFONTFAMILY_TELETYPE);
138         dc.SetFont (font);
139         wxSize size = dc.GetTextExtent (wxT("1234567890123456789012345678"));
140         size.SetHeight (-1);
141
142         add_label_to_sizer (_sizer, this, _("Recipient certificate"), true, wxGBPosition(r, 0));
143         auto s = new wxBoxSizer (wxHORIZONTAL);
144         _recipient_thumbprint = new StaticText (this, wxT (""), wxDefaultPosition, size);
145         _recipient_thumbprint->SetFont (font);
146
147         _get_recipient_from_file = new Button (this, _("Get from file..."));
148         _download_recipient = new Button (this, _("Download..."));
149         s->Add (_recipient_thumbprint, 1, wxRIGHT | wxALIGN_CENTER_VERTICAL | wxALIGN_LEFT, DCPOMATIC_SIZER_X_GAP);
150         s->Add (_get_recipient_from_file, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_X_GAP);
151         s->Add (_download_recipient, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_X_GAP);
152         _sizer->Add (s, wxGBPosition (r, 1));
153         ++r;
154
155         add_label_to_sizer (_sizer, this, _("Filename"), true, wxGBPosition(r, 0));
156         _recipient_file = new wxStaticText(this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(600, -1), wxST_ELLIPSIZE_MIDDLE | wxST_NO_AUTORESIZE);
157         set_recipient_file(recipient_file.get_value_or(""));
158         _sizer->Add (_recipient_file, wxGBPosition(r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_Y_GAP);
159         ++r;
160
161         add_label_to_sizer(_sizer, this, _("Subject common name"), true, wxGBPosition(r, 0));
162         _subject_common_name = new wxStaticText(this, wxID_ANY, wxT(""));
163         _sizer->Add(_subject_common_name, wxGBPosition(r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_Y_GAP);
164         ++r;
165
166         add_label_to_sizer(_sizer, this, _("Subject organization name"), true, wxGBPosition(r, 0));
167         _subject_organization_name = new wxStaticText(this, wxID_ANY, wxT(""));
168         _sizer->Add(_subject_organization_name, wxGBPosition(r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_Y_GAP);
169         ++r;
170
171         add_label_to_sizer(_sizer, this, _("Issuer common name"), true, wxGBPosition(r, 0));
172         _issuer_common_name = new wxStaticText(this, wxID_ANY, wxT(""));
173         _sizer->Add(_issuer_common_name, wxGBPosition(r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_Y_GAP);
174         ++r;
175
176         add_label_to_sizer(_sizer, this, _("Issuer organization name"), true, wxGBPosition(r, 0));
177         _issuer_organization_name = new wxStaticText(this, wxID_ANY, wxT(""));
178         _sizer->Add(_issuer_organization_name, wxGBPosition(r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_Y_GAP);
179         ++r;
180
181         set_recipient (recipient);
182
183         {
184                 int flags = wxALIGN_CENTER_VERTICAL | wxTOP;
185 #ifdef __WXOSX__
186                 flags |= wxALIGN_RIGHT;
187                 auto m = new StaticText (this, _("Other trusted devices") + wxT(":"));
188 #else
189                 auto m = new StaticText (this, _("Other trusted devices"));
190 #endif
191                 _sizer->Add (m, wxGBPosition(r, 0), wxDefaultSpan, flags, DCPOMATIC_SIZER_Y_GAP);
192         }
193         ++r;
194
195         vector<EditableListColumn> columns;
196         columns.push_back (EditableListColumn(_("Thumbprint")));
197         _trusted_device_list = new EditableList<TrustedDevice, TrustedDeviceDialog> (
198                 this,
199                 columns,
200                 bind (&ScreenDialog::trusted_devices, this),
201                 bind (&ScreenDialog::set_trusted_devices, this, _1),
202                 [] (TrustedDevice const& d, int) {
203                         return d.thumbprint();
204                 },
205                 EditableListTitle::INVISIBLE,
206                 EditableListButton::NEW | EditableListButton::EDIT | EditableListButton::REMOVE
207                 );
208
209         _sizer->Add (_trusted_device_list, wxGBPosition (r, 0), wxGBSpan (1, 3), wxEXPAND);
210         ++r;
211
212         _name->Bind (wxEVT_TEXT, boost::bind (&ScreenDialog::setup_sensitivity, this));
213         _get_recipient_from_file->Bind (wxEVT_BUTTON, boost::bind (&ScreenDialog::get_recipient_from_file, this));
214         _download_recipient->Bind (wxEVT_BUTTON, boost::bind (&ScreenDialog::download_recipient, this));
215
216         overall_sizer->Add (_sizer, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
217
218         auto buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
219         if (buttons) {
220                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
221         }
222
223         overall_sizer->Layout ();
224         overall_sizer->SetSizeHints (this);
225
226         setup_sensitivity ();
227 }
228
229
230 string
231 ScreenDialog::name () const
232 {
233         return wx_to_std (_name->GetValue());
234 }
235
236
237 string
238 ScreenDialog::notes () const
239 {
240         return wx_to_std (_notes->GetValue());
241 }
242
243
244 optional<dcp::Certificate>
245 ScreenDialog::recipient () const
246 {
247         return _recipient;
248 }
249
250
251 optional<string>
252 ScreenDialog::recipient_file () const
253 {
254         auto const f = wx_to_std(_recipient_file->GetLabel());
255         if (f.empty()) {
256                 return {};
257         }
258         return f;
259 }
260
261
262 void
263 ScreenDialog::load_recipient (boost::filesystem::path file)
264 {
265         try {
266                 /* Load this as a chain, in case it is one, and then pick the leaf certificate */
267                 dcp::CertificateChain c (dcp::file_to_string(file));
268                 if (c.unordered().empty()) {
269                         error_dialog (this, _("Could not read certificate file."));
270                         return;
271                 }
272                 set_recipient (c.leaf ());
273                 set_recipient_file(file.string());
274         } catch (dcp::MiscError& e) {
275                 error_dialog (this, _("Could not read certificate file."), std_to_wx(e.what()));
276         }
277 }
278
279
280 void
281 ScreenDialog::get_recipient_from_file ()
282 {
283         auto dialog = make_wx<FileDialog>(this, _("Select Certificate File"), wxEmptyString, wxFD_DEFAULT_STYLE , "SelectCertificatePath");
284         if (dialog->show()) {
285                 load_recipient(dialog->paths()[0]);
286         }
287
288         setup_sensitivity ();
289 }
290
291
292 void
293 ScreenDialog::download_recipient ()
294 {
295         auto dialog = make_wx<DownloadCertificateDialog>(this);
296         if (dialog->ShowModal() == wxID_OK) {
297                 set_recipient(dialog->certificate());
298                 set_recipient_file(dialog->url());
299         }
300         setup_sensitivity ();
301 }
302
303
304 void
305 ScreenDialog::setup_sensitivity ()
306 {
307         auto ok = dynamic_cast<wxButton*> (FindWindowById(wxID_OK, this));
308         if (ok) {
309                 ok->Enable (static_cast<bool>(_recipient) && !_name->GetValue().IsEmpty());
310         }
311 }
312
313
314 void
315 ScreenDialog::set_recipient (optional<dcp::Certificate> r)
316 {
317         _recipient = r;
318
319         if (_recipient) {
320                 _recipient_thumbprint->SetLabel (std_to_wx (_recipient->thumbprint ()));
321                 _subject_common_name->SetLabel(std_to_wx(_recipient->subject_common_name()));
322                 _subject_organization_name->SetLabel(std_to_wx(_recipient->subject_organization_name()));
323                 _issuer_common_name->SetLabel(std_to_wx(_recipient->issuer_common_name()));
324                 _issuer_organization_name->SetLabel(std_to_wx(_recipient->issuer_organization_name()));
325                 _sizer->Layout ();
326         }
327 }
328
329
330 void
331 ScreenDialog::set_recipient_file(string file)
332 {
333         checked_set(_recipient_file, file);
334         _recipient_file->SetToolTip(std_to_wx(file));
335 }
336