Logging improvements to allow prettier displays in the server GUI.
[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 #include <iostream>
29
30 using std::list;
31 using std::string;
32 using std::vector;
33 using std::cout;
34 using boost::optional;
35 using boost::algorithm::split;
36 using boost::algorithm::is_any_of;
37
38 DolbyCertificateDialog::DolbyCertificateDialog (wxWindow* parent, boost::function<void (boost::filesystem::path)> load)
39         : DownloadCertificateDialog (parent, load)
40 {
41         add (_("Country"), true);
42         _country = add (new wxChoice (this, wxID_ANY));
43         _country->Append (N_("Hashemite Kingdom of Jordan"));
44
45         add (_("Cinema"), true);
46         _cinema = add (new wxChoice (this, wxID_ANY));
47         _cinema->Append (N_("Motion Picture Solutions London Mobile & QC"));
48
49         add (_("Serial number"), true);
50         _serial = add (new wxChoice (this, wxID_ANY));
51
52         add_common_widgets ();
53
54         _country->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&DolbyCertificateDialog::country_selected, this));
55         _cinema->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&DolbyCertificateDialog::cinema_selected, this));
56         _serial->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&DolbyCertificateDialog::serial_selected, this));
57         Bind (wxEVT_IDLE, boost::bind (&DolbyCertificateDialog::setup_countries, this));
58
59         _country->Clear ();
60         _cinema->Clear ();
61 }
62
63 list<string>
64 DolbyCertificateDialog::get_dir (string dir) const
65 {
66         string url = String::compose ("ftp://dolbyrootcertificates:houro61l@ftp.dolby.co.uk/SHA256/%1", dir);
67         return ftp_ls (url);
68 }
69
70 void
71 DolbyCertificateDialog::setup_countries ()
72 {
73         if (_country->GetCount() > 0) {
74                 /* Already set up */
75                 return;
76         }
77
78         _country->Append (_("Fetching..."));
79         _country->SetSelection (0);
80
81 #ifdef DCPOMATIC_OSX
82         /* See DoremiCertificateDialog for discussion about this daft delay */
83         wxMilliSleep (200);
84 #endif
85         signal_manager->when_idle (boost::bind (&DolbyCertificateDialog::finish_setup_countries, this));
86 }
87
88 void
89 DolbyCertificateDialog::finish_setup_countries ()
90 {
91         _country->Clear ();
92         BOOST_FOREACH (string i, get_dir ("")) {
93                 _country->Append (std_to_wx (i));
94         }
95 }
96
97 void
98 DolbyCertificateDialog::country_selected ()
99 {
100         _cinema->Clear ();
101         _cinema->Append (_("Fetching..."));
102         _cinema->SetSelection (0);
103
104 #ifdef DCPOMATIC_OSX
105         wxMilliSleep (200);
106 #endif
107         signal_manager->when_idle (boost::bind (&DolbyCertificateDialog::finish_country_selected, this));
108 }
109
110 void
111 DolbyCertificateDialog::finish_country_selected ()
112 {
113         _cinema->Clear ();
114         BOOST_FOREACH (string i, get_dir (wx_to_std (_country->GetStringSelection()))) {
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         signal_manager->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
137         _serial->Clear ();
138         BOOST_FOREACH (string i, get_dir (dir)) {
139                 vector<string> a;
140                 split (a, i, is_any_of ("-_"));
141                 if (a.size() >= 4) {
142                         _serial->Append (std_to_wx (a[3]), new wxStringClientData (std_to_wx (i)));
143                 }
144         }
145 }
146
147 void
148 DolbyCertificateDialog::serial_selected ()
149 {
150         _download->Enable (true);
151 }
152
153 void
154 DolbyCertificateDialog::download ()
155 {
156         downloaded (false);
157         _message->SetLabel (_("Downloading certificate"));
158
159 #ifdef DCPOMATIC_OSX
160         wxMilliSleep (200);
161 #endif
162
163         signal_manager->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                 downloaded (true);
193         }
194 }