Remove all use of stringstream in an attempt to fix
[dcpomatic.git] / src / wx / dolby_doremi_certificate_panel.cc
1 /*
2     Copyright (C) 2014-2015 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 "dolby_doremi_certificate_panel.h"
22 #include "download_certificate_dialog.h"
23 #include "wx_util.h"
24 #include "lib/compose.hpp"
25 #include "lib/util.h"
26 #include "lib/signal_manager.h"
27 #include "lib/internet.h"
28 #include <dcp/raw_convert.h>
29 #include <curl/curl.h>
30 #include <zip.h>
31 #include <boost/foreach.hpp>
32 #include <iostream>
33
34 using std::string;
35 using std::cout;
36 using std::list;
37 using boost::function;
38 using boost::optional;
39
40 DolbyDoremiCertificatePanel::DolbyDoremiCertificatePanel (wxWindow* parent, DownloadCertificateDialog* dialog)
41         : DownloadCertificatePanel (parent, dialog)
42 {
43         add_label_to_sizer (_table, this, _("Serial number"), true);
44         _serial = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, wxSize (300, -1));
45         _table->Add (_serial, 1, wxEXPAND);
46
47         _serial->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&DownloadCertificateDialog::setup_sensitivity, _dialog));
48
49         layout ();
50 }
51
52 void
53 DolbyDoremiCertificatePanel::download (wxStaticText* message)
54 {
55         message->SetLabel (_("Downloading certificate"));
56
57         /* Hack: without this the SetLabel() above has no visible effect */
58         wxMilliSleep (200);
59
60         signal_manager->when_idle (boost::bind (&DolbyDoremiCertificatePanel::finish_download, this, wx_to_std (_serial->GetValue ()), message));
61 }
62
63 static void
64 try_dcp2000 (list<string>& urls, list<string>& files, string prefix, string serial)
65 {
66         urls.push_back (String::compose ("%1%2xxx/dcp2000-%3.dcicerts.zip", prefix, serial.substr(0, 3), serial));
67         files.push_back (String::compose ("dcp2000-%1.cert.sha256.pem", serial));
68
69         urls.push_back (String::compose ("%1%2xxx/dcp2000-%3.dcicerts.zip", prefix, serial.substr(0, 3), serial));
70         files.push_back (String::compose ("dcp2000-%1.cert.sha256.pem", serial));
71
72         urls.push_back (String::compose ("%1%2xxx/dcp2000-%3.certs.zip", prefix, serial.substr(0, 3), serial));
73         files.push_back (String::compose ("dcp2000-%1.cert.sha256.pem", serial));
74 }
75
76 static void
77 try_imb (list<string>& urls, list<string>& files, string prefix, string serial)
78 {
79         urls.push_back (String::compose ("%1%2xxx/imb-%3.dcicerts.zip", prefix, serial.substr(0, 3), serial));
80         files.push_back (String::compose ("imb-%1.cert.sha256.pem", serial));
81 }
82
83 static void
84 try_ims (list<string>& urls, list<string>& files, string prefix, string serial)
85 {
86         urls.push_back (String::compose ("%1%2xxx/ims-%3.dcicerts.zip", prefix, serial.substr(0, 3), serial));
87         files.push_back (String::compose ("ims-%1.cert.sha256.pem", serial));
88 }
89
90 static void
91 try_cat862 (list<string>& urls, list<string>& files, string prefix, string serial)
92 {
93         int const serial_int = dcp::raw_convert<int> (serial);
94
95         string cat862;
96         if (serial_int <= 510999) {
97                 cat862 = "CAT862_510999_and_lower";
98         } else if (serial_int >= 617000) {
99                 cat862 = "CAT862_617000_and_higher";
100         } else {
101                 int const lower = serial_int - (serial_int % 1000);
102                 cat862 = String::compose ("CAT862_%1-%2", lower, lower + 999);
103         }
104
105         urls.push_back (String::compose ("%1%2/cert_Dolby256-CAT862-%3.zip", prefix, cat862, serial_int));
106         files.push_back (String::compose ("cert_Dolby256-CAT862-%1.pem.crt", serial_int));
107 }
108
109 static void
110 try_dsp100 (list<string>& urls, list<string>& files, string prefix, string serial)
111 {
112         int const serial_int = dcp::raw_convert<int> (serial);
113
114         string dsp100;
115         if (serial_int <= 999) {
116                 dsp100 = "DSP100_053_thru_999";
117         } else if (serial_int >= 3000) {
118                 dsp100 = "DSP100_3000_and_higher";
119         } else {
120                 int const lower = serial_int - (serial_int % 1000);
121                 dsp100 = String::compose ("DSP100_%1_thru_%2", lower, lower + 999);
122         }
123
124         urls.push_back (String::compose ("%1%2/cert_Dolby256-DSP100-%3.zip", prefix, dsp100, serial_int));
125         files.push_back (String::compose ("cert_Dolby256-DSP100-%1.pem.crt", serial_int));
126 }
127
128 static void
129 try_cat745 (list<string>& urls, list<string>& files, string prefix, string serial)
130 {
131         int const serial_int = dcp::raw_convert<int> (serial.substr (1));
132
133         string cat745;
134         if (serial_int <= 999) {
135                 cat745 = "CAT745_1_thru_999";
136         } else if (serial_int >= 6000) {
137                 cat745 = "CAT745_6000_and_higher";
138         } else {
139                 int const lower = serial_int - (serial_int % 1000);
140                 cat745 = String::compose ("CAT745_%1_thru_%2", lower, lower + 999);
141         }
142
143         urls.push_back (String::compose ("%1%2/cert_Dolby-CAT745-%3.zip", prefix, cat745, serial_int));
144         files.push_back (String::compose ("cert_Dolby-CAT745-%1.pem.crt", serial_int));
145 }
146
147 static void
148 try_cp850 (list<string>& urls, list<string>& files, string prefix, string serial)
149 {
150         int const serial_int = dcp::raw_convert<int> (serial.substr (1));
151
152         int const lower = serial_int - (serial_int % 1000);
153         urls.push_back (String::compose ("%1CP850_CAT1600_F%2-F%3/cert_RMB_SPB_MDE_FMA.Dolby-CP850-F%4.zip", prefix, lower, lower + 999, serial_int));
154         files.push_back (String::compose ("cert_RMB_SPB_MDE_FMA.Dolby-CP850-F%1.pem.crt", serial_int));
155 }
156
157 void
158 DolbyDoremiCertificatePanel::finish_download (string serial, wxStaticText* message)
159 {
160         /* Try dcp2000, imb and ims prefixes (see mantis #375) */
161
162         string const prefix = "ftp://anonymous@ftp.cinema.dolby.com/Certificates/";
163         list<string> urls;
164         list<string> files;
165
166         bool starts_with_digit = false;
167         optional<char> starting_char;
168
169         if (!serial.empty()) {
170                 if (isdigit (serial[0])) {
171                         starts_with_digit = true;
172                 } else {
173                         starting_char = serial[0];
174                 }
175         }
176
177         if (starts_with_digit) {
178                 try_dcp2000 (urls, files, prefix, serial);
179                 try_imb (urls, files, prefix, serial);
180                 try_ims (urls, files, prefix, serial);
181                 try_cat862 (urls, files, prefix, serial);
182                 try_dsp100 (urls, files, prefix, serial);
183         } else if (starting_char == 'H') {
184                 try_cat745 (urls, files, prefix, serial);
185         } else if (starting_char == 'F') {
186                 try_cp850 (urls, files, prefix, serial);
187         }
188
189         list<string> errors;
190         bool ok = false;
191         list<string>::const_iterator i = urls.begin ();
192         list<string>::const_iterator j = files.begin ();
193         while (!ok && i != urls.end ()) {
194                 optional<string> error = get_from_zip_url (*i++, *j++, true, boost::bind (&DownloadCertificatePanel::load, this, _1));
195                 if (error) {
196                         errors.push_back (error.get ());
197                 } else {
198                         ok = true;
199                 }
200         }
201
202         if (ok) {
203                 message->SetLabel (_("Certificate downloaded"));
204                 _dialog->setup_sensitivity ();
205         } else {
206                 message->SetLabel (wxT (""));
207
208                 string s;
209                 BOOST_FOREACH (string e, errors) {
210                         s += e + "\n";
211                 }
212
213                 error_dialog (this, std_to_wx (s));
214         }
215 }
216
217 bool
218 DolbyDoremiCertificatePanel::ready_to_download () const
219 {
220         return !_serial->IsEmpty ();
221 }