4 This file is part of qgpgme, the Qt API binding for gpgme
5 Copyright (c) 2004,2005 Klarälvdalens Datakonsult AB
6 Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik
7 Software engineering by Intevation GmbH
9 QGpgME is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
14 QGpgME is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 In addition, as a special exception, the copyright holders give
24 permission to link the code of this program with any edition of
25 the Qt library by Trolltech AS, Norway (or with modified versions
26 of Qt that use the same license as Qt), and distribute linked
27 combinations including the two. You must obey the GNU General
28 Public License in all respects for all of the code used other than
29 Qt. If you modify this file, you may extend this exception to
30 your version of the file, but you are not obligated to do so. If
31 you do not wish to do so, delete this exception statement from
39 #include "qgpgmebackend.h"
43 #include "engineinfo.h"
45 #include "protocol_p.h"
50 const char QGpgME::QGpgMEBackend::OpenPGP[] = "OpenPGP";
51 const char QGpgME::QGpgMEBackend::SMIME[] = "SMIME";
54 QGpgME::QGpgMEBackend::QGpgMEBackend()
55 : mCryptoConfig(nullptr),
56 mOpenPGPProtocol(nullptr),
57 mSMIMEProtocol(nullptr)
59 GpgME::initializeLibrary();
62 QGpgME::QGpgMEBackend::~QGpgMEBackend()
64 delete mCryptoConfig; mCryptoConfig = nullptr;
65 delete mOpenPGPProtocol; mOpenPGPProtocol = nullptr;
66 delete mSMIMEProtocol; mSMIMEProtocol = nullptr;
69 QString QGpgME::QGpgMEBackend::name() const
71 return QStringLiteral("gpgme");
74 QString QGpgME::QGpgMEBackend::displayName() const
76 return QStringLiteral("GpgME");
79 QGpgME::CryptoConfig *QGpgME::QGpgMEBackend::config() const
82 if (GpgME::hasFeature(GpgME::GpgConfEngineFeature, 0)) {
83 mCryptoConfig = new QGpgMENewCryptoConfig;
89 static bool check(GpgME::Protocol proto, QString *reason)
91 if (!GpgME::checkEngine(proto)) {
99 Port away from localised string or delete.
100 const GpgME::EngineInfo ei = GpgME::engineInfo(proto);
102 *reason = i18n("GPGME was compiled without support for %1.", proto == GpgME::CMS ? QLatin1String("S/MIME") : QLatin1String("OpenPGP"));
103 } else if (ei.fileName() && !ei.version()) {
104 *reason = i18n("Engine %1 is not installed properly.", QFile::decodeName(ei.fileName()));
105 } else if (ei.fileName() && ei.version() && ei.requiredVersion())
106 *reason = i18n("Engine %1 version %2 installed, "
107 "but at least version %3 is required.",
108 QFile::decodeName(ei.fileName()), QLatin1String(ei.version()), QLatin1String(ei.requiredVersion()));
110 *reason = i18n("Unknown problem with engine for protocol %1.", proto == GpgME::CMS ? QLatin1String("S/MIME") : QLatin1String("OpenPGP"));
116 bool QGpgME::QGpgMEBackend::checkForOpenPGP(QString *reason) const
118 return check(GpgME::OpenPGP, reason);
121 bool QGpgME::QGpgMEBackend::checkForSMIME(QString *reason) const
123 return check(GpgME::CMS, reason);
126 bool QGpgME::QGpgMEBackend::checkForProtocol(const char *name, QString *reason) const
128 if (qstricmp(name, OpenPGP) == 0) {
129 return check(GpgME::OpenPGP, reason);
131 if (qstricmp(name, SMIME) == 0) {
132 return check(GpgME::CMS, reason);
135 *reason = QStringLiteral("Unsupported protocol \"%1\"").arg(QLatin1String(name));
140 QGpgME::Protocol *QGpgME::QGpgMEBackend::openpgp() const
142 if (!mOpenPGPProtocol)
143 if (checkForOpenPGP()) {
144 mOpenPGPProtocol = new ::Protocol(GpgME::OpenPGP);
146 return mOpenPGPProtocol;
149 QGpgME::Protocol *QGpgME::QGpgMEBackend::smime() const
152 if (checkForSMIME()) {
153 mSMIMEProtocol = new ::Protocol(GpgME::CMS);
155 return mSMIMEProtocol;
158 QGpgME::Protocol *QGpgME::QGpgMEBackend::protocol(const char *name) const
160 if (qstricmp(name, OpenPGP) == 0) {
163 if (qstricmp(name, SMIME) == 0) {
169 bool QGpgME::QGpgMEBackend::supportsProtocol(const char *name) const
171 return qstricmp(name, OpenPGP) == 0 || qstricmp(name, SMIME) == 0;
174 const char *QGpgME::QGpgMEBackend::enumerateProtocols(int i) const
177 case 0: return OpenPGP;
178 case 1: return SMIME;
179 default: return nullptr;
183 static QGpgME::QGpgMEBackend *gpgmeBackend;
185 QGpgME::CryptoConfig *QGpgME::cryptoConfig()
188 gpgmeBackend = new QGpgME::QGpgMEBackend();
190 return gpgmeBackend->config();
194 QGpgME::Protocol *QGpgME::openpgp()
197 gpgmeBackend = new QGpgME::QGpgMEBackend();
199 return gpgmeBackend->openpgp();
202 QGpgME::Protocol *QGpgME::smime()
205 gpgmeBackend = new QGpgME::QGpgMEBackend();
207 return gpgmeBackend->smime();