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