f7393f09d448abd868a1b7bd50a5379a23049568
[platform/upstream/gpgme.git] / lang / qt / src / qgpgmebackend.cpp
1 /*
2     qgpgmebackend.cpp
3
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
8
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.
13
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.
18
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
22
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
32     your version.
33 */
34
35 #ifdef HAVE_CONFIG_H
36  #include "config.h"
37 #endif
38
39 #include "qgpgmebackend.h"
40
41
42 #include "error.h"
43 #include "engineinfo.h"
44
45 #include "protocol_p.h"
46
47 #include <QFile>
48 #include <QString>
49
50 const char QGpgME::QGpgMEBackend::OpenPGP[] = "OpenPGP";
51 const char QGpgME::QGpgMEBackend::SMIME[] = "SMIME";
52
53
54 QGpgME::QGpgMEBackend::QGpgMEBackend()
55     : mCryptoConfig(nullptr),
56       mOpenPGPProtocol(nullptr),
57       mSMIMEProtocol(nullptr)
58 {
59     GpgME::initializeLibrary();
60 }
61
62 QGpgME::QGpgMEBackend::~QGpgMEBackend()
63 {
64     delete mCryptoConfig; mCryptoConfig = nullptr;
65     delete mOpenPGPProtocol; mOpenPGPProtocol = nullptr;
66     delete mSMIMEProtocol; mSMIMEProtocol = nullptr;
67 }
68
69 QString QGpgME::QGpgMEBackend::name() const
70 {
71     return QStringLiteral("gpgme");
72 }
73
74 QString QGpgME::QGpgMEBackend::displayName() const
75 {
76     return QStringLiteral("GpgME");
77 }
78
79 QGpgME::CryptoConfig *QGpgME::QGpgMEBackend::config() const
80 {
81     if (!mCryptoConfig) {
82         if (GpgME::hasFeature(GpgME::GpgConfEngineFeature, 0)) {
83             mCryptoConfig = new QGpgMENewCryptoConfig;
84         }
85     }
86     return mCryptoConfig;
87 }
88
89 static bool check(GpgME::Protocol proto, QString *reason)
90 {
91     if (!GpgME::checkEngine(proto)) {
92         return true;
93     }
94     if (!reason) {
95         return false;
96     }
97     // error, check why:
98 #if 0
99 Port away from localised string or delete.
100     const GpgME::EngineInfo ei = GpgME::engineInfo(proto);
101     if (ei.isNull()) {
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()));
109     else {
110         *reason = i18n("Unknown problem with engine for protocol %1.", proto == GpgME::CMS ? QLatin1String("S/MIME") : QLatin1String("OpenPGP"));
111     }
112 #endif
113     return false;
114 }
115
116 bool QGpgME::QGpgMEBackend::checkForOpenPGP(QString *reason) const
117 {
118     return check(GpgME::OpenPGP, reason);
119 }
120
121 bool QGpgME::QGpgMEBackend::checkForSMIME(QString *reason) const
122 {
123     return check(GpgME::CMS, reason);
124 }
125
126 bool QGpgME::QGpgMEBackend::checkForProtocol(const char *name, QString *reason) const
127 {
128     if (qstricmp(name, OpenPGP) == 0) {
129         return check(GpgME::OpenPGP, reason);
130     }
131     if (qstricmp(name, SMIME) == 0) {
132         return check(GpgME::CMS, reason);
133     }
134     if (reason) {
135         *reason = QStringLiteral("Unsupported protocol \"%1\"").arg(QLatin1String(name));
136     }
137     return false;
138 }
139
140 QGpgME::Protocol *QGpgME::QGpgMEBackend::openpgp() const
141 {
142     if (!mOpenPGPProtocol)
143         if (checkForOpenPGP()) {
144             mOpenPGPProtocol = new ::Protocol(GpgME::OpenPGP);
145         }
146     return mOpenPGPProtocol;
147 }
148
149 QGpgME::Protocol *QGpgME::QGpgMEBackend::smime() const
150 {
151     if (!mSMIMEProtocol)
152         if (checkForSMIME()) {
153             mSMIMEProtocol = new ::Protocol(GpgME::CMS);
154         }
155     return mSMIMEProtocol;
156 }
157
158 QGpgME::Protocol *QGpgME::QGpgMEBackend::protocol(const char *name) const
159 {
160     if (qstricmp(name, OpenPGP) == 0) {
161         return openpgp();
162     }
163     if (qstricmp(name, SMIME) == 0) {
164         return smime();
165     }
166     return nullptr;
167 }
168
169 bool QGpgME::QGpgMEBackend::supportsProtocol(const char *name) const
170 {
171     return qstricmp(name, OpenPGP) == 0 || qstricmp(name, SMIME) == 0;
172 }
173
174 const char *QGpgME::QGpgMEBackend::enumerateProtocols(int i) const
175 {
176     switch (i) {
177     case 0: return OpenPGP;
178     case 1: return SMIME;
179     default: return nullptr;
180     }
181 }
182
183 static QGpgME::QGpgMEBackend *gpgmeBackend;
184
185 QGpgME::CryptoConfig *QGpgME::cryptoConfig()
186 {
187     if (!gpgmeBackend) {
188         gpgmeBackend = new QGpgME::QGpgMEBackend();
189     }
190     return gpgmeBackend->config();
191
192 }
193
194 QGpgME::Protocol *QGpgME::openpgp()
195 {
196     if (!gpgmeBackend) {
197         gpgmeBackend = new QGpgME::QGpgMEBackend();
198     }
199     return gpgmeBackend->openpgp();
200 }
201
202 QGpgME::Protocol *QGpgME::smime()
203 {
204     if (!gpgmeBackend) {
205         gpgmeBackend = new QGpgME::QGpgMEBackend();
206     }
207     return gpgmeBackend->smime();
208 }