Rearrange cerficate download UI a bit.
[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 <iostream>
30
31 using std::string;
32 using std::cout;
33 using boost::function;
34 using boost::optional;
35
36 DoremiCertificatePanel::DoremiCertificatePanel (wxWindow* parent, DownloadCertificateDialog* dialog)
37         : DownloadCertificatePanel (parent, dialog)
38 {
39         add_label_to_sizer (_table, this, _("Server serial number"), true);
40         _serial = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, wxSize (300, -1));
41         _table->Add (_serial, 1, wxEXPAND);
42
43         _serial->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&DownloadCertificateDialog::setup_sensitivity, _dialog));
44
45         layout ();
46 }
47
48 void
49 DoremiCertificatePanel::download (wxStaticText* message)
50 {
51         string const serial = wx_to_std (_serial->GetValue ());
52         if (serial.length() != 6) {
53                 error_dialog (this, _("Doremi serial numbers must have 6 digits"));
54                 return;
55         }
56
57         message->SetLabel (_("Downloading certificate"));
58
59         /* Hack: without this the SetLabel() above has no visible effect */
60         wxMilliSleep (200);
61
62         signal_manager->when_idle (boost::bind (&DoremiCertificatePanel::finish_download, this, serial, message));
63 }
64
65 void
66 DoremiCertificatePanel::finish_download (string serial, wxStaticText* message)
67 {
68         /* Try dcp2000, imb and ims prefixes (see mantis #375) */
69
70         optional<string> error = get_from_zip_url (
71                 String::compose (
72                         "ftp://service:t3chn1c1an@ftp.doremilabs.com/Certificates/%1xxx/dcp2000-%2.dcicerts.zip",
73                         serial.substr(0, 3), serial
74                         ),
75                 String::compose ("dcp2000-%1.cert.sha256.pem", serial),
76                 boost::bind (&DownloadCertificatePanel::load, this, _1)
77                 );
78
79         if (error) {
80                 error = get_from_zip_url (
81                 String::compose (
82                         "ftp://service:t3chn1c1an@ftp.doremilabs.com/Certificates/%1xxx/imb-%2.dcicerts.zip",
83                         serial.substr(0, 3), serial
84                         ),
85                 String::compose ("imb-%1.cert.sha256.pem", serial),
86                 boost::bind (&DownloadCertificatePanel::load, this, _1)
87                 );
88         }
89
90         if (error) {
91                 error = get_from_zip_url (
92                 String::compose (
93                         "ftp://service:t3chn1c1an@ftp.doremilabs.com/Certificates/%1xxx/ims-%2.dcicerts.zip",
94                         serial.substr(0, 3), serial
95                         ),
96                 String::compose ("ims-%1.cert.sha256.pem", serial),
97                 boost::bind (&DownloadCertificatePanel::load, this, _1)
98                 );
99         }
100
101         if (error) {
102                 message->SetLabel (wxT (""));
103                 error_dialog (this, std_to_wx (error.get ()));
104         } else {
105                 message->SetLabel (_("Certificate downloaded"));
106         }
107 }
108
109 bool
110 DoremiCertificatePanel::ready_to_download () const
111 {
112         return !_serial->IsEmpty ();
113 }