bc7065f294ede1d823e749bdfba09c0e84dd5abe
[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
45 static string
46 column (TrustedDevice d)
47 {
48         return d.thumbprint ();
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                 wxFileDialog* d = new wxFileDialog (this, _("Trusted Device certificate"));
69                 d->ShowModal ();
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         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                 string 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 optional<TrustedDevice> ();
94         }
95
96 private:
97         wxTextCtrl* _thumbprint;
98         wxButton* _file;
99         boost::optional<dcp::Certificate> _certificate;
100 };
101
102 ScreenDialog::ScreenDialog (
103         wxWindow* parent, wxString title, string name, string notes, optional<dcp::Certificate> recipient, vector<TrustedDevice> trusted_devices
104         )
105         : wxDialog (parent, wxID_ANY, title)
106         , _recipient (recipient)
107         , _trusted_devices (trusted_devices)
108 {
109         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
110         SetSizer (overall_sizer);
111
112         _sizer = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
113         int r = 0;
114
115         add_label_to_sizer (_sizer, this, _("Name"), true, wxGBPosition (r, 0));
116         _name = new wxTextCtrl (this, wxID_ANY, std_to_wx (name), wxDefaultPosition, wxSize (320, -1));
117         _sizer->Add (_name, wxGBPosition (r, 1));
118         ++r;
119
120         add_label_to_sizer (_sizer, this, _("Notes"), true, wxGBPosition (r, 0));
121         _notes = new wxTextCtrl (this, wxID_ANY, std_to_wx (notes), wxDefaultPosition, wxSize (320, -1));
122         _sizer->Add (_notes, wxGBPosition (r, 1));
123         ++r;
124
125         wxClientDC dc (this);
126         wxFont font = _name->GetFont ();
127         font.SetFamily (wxFONTFAMILY_TELETYPE);
128         dc.SetFont (font);
129         wxSize size = dc.GetTextExtent (wxT ("1234567890123456789012345678"));
130         size.SetHeight (-1);
131
132         add_label_to_sizer (_sizer, this, _("Recipient certificate"), true, wxGBPosition (r, 0));
133         wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
134         _recipient_thumbprint = new StaticText (this, wxT (""), wxDefaultPosition, size);
135         _recipient_thumbprint->SetFont (font);
136         set_recipient (recipient);
137         _get_recipient_from_file = new Button (this, _("Get from file..."));
138         _download_recipient = new Button (this, _("Download..."));
139         s->Add (_recipient_thumbprint, 1, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
140         s->Add (_get_recipient_from_file, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_X_GAP);
141         s->Add (_download_recipient, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_X_GAP);
142         _sizer->Add (s, wxGBPosition (r, 1));
143         ++r;
144
145         add_label_to_sizer (_sizer, this, _("Other trusted devices"), true, wxGBPosition (r, 0));
146         ++r;
147
148         vector<EditableListColumn> columns;
149         columns.push_back (EditableListColumn(wx_to_std(_("Thumbprint"))));
150         _trusted_device_list = new EditableList<TrustedDevice, TrustedDeviceDialog> (
151                 this,
152                 columns,
153                 bind (&ScreenDialog::trusted_devices, this),
154                 bind (&ScreenDialog::set_trusted_devices, this, _1),
155                 bind (&column, _1),
156                 false
157                 );
158
159         _sizer->Add (_trusted_device_list, wxGBPosition (r, 0), wxGBSpan (1, 3), wxEXPAND);
160         ++r;
161
162         _name->Bind (wxEVT_TEXT, boost::bind (&ScreenDialog::setup_sensitivity, this));
163         _get_recipient_from_file->Bind (wxEVT_BUTTON, boost::bind (&ScreenDialog::get_recipient_from_file, this));
164         _download_recipient->Bind (wxEVT_BUTTON, boost::bind (&ScreenDialog::download_recipient, this));
165
166         overall_sizer->Add (_sizer, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
167
168         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
169         if (buttons) {
170                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
171         }
172
173         overall_sizer->Layout ();
174         overall_sizer->SetSizeHints (this);
175
176         setup_sensitivity ();
177 }
178
179 string
180 ScreenDialog::name () const
181 {
182         return wx_to_std (_name->GetValue());
183 }
184
185 string
186 ScreenDialog::notes () const
187 {
188         return wx_to_std (_notes->GetValue());
189 }
190
191 optional<dcp::Certificate>
192 ScreenDialog::recipient () const
193 {
194         return _recipient;
195 }
196
197 void
198 ScreenDialog::load_recipient (boost::filesystem::path file)
199 {
200         try {
201                 /* Load this as a chain, in case it is one, and then pick the leaf certificate */
202                 dcp::CertificateChain c (dcp::file_to_string (file));
203                 if (c.unordered().empty()) {
204                         error_dialog (this, _("Could not read certificate file."));
205                         return;
206                 }
207                 set_recipient (c.leaf ());
208         } catch (dcp::MiscError& e) {
209                 error_dialog (this, _("Could not read certificate file."), std_to_wx(e.what()));
210         }
211 }
212
213 void
214 ScreenDialog::get_recipient_from_file ()
215 {
216         wxFileDialog* d = new wxFileDialog (this, _("Select Certificate File"));
217         if (d->ShowModal () == wxID_OK) {
218                 load_recipient (boost::filesystem::path (wx_to_std (d->GetPath ())));
219         }
220         d->Destroy ();
221
222         setup_sensitivity ();
223 }
224
225 void
226 ScreenDialog::download_recipient ()
227 {
228         DownloadCertificateDialog* d = new DownloadCertificateDialog (this);
229         if (d->ShowModal() == wxID_OK) {
230                 set_recipient (d->certificate ());
231         }
232         d->Destroy ();
233         setup_sensitivity ();
234 }
235
236 void
237 ScreenDialog::setup_sensitivity ()
238 {
239         wxButton* ok = dynamic_cast<wxButton*> (FindWindowById (wxID_OK, this));
240         if (ok) {
241                 ok->Enable (static_cast<bool>(_recipient) && !_name->GetValue().IsEmpty());
242         }
243 }
244
245 void
246 ScreenDialog::set_recipient (optional<dcp::Certificate> r)
247 {
248         _recipient = r;
249
250         if (_recipient) {
251                 _recipient_thumbprint->SetLabel (std_to_wx (_recipient->thumbprint ()));
252                 _sizer->Layout ();
253         }
254 }