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