Remove insistence on 6-figure Doremi serials (fixes #768).
[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         message->SetLabel (_("Downloading certificate"));
54
55         /* Hack: without this the SetLabel() above has no visible effect */
56         wxMilliSleep (200);
57
58         signal_manager->when_idle (boost::bind (&DoremiCertificatePanel::finish_download, this, wx_to_std (_serial->GetValue ()), message));
59 }
60
61 void
62 DoremiCertificatePanel::finish_download (string serial, wxStaticText* message)
63 {
64         /* Try dcp2000, imb and ims prefixes (see mantis #375) */
65
66         list<string> errors;
67
68         optional<string> error = get_from_zip_url (
69                 String::compose (
70                         "ftp://service:t3chn1c1an@ftp.doremilabs.com/Certificates/%1xxx/dcp2000-%2.dcicerts.zip",
71                         serial.substr(0, 3), serial
72                         ),
73                 String::compose ("dcp2000-%1.cert.sha256.pem", serial),
74                 boost::bind (&DownloadCertificatePanel::load, this, _1)
75                 );
76
77         if (error) {
78                 errors.push_back (error.get ());
79                 error = get_from_zip_url (
80                 String::compose (
81                         "ftp://service:t3chn1c1an@ftp.doremilabs.com/Certificates/%1xxx/dcp2000-%2.certs.zip",
82                         serial.substr(0, 3), serial
83                         ),
84                 String::compose ("dcp2000-%1.cert.sha256.pem", serial),
85                 boost::bind (&DownloadCertificatePanel::load, this, _1)
86                 );
87         }
88
89         if (error) {
90                 errors.push_back (error.get ());
91                 error = get_from_zip_url (
92                 String::compose (
93                         "ftp://service:t3chn1c1an@ftp.doremilabs.com/Certificates/%1xxx/imb-%2.dcicerts.zip",
94                         serial.substr(0, 3), serial
95                         ),
96                 String::compose ("imb-%1.cert.sha256.pem", serial),
97                 boost::bind (&DownloadCertificatePanel::load, this, _1)
98                 );
99         }
100
101         if (error) {
102                 errors.push_back (error.get ());
103                 error = get_from_zip_url (
104                 String::compose (
105                         "ftp://service:t3chn1c1an@ftp.doremilabs.com/Certificates/%1xxx/ims-%2.dcicerts.zip",
106                         serial.substr(0, 3), serial
107                         ),
108                 String::compose ("ims-%1.cert.sha256.pem", serial),
109                 boost::bind (&DownloadCertificatePanel::load, this, _1)
110                 );
111         }
112
113         if (error) {
114                 errors.push_back (error.get ());
115                 message->SetLabel (wxT (""));
116
117                 SafeStringStream s;
118                 BOOST_FOREACH (string e, errors) {
119                         s << e << "\n";
120                 }
121
122                 error_dialog (this, std_to_wx (s.str ()));
123         } else {
124                 message->SetLabel (_("Certificate downloaded"));
125                 _dialog->setup_sensitivity ();
126         }
127 }
128
129 bool
130 DoremiCertificatePanel::ready_to_download () const
131 {
132         return !_serial->IsEmpty ();
133 }