b685f884aab55fe5f993715ed0cafd0529893f2e
[dcpomatic.git] / src / wx / recipient_dialog.cc
1 /*
2     Copyright (C) 2012-2020 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 "recipient_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/exceptions.h>
31 #include <dcp/certificate_chain.h>
32 #include <dcp/warnings.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::cout;
41 using std::list;
42 using std::string;
43 using std::vector;
44 using boost::bind;
45 using boost::optional;
46 #if BOOST_VERSION >= 106100
47 using namespace boost::placeholders;
48 #endif
49
50
51 static string
52 column (string s)
53 {
54         return s;
55 }
56
57
58 RecipientDialog::RecipientDialog (
59         wxWindow* parent, wxString title, string name, string notes, list<string> emails, int utc_offset_hour, int utc_offset_minute, optional<dcp::Certificate> recipient
60         )
61         : wxDialog (parent, wxID_ANY, title)
62         , _recipient (recipient)
63 {
64         auto overall_sizer = new wxBoxSizer (wxVERTICAL);
65         SetSizer (overall_sizer);
66
67         _sizer = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
68         int r = 0;
69
70         add_label_to_sizer (_sizer, this, _("Name"), true, wxGBPosition (r, 0));
71         _name = new wxTextCtrl (this, wxID_ANY, std_to_wx (name), wxDefaultPosition, wxSize (320, -1));
72         _sizer->Add (_name, wxGBPosition (r, 1));
73         ++r;
74
75         add_label_to_sizer (_sizer, this, _("Notes"), true, wxGBPosition (r, 0));
76         _notes = new wxTextCtrl (this, wxID_ANY, std_to_wx (notes), wxDefaultPosition, wxSize (320, -1));
77         _sizer->Add (_notes, wxGBPosition (r, 1));
78         ++r;
79
80         add_label_to_sizer (_sizer, this, _("UTC offset (time zone)"), true, wxGBPosition (r, 0));
81         _utc_offset = new wxChoice (this, wxID_ANY);
82         _sizer->Add (_utc_offset, wxGBPosition (r, 1));
83         ++r;
84
85         add_label_to_sizer (_sizer, this, _("Email addresses for KDM delivery"), false, wxGBPosition (r, 0), wxGBSpan (1, 2));
86         ++r;
87
88         copy (emails.begin(), emails.end(), back_inserter (_emails));
89
90         vector<EditableListColumn> columns;
91         columns.push_back (EditableListColumn(_("Address")));
92         _email_list = new EditableList<string, EmailDialog> (
93                 this, columns, bind(&RecipientDialog::get_emails, this), bind(&RecipientDialog::set_emails, this, _1), bind(&column, _1),
94                 EditableListTitle::VISIBLE,
95                 EditableListButton::NEW | EditableListButton::EDIT | EditableListButton::REMOVE
96                 );
97
98         _sizer->Add (_email_list, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND);
99         ++r;
100
101         wxClientDC dc (this);
102         auto font = _name->GetFont ();
103         font.SetFamily (wxFONTFAMILY_TELETYPE);
104         dc.SetFont (font);
105         auto size = dc.GetTextExtent(wxT("1234567890123456789012345678"));
106         size.SetHeight (-1);
107
108         add_label_to_sizer (_sizer, this, _("Recipient certificate"), true, wxGBPosition (r, 0));
109         auto s = new wxBoxSizer (wxHORIZONTAL);
110         _recipient_thumbprint = new StaticText (this, wxT (""), wxDefaultPosition, size);
111         _recipient_thumbprint->SetFont (font);
112         set_recipient (recipient);
113         _get_recipient_from_file = new Button (this, _("Get from file..."));
114         s->Add (_recipient_thumbprint, 1, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
115         s->Add (_get_recipient_from_file, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_X_GAP);
116         _sizer->Add (s, wxGBPosition (r, 1));
117         ++r;
118
119         add_label_to_sizer (_sizer, this, _("Other trusted devices"), true, wxGBPosition (r, 0));
120         ++r;
121
122         _name->Bind (wxEVT_TEXT, boost::bind (&RecipientDialog::setup_sensitivity, this));
123         _get_recipient_from_file->Bind (wxEVT_BUTTON, boost::bind (&RecipientDialog::get_recipient_from_file, this));
124
125         overall_sizer->Add (_sizer, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
126
127         auto buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
128         if (buttons) {
129                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
130         }
131
132         /* Default to UTC */
133         size_t sel = get_offsets (_offsets);
134         for (size_t i = 0; i < _offsets.size(); ++i) {
135                 _utc_offset->Append (_offsets[i].name);
136                 if (_offsets[i].hour == utc_offset_hour && _offsets[i].minute == utc_offset_minute) {
137                         sel = i;
138                 }
139         }
140
141         _utc_offset->SetSelection (sel);
142
143         overall_sizer->Layout ();
144         overall_sizer->SetSizeHints (this);
145
146         setup_sensitivity ();
147 }
148
149
150 string
151 RecipientDialog::name () const
152 {
153         return wx_to_std (_name->GetValue());
154 }
155
156
157 string
158 RecipientDialog::notes () const
159 {
160         return wx_to_std (_notes->GetValue());
161 }
162
163
164 optional<dcp::Certificate>
165 RecipientDialog::recipient () const
166 {
167         return _recipient;
168 }
169
170
171 void
172 RecipientDialog::load_recipient (boost::filesystem::path file)
173 {
174         try {
175                 /* Load this as a chain, in case it is one, and then pick the leaf certificate */
176                 dcp::CertificateChain c (dcp::file_to_string (file));
177                 if (c.unordered().empty()) {
178                         error_dialog (this, _("Could not read certificate file."));
179                         return;
180                 }
181                 set_recipient (c.leaf ());
182         } catch (dcp::MiscError& e) {
183                 error_dialog (this, _("Could not read certificate file."), std_to_wx(e.what()));
184         }
185 }
186
187
188 void
189 RecipientDialog::get_recipient_from_file ()
190 {
191         auto d = make_wx<wxFileDialog>(this, _("Select Certificate File"));
192         if (d->ShowModal () == wxID_OK) {
193                 load_recipient (boost::filesystem::path (wx_to_std (d->GetPath ())));
194         }
195
196         setup_sensitivity ();
197 }
198
199
200 void
201 RecipientDialog::setup_sensitivity ()
202 {
203         auto ok = dynamic_cast<wxButton*> (FindWindowById (wxID_OK, this));
204         if (ok) {
205                 ok->Enable (static_cast<bool>(_recipient) && !_name->GetValue().IsEmpty());
206         }
207 }
208
209
210 void
211 RecipientDialog::set_recipient (optional<dcp::Certificate> r)
212 {
213         _recipient = r;
214
215         if (_recipient) {
216                 _recipient_thumbprint->SetLabel (std_to_wx (_recipient->thumbprint ()));
217                 _sizer->Layout ();
218         }
219 }
220
221
222 vector<string>
223 RecipientDialog::get_emails () const
224 {
225         return _emails;
226 }
227
228
229 void
230 RecipientDialog::set_emails (vector<string> e)
231 {
232         _emails = e;
233 }
234
235
236 list<string>
237 RecipientDialog::emails () const
238 {
239         list<string> e;
240         copy (_emails.begin(), _emails.end(), back_inserter(e));
241         return e;
242 }
243
244
245 int
246 RecipientDialog::utc_offset_hour () const
247 {
248         int const sel = _utc_offset->GetSelection();
249         if (sel < 0 || sel > int (_offsets.size())) {
250                 return 0;
251         }
252
253         return _offsets[sel].hour;
254 }
255
256 int
257 RecipientDialog::utc_offset_minute () const
258 {
259         int const sel = _utc_offset->GetSelection();
260         if (sel < 0 || sel > int (_offsets.size())) {
261                 return 0;
262         }
263
264         return _offsets[sel].minute;
265 }
266
267