4b37b1ae6c4d3d560380746813e27a53c1c338b5
[dcpomatic.git] / src / wx / doremi_certificate_dialog.cc
1 /*
2     Copyright (C) 2014 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 <curl/curl.h>
21 #include <zip.h>
22 #include "lib/compose.hpp"
23 #include "lib/util.h"
24 #include "doremi_certificate_dialog.h"
25 #include "wx_util.h"
26
27 using std::string;
28 using boost::function;
29
30 DoremiCertificateDialog::DoremiCertificateDialog (wxWindow* parent, function<void (boost::filesystem::path)> load)
31         : DownloadCertificateDialog (parent, load)
32 {
33         add (_("Server serial number"), true);
34         _serial = add (new wxTextCtrl (this, wxID_ANY));
35
36         add_common_widgets ();
37 }
38
39
40
41 static size_t
42 ftp_data (void* buffer, size_t size, size_t nmemb, void* stream)
43 {
44         FILE* f = reinterpret_cast<FILE*> (stream);
45         return fwrite (buffer, size, nmemb, f);
46 }
47
48 void
49 DoremiCertificateDialog::download ()
50 {
51         string const serial = wx_to_std (_serial->GetValue ());
52         if (serial.length() != 6) {
53                 _message->SetLabel (_("Doremi serial numbers must have 6 digits"));
54                 return;
55         }
56         
57         CURL* curl = curl_easy_init ();
58         if (!curl) {
59                 _message->SetLabel (N_("Could not set up libcurl"));
60                 return;
61         }
62         
63         string const url = String::compose (
64                 "ftp://service:t3chn1c1an@ftp.doremilabs.com/Certificates/%1xxx/dcp2000-%2.dcicerts.zip",
65                 serial.substr(0, 3), serial
66                 );
67         
68         curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
69         
70         ScopedTemporary temp_zip;
71         FILE* f = temp_zip.open ("wb");
72         curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ftp_data);
73         curl_easy_setopt (curl, CURLOPT_WRITEDATA, f);
74
75         _message->SetLabel (_("Downloading certificate from Doremi"));
76         run_gui_loop ();
77
78         CURLcode const cr = curl_easy_perform (curl);
79
80         _gauge->SetValue (50);
81         run_gui_loop ();
82         
83         temp_zip.close ();
84         curl_easy_cleanup (curl);
85         if (cr != CURLE_OK) {
86                 _message->SetLabel (wxString::Format (_("Certificate download failed (%d)"), cr));
87                 return;
88         }
89         
90         _message->SetLabel (_("Unpacking"));
91         run_gui_loop ();
92         
93         struct zip* zip = zip_open (temp_zip.c_str(), 0, 0);
94         if (!zip) {
95                 _message->SetLabel (N_("Could not open certificate ZIP file"));
96                 return;
97         }
98         
99         string const name_in_zip = String::compose ("dcp2000-%1.cert.sha256.pem", serial);
100         struct zip_file* zip_file = zip_fopen (zip, name_in_zip.c_str(), 0);
101         if (!zip_file) {
102                 _message->SetLabel (N_("Could not find certificate in ZIP file"));
103                 return;
104         }
105         
106         ScopedTemporary temp_cert;
107         f = temp_cert.open ("wb");
108         char buffer[4096];
109         while (1) {
110                 int const N = zip_fread (zip_file, buffer, sizeof (buffer));
111                 fwrite (buffer, 1, N, f);
112                 if (N < int (sizeof (buffer))) {
113                         break;
114                 }
115         }
116         temp_cert.close ();
117         
118         _gauge->SetValue (100);
119         _message->SetLabel (_("OK"));
120         _load (temp_cert.file ());
121 }