summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRolf Eike Beer <kde@opensource.sf-tec.de>2016-10-19 20:08:45 (GMT)
committerRolf Eike Beer <kde@opensource.sf-tec.de>2016-10-19 20:08:45 (GMT)
commitd4916218686561997c29b75499a1cc20113750d8 (patch)
tree9ffd3c71ab692ef6d3240bfd23fa39069973da71
parentf33f8c759a13da6ab663bd9fd1baef17a238836d (diff)
use --list-config to request certain information from GnuPG
This is available since GnuPG 1.3.5, so it's safe to use. This makes parsing certain output much more reliable.
-rw-r--r--core/KGpgGroupNode.cpp25
-rw-r--r--core/KGpgGroupNode.h11
-rw-r--r--core/KGpgRootNode.cpp10
-rw-r--r--gpgproc.cpp51
-rw-r--r--gpgproc.h18
-rw-r--r--model/kgpgitemmodel.cpp5
6 files changed, 56 insertions, 64 deletions
diff --git a/core/KGpgGroupNode.cpp b/core/KGpgGroupNode.cpp
index b33e689..7ac2f80 100644
--- a/core/KGpgGroupNode.cpp
+++ b/core/KGpgGroupNode.cpp
@@ -253,28 +253,3 @@ KGpgGroupNode::remove()
conffile.resize(0);
t << lines.join(QLatin1String("\n")) + QLatin1Char('\n');
}
-
-QStringList
-KGpgGroupNode::readGroups()
-{
- QStringList groups;
- QFile qfile(KGpgSettings::gpgConfigPath());
-
- if (!qfile.exists() || !qfile.open(QIODevice::ReadOnly))
- return groups;
-
- QTextStream t(&qfile);
-
- while (!t.atEnd()) {
- QString line = t.readLine().simplified().section(QLatin1Char('#'), 0, 0);
- if (!KGpgGroupNodePrivate::groupPattern().exactMatch(line))
- continue;
-
- // remove the "group " at the start
- line.remove(0, 6);
- // transform it in a simple space separated list
- groups.append(line.replace(QLatin1Char('='), QLatin1Char(' ')).simplified());
- }
-
- return groups;
-}
diff --git a/core/KGpgGroupNode.h b/core/KGpgGroupNode.h
index 5aca837..99f55ee 100644
--- a/core/KGpgGroupNode.h
+++ b/core/KGpgGroupNode.h
@@ -1,4 +1,4 @@
-/* Copyright 2008,2009,2010,2012 Rolf Eike Beer <kde@opensource.sf-tec.de>
+/* Copyright 2008,2009,2010,2012,2016 Rolf Eike Beer <kde@opensource.sf-tec.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@@ -70,15 +70,6 @@ public:
* Remove this group from the GnuPG config file
*/
void remove();
-
- /**
- * @brief get all groups from GnuPG config file
- * @return list of groups names and their keys
- *
- * The strings are themself space separated list. The first entry is the
- * group name, the others are the keys inside
- */
- static QStringList readGroups();
};
#endif /* KGPGGROUPNODE_H */
diff --git a/core/KGpgRootNode.cpp b/core/KGpgRootNode.cpp
index cd464d8..4a1cc7a 100644
--- a/core/KGpgRootNode.cpp
+++ b/core/KGpgRootNode.cpp
@@ -1,4 +1,4 @@
-/* Copyright 2008,2009,2010,2012,2013 Rolf Eike Beer <kde@opensource.sf-tec.de>
+/* Copyright 2008,2009,2010,2012,2013,2016 Rolf Eike Beer <kde@opensource.sf-tec.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@@ -56,9 +56,11 @@ void
KGpgRootNode::addGroups(const QStringList &groups)
{
foreach (const QString &group, groups) {
- QStringList members = group.split(QLatin1Char(' '));
- const QString groupName = members.takeFirst();
- new KGpgGroupNode(this, groupName, members);
+ const QStringList parts = group.split(QLatin1Char(':'));
+ if (parts.count() < 2)
+ continue;
+ const QString groupName = parts.first();
+ new KGpgGroupNode(this, groupName, parts.at(1).split(QLatin1Char(';')));
}
}
diff --git a/gpgproc.cpp b/gpgproc.cpp
index 6fba481..dd38434 100644
--- a/gpgproc.cpp
+++ b/gpgproc.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2007,2008,2009,2010,2011,2012,2013,2014 Rolf Eike Beer <kde@opensource.sf-tec.de>
+ * Copyright (C) 2007,2008,2009,2010,2011,2012,2013,2014,2016 Rolf Eike Beer <kde@opensource.sf-tec.de>
*/
/***************************************************************************
@@ -316,35 +316,23 @@ int GPGProc::gpgVersion(const QString &vstr)
QString GPGProc::gpgVersionString(const QString &binary)
{
- GPGProc process(0, binary);
- process << QLatin1String( "--version" );
- process.start();
- process.waitForFinished(-1);
+ const QStringList vlist = getGgpParsedConfig(binary, "version");
- if (process.exitCode() == 255)
+ if (vlist.empty())
return QString();
- QString line;
- if (process.readln(line) != -1)
- return line.simplified().section(QLatin1Char( ' ' ), -1);
- else
- return QString();
+ return vlist.first().split(QLatin1Char(':')).first();
}
QStringList
GPGProc::getGpgPubkeyAlgorithms(const QString &binary)
{
- QStringList ret;
+ QStringList ret = getGgpParsedConfig(binary, "pubkeyname");
- const auto algorithms = getGpgStatusLine(binary, QLatin1String("Pubkey:")).split(QLatin1Char(','));
- for (const QString &s : algorithms) {
- QString t = s.trimmed();
- if (t == QLatin1String("?"))
- continue;
- ret << t;
- }
+ if (ret.isEmpty())
+ return ret;
- return ret;
+ return ret.first().split(QLatin1Char(':')).first().split(QLatin1Char(';'));
}
QString GPGProc::getGpgStartupError(const QString &binary)
@@ -366,6 +354,29 @@ QString GPGProc::getGpgStartupError(const QString &binary)
return result;
}
+QStringList GPGProc::getGgpParsedConfig(const QString &binary, const QByteArray &key)
+{
+ GPGProc process(0, binary);
+ process << QLatin1String("--list-config") << QLatin1String("--with-colons");
+ process.start();
+ process.waitForFinished(-1);
+
+ QStringList result;
+ QByteArray filter = "cfg:";
+ if (!key.isEmpty())
+ filter += key + ':';
+
+ while (process.hasLineStandardOutput()) {
+ QByteArray tmp;
+ process.readLineStandardOutput(&tmp);
+
+ if (tmp.startsWith(filter))
+ result << QString::fromUtf8(tmp.mid(filter.length()));
+ }
+
+ return result;
+}
+
QString GPGProc::getGpgHome(const QString &binary)
{
// First try: if environment is set GnuPG will use that directory
diff --git a/gpgproc.h b/gpgproc.h
index 2c232fb..cd751a2 100644
--- a/gpgproc.h
+++ b/gpgproc.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2007,2008,2009,2010,2011,2012,2013,2014 Rolf Eike Beer <kde@opensource.sf-tec.de>
+ * Copyright (C) 2007,2008,2009,2010,2011,2012,2013,2014,2016 Rolf Eike Beer <kde@opensource.sf-tec.de>
*/
/***************************************************************************
@@ -13,10 +13,11 @@
#ifndef GPGPROC_H
#define GPGPROC_H
-#include <QString>
-
#include "klinebufferedprocess.h"
+#include <QByteArray>
+#include <QString>
+
class QStringList;
/**
@@ -154,6 +155,17 @@ public:
* @return the error message GnuPG gave out (if any)
*/
static QString getGpgStartupError(const QString &binary);
+
+ /**
+ * @brief run GnuPG and let it return it's config output
+ * @param binary the GnuPG binary to run
+ * @param key if only fields of a given type should be returned
+ * @return all matching fields
+ *
+ * In case a key is given the key is already removed from the
+ * returned lines.
+ */
+ static QStringList getGgpParsedConfig(const QString &binary, const QByteArray &key = QByteArray());
signals:
/**
* Emitted when the process is ready for reading.
diff --git a/model/kgpgitemmodel.cpp b/model/kgpgitemmodel.cpp
index 88d7eb0..36d8d3c 100644
--- a/model/kgpgitemmodel.cpp
+++ b/model/kgpgitemmodel.cpp
@@ -1,4 +1,4 @@
-/* Copyright 2008,2009,2010,2011,2012,2013 Rolf Eike Beer <kde@opensource.sf-tec.de>
+/* Copyright 2008,2009,2010,2011,2012,2013,2016 Rolf Eike Beer <kde@opensource.sf-tec.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@@ -18,6 +18,7 @@
*/
#include "kgpgitemmodel.h"
+#include "gpgproc.h"
#include "kgpgsettings.h"
#include "core/convert.h"
#include "core/images.h"
@@ -503,7 +504,7 @@ KGpgItemModel::refreshGroups()
endRemoveRows();
}
- const QStringList groups = KGpgGroupNode::readGroups();
+ const QStringList groups = GPGProc::getGgpParsedConfig(KGpgSettings::gpgBinaryPath(), "group");
if (groups.isEmpty())
return;