38e6a85bfff392c885a6c9d0869cfe664480484a
[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 "screen_dialog.h"
25 #include "static_text.h"
26 #include "table_dialog.h"
27 #include "wx_util.h"
28 #include "lib/compose.hpp"
29 #include "lib/util.h"
30 #include <dcp/warnings.h>
31 #include <dcp/exceptions.h>
32 #include <dcp/certificate_chain.h>
33 LIBDCP_DISABLE_WARNINGS
34 #include <wx/filepicker.h>
35 #include <wx/validate.h>
36 LIBDCP_ENABLE_WARNINGS
37 #include <iostream>
38
39
40 using std::string;
41 using std::cout;
42 using std::vector;
43 using boost::optional;
44 using boost::bind;
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                 auto d = new wxFileDialog (this, _("Trusted Device certificate"));
68                 if (d->ShowModal() == wxID_OK) {
69                         try {
70                                 _certificate = dcp::Certificate(dcp::file_to_string(wx_to_std(d->GetPath())));
71                                 _thumbprint->SetValue (std_to_wx(_certificate->thumbprint()));
72                         } catch (dcp::MiscError& e) {
73                                 error_dialog (this, wxString::Format(_("Could not load certficate (%s)"), std_to_wx(e.what())));
74                         }
75                 }
76         }
77
78         void set (TrustedDevice t)
79         {
80                 _certificate = t.certificate ();
81                 _thumbprint->SetValue (std_to_wx(t.thumbprint()));
82         }
83
84         optional<TrustedDevice> get ()
85         {
86                 auto const t = wx_to_std (_thumbprint->GetValue());
87                 if (_certificate && _certificate->thumbprint() == t) {
88                         return TrustedDevice (*_certificate);
89                 } else if (t.length() == 28) {
90                         return TrustedDevice (t);
91                 }
92
93                 return {};
94         }
95
96 private:
97         wxTextCtrl* _thumbprint;
98         wxButton* _file;
99         boost::optional<dcp::Certificate> _certificate;
100 };
101
102
103 ScreenDialog::ScreenDialog (
104         wxWindow* parent,
105         wxString title,
106         string name,
107         string notes,
108         optional<dcp::Certificate> recipient,
109         optional<string> recipient_file,
110         vector<TrustedDevice> trusted_devices
111         )
112         : wxDialog (parent, wxID_ANY, title)
113         , _recipient (recipient)
114         , _trusted_devices (trusted_devices)
115 {
116         auto overall_sizer = new wxBoxSizer (wxVERTICAL);
117         SetSizer (overall_sizer);
118
119         _sizer = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
120         int r = 0;
121
122         add_label_to_sizer (_sizer, this, _("Name"), true, wxGBPosition(r, 0));
123         _name = new wxTextCtrl (this, wxID_ANY, std_to_wx (name), wxDefaultPosition, wxSize (320, -1));
124         _sizer->Add (_name, wxGBPosition (r, 1));
125         ++r;
126
127         add_label_to_sizer (_sizer, this, _("Notes"), true, wxGBPosition(r, 0));
128         _notes = new wxTextCtrl (this, wxID_ANY, std_to_wx(notes), wxDefaultPosition, wxSize(320, -1));
129         _sizer->Add (_notes, wxGBPosition(r, 1));
130         ++r;
131
132         wxClientDC dc (this);
133         wxFont font = _name->GetFont ();
134         font.SetFamily (wxFONTFAMILY_TELETYPE);
135         dc.SetFont (font);
136         wxSize size = dc.GetTextExtent (wxT("1234567890123456789012345678"));
137         size.SetHeight (-1);
138
139         add_label_to_sizer (_sizer, this, _("Recipient certificate"), true, wxGBPosition(r, 0));
140         auto s = new wxBoxSizer (wxHORIZONTAL);
141         _recipient_thumbprint = new StaticText (this, wxT (""), wxDefaultPosition, size);
142         _recipient_thumbprint->SetFont (font);
143         set_recipient (recipient);
144
145         _get_recipient_from_file = new Button (this, _("Get from file..."));
146         _download_recipient = new Button (this, _("Download..."));
147         s->Add (_recipient_thumbprint, 1, wxRIGHT | wxALIGN_CENTER_VERTICAL | wxALIGN_LEFT, DCPOMATIC_SIZER_X_GAP);
148         s->Add (_get_recipient_from_file, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_X_GAP);
149         s->Add (_download_recipient, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_X_GAP);
150         _sizer->Add (s, wxGBPosition (r, 1));
151         ++r;
152
153         add_label_to_sizer (_sizer, this, _("Filename"), true, wxGBPosition(r, 0));
154         _recipient_file = new wxStaticText (this, wxID_ANY, wxT(""));
155         checked_set (_recipient_file, recipient_file.get_value_or(""));
156         _sizer->Add (_recipient_file, wxGBPosition(r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_Y_GAP);
157         ++r;
158
159         {
160                 int flags = wxALIGN_CENTER_VERTICAL | wxTOP;
161 #ifdef __WXOSX__
162                 flags |= wxALIGN_RIGHT;
163                 auto m = new StaticText (this, _("Other trusted devices") + wxT(":"));
164 #else
165                 auto m = new StaticText (this, _("Other trusted devices"));
166 #endif
167                 _sizer->Add (m, wxGBPosition(r, 0), wxDefaultSpan, flags, DCPOMATIC_SIZER_Y_GAP);
168         }
169         ++r;
170
171         vector<EditableListColumn> columns;
172         columns.push_back (EditableListColumn(_("Thumbprint")));
173         _trusted_device_list = new EditableList<TrustedDevice, TrustedDeviceDialog> (
174                 this,
175                 columns,
176                 bind (&ScreenDialog::trusted_devices, this),
177                 bind (&ScreenDialog::set_trusted_devices, this, _1),
178                 [] (TrustedDevice const& d, int) {
179                         return d.thumbprint();
180                 },
181                 EditableListTitle::INVISIBLE,
182                 EditableListButton::NEW | EditableListButton::EDIT | EditableListButton::REMOVE
183                 );
184
185         _sizer->Add (_trusted_device_list, wxGBPosition (r, 0), wxGBSpan (1, 3), wxEXPAND);
186         ++r;
187
188         _name->Bind (wxEVT_TEXT, boost::bind (&ScreenDialog::setup_sensitivity, this));
189         _get_recipient_from_file->Bind (wxEVT_BUTTON, boost::bind (&ScreenDialog::get_recipient_from_file, this));
190         _download_recipient->Bind (wxEVT_BUTTON, boost::bind (&ScreenDialog::download_recipient, this));
191
192         overall_sizer->Add (_sizer, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
193
194         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
195         if (buttons) {
196                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
197         }
198
199         overall_sizer->Layout ();
200         overall_sizer->SetSizeHints (this);
201
202         setup_sensitivity ();
203 }
204
205
206 string
207 ScreenDialog::name () const
208 {
209         return wx_to_std (_name->GetValue());
210 }
211
212
213 string
214 ScreenDialog::notes () const
215 {
216         return wx_to_std (_notes->GetValue());
217 }
218
219
220 optional<dcp::Certificate>
221 ScreenDialog::recipient () const
222 {
223         return _recipient;
224 }
225
226
227 optional<string>
228 ScreenDialog::recipient_file () const
229 {
230         auto const f = wx_to_std(_recipient_file->GetLabel());
231         if (f.empty()) {
232                 return {};
233         }
234         return f;
235 }
236
237
238 void
239 ScreenDialog::load_recipient (boost::filesystem::path file)
240 {
241         try {
242                 /* Load this as a chain, in case it is one, and then pick the leaf certificate */
243                 dcp::CertificateChain c (dcp::file_to_string(file));
244                 if (c.unordered().empty()) {
245                         error_dialog (this, _("Could not read certificate file."));
246                         return;
247                 }
248                 set_recipient (c.leaf ());
249                 checked_set (_recipient_file, file.string());
250         } catch (dcp::MiscError& e) {
251                 error_dialog (this, _("Could not read certificate file."), std_to_wx(e.what()));
252         }
253 }
254
255
256 void
257 ScreenDialog::get_recipient_from_file ()
258 {
259         auto d = new wxFileDialog (this, _("Select Certificate File"));
260         if (d->ShowModal() == wxID_OK) {
261                 load_recipient (boost::filesystem::path(wx_to_std(d->GetPath())));
262         }
263         d->Destroy ();
264
265         setup_sensitivity ();
266 }
267
268
269 void
270 ScreenDialog::download_recipient ()
271 {
272         auto d = new DownloadCertificateDialog (this);
273         if (d->ShowModal() == wxID_OK) {
274                 set_recipient (d->certificate());
275                 checked_set (_recipient_file, d->url());
276         }
277         d->Destroy ();
278         setup_sensitivity ();
279 }
280
281
282 void
283 ScreenDialog::setup_sensitivity ()
284 {
285         auto ok = dynamic_cast<wxButton*> (FindWindowById(wxID_OK, this));
286         if (ok) {
287                 ok->Enable (static_cast<bool>(_recipient) && !_name->GetValue().IsEmpty());
288         }
289 }
290
291
292 void
293 ScreenDialog::set_recipient (optional<dcp::Certificate> r)
294 {
295         _recipient = r;
296
297         if (_recipient) {
298                 _recipient_thumbprint->SetLabel (std_to_wx (_recipient->thumbprint ()));
299                 _sizer->Layout ();
300         }
301 }