Tweak label.
[dcpomatic.git] / src / wx / dolby_doremi_certificate_panel.cc
1 /*
2     Copyright (C) 2014-2015 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 "dolby_doremi_certificate_panel.h"
21 #include "download_certificate_dialog.h"
22 #include "wx_util.h"
23 #include "lib/compose.hpp"
24 #include "lib/util.h"
25 #include "lib/signal_manager.h"
26 #include "lib/internet.h"
27 #include <dcp/raw_convert.h>
28 #include <curl/curl.h>
29 #include <zip.h>
30 #include <boost/foreach.hpp>
31 #include <iostream>
32
33 using std::string;
34 using std::cout;
35 using std::list;
36 using boost::function;
37 using boost::optional;
38
39 DolbyDoremiCertificatePanel::DolbyDoremiCertificatePanel (wxWindow* parent, DownloadCertificateDialog* dialog)
40         : DownloadCertificatePanel (parent, dialog)
41 {
42         add_label_to_sizer (_table, this, _("Serial number"), true);
43         _serial = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, wxSize (300, -1));
44         _table->Add (_serial, 1, wxEXPAND);
45
46         _serial->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&DownloadCertificateDialog::setup_sensitivity, _dialog));
47
48         layout ();
49 }
50
51 void
52 DolbyDoremiCertificatePanel::download (wxStaticText* message)
53 {
54         message->SetLabel (_("Downloading certificate"));
55
56         /* Hack: without this the SetLabel() above has no visible effect */
57         wxMilliSleep (200);
58
59         signal_manager->when_idle (boost::bind (&DolbyDoremiCertificatePanel::finish_download, this, wx_to_std (_serial->GetValue ()), message));
60 }
61
62 void
63 DolbyDoremiCertificatePanel::finish_download (string serial, wxStaticText* message)
64 {
65         /* Try dcp2000, imb and ims prefixes (see mantis #375) */
66
67         string const prefix = "ftp://anonymous@ftp.cinema.dolby.com/Certificates/";
68         list<string> urls;
69         list<string> files;
70
71         urls.push_back (String::compose ("%1%2xxx/dcp2000-%3.dcicerts.zip", prefix, serial.substr(0, 3), serial));
72         files.push_back (String::compose ("dcp2000-%1.cert.sha256.pem", serial));
73
74         urls.push_back (String::compose ("%1%2xxx/dcp2000-%3.dcicerts.zip", prefix, serial.substr(0, 3), serial));
75         files.push_back (String::compose ("dcp2000-%1.cert.sha256.pem", serial));
76
77         urls.push_back (String::compose ("%1%2xxx/dcp2000-%3.certs.zip", prefix, serial.substr(0, 3), serial));
78         files.push_back (String::compose ("dcp2000-%1.cert.sha256.pem", serial));
79
80         urls.push_back (String::compose ("%1%2xxx/imb-%3.dcicerts.zip", prefix, serial.substr(0, 3), serial));
81         files.push_back (String::compose ("imb-%1.cert.sha256.pem", serial));
82
83         urls.push_back (String::compose ("%1%2xxx/ims-%3.dcicerts.zip", prefix, serial.substr(0, 3), serial));
84         files.push_back (String::compose ("ims-%1.cert.sha256.pem", serial));
85
86         int const serial_int = dcp::raw_convert<int> (serial);
87
88         string cat862;
89         if (serial_int <= 510999) {
90                 cat862 = "CAT862_510999_and_lower";
91         } else if (serial_int >= 617000) {
92                 cat862 = "CAT862_617000_and_higher";
93         } else {
94                 int const lower = serial_int - (serial_int % 1000);
95                 cat862 = String::compose ("CAT862_%1-%2", lower, lower + 999);
96         }
97
98         urls.push_back (String::compose ("%1%2/cert_Dolby256-CAT862-%3.zip", prefix, cat862, serial_int));
99         files.push_back (String::compose ("cert_Dolby256-CAT862-%1.pem.crt", serial_int));
100
101         string dsp100;
102         if (serial_int <= 999) {
103                 dsp100 = "DSP100_053_thru_999";
104         } else if (serial_int >= 3000) {
105                 dsp100 = "DSP100_3000_and_higher";
106         } else {
107                 int const lower = serial_int - (serial_int % 1000);
108                 dsp100 = String::compose ("DSP100_%1_thru_%2", lower, lower + 999);
109         }
110
111         urls.push_back (String::compose ("%1%2/cert_Dolby256-DSP100-%3.zip", prefix, dsp100, serial_int));
112         files.push_back (String::compose ("cert_Dolby256-DSP100-%1.pem.crt", serial_int));
113
114         list<string> errors;
115         bool ok = false;
116         list<string>::const_iterator i = urls.begin ();
117         list<string>::const_iterator j = files.begin ();
118         while (!ok && i != urls.end ()) {
119                 optional<string> error = get_from_zip_url (*i++, *j++, true, boost::bind (&DownloadCertificatePanel::load, this, _1));
120                 if (error) {
121                         errors.push_back (error.get ());
122                 } else {
123                         ok = true;
124                 }
125         }
126
127         if (ok) {
128                 message->SetLabel (_("Certificate downloaded"));
129                 _dialog->setup_sensitivity ();
130         } else {
131                 message->SetLabel (wxT (""));
132
133                 SafeStringStream s;
134                 BOOST_FOREACH (string e, errors) {
135                         s << e << "\n";
136                 }
137
138                 error_dialog (this, std_to_wx (s.str ()));
139         }
140 }
141
142 bool
143 DolbyDoremiCertificatePanel::ready_to_download () const
144 {
145         return !_serial->IsEmpty ();
146 }