Ignore empty KDM email addresses (#818).
[dcpomatic.git] / src / wx / screen_dialog.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "screen_dialog.h"
21 #include "wx_util.h"
22 #include "file_dialog_wrapper.h"
23 #include "download_certificate_dialog.h"
24 #include "lib/compose.hpp"
25 #include "lib/util.h"
26 #include <dcp/exceptions.h>
27 #include <wx/filepicker.h>
28 #include <wx/validate.h>
29 #include <iostream>
30
31 using std::string;
32 using std::cout;
33 using std::vector;
34 using boost::optional;
35 using boost::bind;
36
37 static string
38 column (dcp::Certificate c)
39 {
40         return c.thumbprint ();
41 }
42
43 class CertificateFileDialogWrapper : public FileDialogWrapper<dcp::Certificate>
44 {
45 public:
46         CertificateFileDialogWrapper (wxWindow* parent)
47                 : FileDialogWrapper<dcp::Certificate> (parent, _("Select certificate file"))
48         {
49
50         }
51 };
52
53 ScreenDialog::ScreenDialog (wxWindow* parent, string title, string name, optional<dcp::Certificate> recipient, vector<dcp::Certificate> trusted_devices)
54         : wxDialog (parent, wxID_ANY, std_to_wx (title))
55         , _recipient (recipient)
56         , _trusted_devices (trusted_devices)
57 {
58         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
59         SetSizer (overall_sizer);
60
61         _sizer = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
62         int r = 0;
63
64         add_label_to_sizer (_sizer, this, _("Name"), true, wxGBPosition (r, 0));
65         _name = new wxTextCtrl (this, wxID_ANY, std_to_wx (name), wxDefaultPosition, wxSize (320, -1));
66         _sizer->Add (_name, wxGBPosition (r, 1));
67         ++r;
68
69         wxClientDC dc (this);
70         wxFont font = _name->GetFont ();
71         font.SetFamily (wxFONTFAMILY_TELETYPE);
72         dc.SetFont (font);
73         wxSize size = dc.GetTextExtent (wxT ("1234567890123456789012345678"));
74         size.SetHeight (-1);
75
76         add_label_to_sizer (_sizer, this, _("Recipient certificate"), true, wxGBPosition (r, 0));
77         wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
78         _recipient_thumbprint = new wxStaticText (this, wxID_ANY, wxT (""), wxDefaultPosition, size);
79         _recipient_thumbprint->SetFont (font);
80         set_recipient (recipient);
81         _get_recipient_from_file = new wxButton (this, wxID_ANY, _("Get from file..."));
82         _download_recipient = new wxButton (this, wxID_ANY, _("Download..."));
83         s->Add (_recipient_thumbprint, 1, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
84         s->Add (_get_recipient_from_file, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_X_GAP);
85         s->Add (_download_recipient, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_X_GAP);
86         _sizer->Add (s, wxGBPosition (r, 1));
87         ++r;
88
89         add_label_to_sizer (_sizer, this, _("Other trusted devices"), true, wxGBPosition (r, 0));
90         ++r;
91
92         vector<string> columns;
93         columns.push_back (wx_to_std (_("Thumbprint")));
94         _trusted_device_list = new EditableList<dcp::Certificate, CertificateFileDialogWrapper> (
95                 this,
96                 columns,
97                 bind (&ScreenDialog::trusted_devices, this),
98                 bind (&ScreenDialog::set_trusted_devices, this, _1),
99                 bind (&always_valid),
100                 bind (&column, _1),
101                 false
102                 );
103
104         _sizer->Add (_trusted_device_list, wxGBPosition (r, 0), wxGBSpan (1, 3), wxEXPAND);
105         ++r;
106
107         _name->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ScreenDialog::setup_sensitivity, this));
108         _get_recipient_from_file->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreenDialog::get_recipient_from_file, this));
109         _download_recipient->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreenDialog::download_recipient, this));
110
111         overall_sizer->Add (_sizer, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
112
113         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
114         if (buttons) {
115                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
116         }
117
118         overall_sizer->Layout ();
119         overall_sizer->SetSizeHints (this);
120
121         setup_sensitivity ();
122 }
123
124 string
125 ScreenDialog::name () const
126 {
127         return wx_to_std (_name->GetValue());
128 }
129
130 optional<dcp::Certificate>
131 ScreenDialog::recipient () const
132 {
133         return _recipient;
134 }
135
136 void
137 ScreenDialog::load_recipient (boost::filesystem::path file)
138 {
139         try {
140                 set_recipient (dcp::Certificate (dcp::file_to_string (file)));
141         } catch (dcp::MiscError& e) {
142                 error_dialog (this, wxString::Format (_("Could not read certificate file (%s)"), std_to_wx(e.what()).data()));
143         }
144 }
145
146 void
147 ScreenDialog::get_recipient_from_file ()
148 {
149         wxFileDialog* d = new wxFileDialog (this, _("Select Certificate File"));
150         if (d->ShowModal () == wxID_OK) {
151                 load_recipient (boost::filesystem::path (wx_to_std (d->GetPath ())));
152         }
153         d->Destroy ();
154
155         setup_sensitivity ();
156 }
157
158 void
159 ScreenDialog::download_recipient ()
160 {
161         DownloadCertificateDialog* d = new DownloadCertificateDialog (this);
162         if (d->ShowModal() == wxID_OK) {
163                 set_recipient (d->certificate ());
164         }
165         d->Destroy ();
166         setup_sensitivity ();
167 }
168
169 void
170 ScreenDialog::setup_sensitivity ()
171 {
172         wxButton* ok = dynamic_cast<wxButton*> (FindWindowById (wxID_OK, this));
173         if (ok) {
174                 ok->Enable (static_cast<bool>(_recipient) && !_name->GetValue().IsEmpty());
175         }
176 }
177
178 void
179 ScreenDialog::set_recipient (optional<dcp::Certificate> r)
180 {
181         _recipient = r;
182
183         if (_recipient) {
184                 _recipient_thumbprint->SetLabel (std_to_wx (_recipient->thumbprint ()));
185                 _sizer->Layout ();
186         }
187 }