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