2 Copyright (C) 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 <curl/curl.h>
22 #include "lib/compose.hpp"
24 #include "doremi_certificate_dialog.h"
28 using boost::function;
30 DoremiCertificateDialog::DoremiCertificateDialog (wxWindow* parent, function<void (boost::filesystem::path)> load)
31 : DownloadCertificateDialog (parent, load)
33 wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
35 add_label_to_sizer (table, this, _("Server serial number"), true);
36 _serial = new wxTextCtrl (this, wxID_ANY);
37 table->Add (_serial, 1, wxEXPAND);
39 _overall_sizer->Add (table, 1, wxALL, DCPOMATIC_DIALOG_BORDER);
41 add_common_widgets ();
47 ftp_data (void* buffer, size_t size, size_t nmemb, void* stream)
49 FILE* f = reinterpret_cast<FILE*> (stream);
50 return fwrite (buffer, size, nmemb, f);
54 DoremiCertificateDialog::download ()
56 string const serial = wx_to_std (_serial->GetValue ());
57 if (serial.length() != 6) {
58 _message->SetLabel (_("Doremi serial numbers must have 6 digits"));
62 CURL* curl = curl_easy_init ();
64 _message->SetLabel (N_("Could not set up libcurl"));
68 string const url = String::compose (
69 "ftp://service:t3chn1c1an@ftp.doremilabs.com/Certificates/%1xxx/dcp2000-%2.dcicerts.zip",
70 serial.substr(0, 3), serial
73 curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
75 ScopedTemporary temp_zip;
76 FILE* f = temp_zip.open ("wb");
77 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ftp_data);
78 curl_easy_setopt (curl, CURLOPT_WRITEDATA, f);
80 _message->SetLabel (_("Downloading certificate from Doremi"));
83 CURLcode const cr = curl_easy_perform (curl);
85 _gauge->SetValue (50);
89 curl_easy_cleanup (curl);
91 _message->SetLabel (wxString::Format (_("Certificate download failed (%d)"), cr));
95 _message->SetLabel (_("Unpacking"));
98 struct zip* zip = zip_open (temp_zip.c_str(), 0, 0);
100 _message->SetLabel (N_("Could not open certificate ZIP file"));
104 string const name_in_zip = String::compose ("dcp2000-%1.cert.sha256.pem", serial);
105 struct zip_file* zip_file = zip_fopen (zip, name_in_zip.c_str(), 0);
107 _message->SetLabel (N_("Could not find certificate in ZIP file"));
111 ScopedTemporary temp_cert;
112 f = temp_cert.open ("wb");
115 int const N = zip_fread (zip_file, buffer, sizeof (buffer));
116 fwrite (buffer, 1, N, f);
117 if (N < int (sizeof (buffer))) {
123 _gauge->SetValue (100);
124 _message->SetLabel (_("OK"));
125 _load (temp_cert.file ());