c67cc46f28803ed66c14529b68189b38ec0455ba
[dcpomatic.git] / src / wx / qube_certificate_panel.cc
1 /*
2     Copyright (C) 2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "qube_certificate_panel.h"
22 #include "download_certificate_dialog.h"
23 #include "wx_util.h"
24 #include "lib/internet.h"
25 #include "lib/compose.hpp"
26 #include "lib/config.h"
27 #include <boost/algorithm/string/predicate.hpp>
28
29 using std::string;
30 using std::list;
31 using boost::optional;
32 #if BOOST_VERSION >= 106100
33 using namespace boost::placeholders;
34 #endif
35
36 static string const base = "ftp://certificates.qubecinema.com/";
37
38 QubeCertificatePanel::QubeCertificatePanel (DownloadCertificateDialog* dialog, string type)
39         : DownloadCertificatePanel (dialog)
40         , _type (type)
41 {
42
43 }
44
45 void
46 QubeCertificatePanel::do_download ()
47 {
48         list<string> files = ls_url(String::compose("%1SMPTE-%2/", base, _type));
49         if (files.empty()) {
50                 error_dialog (this, _("Could not read certificates from Qube server."));
51                 return;
52         }
53
54         string const serial = wx_to_std(_serial->GetValue());
55         optional<string> name;
56         for (auto i: files) {
57                 if (boost::algorithm::starts_with(i, String::compose("%1-%2-", _type, serial))) {
58                         name = i;
59                         break;
60                 }
61         }
62
63         if (!name) {
64                 _dialog->message()->SetLabel(wxT(""));
65                 error_dialog (this, wxString::Format(_("Could not find serial number %s"), std_to_wx(serial).data()));
66                 return;
67         }
68
69         optional<string> error = get_from_url (String::compose("%1SMPTE-%2/%3", base, _type, *name), true, false, boost::bind(&DownloadCertificatePanel::load_certificate, this, _1));
70
71         if (error) {
72                 _dialog->message()->SetLabel(wxT(""));
73                 error_dialog (this, std_to_wx(*error));
74         } else {
75                 _dialog->message()->SetLabel (_("Certificate downloaded"));
76                 _dialog->setup_sensitivity ();
77         }
78 }
79
80 wxString
81 QubeCertificatePanel::name () const
82 {
83         return _("Qube") + " " + std_to_wx(_type);
84 }