Add support for downloading Doremi server certificates.
[dcpomatic.git] / src / wx / screen_dialog.cc
1 /*
2     Copyright (C) 2012-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 <wx/filepicker.h>
21 #include <wx/validate.h>
22 #include <curl/curl.h>
23 #include <zip.h>
24 #include <libdcp/exceptions.h>
25 #include "lib/compose.hpp"
26 #include "lib/util.h"
27 #include "screen_dialog.h"
28 #include "wx_util.h"
29 #include "progress.h"
30
31 using std::string;
32 using std::cout;
33 using boost::shared_ptr;
34
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)
38 {
39         wxFlexGridSizer* table = new wxFlexGridSizer (2, 6, 6);
40         table->AddGrowableCol (1, 1);
41
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);
45
46         add_label_to_sizer (table, this, "Server manufacturer", true);
47         _manufacturer = new wxChoice (this, wxID_ANY);
48         table->Add (_manufacturer, 1, wxEXPAND);
49
50         add_label_to_sizer (table, this, "Server serial number", true);
51         _serial = new wxTextCtrl (this, wxID_ANY);
52         table->Add (_serial, 1, wxEXPAND);
53         
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);
61
62         table->AddSpacer (0);
63         _progress = new Progress (this);
64         table->Add (_progress, 1, wxEXPAND);
65
66         table->AddSpacer (0);
67         _certificate_text = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, wxSize (320, 256), wxTE_MULTILINE | wxTE_READONLY);
68         if (certificate) {
69                 _certificate_text->SetValue (certificate->certificate ());
70         }
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);
75
76         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
77         overall_sizer->Add (table, 1, wxEXPAND | wxALL, 6);
78         
79         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
80         if (buttons) {
81                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
82         }
83
84         SetSizer (overall_sizer);
85         overall_sizer->Layout ();
86         overall_sizer->SetSizeHints (this);
87
88         _manufacturer->Append (_("Unknown"));
89         _manufacturer->Append (_("Doremi"));
90         _manufacturer->Append (_("Other"));
91         _manufacturer->SetSelection (0);
92
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));
97
98         setup_sensitivity ();
99 }
100
101 string
102 ScreenDialog::name () const
103 {
104         return wx_to_std (_name->GetValue());
105 }
106
107 shared_ptr<libdcp::Certificate>
108 ScreenDialog::certificate () const
109 {
110         return _certificate;
111 }
112
113 void
114 ScreenDialog::load_certificate (boost::filesystem::path file)
115 {
116         try {
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()));
121         }
122 }
123
124 void
125 ScreenDialog::select_certificate ()
126 {
127         wxFileDialog* d = new wxFileDialog (this, _("Select Certificate File"));
128
129         if (d->ShowModal () == wxID_OK) {
130                 load_certificate (boost::filesystem::path (wx_to_std (d->GetPath ())));
131         }
132         
133         d->Destroy ();
134
135         setup_sensitivity ();
136 }
137
138 static size_t
139 ftp_data (void* buffer, size_t size, size_t nmemb, void* stream)
140 {
141         FILE* f = reinterpret_cast<FILE*> (stream);
142         return fwrite (buffer, size, nmemb, f);
143 }
144
145 void
146 ScreenDialog::download_certificate ()
147 {
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"));
152                         return;
153                 }
154
155                 CURL* curl = curl_easy_init ();
156                 if (!curl) {
157                         error_dialog (this, N_("Could not set up libcurl"));
158                         return;
159                 }
160
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
164                         );
165
166                 curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
167
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);
175                 temp_zip.close ();
176                 curl_easy_cleanup (curl);
177                 if (cr != CURLE_OK) {
178                         _progress->set_message (wxString::Format (_("Certificate download failed (%d)"), cr));
179                         return;
180                 }
181
182                 _progress->set_message (_("Unpacking"));
183                 struct zip* zip = zip_open (temp_zip.c_str(), 0, 0);
184                 if (!zip) {
185                         _progress->set_message ("Could not open certificate ZIP file");
186                         return;
187                 }
188
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);
191                 if (!zip_file) {
192                         _progress->set_message ("Could not find certificate in ZIP file");
193                         return;
194                 }
195
196                 ScopedTemporary temp_cert;
197                 f = temp_cert.open ("wb");
198                 char buffer[4096];
199                 while (1) {
200                         int const N = zip_fread (zip_file, buffer, sizeof (buffer));
201                         fwrite (buffer, 1, N, f);
202                         if (N < int (sizeof (buffer))) {
203                                 break;
204                         }
205                 }
206                 temp_cert.close ();
207
208                 _progress->set_value (100);
209                 _progress->set_message (_("OK"));
210                 load_certificate (temp_cert.file ());
211         }
212 }
213
214 void
215 ScreenDialog::setup_sensitivity ()
216 {
217         wxButton* ok = dynamic_cast<wxButton*> (FindWindowById (wxID_OK, this));
218         ok->Enable (_certificate);
219
220         _download_certificate->Enable (_manufacturer->GetStringSelection() == _("Doremi") && !_serial->GetValue().IsEmpty ());
221 }