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::cout;
31 using boost::optional;
32 using boost::algorithm::split;
33 using boost::algorithm::is_any_of;
34
35 DolbyCertificateDialog::DolbyCertificateDialog (wxWindow* parent, boost::function<void (boost::filesystem::path)> load)
36         : DownloadCertificateDialog (parent, load)
37 {
38         add (_("Country"), true);
39         _country = add (new wxChoice (this, wxID_ANY));
40         _country->Append (N_("Hashemite Kingdom of Jordan"));
41         
42         add (_("Cinema"), true);
43         _cinema = add (new wxChoice (this, wxID_ANY));
44         _cinema->Append (N_("Motion Picture Solutions London Mobile & QC"));
45
46         add (_("Serial number"), true);
47         _serial = add (new wxChoice (this, wxID_ANY));
48
49         add_common_widgets ();
50
51         _country->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&DolbyCertificateDialog::country_selected, this));
52         _cinema->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&DolbyCertificateDialog::cinema_selected, this));
53         _serial->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&DolbyCertificateDialog::serial_selected, this));
54         Bind (wxEVT_IDLE, boost::bind (&DolbyCertificateDialog::setup_countries, this));
55
56         _country->Clear ();
57         _cinema->Clear ();
58 }
59
60 list<string>
61 DolbyCertificateDialog::get_dir (string dir) const
62 {
63         string url = String::compose ("ftp://dolbyrootcertificates:houro61l@ftp.dolby.co.uk/SHA256/%1", dir);
64         return ftp_ls (url);
65 }
66
67 void
68 DolbyCertificateDialog::setup_countries ()
69 {
70         if (_country->GetCount() > 0) {
71                 /* Already set up */
72                 return;
73         }
74         
75         _country->Append (_("Fetching..."));
76         _country->SetSelection (0);
77         run_gui_loop ();
78         
79         list<string> const countries = get_dir ("");
80         _country->Clear ();
81         for (list<string>::const_iterator i = countries.begin(); i != countries.end(); ++i) {
82                 _country->Append (std_to_wx (*i));
83         }
84 }
85
86 void
87 DolbyCertificateDialog::country_selected ()
88 {
89         _cinema->Clear ();
90         _cinema->Append (_("Fetching..."));
91         _cinema->SetSelection (0);
92         run_gui_loop ();
93         
94         list<string> const cinemas = get_dir (wx_to_std (_country->GetStringSelection()));
95         _cinema->Clear ();
96         for (list<string>::const_iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
97                 _cinema->Append (std_to_wx (*i));
98         }
99 }
100
101 void
102 DolbyCertificateDialog::cinema_selected ()
103 {
104         _serial->Clear ();
105         _serial->Append (_("Fetching..."));
106         _serial->SetSelection (0);
107         run_gui_loop ();
108
109         string const dir = String::compose ("%1/%2", wx_to_std (_country->GetStringSelection()), wx_to_std (_cinema->GetStringSelection()));
110         list<string> const zips = get_dir (dir);
111
112         _serial->Clear ();
113         for (list<string>::const_iterator i = zips.begin(); i != zips.end(); ++i) {
114                 vector<string> a;
115                 split (a, *i, is_any_of ("-_"));
116                 if (a.size() >= 4) {
117                         _serial->Append (std_to_wx (a[3]), new wxStringClientData (std_to_wx (*i)));
118                 }
119         }
120 }
121
122 void
123 DolbyCertificateDialog::serial_selected ()
124 {
125         _download->Enable (true);
126 }
127
128 void
129 DolbyCertificateDialog::download ()
130 {
131         _message->SetLabel (_("Downloading certificate"));
132         run_gui_loop ();
133
134         string const zip = string_client_data (_serial->GetClientObject (_serial->GetSelection ()));
135
136         string const file = String::compose (
137                 "ftp://dolbyrootcertificates:houro61l@ftp.dolby.co.uk/SHA256/%1/%2/%3",
138                 wx_to_std (_country->GetStringSelection()),
139                 wx_to_std (_cinema->GetStringSelection()),
140                 zip
141                 );
142
143         /* Work out the certificate file name inside the zip */
144         vector<string> b;
145         split (b, zip, is_any_of ("_"));
146         if (b.size() < 2) {
147                 _message->SetLabel (_("Unexpected certificate filename form"));
148                 return;
149         }
150         string const cert = b[0] + "_" + b[1] + ".pem.crt";
151
152         optional<string> error = get_from_zip_url (file, cert, _load);
153         if (error) {
154                 _message->SetLabel (std_to_wx (error.get ()));
155         } else {
156                 _message->SetLabel (_("Certificate downloaded"));
157         }
158 }