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