Merge master.
[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 "dolby_certificate_dialog.h"
25 #include "wx_util.h"
26
27 using std::list;
28 using std::string;
29 using std::vector;
30 using std::stringstream;
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         run_gui_loop ();
79         
80         list<string> const countries = get_dir ("");
81         _country->Clear ();
82         for (list<string>::const_iterator i = countries.begin(); i != countries.end(); ++i) {
83                 _country->Append (std_to_wx (*i));
84         }
85 }
86
87 void
88 DolbyCertificateDialog::country_selected ()
89 {
90         _cinema->Clear ();
91         _cinema->Append (_("Fetching..."));
92         _cinema->SetSelection (0);
93         run_gui_loop ();
94         
95         list<string> const cinemas = get_dir (wx_to_std (_country->GetStringSelection()));
96         _cinema->Clear ();
97         for (list<string>::const_iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
98                 _cinema->Append (std_to_wx (*i));
99         }
100 }
101
102 void
103 DolbyCertificateDialog::cinema_selected ()
104 {
105         _serial->Clear ();
106         _serial->Append (_("Fetching..."));
107         _serial->SetSelection (0);
108         run_gui_loop ();
109
110         string const dir = String::compose ("%1/%2", wx_to_std (_country->GetStringSelection()), wx_to_std (_cinema->GetStringSelection()));
111         list<string> const zips = get_dir (dir);
112
113         _serial->Clear ();
114         for (list<string>::const_iterator i = zips.begin(); i != zips.end(); ++i) {
115                 vector<string> a;
116                 split (a, *i, is_any_of ("-_"));
117                 if (a.size() >= 4) {
118                         _serial->Append (std_to_wx (a[3]), new wxStringClientData (std_to_wx (*i)));
119                 }
120         }
121 }
122
123 void
124 DolbyCertificateDialog::serial_selected ()
125 {
126         _download->Enable (true);
127 }
128
129 void
130 DolbyCertificateDialog::download ()
131 {
132         _message->SetLabel (_("Downloading certificate"));
133         run_gui_loop ();
134
135         string const zip = string_client_data (_serial->GetClientObject (_serial->GetSelection ()));
136
137         string const file = String::compose (
138                 "ftp://dolbyrootcertificates:houro61l@ftp.dolby.co.uk/SHA256/%1/%2/%3",
139                 wx_to_std (_country->GetStringSelection()),
140                 wx_to_std (_cinema->GetStringSelection()),
141                 zip
142                 );
143
144         /* Work out the certificate file name inside the zip */
145         vector<string> b;
146         split (b, zip, is_any_of ("_"));
147         if (b.size() < 2) {
148                 _message->SetLabel (_("Unexpected certificate filename form"));
149                 return;
150         }
151         string const cert = b[0] + "_" + b[1] + ".pem.crt";
152
153         optional<string> error = get_from_zip_url (file, cert, _load);
154         if (error) {
155                 _message->SetLabel (std_to_wx (error.get ()));
156         } else {
157                 _message->SetLabel (_("Certificate downloaded"));
158         }
159 }