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