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