ffb2a0f99c05f07917cbadd28ac8df970ea48481
[dcpomatic.git] / src / wx / doremi_certificate_dialog.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 <curl/curl.h>
21 #include <zip.h>
22 #include "lib/compose.hpp"
23 #include "lib/util.h"
24 #include "lib/signal_manager.h"
25 #include "lib/internet.h"
26 #include "doremi_certificate_dialog.h"
27 #include "wx_util.h"
28 #include <iostream>
29
30 using std::string;
31 using std::cout;
32 using boost::function;
33 using boost::optional;
34
35 DoremiCertificateDialog::DoremiCertificateDialog (wxWindow* parent, function<void (boost::filesystem::path)> load)
36         : DownloadCertificateDialog (parent, load)
37 {
38         add (_("Server serial number"), true);
39         _serial = add (new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, wxSize (300, -1)));
40
41         _serial->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&DoremiCertificateDialog::set_sensitivity, this));
42
43         add_common_widgets ();
44 }
45
46 void
47 DoremiCertificateDialog::download ()
48 {
49         string const serial = wx_to_std (_serial->GetValue ());
50         if (serial.length() != 6) {
51                 error_dialog (this, _("Doremi serial numbers must have 6 digits"));
52                 return;
53         }
54
55         downloaded (false);
56         _message->SetLabel (_("Downloading certificate"));
57
58         /* Hack: without this the SetLabel() above has no visible effect */
59         wxMilliSleep (200);
60
61         signal_manager->when_idle (boost::bind (&DoremiCertificateDialog::finish_download, this, serial));
62 }
63
64 void
65 DoremiCertificateDialog::finish_download (string serial)
66 {
67         /* Try dcp2000, imb and ims prefixes (see mantis #375) */
68
69         optional<string> error = get_from_zip_url (
70                 String::compose (
71                         "ftp://service:t3chn1c1an@ftp.doremilabs.com/Certificates/%1xxx/dcp2000-%2.dcicerts.zip",
72                         serial.substr(0, 3), serial
73                         ),
74                 String::compose ("dcp2000-%1.cert.sha256.pem", serial),
75                 _load
76                 );
77
78         if (error) {
79                 error = get_from_zip_url (
80                 String::compose (
81                         "ftp://service:t3chn1c1an@ftp.doremilabs.com/Certificates/%1xxx/imb-%2.dcicerts.zip",
82                         serial.substr(0, 3), serial
83                         ),
84                 String::compose ("imb-%1.cert.sha256.pem", serial),
85                 _load
86                 );
87         }
88
89         if (error) {
90                 error = get_from_zip_url (
91                 String::compose (
92                         "ftp://service:t3chn1c1an@ftp.doremilabs.com/Certificates/%1xxx/ims-%2.dcicerts.zip",
93                         serial.substr(0, 3), serial
94                         ),
95                 String::compose ("ims-%1.cert.sha256.pem", serial),
96                 _load
97                 );
98         }
99
100         if (error) {
101                 _message->SetLabel (wxT (""));
102                 error_dialog (this, std_to_wx (error.get ()));
103         } else {
104                 _message->SetLabel (_("Certificate downloaded"));
105                 downloaded (true);
106         }
107 }
108
109 void
110 DoremiCertificateDialog::set_sensitivity ()
111 {
112         _download->Enable (!_serial->IsEmpty ());
113 }