5e094844de59697771c0af68827733ceddebf983
[dcpomatic.git] / src / wx / dolby_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 <boost/algorithm/string.hpp>
21 #include <curl/curl.h>
22 #include "lib/compose.hpp"
23 #include "lib/internet.h"
24 #include "lib/ui_signaller.h"
25 #include "dolby_certificate_dialog.h"
26 #include "wx_util.h"
27
28 using std::list;
29 using std::string;
30 using std::vector;
31 using std::cout;
32 using boost::optional;
33 using boost::algorithm::split;
34 using boost::algorithm::is_any_of;
35
36 DolbyCertificateDialog::DolbyCertificateDialog (wxWindow* parent, boost::function<void (boost::filesystem::path)> load)
37         : DownloadCertificateDialog (parent, load)
38 {
39         add (_("Country"), true);
40         _country = add (new wxChoice (this, wxID_ANY));
41         _country->Append (N_("Hashemite Kingdom of Jordan"));
42         
43         add (_("Cinema"), true);
44         _cinema = add (new wxChoice (this, wxID_ANY));
45         _cinema->Append (N_("Motion Picture Solutions London Mobile & QC"));
46
47         add (_("Serial number"), true);
48         _serial = add (new wxChoice (this, wxID_ANY));
49
50         add_common_widgets ();
51
52         _country->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&DolbyCertificateDialog::country_selected, this));
53         _cinema->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&DolbyCertificateDialog::cinema_selected, this));
54         _serial->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&DolbyCertificateDialog::serial_selected, this));
55         Bind (wxEVT_IDLE, boost::bind (&DolbyCertificateDialog::setup_countries, this));
56
57         _country->Clear ();
58         _cinema->Clear ();
59 }
60
61 list<string>
62 DolbyCertificateDialog::get_dir (string dir) const
63 {
64         string url = String::compose ("ftp://dolbyrootcertificates:houro61l@ftp.dolby.co.uk/SHA256/%1", dir);
65         return ftp_ls (url);
66 }
67
68 void
69 DolbyCertificateDialog::setup_countries ()
70 {
71         if (_country->GetCount() > 0) {
72                 /* Already set up */
73                 return;
74         }
75         
76         _country->Append (_("Fetching..."));
77         _country->SetSelection (0);
78
79 #ifdef DCPOMATIC_OSX
80         /* See DoremiCertificateDialog for discussion about this daft delay */
81         wxMilliSleep (200);
82 #endif
83         ui_signaller->when_idle (boost::bind (&DolbyCertificateDialog::finish_setup_countries, this));
84 }
85
86 void
87 DolbyCertificateDialog::finish_setup_countries ()
88 {
89         list<string> const countries = get_dir ("");
90         _country->Clear ();
91         for (list<string>::const_iterator i = countries.begin(); i != countries.end(); ++i) {
92                 _country->Append (std_to_wx (*i));
93         }
94 }
95
96 void
97 DolbyCertificateDialog::country_selected ()
98 {
99         _cinema->Clear ();
100         _cinema->Append (_("Fetching..."));
101         _cinema->SetSelection (0);
102
103 #ifdef DCPOMATIC_OSX
104         wxMilliSleep (200);
105 #endif  
106         ui_signaller->when_idle (boost::bind (&DolbyCertificateDialog::finish_country_selected, this));
107 }
108
109 void
110 DolbyCertificateDialog::finish_country_selected ()
111 {
112         list<string> const cinemas = get_dir (wx_to_std (_country->GetStringSelection()));
113         _cinema->Clear ();
114         for (list<string>::const_iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
115                 _cinema->Append (std_to_wx (*i));
116         }
117 }
118
119 void
120 DolbyCertificateDialog::cinema_selected ()
121 {
122         _serial->Clear ();
123         _serial->Append (_("Fetching..."));
124         _serial->SetSelection (0);
125
126 #ifdef DCPOMATIC_OSX
127         wxMilliSleep (200);
128 #endif
129         ui_signaller->when_idle (boost::bind (&DolbyCertificateDialog::finish_cinema_selected, this));
130 }
131
132 void
133 DolbyCertificateDialog::finish_cinema_selected ()
134 {
135         string const dir = String::compose ("%1/%2", wx_to_std (_country->GetStringSelection()), wx_to_std (_cinema->GetStringSelection()));
136         list<string> const zips = get_dir (dir);
137
138         _serial->Clear ();
139         for (list<string>::const_iterator i = zips.begin(); i != zips.end(); ++i) {
140                 vector<string> a;
141                 split (a, *i, is_any_of ("-_"));
142                 if (a.size() >= 4) {
143                         _serial->Append (std_to_wx (a[3]), new wxStringClientData (std_to_wx (*i)));
144                 }
145         }
146 }
147
148 void
149 DolbyCertificateDialog::serial_selected ()
150 {
151         _download->Enable (true);
152 }
153
154 void
155 DolbyCertificateDialog::download ()
156 {
157         _message->SetLabel (_("Downloading certificate"));
158
159 #ifdef DCPOMATIC_OSX
160         wxMilliSleep (200);
161 #endif
162
163         ui_signaller->when_idle (boost::bind (&DolbyCertificateDialog::finish_download, this));
164 }
165
166 void
167 DolbyCertificateDialog::finish_download ()
168 {
169         string const zip = string_client_data (_serial->GetClientObject (_serial->GetSelection ()));
170
171         string const file = String::compose (
172                 "ftp://dolbyrootcertificates:houro61l@ftp.dolby.co.uk/SHA256/%1/%2/%3",
173                 wx_to_std (_country->GetStringSelection()),
174                 wx_to_std (_cinema->GetStringSelection()),
175                 zip
176                 );
177
178         /* Work out the certificate file name inside the zip */
179         vector<string> b;
180         split (b, zip, is_any_of ("_"));
181         if (b.size() < 2) {
182                 _message->SetLabel (_("Unexpected certificate filename form"));
183                 return;
184         }
185         string const cert = b[0] + "_" + b[1] + ".pem.crt";
186
187         optional<string> error = get_from_zip_url (file, cert, _load);
188         if (error) {
189                 _message->SetLabel (std_to_wx (error.get ()));
190         } else {
191                 _message->SetLabel (_("Certificate downloaded"));
192         }
193 }