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