aaebb7390831ee709bc7f85efeffe74eaeac9aca
[dcpomatic.git] / src / wx / doremi_certificate_panel.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 "doremi_certificate_panel.h"
21 #include "download_certificate_dialog.h"
22 #include "wx_util.h"
23 #include "lib/compose.hpp"
24 #include "lib/util.h"
25 #include "lib/signal_manager.h"
26 #include "lib/internet.h"
27 #include <curl/curl.h>
28 #include <zip.h>
29 #include <boost/foreach.hpp>
30 #include <iostream>
31
32 using std::string;
33 using std::cout;
34 using std::list;
35 using boost::function;
36 using boost::optional;
37
38 DoremiCertificatePanel::DoremiCertificatePanel (wxWindow* parent, DownloadCertificateDialog* dialog)
39         : DownloadCertificatePanel (parent, dialog)
40 {
41         add_label_to_sizer (_table, this, _("Server serial number"), true);
42         _serial = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, wxSize (300, -1));
43         _table->Add (_serial, 1, wxEXPAND);
44
45         _serial->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&DownloadCertificateDialog::setup_sensitivity, _dialog));
46
47         layout ();
48 }
49
50 void
51 DoremiCertificatePanel::download (wxStaticText* message)
52 {
53         string const serial = wx_to_std (_serial->GetValue ());
54         if (serial.length() != 6) {
55                 error_dialog (this, _("Doremi serial numbers must have 6 digits"));
56                 return;
57         }
58
59         message->SetLabel (_("Downloading certificate"));
60
61         /* Hack: without this the SetLabel() above has no visible effect */
62         wxMilliSleep (200);
63
64         signal_manager->when_idle (boost::bind (&DoremiCertificatePanel::finish_download, this, serial, message));
65 }
66
67 void
68 DoremiCertificatePanel::finish_download (string serial, wxStaticText* message)
69 {
70         /* Try dcp2000, imb and ims prefixes (see mantis #375) */
71
72         list<string> errors;
73
74         optional<string> error = get_from_zip_url (
75                 String::compose (
76                         "ftp://service:t3chn1c1an@ftp.doremilabs.com/Certificates/%1xxx/dcp2000-%2.dcicerts.zip",
77                         serial.substr(0, 3), serial
78                         ),
79                 String::compose ("dcp2000-%1.cert.sha256.pem", serial),
80                 boost::bind (&DownloadCertificatePanel::load, this, _1)
81                 );
82
83         if (error) {
84                 errors.push_back (error.get ());
85                 error = get_from_zip_url (
86                 String::compose (
87                         "ftp://service:t3chn1c1an@ftp.doremilabs.com/Certificates/%1xxx/dcp2000-%2.certs.zip",
88                         serial.substr(0, 3), serial
89                         ),
90                 String::compose ("dcp2000-%1.cert.sha256.pem", serial),
91                 boost::bind (&DownloadCertificatePanel::load, this, _1)
92                 );
93         }
94
95         if (error) {
96                 errors.push_back (error.get ());
97                 error = get_from_zip_url (
98                 String::compose (
99                         "ftp://service:t3chn1c1an@ftp.doremilabs.com/Certificates/%1xxx/imb-%2.dcicerts.zip",
100                         serial.substr(0, 3), serial
101                         ),
102                 String::compose ("imb-%1.cert.sha256.pem", serial),
103                 boost::bind (&DownloadCertificatePanel::load, this, _1)
104                 );
105         }
106
107         if (error) {
108                 errors.push_back (error.get ());
109                 error = get_from_zip_url (
110                 String::compose (
111                         "ftp://service:t3chn1c1an@ftp.doremilabs.com/Certificates/%1xxx/ims-%2.dcicerts.zip",
112                         serial.substr(0, 3), serial
113                         ),
114                 String::compose ("ims-%1.cert.sha256.pem", serial),
115                 boost::bind (&DownloadCertificatePanel::load, this, _1)
116                 );
117         }
118
119         if (error) {
120                 errors.push_back (error.get ());
121                 message->SetLabel (wxT (""));
122
123                 SafeStringStream s;
124                 BOOST_FOREACH (string e, errors) {
125                         s << e << "\n";
126                 }
127
128                 error_dialog (this, std_to_wx (s.str ()));
129         } else {
130                 message->SetLabel (_("Certificate downloaded"));
131                 _dialog->setup_sensitivity ();
132         }
133 }
134
135 bool
136 DoremiCertificatePanel::ready_to_download () const
137 {
138         return !_serial->IsEmpty ();
139 }