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