d44c9e737816a61c8069fd6a2f383bbf51ef2da3
[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 <curl/curl.h>
21 #include "lib/compose.hpp"
22 #include "dolby_certificate_dialog.h"
23 #include "wx_util.h"
24
25 using std::list;
26 using std::string;
27 using std::stringstream;
28 using std::cout;
29
30 DolbyCertificateDialog::DolbyCertificateDialog (wxWindow* parent, boost::function<void (boost::filesystem::path)> load)
31         : DownloadCertificateDialog (parent, load)
32 {
33         wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
34
35         _country = new wxChoice (this, wxID_ANY);
36         add_label_to_sizer (table, this, _("Country"), true);
37         table->Add (_country, 1, wxEXPAND | wxLEFT | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
38         _country->Append (N_("Hashemite Kingdom of Jordan"));
39         
40         _cinema = new wxChoice (this, wxID_ANY);
41         add_label_to_sizer (table, this, _("Cinema"), true);
42         table->Add (_cinema, 1, wxEXPAND | wxLEFT | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
43         _cinema->Append (N_("Wometco Dominicana Palacio Del Cine"));
44         _overall_sizer->Add (table);
45
46         add_common_widgets ();
47
48         _country->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&DolbyCertificateDialog::country_selected, this));
49         _cinema->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&DolbyCertificateDialog::cinema_selected, this));
50
51         _country->Clear ();
52         _cinema->Clear ();
53 }
54
55 static size_t
56 ftp_data_ls (void* buffer, size_t size, size_t nmemb, void* data)
57 {
58         string* s = reinterpret_cast<string *> (data);
59         uint8_t* b = reinterpret_cast<uint8_t *> (buffer);
60         for (size_t i = 0; i < (size * nmemb); ++i) {
61                 *s += b[i];
62         }
63         return nmemb;
64 }
65
66 list<string>
67 DolbyCertificateDialog::ftp_ls (string dir) const
68 {
69         CURL* curl = curl_easy_init ();
70         if (!curl) {
71                 _message->SetLabel (N_("Could not set up libcurl"));
72                 return list<string> ();
73         }
74
75         string url = String::compose ("ftp://dolbyrootcertificates:houro61l@ftp.dolby.co.uk/SHA256/%1", dir);
76         if (url.substr (url.length() - 1, 1) != "/") {
77                 url += "/";
78         }
79         curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
80
81         string ls_raw;
82         struct curl_slist* commands = 0;
83         commands = curl_slist_append (commands, "NLST");
84         curl_easy_setopt (curl, CURLOPT_POSTQUOTE, commands);
85         curl_easy_setopt (curl, CURLOPT_WRITEDATA, &ls_raw);
86         curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ftp_data_ls);
87         curl_easy_setopt (curl, CURLOPT_FTP_USE_EPSV, 0);
88         CURLcode const r = curl_easy_perform (curl);
89         if (r != CURLE_OK) {
90                 _message->SetLabel (_("Problem occurred when contacting Dolby."));
91                 return list<string> ();
92         }
93
94         stringstream s (ls_raw);
95         string line;
96         list<string> ls;
97         while (s.good ()) {
98                 getline (s, line);
99                 if (line.length() > 55) {
100                         string const file = line.substr (55);
101                         if (file != "." && file != "..") {
102                                 ls.push_back (file);
103                         }
104                 }
105         }
106
107         curl_easy_cleanup (curl);
108
109         return ls;
110 }
111
112 void
113 DolbyCertificateDialog::setup ()
114 {
115         _message->SetLabel (_("Fetching available countries"));
116         run_gui_loop ();
117         list<string> const countries = ftp_ls ("");
118         for (list<string>::const_iterator i = countries.begin(); i != countries.end(); ++i) {
119                 _country->Append (std_to_wx (*i));
120         }
121         _message->SetLabel ("");
122 }
123
124 void
125 DolbyCertificateDialog::country_selected ()
126 {
127         _message->SetLabel (_("Fetching available cinemas"));
128         run_gui_loop ();
129         list<string> const cinemas = ftp_ls (wx_to_std (_country->GetStringSelection()));
130         _cinema->Clear ();
131         for (list<string>::const_iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
132                 _cinema->Append (std_to_wx (*i));
133         }
134         _message->SetLabel ("");
135 }
136
137 void
138 DolbyCertificateDialog::cinema_selected ()
139 {
140         _download->Enable (true);
141 }
142
143 void
144 DolbyCertificateDialog::download ()
145 {
146
147 }