Fix sensitivity of OK in the screen dialog.
[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 "download_certificate_dialog.h"
23 #include "lib/compose.hpp"
24 #include "lib/util.h"
25 #include <dcp/exceptions.h>
26 #include <wx/filepicker.h>
27 #include <wx/validate.h>
28 #include <iostream>
29
30 using std::string;
31 using std::cout;
32 using std::vector;
33 using boost::optional;
34 using boost::bind;
35
36 class FileDialogWrapper
37 {
38 public:
39         FileDialogWrapper (wxWindow* parent)
40                 : _parent (parent)
41         {
42                 _dialog = new wxFileDialog (parent, _("Select certificate file"));
43         }
44
45         void set (dcp::Certificate) {}
46
47         dcp::Certificate get () {
48                 return dcp::Certificate (dcp::file_to_string (wx_to_std (_dialog->GetPath ())));
49         }
50
51         int ShowModal ()
52         {
53                 return _dialog->ShowModal ();
54         }
55
56         void Destroy ()
57         {
58                 _dialog->Destroy ();
59                 /* eek! */
60                 delete this;
61         }
62
63 private:
64         wxWindow* _parent;
65         wxFileDialog* _dialog;
66 };
67
68 static string
69 column (dcp::Certificate c)
70 {
71         return c.thumbprint ();
72 }
73
74 ScreenDialog::ScreenDialog (wxWindow* parent, string title, string name, optional<dcp::Certificate> recipient, vector<dcp::Certificate> trusted_devices)
75         : wxDialog (parent, wxID_ANY, std_to_wx (title))
76         , _recipient (recipient)
77         , _trusted_devices (trusted_devices)
78 {
79         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
80         SetSizer (overall_sizer);
81
82         _sizer = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
83         int r = 0;
84
85         add_label_to_sizer (_sizer, this, _("Name"), true, wxGBPosition (r, 0));
86         _name = new wxTextCtrl (this, wxID_ANY, std_to_wx (name), wxDefaultPosition, wxSize (320, -1));
87         _sizer->Add (_name, wxGBPosition (r, 1));
88         ++r;
89
90         wxClientDC dc (this);
91         wxFont font = _name->GetFont ();
92         font.SetFamily (wxFONTFAMILY_TELETYPE);
93         dc.SetFont (font);
94         wxSize size = dc.GetTextExtent (wxT ("1234567890123456789012345678"));
95         size.SetHeight (-1);
96
97         add_label_to_sizer (_sizer, this, _("Recipient certificate"), true, wxGBPosition (r, 0));
98         wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
99         _recipient_thumbprint = new wxStaticText (this, wxID_ANY, wxT (""), wxDefaultPosition, size);
100         _recipient_thumbprint->SetFont (font);
101         set_recipient (recipient);
102         _get_recipient_from_file = new wxButton (this, wxID_ANY, _("Get from file..."));
103         _download_recipient = new wxButton (this, wxID_ANY, _("Download..."));
104         s->Add (_recipient_thumbprint, 1, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
105         s->Add (_get_recipient_from_file, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_X_GAP);
106         s->Add (_download_recipient, 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         vector<string> columns;
114         columns.push_back (wx_to_std (_("Thumbprint")));
115         _trusted_device_list = new EditableList<dcp::Certificate, FileDialogWrapper> (
116                 this, columns, bind (&ScreenDialog::trusted_devices, this), bind (&ScreenDialog::set_trusted_devices, this, _1), bind (&column, _1), false
117                 );
118
119         _sizer->Add (_trusted_device_list, wxGBPosition (r, 0), wxGBSpan (1, 3), wxEXPAND);
120         ++r;
121
122         _name->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ScreenDialog::setup_sensitivity, this));
123         _get_recipient_from_file->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreenDialog::get_recipient_from_file, this));
124         _download_recipient->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreenDialog::download_recipient, this));
125
126         overall_sizer->Add (_sizer, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
127
128         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
129         if (buttons) {
130                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
131         }
132
133         overall_sizer->Layout ();
134         overall_sizer->SetSizeHints (this);
135
136         setup_sensitivity ();
137 }
138
139 string
140 ScreenDialog::name () const
141 {
142         return wx_to_std (_name->GetValue());
143 }
144
145 optional<dcp::Certificate>
146 ScreenDialog::recipient () const
147 {
148         return _recipient;
149 }
150
151 void
152 ScreenDialog::load_recipient (boost::filesystem::path file)
153 {
154         try {
155                 set_recipient (dcp::Certificate (dcp::file_to_string (file)));
156         } catch (dcp::MiscError& e) {
157                 error_dialog (this, wxString::Format (_("Could not read certificate file (%s)"), std_to_wx(e.what()).data()));
158         }
159 }
160
161 void
162 ScreenDialog::get_recipient_from_file ()
163 {
164         wxFileDialog* d = new wxFileDialog (this, _("Select Certificate File"));
165         if (d->ShowModal () == wxID_OK) {
166                 load_recipient (boost::filesystem::path (wx_to_std (d->GetPath ())));
167         }
168         d->Destroy ();
169
170         setup_sensitivity ();
171 }
172
173 void
174 ScreenDialog::download_recipient ()
175 {
176         DownloadCertificateDialog* d = new DownloadCertificateDialog (this);
177         if (d->ShowModal() == wxID_OK) {
178                 set_recipient (d->certificate ());
179         }
180         d->Destroy ();
181         setup_sensitivity ();
182 }
183
184 void
185 ScreenDialog::setup_sensitivity ()
186 {
187         wxButton* ok = dynamic_cast<wxButton*> (FindWindowById (wxID_OK, this));
188         if (ok) {
189                 ok->Enable (static_cast<bool>(_recipient) && !_name->GetValue().IsEmpty());
190         }
191 }
192
193 void
194 ScreenDialog::set_recipient (optional<dcp::Certificate> r)
195 {
196         _recipient = r;
197
198         if (_recipient) {
199                 _recipient_thumbprint->SetLabel (std_to_wx (_recipient->thumbprint ()));
200                 _sizer->Layout ();
201         }
202 }