SVN_SILENT made messages (.desktop file) - always resolve ours
[kdepim.git] / kleopatra / utils / gnupg-helper.cpp
blobc7ffdbde6f0e5bf8e8b5992e4bfda10bdc78d433
1 /* -*- mode: c++; c-basic-offset:4 -*-
2 utils/gnupg-helper.cpp
4 This file is part of Kleopatra, the KDE keymanager
5 Copyright (c) 2008 Klarälvdalens Datakonsult AB
7 Kleopatra is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 Kleopatra is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 In addition, as a special exception, the copyright holders give
22 permission to link the code of this program with any edition of
23 the Qt library by Trolltech AS, Norway (or with modified versions
24 of Qt that use the same license as Qt), and distribute linked
25 combinations including the two. You must obey the GNU General
26 Public License in all respects for all of the code used other than
27 Qt. If you modify this file, you may extend this exception to
28 your version of the file, but you are not obligated to do so. If
29 you do not wish to do so, delete this exception statement from
30 your version.
33 #include <config-kleopatra.h>
35 #include "gnupg-helper.h"
37 #include "utils/hex.h"
39 #include <gpgme++/engineinfo.h>
41 #include "kleopatra_debug.h"
43 #include <QDir>
44 #include <QFile>
45 #include <QString>
46 #include <QProcess>
47 #include <QByteArray>
48 #include <QStandardPaths>
49 #include <gpg-error.h>
51 #ifdef Q_OS_WIN
52 #include "gnupg-registry.h"
53 #endif // Q_OS_WIN
55 QString Kleo::gnupgHomeDirectory()
57 #ifdef Q_OS_WIN
58 return QFile::decodeName(default_homedir());
59 #else
60 const QByteArray gnupgHome = qgetenv("GNUPGHOME");
61 if (!gnupgHome.isEmpty()) {
62 return QFile::decodeName(gnupgHome);
63 } else {
64 return QDir::homePath() + QLatin1String("/.gnupg");
66 #endif
69 int Kleo::makeGnuPGError(int code)
71 return gpg_error(static_cast<gpg_err_code_t>(code));
74 static QString findGpgExe(GpgME::Engine engine, const QString &exe)
76 const GpgME::EngineInfo info = GpgME::engineInfo(engine);
77 return info.fileName() ? QFile::decodeName(info.fileName()) : QStandardPaths::findExecutable(exe);
80 QString Kleo::gpgConfPath()
82 return findGpgExe(GpgME::GpgConfEngine, QStringLiteral("gpgconf"));
85 QString Kleo::gpgSmPath()
87 return findGpgExe(GpgME::GpgSMEngine, QStringLiteral("gpgsm"));
90 QString Kleo::gpgPath()
92 return findGpgExe(GpgME::GpgEngine, QStringLiteral("gpg"));
95 QStringList Kleo::gnupgFileBlacklist()
97 return QStringList()
98 << QStringLiteral("dirmngr-cache.d")
99 << QStringLiteral("S.uiserver")
100 << QStringLiteral("S.gpg-agent")
101 << QStringLiteral("random_seed")
102 << QStringLiteral("*~")
103 << QStringLiteral("*.bak")
104 << QStringLiteral("*.lock")
105 << QStringLiteral("*.tmp")
106 << QStringLiteral("reader_*.status")
110 QString Kleo::gpg4winInstallPath()
112 return gpgConfListDir("bindir");
115 QString Kleo::gpgConfListDir(const char *which)
117 if (!which || !*which) {
118 return QString();
120 const QString gpgConfPath = Kleo::gpgConfPath();
121 if (gpgConfPath.isEmpty()) {
122 return QString();
124 QProcess gpgConf;
125 qCDebug(KLEOPATRA_LOG) << "gpgConfListDir: starting " << qPrintable(gpgConfPath) << " --list-dirs";
126 gpgConf.start(gpgConfPath, QStringList() << QStringLiteral("--list-dirs"));
127 if (!gpgConf.waitForFinished()) {
128 qCDebug(KLEOPATRA_LOG) << "gpgConfListDir(): failed to execute gpgconf: " << qPrintable(gpgConf.errorString());
129 qCDebug(KLEOPATRA_LOG) << "output was:" << endl << gpgConf.readAllStandardError().constData();
130 return QString();
132 const QList<QByteArray> lines = gpgConf.readAllStandardOutput().split('\n');
133 Q_FOREACH (const QByteArray &line, lines)
134 if (line.startsWith(which) && line[qstrlen(which)] == ':') {
135 const int begin = qstrlen(which) + 1;
136 int end = line.size();
137 while (end && (line[end - 1] == '\n' || line[end - 1] == '\r')) {
138 --end;
140 const QString result = QDir::fromNativeSeparators(QFile::decodeName(hexdecode(line.mid(begin, end - begin))));
141 qCDebug(KLEOPATRA_LOG) << "gpgConfListDir: found " << qPrintable(result)
142 << " for '" << which << "'entry";
143 return result;
145 qCDebug(KLEOPATRA_LOG) << "gpgConfListDir(): didn't find '" << which << "'"
146 << "entry in output:" << endl << gpgConf.readAllStandardError().constData();
147 return QString();