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