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