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