Cleanup of various dialogs by inherting TableDialog.
[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         add (_("Country"), true);
34         _country = add (new wxChoice (this, wxID_ANY));
35         _country->Append (N_("Hashemite Kingdom of Jordan"));
36         
37         add (_("Cinema"), true);
38         _cinema = add (new wxChoice (this, wxID_ANY));
39         _cinema->Append (N_("Motion Picture Solutions London Mobile & QC"));
40
41         add_common_widgets ();
42
43         _country->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&DolbyCertificateDialog::country_selected, this));
44         _cinema->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&DolbyCertificateDialog::cinema_selected, this));
45         Bind (wxEVT_IDLE, boost::bind (&DolbyCertificateDialog::setup_countries, this));
46
47         _country->Clear ();
48         _cinema->Clear ();
49 }
50
51 static size_t
52 ftp_data_ls (void* buffer, size_t size, size_t nmemb, void* data)
53 {
54         string* s = reinterpret_cast<string *> (data);
55         uint8_t* b = reinterpret_cast<uint8_t *> (buffer);
56         for (size_t i = 0; i < (size * nmemb); ++i) {
57                 *s += b[i];
58         }
59         return nmemb;
60 }
61
62 list<string>
63 DolbyCertificateDialog::ftp_ls (string dir) const
64 {
65         CURL* curl = curl_easy_init ();
66         if (!curl) {
67                 _message->SetLabel (N_("Could not set up libcurl"));
68                 return list<string> ();
69         }
70
71         string url = String::compose ("ftp://dolbyrootcertificates:houro61l@ftp.dolby.co.uk/SHA256/%1", dir);
72         if (url.substr (url.length() - 1, 1) != "/") {
73                 url += "/";
74         }
75         curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
76
77         string ls_raw;
78         struct curl_slist* commands = 0;
79         commands = curl_slist_append (commands, "NLST");
80         curl_easy_setopt (curl, CURLOPT_POSTQUOTE, commands);
81         curl_easy_setopt (curl, CURLOPT_WRITEDATA, &ls_raw);
82         curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ftp_data_ls);
83         curl_easy_setopt (curl, CURLOPT_FTP_USE_EPSV, 0);
84         CURLcode const r = curl_easy_perform (curl);
85         if (r != CURLE_OK) {
86                 _message->SetLabel (_("Problem occurred when contacting Dolby."));
87                 return list<string> ();
88         }
89
90         stringstream s (ls_raw);
91         string line;
92         list<string> ls;
93         while (s.good ()) {
94                 getline (s, line);
95                 if (line.length() > 55) {
96                         string const file = line.substr (55);
97                         if (file != "." && file != "..") {
98                                 ls.push_back (file);
99                         }
100                 }
101         }
102
103         curl_easy_cleanup (curl);
104
105         return ls;
106 }
107
108 void
109 DolbyCertificateDialog::setup_countries ()
110 {
111         if (_country->GetCount() > 0) {
112                 /* Already set up */
113                 return;
114         }
115         
116         _country->Append (_("Fetching..."));
117         _country->SetSelection (0);
118         run_gui_loop ();
119         
120         list<string> const countries = ftp_ls ("");
121         _country->Clear ();
122         for (list<string>::const_iterator i = countries.begin(); i != countries.end(); ++i) {
123                 _country->Append (std_to_wx (*i));
124         }
125 }
126
127 void
128 DolbyCertificateDialog::country_selected ()
129 {
130         _cinema->Clear ();
131         _cinema->Append (_("Fetching..."));
132         _cinema->SetSelection (0);
133         run_gui_loop ();
134         
135         list<string> const cinemas = ftp_ls (wx_to_std (_country->GetStringSelection()));
136         _cinema->Clear ();
137         for (list<string>::const_iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
138                 _cinema->Append (std_to_wx (*i));
139         }
140 }
141
142 void
143 DolbyCertificateDialog::cinema_selected ()
144 {
145         _download->Enable (true);
146 }
147
148 void
149 DolbyCertificateDialog::download ()
150 {
151
152 }