2 Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include <wx/filepicker.h>
21 #include <wx/validate.h>
22 #include <curl/curl.h>
24 #include <libdcp/exceptions.h>
25 #include "lib/compose.hpp"
27 #include "screen_dialog.h"
33 using boost::shared_ptr;
35 ScreenDialog::ScreenDialog (wxWindow* parent, string title, string name, shared_ptr<libdcp::Certificate> certificate)
36 : wxDialog (parent, wxID_ANY, std_to_wx (title))
37 , _certificate (certificate)
39 wxFlexGridSizer* table = new wxFlexGridSizer (2, 6, 6);
40 table->AddGrowableCol (1, 1);
42 add_label_to_sizer (table, this, "Name", true);
43 _name = new wxTextCtrl (this, wxID_ANY, std_to_wx (name), wxDefaultPosition, wxSize (320, -1));
44 table->Add (_name, 1, wxEXPAND);
46 add_label_to_sizer (table, this, "Server manufacturer", true);
47 _manufacturer = new wxChoice (this, wxID_ANY);
48 table->Add (_manufacturer, 1, wxEXPAND);
50 add_label_to_sizer (table, this, "Server serial number", true);
51 _serial = new wxTextCtrl (this, wxID_ANY);
52 table->Add (_serial, 1, wxEXPAND);
54 add_label_to_sizer (table, this, "Certificate", true);
55 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
56 _load_certificate = new wxButton (this, wxID_ANY, _("Load from file..."));
57 _download_certificate = new wxButton (this, wxID_ANY, _("Download"));
58 s->Add (_load_certificate, 1, wxEXPAND);
59 s->Add (_download_certificate, 1, wxEXPAND);
60 table->Add (s, 1, wxEXPAND);
63 _progress = new Progress (this);
64 table->Add (_progress, 1, wxEXPAND);
67 _certificate_text = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, wxSize (320, 256), wxTE_MULTILINE | wxTE_READONLY);
69 _certificate_text->SetValue (certificate->certificate ());
71 wxFont font = wxSystemSettings::GetFont (wxSYS_ANSI_FIXED_FONT);
72 font.SetPointSize (font.GetPointSize() / 2);
73 _certificate_text->SetFont (font);
74 table->Add (_certificate_text, 1, wxEXPAND);
76 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
77 overall_sizer->Add (table, 1, wxEXPAND | wxALL, 6);
79 wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
81 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
84 SetSizer (overall_sizer);
85 overall_sizer->Layout ();
86 overall_sizer->SetSizeHints (this);
88 _manufacturer->Append (_("Unknown"));
89 _manufacturer->Append (_("Doremi"));
90 _manufacturer->Append (_("Other"));
91 _manufacturer->SetSelection (0);
93 _load_certificate->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreenDialog::select_certificate, this));
94 _download_certificate->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreenDialog::download_certificate, this));
95 _manufacturer->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&ScreenDialog::setup_sensitivity, this));
96 _serial->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ScreenDialog::setup_sensitivity, this));
102 ScreenDialog::name () const
104 return wx_to_std (_name->GetValue());
107 shared_ptr<libdcp::Certificate>
108 ScreenDialog::certificate () const
114 ScreenDialog::load_certificate (boost::filesystem::path file)
117 _certificate.reset (new libdcp::Certificate (file));
118 _certificate_text->SetValue (_certificate->certificate ());
119 } catch (libdcp::MiscError& e) {
120 error_dialog (this, String::compose ("Could not read certificate file (%1)", e.what()));
125 ScreenDialog::select_certificate ()
127 wxFileDialog* d = new wxFileDialog (this, _("Select Certificate File"));
129 if (d->ShowModal () == wxID_OK) {
130 load_certificate (boost::filesystem::path (wx_to_std (d->GetPath ())));
135 setup_sensitivity ();
139 ftp_data (void* buffer, size_t size, size_t nmemb, void* stream)
141 FILE* f = reinterpret_cast<FILE*> (stream);
142 return fwrite (buffer, size, nmemb, f);
146 ScreenDialog::download_certificate ()
148 if (_manufacturer->GetStringSelection() == _("Doremi")) {
149 string const serial = wx_to_std (_serial->GetValue ());
150 if (serial.length() != 6) {
151 error_dialog (this, _("Doremi serial numbers must have 6 numbers"));
155 CURL* curl = curl_easy_init ();
157 error_dialog (this, N_("Could not set up libcurl"));
161 string const url = String::compose (
162 "ftp://service:t3chn1c1an@ftp.doremilabs.com/Certificates/%1xxx/dcp2000-%2.dcicerts.zip",
163 serial.substr(0, 3), serial
166 curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
168 ScopedTemporary temp_zip;
169 FILE* f = temp_zip.open ("wb");
170 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ftp_data);
171 curl_easy_setopt (curl, CURLOPT_WRITEDATA, f);
172 _progress->set_message (_("Downloading certificate from Doremi"));
173 CURLcode const cr = curl_easy_perform (curl);
174 _progress->set_value (50);
176 curl_easy_cleanup (curl);
177 if (cr != CURLE_OK) {
178 _progress->set_message (wxString::Format (_("Certificate download failed (%d)"), cr));
182 _progress->set_message (_("Unpacking"));
183 struct zip* zip = zip_open (temp_zip.c_str(), 0, 0);
185 _progress->set_message ("Could not open certificate ZIP file");
189 string const name_in_zip = String::compose ("dcp2000-%1.cert.sha256.pem", serial);
190 struct zip_file* zip_file = zip_fopen (zip, name_in_zip.c_str(), 0);
192 _progress->set_message ("Could not find certificate in ZIP file");
196 ScopedTemporary temp_cert;
197 f = temp_cert.open ("wb");
200 int const N = zip_fread (zip_file, buffer, sizeof (buffer));
201 fwrite (buffer, 1, N, f);
202 if (N < int (sizeof (buffer))) {
208 _progress->set_value (100);
209 _progress->set_message (_("OK"));
210 load_certificate (temp_cert.file ());
215 ScreenDialog::setup_sensitivity ()
217 wxButton* ok = dynamic_cast<wxButton*> (FindWindowById (wxID_OK, this));
218 ok->Enable (_certificate);
220 _download_certificate->Enable (_manufacturer->GetStringSelection() == _("Doremi") && !_serial->GetValue().IsEmpty ());