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