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