Page MenuHome GnuPG

No OneTemporary

diff --git a/autotests/abstractkeylistmodeltest.cpp b/autotests/abstractkeylistmodeltest.cpp
index 7b44617b..46b2ca8f 100644
--- a/autotests/abstractkeylistmodeltest.cpp
+++ b/autotests/abstractkeylistmodeltest.cpp
@@ -1,207 +1,206 @@
/*
autotests/abstractkeylistmodeltest.cpp
This file is part of libkleopatra's test suite.
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "abstractkeylistmodeltest.h"
#include "kleo/keygroup.h"
#include "models/keylistmodel.h"
#include <gpgme++/key.h>
#include <gpgme.h>
#include <QTest>
using namespace Kleo;
using namespace GpgME;
namespace
{
Key createTestKey(const char *uid)
{
static int count = 0;
count++;
gpgme_key_t key;
gpgme_key_from_uid(&key, uid);
const QByteArray fingerprint = QByteArray::number(count, 16).rightJustified(40, '0');
key->fpr = strdup(fingerprint.constData());
return Key(key, false);
}
KeyGroup createGroup(const QString &name,
const std::vector<Key> &keys = std::vector<Key>(),
- KeyGroup::Source source = KeyGroup::UnknownSource,
+ KeyGroup::Source source = KeyGroup::ApplicationConfig,
const QString &configName = QString())
{
- static KeyGroup::Id nextGroupId = 0;
-
- KeyGroup g(nextGroupId, name, keys, source);
- ++nextGroupId;
- g.setConfigName(configName);
+ const KeyGroup::Id groupId = (source == KeyGroup::ApplicationConfig) ?
+ (configName.isEmpty() ? name : configName) :
+ name;
+ KeyGroup g(groupId, name, keys, source);
return g;
}
}
void AbstractKeyListModelTest::testCreation()
{
QScopedPointer<AbstractKeyListModel> model(createModel());
QCOMPARE( model->rowCount(), 0 );
}
void AbstractKeyListModelTest::testSetKeys()
{
QScopedPointer<AbstractKeyListModel> model(createModel());
const std::vector<Key> keys = {
createTestKey("test1@example.net")
};
model->setKeys(keys);
QCOMPARE( model->rowCount(), 1 );
QVERIFY( model->index(keys[0]).isValid() );
const std::vector<Key> otherKeys = {
createTestKey("test2@example.net"),
createTestKey("test3@example.net")
};
model->setKeys(otherKeys);
QCOMPARE( model->rowCount(), 2 );
QVERIFY( model->index(otherKeys[0]).isValid() );
QVERIFY( model->index(otherKeys[1]).isValid() );
QVERIFY( !model->index(keys[0]).isValid() );
}
void AbstractKeyListModelTest::testSetGroups()
{
QScopedPointer<AbstractKeyListModel> model(createModel());
const std::vector<KeyGroup> groups = {
createGroup("test1")
};
model->setGroups(groups);
QCOMPARE( model->rowCount(), 1 );
QVERIFY( model->index(groups[0]).isValid() );
const std::vector<KeyGroup> otherGroups = {
createGroup("test2"),
createGroup("test3")
};
model->setGroups(otherGroups);
QCOMPARE( model->rowCount(), 2 );
QVERIFY( model->index(otherGroups[0]).isValid() );
QVERIFY( model->index(otherGroups[1]).isValid() );
QVERIFY( !model->index(groups[0]).isValid() );
}
void AbstractKeyListModelTest::testKeys()
{
QScopedPointer<AbstractKeyListModel> model(createModel());
const Key key = createTestKey("test@example.net");
const KeyGroup group = createGroup("test", {key});
model->setKeys({key});
model->setGroups({group});
QCOMPARE( model->rowCount(), 2 );
const QModelIndex keyIndex = model->index(key);
QVERIFY( keyIndex.isValid() );
const QModelIndex groupIndex = model->index(group);
QVERIFY( groupIndex.isValid() );
{
const auto keys = model->keys({});
QCOMPARE( keys.size(), 0 );
}
{
const auto keys = model->keys({keyIndex});
QCOMPARE( keys.size(), 1 );
QCOMPARE( keys[0].userID(0).addrSpec(), UserID::addrSpecFromString("test@example.net") );
}
{
// duplicate keys are removed from result
const auto keys = model->keys({keyIndex, keyIndex});
QCOMPARE( keys.size(), 1 );
QCOMPARE( keys[0].userID(0).addrSpec(), UserID::addrSpecFromString("test@example.net") );
}
{
// null keys are removed from result
const auto keys = model->keys({groupIndex});
QCOMPARE( keys.size(), 0 );
}
}
void AbstractKeyListModelTest::testIndex()
{
QScopedPointer<AbstractKeyListModel> model(createModel());
const Key key = createTestKey("test@example.net");
const std::vector<KeyGroup> groups = {
createGroup("test", {key}, KeyGroup::UnknownSource),
createGroup("test", {key}, KeyGroup::GnuPGConfig),
createGroup("test", {key}, KeyGroup::ApplicationConfig, "test"),
createGroup("test", {key}, KeyGroup::ApplicationConfig, "otherConfigName")
};
model->setKeys({key});
model->setGroups(groups);
const QModelIndex keyIndex = model->index(0, 0);
QVERIFY( keyIndex.isValid() );
QVERIFY( !model->key(keyIndex).isNull() );
const QModelIndex groupIndex = model->index(1, 0);
QVERIFY( groupIndex.isValid() );
QVERIFY( !model->group(groupIndex).isNull() );
}
void AbstractKeyListModelTest::testIndexForGroup()
{
QScopedPointer<AbstractKeyListModel> model(createModel());
const Key key = createTestKey("test@example.net");
const std::vector<KeyGroup> groups = {
createGroup("test", {key}, KeyGroup::UnknownSource),
createGroup("test", {key}, KeyGroup::GnuPGConfig),
createGroup("test", {key}, KeyGroup::ApplicationConfig, "test"),
createGroup("test", {key}, KeyGroup::ApplicationConfig, "otherConfigName")
};
model->setKeys({key});
model->setGroups(groups);
QSet<int> rows;
for (const KeyGroup &group : groups) {
const QModelIndex groupIndex = model->index(group);
QVERIFY( groupIndex.isValid() );
rows.insert(groupIndex.row());
}
QCOMPARE(rows.size(), 4);
}
void AbstractKeyListModelTest::testClear()
{
QScopedPointer<AbstractKeyListModel> model(createModel());
model->setGroups({
createGroup("test")
});
model->clear(AbstractKeyListModel::Keys);
QCOMPARE( model->rowCount(), 1 );
model->clear(AbstractKeyListModel::Groups);
QCOMPARE( model->rowCount(), 0 );
}
diff --git a/src/kleo/keygroup.cpp b/src/kleo/keygroup.cpp
index 3f56ef36..0d3b11c5 100644
--- a/src/kleo/keygroup.cpp
+++ b/src/kleo/keygroup.cpp
@@ -1,155 +1,140 @@
/*
kleo/keygroup.cpp
This file is part of libkleopatra, the KDE keymanagement library
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "keygroup.h"
#include <QString>
#include <gpgme++/key.h>
using namespace Kleo;
using namespace GpgME;
-static const KeyGroup::Id nullId = -1;
-
class KeyGroup::Private
{
public:
explicit Private(Id id, const QString &name, const std::vector<Key> &keys, Source source);
Id id;
QString name;
- QString configName;
Keys keys;
Source source;
bool isImmutable = true;
};
KeyGroup::Private::Private(Id id, const QString &name, const std::vector<Key> &keys, Source source)
: id(id)
, name(name)
, keys(keys.cbegin(), keys.cend())
, source(source)
{
}
KeyGroup::KeyGroup()
- : KeyGroup(nullId, QString(), {}, UnknownSource)
+ : KeyGroup(QString(), QString(), {}, UnknownSource)
{
}
KeyGroup::~KeyGroup() = default;
KeyGroup::KeyGroup(Id id, const QString &name, const std::vector<Key> &keys, Source source)
: d(new Private(id, name, keys, source))
{
}
KeyGroup::KeyGroup(const KeyGroup &other)
: d(new Private(*other.d))
{
}
KeyGroup &KeyGroup::operator=(const KeyGroup &other)
{
*d = *other.d;
return *this;
}
KeyGroup::KeyGroup(KeyGroup &&other) = default;
KeyGroup &KeyGroup::operator=(KeyGroup &&other) = default;
bool KeyGroup::isNull() const
{
- return !d || d->id == nullId;
+ return !d || d->id.isEmpty();
}
KeyGroup::Id KeyGroup::id() const
{
- return d ? d->id : nullId;
+ return d ? d->id : QString();
}
void KeyGroup::setName(const QString &name)
{
if (d) {
d->name = name;
}
}
QString KeyGroup::name() const
{
return d ? d->name : QString();
}
void KeyGroup::setKeys(const KeyGroup::Keys &keys)
{
if (d) {
d->keys = keys;
}
}
void KeyGroup::setKeys(const std::vector<GpgME::Key> &keys)
{
if (d) {
d->keys = Keys(keys.cbegin(), keys.cend());
}
}
const KeyGroup::Keys &KeyGroup::keys() const
{
static const Keys empty;
return d ? d->keys : empty;
}
KeyGroup::Source KeyGroup::source() const
{
return d ? d->source : UnknownSource;
}
-void KeyGroup::setConfigName(const QString &configName)
-{
- if (d) {
- d->configName = configName;
- }
-}
-
-QString KeyGroup::configName() const
-{
- return d ? d->configName : QString();
-}
-
void KeyGroup::setIsImmutable(bool isImmutable)
{
if (d) {
d->isImmutable = isImmutable;
}
}
bool KeyGroup::isImmutable() const
{
return d ? d->isImmutable : true;
}
bool KeyGroup::insert(const GpgME::Key &key)
{
if (!d || key.isNull()) {
return false;
}
return d->keys.insert(key).second;
}
bool KeyGroup::erase(const GpgME::Key &key)
{
if (!d || key.isNull()) {
return false;
}
return d->keys.erase(key) > 0;
}
diff --git a/src/kleo/keygroup.h b/src/kleo/keygroup.h
index 43259ee6..4e409f12 100644
--- a/src/kleo/keygroup.h
+++ b/src/kleo/keygroup.h
@@ -1,84 +1,81 @@
/*
kleo/keygroup.h
This file is part of libkleopatra, the KDE keymanagement library
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef LIBKLEO_KEYGROUP_H
#define LIBKLEO_KEYGROUP_H
#include "kleo_export.h"
#include <Libkleo/Predicates>
#include <memory>
#include <set>
#include <vector>
class QString;
namespace GpgME
{
class Key;
}
namespace Kleo
{
class KLEO_EXPORT KeyGroup
{
public:
- typedef int32_t Id;
+ typedef QString Id;
typedef std::set<GpgME::Key, _detail::ByFingerprint<std::less>> Keys;
enum Source {
UnknownSource,
ApplicationConfig,
GnuPGConfig,
Tags
};
KeyGroup();
~KeyGroup();
explicit KeyGroup(Id id, const QString &name, const std::vector<GpgME::Key> &keys, Source source);
KeyGroup(const KeyGroup &other);
KeyGroup &operator=(const KeyGroup &other);
KeyGroup(KeyGroup &&other);
KeyGroup &operator=(KeyGroup &&other);
bool isNull() const;
Id id() const;
Source source() const;
void setName(const QString &name);
QString name() const;
void setKeys(const Keys &keys);
void setKeys(const std::vector<GpgME::Key> &keys);
const Keys &keys() const;
- void setConfigName(const QString &configName);
- QString configName() const;
-
void setIsImmutable(bool isImmutable);
bool isImmutable() const;
bool insert(const GpgME::Key &key);
bool erase(const GpgME::Key &key);
private:
class Private;
std::unique_ptr<Private> d;
};
}
#endif // LIBKLEO_KEYGROUP_H
diff --git a/src/models/keycache.cpp b/src/models/keycache.cpp
index 74f8b490..0d2a0c13 100644
--- a/src/models/keycache.cpp
+++ b/src/models/keycache.cpp
@@ -1,1500 +1,1526 @@
/* -*- mode: c++; c-basic-offset:4 -*-
models/keycache.cpp
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2007, 2008 Klarälvdalens Datakonsult AB
SPDX-FileCopyrightText: 2018 Intevation GmbH
SPDX-FileCopyrightText: 2020 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "keycache.h"
#include "keycache_p.h"
-#include "libkleo_debug.h"
-
#include "kleo/keygroup.h"
#include "kleo/predicates.h"
#include "kleo/stl_util.h"
#include "kleo/dn.h"
#include "utils/filesystemwatcher.h"
#include <KConfigGroup>
#include <KSharedConfig>
#include <gpgme++/error.h>
#include <gpgme++/context.h>
#include <gpgme++/key.h>
#include <gpgme++/decryptionresult.h>
#include <gpgme++/verificationresult.h>
#include <gpgme++/keylistresult.h>
#include <qgpgme/protocol.h>
#include <qgpgme/listallkeysjob.h>
#include <qgpgme/cryptoconfig.h>
#include <gpg-error.h>
//#include <kmime/kmime_header_parsing.h>
#include <QPointer>
#include <QTimer>
#include <QEventLoop>
#include <utility>
#include <algorithm>
#include <functional>
#include <iterator>
+#include "kleo/debug.h"
+#include "libkleo_debug.h"
+
using namespace Kleo;
using namespace GpgME;
using namespace KMime::Types;
static const unsigned int hours2ms = 1000 * 60 * 60;
static const QString groupNamePrefix = QStringLiteral("Group-");
//
//
// KeyCache
//
//
namespace
{
make_comparator_str(ByEMail, .first.c_str());
}
class KeyCache::Private
{
friend class ::Kleo::KeyCache;
KeyCache *const q;
public:
explicit Private(KeyCache *qq) : q(qq), m_refreshInterval(1), m_initalized(false), m_pgpOnly(true), m_remarks_enabled(false)
{
connect(&m_autoKeyListingTimer, &QTimer::timeout, q, [this]() { q->startKeyListing(); });
updateAutoKeyListingTimer();
}
~Private()
{
if (m_refreshJob) {
m_refreshJob->cancel();
}
}
template < template <template <typename U> class Op> class Comp>
std::vector<Key>::const_iterator find(const std::vector<Key> &keys, const char *key) const
{
ensureCachePopulated();
const auto it =
std::lower_bound(keys.begin(), keys.end(), key, Comp<std::less>());
if (it == keys.end() || Comp<std::equal_to>()(*it, key)) {
return it;
} else {
return keys.end();
}
}
template < template <template <typename U> class Op> class Comp>
std::vector<Subkey>::const_iterator find(const std::vector<Subkey> &keys, const char *key) const
{
ensureCachePopulated();
const auto it =
std::lower_bound(keys.begin(), keys.end(), key, Comp<std::less>());
if (it == keys.end() || Comp<std::equal_to>()(*it, key)) {
return it;
} else {
return keys.end();
}
}
std::vector<Key>::const_iterator find_fpr(const char *fpr) const
{
return find<_detail::ByFingerprint>(by.fpr, fpr);
}
std::pair< std::vector< std::pair<std::string, Key> >::const_iterator,
std::vector< std::pair<std::string, Key> >::const_iterator >
find_email(const char *email) const
{
ensureCachePopulated();
return std::equal_range(by.email.begin(), by.email.end(),
email, ByEMail<std::less>());
}
std::vector<Key> find_mailbox(const QString &email, bool sign) const;
std::vector<Subkey>::const_iterator find_keygrip(const char *keygrip) const
{
return find<_detail::ByKeyGrip>(by.keygrip, keygrip);
}
std::vector<Subkey>::const_iterator find_subkeyid(const char *subkeyid) const
{
return find<_detail::ByKeyID>(by.subkeyid, subkeyid);
}
std::vector<Key>::const_iterator find_keyid(const char *keyid) const
{
return find<_detail::ByKeyID>(by.keyid, keyid);
}
std::vector<Key>::const_iterator find_shortkeyid(const char *shortkeyid) const
{
return find<_detail::ByShortKeyID>(by.shortkeyid, shortkeyid);
}
std::pair <
std::vector<Key>::const_iterator,
std::vector<Key>::const_iterator
> find_subjects(const char *chain_id) const
{
ensureCachePopulated();
return std::equal_range(by.chainid.begin(), by.chainid.end(),
chain_id, _detail::ByChainID<std::less>());
}
void refreshJobDone(const KeyListResult &result);
void setRefreshInterval(int interval)
{
m_refreshInterval = interval;
updateAutoKeyListingTimer();
}
int refreshInterval() const
{
return m_refreshInterval;
}
void updateAutoKeyListingTimer()
{
setAutoKeyListingInterval(hours2ms * m_refreshInterval);
}
void setAutoKeyListingInterval(int ms)
{
m_autoKeyListingTimer.stop();
m_autoKeyListingTimer.setInterval(ms);
if (ms != 0) {
m_autoKeyListingTimer.start();
}
}
void ensureCachePopulated() const;
std::vector<Key> getKeysForGroup(const QStringList &fingerprints)
{
std::vector<Key> groupKeys;
groupKeys.reserve(fingerprints.size());
for (const QString &fpr : fingerprints) {
const Key key = q->findByFingerprint(fpr.toLatin1().constData());
if (key.isNull()) {
qCDebug (LIBKLEO_LOG) << "Ignoring unknown key with fingerprint:" << fpr;
continue;
}
groupKeys.push_back(key);
}
return groupKeys;
}
- void addGroup(const QString &name, const std::vector<GpgME::Key> &keys, KeyGroup::Source source,
- const QString &configName = QString(), bool isImmutable = true)
+ void addGroup(KeyGroup::Source source, const KeyGroup::Id &id,
+ const QString &name, const std::vector<GpgME::Key> &keys,
+ bool isImmutable = true)
{
- KeyGroup g(m_groups.size(), name, keys, source);
- g.setConfigName(configName);
+ KeyGroup g(id, name, keys, source);
g.setIsImmutable(isImmutable);
m_groups.push_back(g);
}
void readGroupsFromGpgConf()
{
// According to Werner Koch groups are more of a hack to solve
// a valid usecase (e.g. several keys defined for an internal mailing list)
// that won't make it in the proper keylist interface. And using gpgconf
// was the suggested way to support groups.
auto conf = QGpgME::cryptoConfig();
if (!conf) {
return;
}
auto entry = conf->entry(QStringLiteral("gpg"),
QStringLiteral("Configuration"),
QStringLiteral("group"));
if (!entry) {
return;
}
// collect the key fingerprints for all groups read from the configuration
QMap<QString, QStringList> fingerprints;
for (const QString &value: entry->stringValueList()) {
const QStringList split = value.split(QLatin1Char('='));
if (split.size() != 2) {
qCDebug (LIBKLEO_LOG) << "Ignoring invalid group config:" << value;
continue;
}
const QString groupName = split[0];
const QString fingerprint = split[1];
fingerprints[groupName].push_back(fingerprint);
}
// add all groups read from the configuration to the list of groups
for (auto it = fingerprints.cbegin(); it != fingerprints.cend(); ++it) {
const QString groupName = it.key();
const std::vector<Key> groupKeys = getKeysForGroup(it.value());
- addGroup(groupName, groupKeys, KeyGroup::GnuPGConfig);
+ addGroup(KeyGroup::GnuPGConfig, groupName, groupName, groupKeys);
}
}
void readGroupsFromGroupsConfig()
{
if (m_groupsConfigName.isEmpty()) {
return;
}
const KSharedConfigPtr groupsConfig = KSharedConfig::openConfig(m_groupsConfigName);
const QStringList configGroups = groupsConfig->groupList();
for (const QString &configGroupName : configGroups) {
qCDebug(LIBKLEO_LOG) << "Reading config group" << configGroupName;
if (configGroupName.startsWith(groupNamePrefix)) {
const QString keyGroupId = configGroupName.mid(groupNamePrefix.size());
if (keyGroupId.isEmpty()) {
qCWarning(LIBKLEO_LOG) << "Config group" << configGroupName << "has empty group id";
continue;
}
const KConfigGroup configGroup = groupsConfig->group(configGroupName);
const QString keyGroupName = configGroup.readEntry("Name", QString());
if (keyGroupName.isEmpty()) {
qCWarning(LIBKLEO_LOG) << "Config group" << configGroupName << "has empty or missing Name entry";
continue;
}
const QStringList fingerprints = configGroup.readEntry("Keys", QStringList());
const std::vector<Key> groupKeys = getKeysForGroup(fingerprints);
qCDebug(LIBKLEO_LOG) << "Read group with id" << keyGroupId << ", name" << keyGroupName << ", and keys" << fingerprints;
const bool isImmutable = configGroup.isEntryImmutable("Keys");
- addGroup(keyGroupName, groupKeys, KeyGroup::ApplicationConfig, keyGroupId, isImmutable);
+ addGroup(KeyGroup::ApplicationConfig, keyGroupId, keyGroupName, groupKeys, isImmutable);
}
}
}
- void writeGroupToGroupsConfig(const KeyGroup &group)
+ bool writeGroupToGroupsConfig(const KeyGroup &group)
{
- Q_ASSERT(!group.configName().isEmpty());
+ Q_ASSERT(!group.isNull());
+ Q_ASSERT(group.source() == KeyGroup::ApplicationConfig);
+ if (group.source() != KeyGroup::ApplicationConfig) {
+ qCDebug(LIBKLEO_LOG) << "writeGroupToGroupsConfig - group cannot be written to application configuration:" << group;
+ return false;
+ }
if (m_groupsConfigName.isEmpty()) {
- return;
+ qCDebug(LIBKLEO_LOG) << "writeGroupToGroupsConfig - name of application configuration file is unset";
+ return false;
}
KSharedConfigPtr groupsConfig = KSharedConfig::openConfig(m_groupsConfigName);
- KConfigGroup configGroup = groupsConfig->group(groupNamePrefix + group.configName());
+ KConfigGroup configGroup = groupsConfig->group(groupNamePrefix + group.id());
Q_ASSERT(!configGroup.isEntryImmutable("Keys"));
qCDebug(LIBKLEO_LOG) << "Writing config group" << configGroup.name();
configGroup.writeEntry("Name", group.name());
const KeyGroup::Keys &keys = group.keys();
QStringList fingerprints;
fingerprints.reserve(keys.size());
std::transform(keys.cbegin(), keys.cend(),
std::back_inserter(fingerprints),
[] (const Key &key) {
return QString::fromLatin1(key.primaryFingerprint());
});
configGroup.writeEntry("Keys", fingerprints);
+
+ return true;
}
void updateGroupCache()
{
// Update Group Keys
// this is a quick thing as it only involves reading the config
// so no need for a job.
m_groups.clear();
readGroupsFromGpgConf();
readGroupsFromGroupsConfig();
}
private:
QPointer<RefreshKeysJob> m_refreshJob;
std::vector<std::shared_ptr<FileSystemWatcher> > m_fsWatchers;
QTimer m_autoKeyListingTimer;
int m_refreshInterval;
struct By {
std::vector<Key> fpr, keyid, shortkeyid, chainid;
std::vector< std::pair<std::string, Key> > email;
std::vector<Subkey> subkeyid, keygrip;
} by;
bool m_initalized;
bool m_pgpOnly;
bool m_remarks_enabled;
QString m_groupsConfigName;
std::vector<KeyGroup> m_groups;
};
std::shared_ptr<const KeyCache> KeyCache::instance()
{
return mutableInstance();
}
std::shared_ptr<KeyCache> KeyCache::mutableInstance()
{
static std::weak_ptr<KeyCache> self;
try {
return std::shared_ptr<KeyCache>(self);
} catch (const std::bad_weak_ptr &) {
const std::shared_ptr<KeyCache> s(new KeyCache);
self = s;
return s;
}
}
KeyCache::KeyCache()
: QObject(), d(new Private(this))
{
}
KeyCache::~KeyCache() {}
void KeyCache::setGroupsConfig(const QString &filename)
{
d->m_groupsConfigName = filename;
}
void KeyCache::enableFileSystemWatcher(bool enable)
{
for (const auto &i : qAsConst(d->m_fsWatchers)) {
i->setEnabled(enable);
}
}
void KeyCache::setRefreshInterval(int hours)
{
d->setRefreshInterval(hours);
}
int KeyCache::refreshInterval() const
{
return d->refreshInterval();
}
void KeyCache::reload(GpgME::Protocol /*proto*/)
{
if (d->m_refreshJob) {
return;
}
d->updateAutoKeyListingTimer();
enableFileSystemWatcher(false);
d->m_refreshJob = new RefreshKeysJob(this);
connect(d->m_refreshJob.data(), &RefreshKeysJob::done,
this, [this](const GpgME::KeyListResult &r) {
d->refreshJobDone(r);
});
d->m_refreshJob->start();
}
void KeyCache::cancelKeyListing()
{
if (!d->m_refreshJob) {
return;
}
d->m_refreshJob->cancel();
}
void KeyCache::addFileSystemWatcher(const std::shared_ptr<FileSystemWatcher> &watcher)
{
if (!watcher) {
return;
}
d->m_fsWatchers.push_back(watcher);
connect(watcher.get(), &FileSystemWatcher::directoryChanged,
this, [this]() { startKeyListing(); });
connect(watcher.get(), &FileSystemWatcher::fileChanged,
this, [this]() { startKeyListing(); });
watcher->setEnabled(d->m_refreshJob.isNull());
}
void KeyCache::enableRemarks(bool value)
{
if (!d->m_remarks_enabled && value) {
d->m_remarks_enabled = value;
if (d->m_initalized && !d->m_refreshJob) {
qCDebug(LIBKLEO_LOG) << "Reloading keycache with remarks enabled";
reload();
} else {
connect(d->m_refreshJob.data(), &RefreshKeysJob::done,
this, [this](const GpgME::KeyListResult &) {
qCDebug(LIBKLEO_LOG) << "Reloading keycache with remarks enabled";
QTimer::singleShot(1000, this, [this](){ reload(); });
});
}
} else {
d->m_remarks_enabled = value;
}
}
bool KeyCache::remarksEnabled() const
{
return d->m_remarks_enabled;
}
void KeyCache::Private::refreshJobDone(const KeyListResult &result)
{
q->enableFileSystemWatcher(true);
m_initalized = true;
updateGroupCache();
Q_EMIT q->keyListingDone(result);
}
const Key &KeyCache::findByFingerprint(const char *fpr) const
{
const std::vector<Key>::const_iterator it = d->find_fpr(fpr);
if (it == d->by.fpr.end()) {
static const Key null;
return null;
} else {
return *it;
}
}
const Key &KeyCache::findByFingerprint(const std::string &fpr) const
{
return findByFingerprint(fpr.c_str());
}
std::vector<Key> KeyCache::findByEMailAddress(const char *email) const
{
const auto pair = d->find_email(email);
std::vector<Key> result;
result.reserve(std::distance(pair.first, pair.second));
std::transform(pair.first, pair.second,
std::back_inserter(result),
[](const std::pair<std::string, Key> &pair) {
return pair.second;
});
return result;
}
std::vector<Key> KeyCache::findByEMailAddress(const std::string &email) const
{
return findByEMailAddress(email.c_str());
}
const Key &KeyCache::findByShortKeyID(const char *id) const
{
const std::vector<Key>::const_iterator it = d->find_shortkeyid(id);
if (it != d->by.shortkeyid.end()) {
return *it;
}
static const Key null;
return null;
}
const Key &KeyCache::findByShortKeyID(const std::string &id) const
{
return findByShortKeyID(id.c_str());
}
const Key &KeyCache::findByKeyIDOrFingerprint(const char *id) const
{
{
// try by.fpr first:
const std::vector<Key>::const_iterator it = d->find_fpr(id);
if (it != d->by.fpr.end()) {
return *it;
}
}{
// try by.keyid next:
const std::vector<Key>::const_iterator it = d->find_keyid(id);
if (it != d->by.keyid.end()) {
return *it;
}
}
static const Key null;
return null;
}
const Key &KeyCache::findByKeyIDOrFingerprint(const std::string &id) const
{
return findByKeyIDOrFingerprint(id.c_str());
}
std::vector<Key> KeyCache::findByKeyIDOrFingerprint(const std::vector<std::string> &ids) const
{
std::vector<std::string> keyids;
std::remove_copy_if(ids.begin(), ids.end(), std::back_inserter(keyids),
[](const std::string &str) {
return !str.c_str() || !*str.c_str();
});
// this is just case-insensitive string search:
std::sort(keyids.begin(), keyids.end(), _detail::ByFingerprint<std::less>());
std::vector<Key> result;
result.reserve(keyids.size()); // dups shouldn't happen
d->ensureCachePopulated();
kdtools::set_intersection(d->by.fpr.begin(), d->by.fpr.end(),
keyids.begin(), keyids.end(),
std::back_inserter(result),
_detail::ByFingerprint<std::less>());
if (result.size() < keyids.size()) {
// note that By{Fingerprint,KeyID,ShortKeyID} define the same
// order for _strings_
kdtools::set_intersection(d->by.keyid.begin(), d->by.keyid.end(),
keyids.begin(), keyids.end(),
std::back_inserter(result),
_detail::ByKeyID<std::less>());
}
// duplicates shouldn't happen, but make sure nonetheless:
std::sort(result.begin(), result.end(), _detail::ByFingerprint<std::less>());
result.erase(std::unique(result.begin(), result.end(), _detail::ByFingerprint<std::equal_to>()), result.end());
// we skip looking into short key ids here, as it's highly
// unlikely they're used for this purpose. We might need to revise
// this decision, but only after testing.
return result;
}
const Subkey &KeyCache::findSubkeyByKeyGrip(const char *grip, Protocol protocol) const
{
static const Subkey null;
d->ensureCachePopulated();
const auto range = std::equal_range(d->by.keygrip.begin(), d->by.keygrip.end(),
grip, _detail::ByKeyGrip<std::less>());
if (range.first == d->by.keygrip.end()) {
return null;
} else if (protocol == UnknownProtocol) {
return *range.first;
} else {
for (auto it = range.first; it != range.second; ++it) {
if (it->parent().protocol() == protocol) {
return *it;
}
}
}
return null;
}
const Subkey &KeyCache::findSubkeyByKeyGrip(const std::string &grip, Protocol protocol) const
{
return findSubkeyByKeyGrip(grip.c_str(), protocol);
}
std::vector<Subkey> KeyCache::findSubkeysByKeyID(const std::vector<std::string> &ids) const
{
std::vector<std::string> sorted;
sorted.reserve(ids.size());
std::remove_copy_if(ids.begin(), ids.end(), std::back_inserter(sorted),
[](const std::string &str) {
return !str.c_str() || !*str.c_str();
});
std::sort(sorted.begin(), sorted.end(), _detail::ByKeyID<std::less>());
std::vector<Subkey> result;
d->ensureCachePopulated();
kdtools::set_intersection(d->by.subkeyid.begin(), d->by.subkeyid.end(),
sorted.begin(), sorted.end(),
std::back_inserter(result),
_detail::ByKeyID<std::less>());
return result;
}
std::vector<Key> KeyCache::findRecipients(const DecryptionResult &res) const
{
std::vector<std::string> keyids;
const auto recipients = res.recipients();
for (const DecryptionResult::Recipient &r : recipients)
if (const char *kid = r.keyID()) {
keyids.push_back(kid);
}
const std::vector<Subkey> subkeys = findSubkeysByKeyID(keyids);
std::vector<Key> result;
result.reserve(subkeys.size());
std::transform(subkeys.begin(), subkeys.end(), std::back_inserter(result),
std::mem_fn(&Subkey::parent));
std::sort(result.begin(), result.end(), _detail::ByFingerprint<std::less>());
result.erase(std::unique(result.begin(), result.end(), _detail::ByFingerprint<std::equal_to>()), result.end());
return result;
}
std::vector<Key> KeyCache::findSigners(const VerificationResult &res) const
{
std::vector<std::string> fprs;
const auto signatures = res.signatures();
for (const Signature &s : signatures)
if (const char *fpr = s.fingerprint()) {
fprs.push_back(fpr);
}
return findByKeyIDOrFingerprint(fprs);
}
std::vector<Key> KeyCache::findSigningKeysByMailbox(const QString &mb) const
{
return d->find_mailbox(mb, true);
}
std::vector<Key> KeyCache::findEncryptionKeysByMailbox(const QString &mb) const
{
return d->find_mailbox(mb, false);
}
namespace
{
#define DO( op, meth, meth2 ) if ( op key.meth() ) {} else { qDebug( "rejecting for signing: %s: %s", #meth2, key.primaryFingerprint() ); return false; }
#define ACCEPT( meth ) DO( !!, meth, !meth )
#define REJECT( meth ) DO( !, meth, meth )
struct ready_for_signing : std::unary_function<Key, bool> {
bool operator()(const Key &key) const
{
#if 1
ACCEPT(hasSecret);
ACCEPT(canReallySign);
REJECT(isRevoked);
REJECT(isExpired);
REJECT(isDisabled);
REJECT(isInvalid);
return true;
#else
return key.hasSecret() &&
key.canReallySign() && !key.isRevoked() && !key.isExpired() && !key.isDisabled() && !key.isInvalid();
#endif
#undef DO
}
};
struct ready_for_encryption : std::unary_function<Key, bool> {
#define DO( op, meth, meth2 ) if ( op key.meth() ) {} else { qDebug( "rejecting for encrypting: %s: %s", #meth2, key.primaryFingerprint() ); return false; }
bool operator()(const Key &key) const
{
#if 1
ACCEPT(canEncrypt);
REJECT(isRevoked);
REJECT(isExpired);
REJECT(isDisabled);
REJECT(isInvalid);
return true;
#else
return
key.canEncrypt() && !key.isRevoked() && !key.isExpired() && !key.isDisabled() && !key.isInvalid();
#endif
}
#undef DO
#undef ACCEPT
#undef REJECT
};
}
std::vector<Key> KeyCache::Private::find_mailbox(const QString &email, bool sign) const
{
if (email.isEmpty()) {
return std::vector<Key>();
}
const auto pair = find_email(email.toUtf8().constData());
std::vector<Key> result;
result.reserve(std::distance(pair.first, pair.second));
if (sign)
kdtools::copy_2nd_if(pair.first, pair.second,
std::back_inserter(result),
ready_for_signing());
else
kdtools::copy_2nd_if(pair.first, pair.second,
std::back_inserter(result),
ready_for_encryption());
return result;
}
std::vector<Key> KeyCache::findSubjects(const GpgME::Key &key, Options options) const
{
return findSubjects(std::vector<Key>(1, key), options);
}
std::vector<Key> KeyCache::findSubjects(const std::vector<Key> &keys, Options options) const
{
return findSubjects(keys.begin(), keys.end(), options);
}
std::vector<Key> KeyCache::findSubjects(std::vector<Key>::const_iterator first, std::vector<Key>::const_iterator last, Options options) const
{
if (first == last) {
return std::vector<Key>();
}
std::vector<Key> result;
while (first != last) {
const auto pair = d->find_subjects(first->primaryFingerprint());
result.insert(result.end(), pair.first, pair.second);
++first;
}
std::sort(result.begin(), result.end(), _detail::ByFingerprint<std::less>());
result.erase(std::unique(result.begin(), result.end(), _detail::ByFingerprint<std::equal_to>()), result.end());
if (options & RecursiveSearch) {
const std::vector<Key> furtherSubjects = findSubjects(result, options);
std::vector<Key> combined;
combined.reserve(result.size() + furtherSubjects.size());
std::merge(result.begin(), result.end(),
furtherSubjects.begin(), furtherSubjects.end(),
std::back_inserter(combined),
_detail::ByFingerprint<std::less>());
combined.erase(std::unique(combined.begin(), combined.end(), _detail::ByFingerprint<std::equal_to>()), combined.end());
result.swap(combined);
}
return result;
}
std::vector<Key> KeyCache::findIssuers(const Key &key, Options options) const
{
if (key.isNull()) {
return std::vector<Key>();
}
std::vector<Key> result;
if (options & IncludeSubject) {
result.push_back(key);
}
if (key.isRoot()) {
return result;
}
const Key &issuer = findByFingerprint(key.chainID());
if (issuer.isNull()) {
return result;
}
result.push_back(issuer);
if (!(options & RecursiveSearch)) {
return result;
}
while (!result.back().isNull() && !result.back().isRoot()) {
result.push_back(findByFingerprint(result.back().chainID()));
}
if (result.back().isNull()) {
result.pop_back();
}
return result;
}
std::vector<Key> KeyCache::findIssuers(const std::vector<Key> &keys, Options options) const
{
return findIssuers(keys.begin(), keys.end(), options);
}
std::vector<Key> KeyCache::findIssuers(std::vector<Key>::const_iterator first, std::vector<Key>::const_iterator last, Options options) const
{
if (first == last) {
return std::vector<Key>();
}
// extract chain-ids, identifying issuers:
std::vector<const char *> chainIDs;
chainIDs.reserve(last - first);
kdtools::transform_if(first, last, std::back_inserter(chainIDs),
std::mem_fn(&Key::chainID),
[](const Key &key) { return !key.isRoot(); });
std::sort(chainIDs.begin(), chainIDs.end(), _detail::ByFingerprint<std::less>());
const auto lastUniqueChainID = std::unique(chainIDs.begin(), chainIDs.end(), _detail::ByFingerprint<std::less>());
std::vector<Key> result;
result.reserve(lastUniqueChainID - chainIDs.begin());
d->ensureCachePopulated();
kdtools::set_intersection(d->by.fpr.begin(), d->by.fpr.end(),
chainIDs.begin(), lastUniqueChainID,
std::back_inserter(result),
_detail::ByFingerprint<std::less>());
if (options & IncludeSubject) {
const unsigned int rs = result.size();
result.insert(result.end(), first, last);
std::inplace_merge(result.begin(), result.begin() + rs, result.end(),
_detail::ByFingerprint<std::less>());
}
if (!(options & RecursiveSearch)) {
return result;
}
const std::vector<Key> l2result = findIssuers(result, options & ~IncludeSubject);
const unsigned long result_size = result.size();
result.insert(result.end(), l2result.begin(), l2result.end());
std::inplace_merge(result.begin(), result.begin() + result_size, result.end(),
_detail::ByFingerprint<std::less>());
return result;
}
static std::string email(const UserID &uid)
{
// Prefer the gnupg normalized one
const std::string addr = uid.addrSpec();
if (!addr.empty()) {
return addr;
}
const std::string email = uid.email();
if (email.empty()) {
return DN(uid.id())[QStringLiteral("EMAIL")].trimmed().toUtf8().constData();
}
if (email[0] == '<' && email[email.size() - 1] == '>') {
return email.substr(1, email.size() - 2);
} else {
return email;
}
}
static std::vector<std::string> emails(const Key &key)
{
std::vector<std::string> emails;
const auto userIDs = key.userIDs();
for (const UserID &uid : userIDs) {
const std::string e = email(uid);
if (!e.empty()) {
emails.push_back(e);
}
}
std::sort(emails.begin(), emails.end(), ByEMail<std::less>());
emails.erase(std::unique(emails.begin(), emails.end(), ByEMail<std::equal_to>()), emails.end());
return emails;
}
void KeyCache::remove(const Key &key)
{
if (key.isNull()) {
return;
}
const char *fpr = key.primaryFingerprint();
if (!fpr) {
return;
}
Q_EMIT aboutToRemove(key);
{
const auto range = std::equal_range(d->by.fpr.begin(), d->by.fpr.end(), fpr,
_detail::ByFingerprint<std::less>());
d->by.fpr.erase(range.first, range.second);
}
if (const char *keyid = key.keyID()) {
const auto range = std::equal_range(d->by.keyid.begin(), d->by.keyid.end(), keyid,
_detail::ByKeyID<std::less>());
const auto it = std::remove_if(range.first, range.second,
[fpr](const GpgME::Key &key) {
return _detail::ByFingerprint<std::equal_to>()(fpr, key);
});
d->by.keyid.erase(it, range.second);
}
if (const char *shortkeyid = key.shortKeyID()) {
const auto range = std::equal_range(d->by.shortkeyid.begin(), d->by.shortkeyid.end(), shortkeyid,
_detail::ByShortKeyID<std::less>());
const auto it = std::remove_if(range.first, range.second,
[fpr](const GpgME::Key &key) {
return _detail::ByFingerprint<std::equal_to>()(fpr, key);
});
d->by.shortkeyid.erase(it, range.second);
}
if (const char *chainid = key.chainID()) {
const auto range = std::equal_range(d->by.chainid.begin(), d->by.chainid.end(), chainid,
_detail::ByChainID<std::less>());
const auto range2 = std::equal_range(range.first, range.second, fpr, _detail::ByFingerprint<std::less>());
d->by.chainid.erase(range2.first, range2.second);
}
Q_FOREACH (const std::string &email, emails(key)) {
const auto range = std::equal_range(d->by.email.begin(), d->by.email.end(), email, ByEMail<std::less>());
const auto it = std::remove_if(range.first, range.second,
[fpr](const std::pair<std::string, Key> &pair) {
return qstricmp(fpr, pair.second.primaryFingerprint()) == 0;
});
d->by.email.erase(it, range.second);
}
Q_FOREACH (const Subkey &subkey, key.subkeys()) {
if (const char *keyid = subkey.keyID()) {
const auto range = std::equal_range(d->by.subkeyid.begin(), d->by.subkeyid.end(), keyid,
_detail::ByKeyID<std::less>());
const auto it = std::remove_if(range.first, range.second,
[fpr] (const Subkey &subkey) {
return !qstricmp(fpr, subkey.parent().primaryFingerprint());
});
d->by.subkeyid.erase(it, range.second);
}
if (const char *keygrip = subkey.keyGrip()) {
const auto range = std::equal_range(d->by.keygrip.begin(), d->by.keygrip.end(), keygrip,
_detail::ByKeyGrip<std::less>());
const auto it = std::remove_if(range.first, range.second,
[fpr] (const Subkey &subkey) {
return !qstricmp(fpr, subkey.parent().primaryFingerprint());
});
d->by.keygrip.erase(it, range.second);
}
}
}
void KeyCache::remove(const std::vector<Key> &keys)
{
for (const Key &key : keys) {
remove(key);
}
}
const std::vector<GpgME::Key> &KeyCache::keys() const
{
d->ensureCachePopulated();
return d->by.fpr;
}
std::vector<Key> KeyCache::secretKeys() const
{
std::vector<Key> keys = this->keys();
keys.erase(std::remove_if(keys.begin(), keys.end(),
[](const Key &key) { return !key.hasSecret(); }),
keys.end());
return keys;
}
std::vector<KeyGroup> KeyCache::groups() const
{
d->ensureCachePopulated();
return d->m_groups;
}
-void KeyCache::update(const KeyGroup &group)
+bool KeyCache::update(const KeyGroup &group)
{
Q_ASSERT(!group.isNull());
Q_ASSERT(group.source() == KeyGroup::ApplicationConfig);
Q_ASSERT(!group.isImmutable());
- Q_ASSERT(group.id() < static_cast<signed>(d->m_groups.size()));
- Q_ASSERT(group.id() == d->m_groups[group.id()].id());
+ if (group.isNull() || group.source() != KeyGroup::ApplicationConfig || group.isImmutable()) {
+ qCDebug(LIBKLEO_LOG) << "KeyCache::update - Invalid group:" << group;
+ return false;
+ }
+ const auto it = std::find_if(d->m_groups.cbegin(), d->m_groups.cend(),
+ [group] (const auto &g) {
+ return g.source() == group.source() && g.id() == group.id();
+ });
+ Q_ASSERT(!group.isImmutable());
+ if (it == d->m_groups.cend()) {
+ qCDebug(LIBKLEO_LOG) << "KeyCache::update - Group not found in list of groups:" << group;
+ return false;
+ }
+ const auto groupIndex = std::distance(d->m_groups.cbegin(), it);
- d->writeGroupToGroupsConfig(group);
+ if (!d->writeGroupToGroupsConfig(group)) {
+ qCDebug(LIBKLEO_LOG) << "KeyCache::update - Writing group" << group.id() << "to config file failed";
+ return false;
+ }
- d->m_groups[group.id()] = group;
+ d->m_groups[groupIndex] = group;
Q_EMIT groupUpdated(group);
Q_EMIT keysMayHaveChanged();
+
+ return true;
}
void KeyCache::refresh(const std::vector<Key> &keys)
{
// make this better...
clear();
insert(keys);
}
void KeyCache::insert(const Key &key)
{
insert(std::vector<Key>(1, key));
}
namespace
{
template <
template <template <typename T> class Op> class T1,
template <template <typename T> class Op> class T2
> struct lexicographically
{
using result_type = bool;
template <typename U, typename V>
bool operator()(const U &lhs, const V &rhs) const
{
return
T1<std::less>()(lhs, rhs) ||
(T1<std::equal_to>()(lhs, rhs) &&
T2<std::less>()(lhs, rhs))
;
}
};
}
void KeyCache::insert(const std::vector<Key> &keys)
{
// 1. remove those with empty fingerprints:
std::vector<Key> sorted;
sorted.reserve(keys.size());
std::remove_copy_if(keys.begin(), keys.end(),
std::back_inserter(sorted),
[](const Key &key) {
auto fp = key.primaryFingerprint();
return !fp|| !*fp;
});
Q_FOREACH (const Key &key, sorted) {
remove(key); // this is sub-optimal, but makes implementation from here on much easier
}
// 2. sort by fingerprint:
std::sort(sorted.begin(), sorted.end(), _detail::ByFingerprint<std::less>());
// 2a. insert into fpr index:
std::vector<Key> by_fpr;
by_fpr.reserve(sorted.size() + d->by.fpr.size());
std::merge(sorted.begin(), sorted.end(),
d->by.fpr.begin(), d->by.fpr.end(),
std::back_inserter(by_fpr),
_detail::ByFingerprint<std::less>());
// 3. build email index:
std::vector< std::pair<std::string, Key> > pairs;
pairs.reserve(sorted.size());
Q_FOREACH (const Key &key, sorted) {
const std::vector<std::string> emails = ::emails(key);
for (const std::string &e : emails) {
pairs.push_back(std::make_pair(e, key));
}
}
std::sort(pairs.begin(), pairs.end(), ByEMail<std::less>());
// 3a. insert into email index:
std::vector< std::pair<std::string, Key> > by_email;
by_email.reserve(pairs.size() + d->by.email.size());
std::merge(pairs.begin(), pairs.end(),
d->by.email.begin(), d->by.email.end(),
std::back_inserter(by_email),
ByEMail<std::less>());
// 3.5: stable-sort by chain-id (effectively lexicographically<ByChainID,ByFingerprint>)
std::stable_sort(sorted.begin(), sorted.end(), _detail::ByChainID<std::less>());
// 3.5a: insert into chain-id index:
std::vector<Key> nonroot;
nonroot.reserve(sorted.size());
std::vector<Key> by_chainid;
by_chainid.reserve(sorted.size() + d->by.chainid.size());
std::copy_if(sorted.cbegin(), sorted.cend(),
std::back_inserter(nonroot),
[](const Key &key) { return !key.isRoot(); });
std::merge(nonroot.cbegin(), nonroot.cend(),
d->by.chainid.cbegin(), d->by.chainid.cend(),
std::back_inserter(by_chainid),
lexicographically<_detail::ByChainID, _detail::ByFingerprint>());
// 4. sort by key id:
std::sort(sorted.begin(), sorted.end(), _detail::ByKeyID<std::less>());
// 4a. insert into keyid index:
std::vector<Key> by_keyid;
by_keyid.reserve(sorted.size() + d->by.keyid.size());
std::merge(sorted.begin(), sorted.end(),
d->by.keyid.begin(), d->by.keyid.end(),
std::back_inserter(by_keyid),
_detail::ByKeyID<std::less>());
// 5. sort by short key id:
std::sort(sorted.begin(), sorted.end(), _detail::ByShortKeyID<std::less>());
// 5a. insert into short keyid index:
std::vector<Key> by_shortkeyid;
by_shortkeyid.reserve(sorted.size() + d->by.shortkeyid.size());
std::merge(sorted.begin(), sorted.end(),
d->by.shortkeyid.begin(), d->by.shortkeyid.end(),
std::back_inserter(by_shortkeyid),
_detail::ByShortKeyID<std::less>());
// 6. build subkey ID index:
std::vector<Subkey> subkeys;
subkeys.reserve(sorted.size());
Q_FOREACH (const Key &key, sorted)
Q_FOREACH (const Subkey &subkey, key.subkeys()) {
subkeys.push_back(subkey);
}
// 6a sort by key id:
std::sort(subkeys.begin(), subkeys.end(), _detail::ByKeyID<std::less>());
// 6b. insert into subkey ID index:
std::vector<Subkey> by_subkeyid;
by_subkeyid.reserve(subkeys.size() + d->by.subkeyid.size());
std::merge(subkeys.begin(), subkeys.end(),
d->by.subkeyid.begin(), d->by.subkeyid.end(),
std::back_inserter(by_subkeyid),
_detail::ByKeyID<std::less>());
// 6c. sort by key grip
std::sort(subkeys.begin(), subkeys.end(), _detail::ByKeyGrip<std::less>());
// 6d. insert into subkey keygrip index:
std::vector<Subkey> by_keygrip;
by_keygrip.reserve(subkeys.size() + d->by.keygrip.size());
std::merge(subkeys.begin(), subkeys.end(),
d->by.keygrip.begin(), d->by.keygrip.end(),
std::back_inserter(by_keygrip),
_detail::ByKeyGrip<std::less>());
// now commit (well, we already removed keys...)
by_fpr.swap(d->by.fpr);
by_keyid.swap(d->by.keyid);
by_shortkeyid.swap(d->by.shortkeyid);
by_email.swap(d->by.email);
by_subkeyid.swap(d->by.subkeyid);
by_keygrip.swap(d->by.keygrip);
by_chainid.swap(d->by.chainid);
for (const Key &key : qAsConst(sorted)) {
d->m_pgpOnly &= key.protocol() == GpgME::OpenPGP;
Q_EMIT added(key);
}
Q_EMIT keysMayHaveChanged();
}
void KeyCache::clear()
{
d->by = Private::By();
}
//
//
// RefreshKeysJob
//
//
class KeyCache::RefreshKeysJob::Private
{
RefreshKeysJob *const q;
public:
Private(KeyCache *cache, RefreshKeysJob *qq);
void doStart();
Error startKeyListing(GpgME::Protocol protocol);
void listAllKeysJobDone(const KeyListResult &res, const std::vector<Key> &nextKeys)
{
std::vector<Key> keys;
keys.reserve(m_keys.size() + nextKeys.size());
if (m_keys.empty()) {
keys = nextKeys;
} else
std::merge(m_keys.begin(), m_keys.end(),
nextKeys.begin(), nextKeys.end(),
std::back_inserter(keys),
_detail::ByFingerprint<std::less>());
m_keys.swap(keys);
jobDone(res);
}
void emitDone(const KeyListResult &result);
void updateKeyCache();
QPointer<KeyCache> m_cache;
QVector<QGpgME::ListAllKeysJob*> m_jobsPending;
std::vector<Key> m_keys;
KeyListResult m_mergedResult;
bool m_canceled;
private:
void jobDone(const KeyListResult &res);
};
KeyCache::RefreshKeysJob::Private::Private(KeyCache *cache, RefreshKeysJob *qq)
: q(qq)
, m_cache(cache)
, m_canceled(false)
{
Q_ASSERT(m_cache);
}
void KeyCache::RefreshKeysJob::Private::jobDone(const KeyListResult &result)
{
if (m_canceled) {
q->deleteLater();
return;
}
QObject *const sender = q->sender();
if (sender) {
sender->disconnect(q);
}
Q_ASSERT(m_jobsPending.size() > 0);
m_jobsPending.removeOne(qobject_cast<QGpgME::ListAllKeysJob*>(sender));
m_mergedResult.mergeWith(result);
if (m_jobsPending.size() > 0) {
return;
}
updateKeyCache();
emitDone(m_mergedResult);
}
void KeyCache::RefreshKeysJob::Private::emitDone(const KeyListResult &res)
{
q->deleteLater();
Q_EMIT q->done(res);
}
KeyCache::RefreshKeysJob::RefreshKeysJob(KeyCache *cache, QObject *parent) : QObject(parent), d(new Private(cache, this))
{
}
KeyCache::RefreshKeysJob::~RefreshKeysJob()
{
delete d;
}
void KeyCache::RefreshKeysJob::start()
{
QTimer::singleShot(0, this, [this](){ d->doStart(); });
}
void KeyCache::RefreshKeysJob::cancel()
{
d->m_canceled = true;
std::for_each(d->m_jobsPending.begin(), d->m_jobsPending.end(),
std::mem_fn(&QGpgME::ListAllKeysJob::slotCancel));
Q_EMIT canceled();
}
void KeyCache::RefreshKeysJob::Private::doStart()
{
if (m_canceled) {
q->deleteLater();
return;
}
Q_ASSERT(m_jobsPending.size() == 0);
m_mergedResult.mergeWith(KeyListResult(startKeyListing(GpgME::OpenPGP)));
m_mergedResult.mergeWith(KeyListResult(startKeyListing(GpgME::CMS)));
if (m_jobsPending.size() != 0) {
return;
}
const bool hasError = m_mergedResult.error() || m_mergedResult.error().isCanceled();
emitDone(hasError ? m_mergedResult : KeyListResult(Error(GPG_ERR_UNSUPPORTED_OPERATION)));
}
void KeyCache::RefreshKeysJob::Private::updateKeyCache()
{
if (!m_cache || m_canceled) {
q->deleteLater();
return;
}
std::vector<Key> cachedKeys = m_cache->initialized() ? m_cache->keys() : std::vector<Key>();
std::sort(cachedKeys.begin(), cachedKeys.end(), _detail::ByFingerprint<std::less>());
std::vector<Key> keysToRemove;
std::set_difference(cachedKeys.begin(), cachedKeys.end(),
m_keys.begin(), m_keys.end(),
std::back_inserter(keysToRemove),
_detail::ByFingerprint<std::less>());
m_cache->remove(keysToRemove);
m_cache->refresh(m_keys);
}
Error KeyCache::RefreshKeysJob::Private::startKeyListing(GpgME::Protocol proto)
{
const auto * const protocol = (proto == GpgME::OpenPGP) ? QGpgME::openpgp() : QGpgME::smime();
if (!protocol) {
return Error();
}
QGpgME::ListAllKeysJob *const job = protocol->listAllKeysJob(/*includeSigs*/false, /*validate*/true);
if (!job) {
return Error();
}
#if 0
aheinecke: 2017.01.12:
For unknown reasons the new style connect fails at runtime
over library borders into QGpgME from the GpgME repo
when cross compiled for Windows and default arguments
are used in the Signal.
This was tested with gcc 4.9 (Mingw 3.0.2) and we could not
find an explanation for this. So until this is fixed or we understand
the problem we need to use the old style connect for QGpgME signals.
The new style connect of the canceled signal right below
works fine.
connect(job, &QGpgME::ListAllKeysJob::result,
q, [this](const GpgME::KeyListResult &res, const std::vector<GpgME::Key> &keys) {
listAllKeysJobDone(res, keys);
});
#endif
connect(job, SIGNAL(result(GpgME::KeyListResult,std::vector<GpgME::Key>)),
q, SLOT(listAllKeysJobDone(GpgME::KeyListResult,std::vector<GpgME::Key>)));
connect(q, &RefreshKeysJob::canceled, job, &QGpgME::Job::slotCancel);
// Only do this for initialized keycaches to avoid huge waits for
// signature notations during initial keylisting.
if (proto == GpgME::OpenPGP && m_cache->remarksEnabled() && m_cache->initialized()) {
auto ctx = QGpgME::Job::context(job);
if (ctx) {
ctx->addKeyListMode(KeyListMode::Signatures |
KeyListMode::SignatureNotations);
}
}
const Error error = job->start(true);
if (!error && !error.isCanceled()) {
m_jobsPending.push_back(job);
}
return error;
}
bool KeyCache::initialized() const
{
return d->m_initalized;
}
void KeyCache::Private::ensureCachePopulated() const
{
if (!m_initalized) {
q->startKeyListing();
QEventLoop loop;
loop.connect(q, &KeyCache::keyListingDone,
&loop, &QEventLoop::quit);
qCDebug(LIBKLEO_LOG) << "Waiting for keycache.";
loop.exec();
qCDebug(LIBKLEO_LOG) << "Keycache available.";
}
}
bool KeyCache::pgpOnly() const
{
return d->m_pgpOnly;
}
static bool keyIsOk(const Key &k)
{
return !k.isExpired() && !k.isRevoked() && !k.isInvalid() && !k.isDisabled();
}
static bool uidIsOk(const UserID &uid)
{
return keyIsOk(uid.parent()) && !uid.isRevoked() && !uid.isInvalid();
}
static bool subkeyIsOk(const Subkey &s)
{
return !s.isRevoked() && !s.isInvalid() && !s.isDisabled();
}
std::vector<GpgME::Key> KeyCache::findBestByMailBox(const char *addr, GpgME::Protocol proto,
bool needSign, bool needEncrypt) const
{
d->ensureCachePopulated();
std::vector<GpgME::Key> ret;
if (!addr) {
return ret;
}
if (proto == Protocol::OpenPGP) {
// Groups take precedence over automatic keys. We return all here
// so if a group key for example is expired we can show that.
ret = getGroupKeys(QString::fromUtf8(addr));
if (!ret.empty()) {
return ret;
}
}
Key keyC;
UserID uidC;
for (const Key &k: findByEMailAddress(addr)) {
if (proto != Protocol::UnknownProtocol && k.protocol() != proto) {
continue;
}
if (needEncrypt && !k.canEncrypt()) {
continue;
}
if (needSign && (!k.canSign() || !k.hasSecret())) {
continue;
}
/* First get the uid that matches the mailbox */
for (const UserID &u: k.userIDs()) {
if (QString::fromStdString(u.addrSpec()).toLower() != QString::fromUtf8(addr).toLower()) {
continue;
}
if (uidC.isNull()) {
keyC = k;
uidC = u;
} else if ((!uidIsOk(uidC) && uidIsOk(u)) || uidC.validity() < u.validity()) {
/* Validity of the new key is better. */
uidC = u;
keyC = k;
} else if (uidC.validity() == u.validity() && uidIsOk(u)) {
/* Both are the same check which one is newer. */
time_t oldTime = 0;
for (const Subkey &s: keyC.subkeys()) {
if (!subkeyIsOk(s)) {
continue;
}
if (needSign && s.canSign()) {
oldTime = s.creationTime();
} else if (needEncrypt && s.canEncrypt()) {
oldTime = s.creationTime();
} else if (s.canSign() || s.canEncrypt()) {
oldTime = s.creationTime();
}
}
time_t newTime = 0;
for (const Subkey &s: k.subkeys()) {
if (!subkeyIsOk(s)) {
continue;
}
if (needSign && s.canSign()) {
newTime = s.creationTime();
} else if (needEncrypt && s.canEncrypt()) {
newTime = s.creationTime();
} else if (s.canSign() || s.canEncrypt()) {
newTime = s.creationTime();
}
}
if (newTime > oldTime) {
uidC = u;
keyC = k;
}
}
}
}
ret.push_back(keyC);
return ret;
}
std::vector<Key> KeyCache::getGroupKeys(const QString &groupName) const
{
std::vector<Key> result;
for (const KeyGroup &g : qAsConst(d->m_groups)) {
if (g.name() == groupName) {
const KeyGroup::Keys &keys = g.keys();
std::copy(keys.cbegin(), keys.cend(), std::back_inserter(result));
}
}
_detail::sort_by_fpr(result);
_detail::remove_duplicates_by_fpr(result);
return result;
}
#include "moc_keycache_p.cpp"
#include "moc_keycache.cpp"
diff --git a/src/models/keycache.h b/src/models/keycache.h
index aacf0ba8..71b411e3 100644
--- a/src/models/keycache.h
+++ b/src/models/keycache.h
@@ -1,179 +1,179 @@
/* -*- mode: c++; c-basic-offset:4 -*-
models/keycache.h
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __KLEOPATRA_MODELS_KEYCACHE_H__
#define __KLEOPATRA_MODELS_KEYCACHE_H__
#include <QObject>
#include <kleo_export.h>
#include <gpgme++/global.h>
#include <memory>
#include <string>
#include <vector>
namespace GpgME
{
class Key;
class DecryptionResult;
class VerificationResult;
class KeyListResult;
class Subkey;
}
namespace KMime
{
namespace Types
{
class Mailbox;
}
}
namespace Kleo
{
class FileSystemWatcher;
class KeyGroup;
class KLEO_EXPORT KeyCache : public QObject
{
Q_OBJECT
protected:
explicit KeyCache();
public:
static std::shared_ptr<const KeyCache> instance();
static std::shared_ptr<KeyCache> mutableInstance();
~KeyCache();
void setGroupsConfig(const QString &filename);
void insert(const GpgME::Key &key);
void insert(const std::vector<GpgME::Key> &keys);
void refresh(const std::vector<GpgME::Key> &keys);
- void update(const KeyGroup &group);
+ bool update(const KeyGroup &group);
void remove(const GpgME::Key &key);
void remove(const std::vector<GpgME::Key> &keys);
void addFileSystemWatcher(const std::shared_ptr<FileSystemWatcher> &watcher);
void enableFileSystemWatcher(bool enable);
void setRefreshInterval(int hours);
int refreshInterval() const;
void enableRemarks(bool enable);
bool remarksEnabled() const;
const std::vector<GpgME::Key> &keys() const;
std::vector<GpgME::Key> secretKeys() const;
std::vector<KeyGroup> groups() const;
const GpgME::Key &findByFingerprint(const char *fpr) const;
const GpgME::Key &findByFingerprint(const std::string &fpr) const;
std::vector<GpgME::Key> findByEMailAddress(const char *email) const;
std::vector<GpgME::Key> findByEMailAddress(const std::string &email) const;
/** Look through the cache and search for the best key for a mailbox taking
* groups into account.
*
* The best key is the key with a UID for the provided mailbox that
* has the highest validity and a subkey that is capable for the given
* usage.
* If more then one key have a UID with the same validity
* the most recently created key is taken.
*
* The return value is a list because of groups. A group is
* a gnupg config entry that allows a user to configure multiple
* keys for a single mailbox. Only supported for OpenPGP currently.
*
* @returns only the "best" key for the mailbox, or a list if it is a group. */
std::vector<GpgME::Key> findBestByMailBox(const char *addr, GpgME::Protocol proto,
bool needSign, bool needEncrypt) const;
const GpgME::Key &findByShortKeyID(const char *id) const;
const GpgME::Key &findByShortKeyID(const std::string &id) const;
const GpgME::Key &findByKeyIDOrFingerprint(const char *id) const;
const GpgME::Key &findByKeyIDOrFingerprint(const std::string &id) const;
std::vector<GpgME::Key> findByKeyIDOrFingerprint(const std::vector<std::string> &ids) const;
const GpgME::Subkey &findSubkeyByKeyGrip(const char *grip, GpgME::Protocol protocol = GpgME::UnknownProtocol) const;
const GpgME::Subkey &findSubkeyByKeyGrip(const std::string &grip, GpgME::Protocol protocol = GpgME::UnknownProtocol) const;
std::vector<GpgME::Subkey> findSubkeysByKeyID(const std::vector<std::string> &ids) const;
std::vector<GpgME::Key> findRecipients(const GpgME::DecryptionResult &result) const;
std::vector<GpgME::Key> findSigners(const GpgME::VerificationResult &result) const;
std::vector<GpgME::Key> findSigningKeysByMailbox(const QString &mb) const;
std::vector<GpgME::Key> findEncryptionKeysByMailbox(const QString &mb) const;
/** Check for group keys.
*
* @returns A list of keys configured for groupName. Empty if no group cached.*/
std::vector<GpgME::Key> getGroupKeys(const QString &groupName) const;
enum Option {
NoOption = 0,
RecursiveSearch = 1,
IncludeSubject = 2
};
Q_DECLARE_FLAGS(Options, Option)
std::vector<GpgME::Key> findSubjects(const GpgME::Key &key, Options option = RecursiveSearch) const;
std::vector<GpgME::Key> findSubjects(const std::vector<GpgME::Key> &keys, Options options = RecursiveSearch) const;
std::vector<GpgME::Key> findSubjects(std::vector<GpgME::Key>::const_iterator first, std::vector<GpgME::Key>::const_iterator last, Options options = RecursiveSearch) const;
std::vector<GpgME::Key> findIssuers(const GpgME::Key &key, Options options = RecursiveSearch) const;
std::vector<GpgME::Key> findIssuers(const std::vector<GpgME::Key> &keys, Options options = RecursiveSearch) const;
std::vector<GpgME::Key> findIssuers(std::vector<GpgME::Key>::const_iterator first, std::vector<GpgME::Key>::const_iterator last, Options options = RecursiveSearch) const;
/** Check if at least one keylisting was finished. */
bool initialized() const;
/** Check if all keys have OpenPGP Protocol. */
bool pgpOnly() const;
public Q_SLOTS:
void clear();
void startKeyListing(GpgME::Protocol proto = GpgME::UnknownProtocol)
{
reload(proto);
}
void reload(GpgME::Protocol proto = GpgME::UnknownProtocol);
void cancelKeyListing();
Q_SIGNALS:
//void changed( const GpgME::Key & key );
void aboutToRemove(const GpgME::Key &key);
void added(const GpgME::Key &key);
void keyListingDone(const GpgME::KeyListResult &result);
void keysMayHaveChanged();
void groupUpdated(const KeyGroup &group);
private:
class RefreshKeysJob;
class Private;
QScopedPointer<Private> const d;
};
}
Q_DECLARE_OPERATORS_FOR_FLAGS(Kleo::KeyCache::Options)
#endif /* __KLEOPATRA_MODELS_KEYCACHE_H__ */
diff --git a/src/models/keylistmodel.cpp b/src/models/keylistmodel.cpp
index ce49988b..5eea7083 100644
--- a/src/models/keylistmodel.cpp
+++ b/src/models/keylistmodel.cpp
@@ -1,1238 +1,1234 @@
/* -*- mode: c++; c-basic-offset:4 -*-
models/keylistmodel.cpp
This file is part of libkleopatra, the KDE keymanagement library
SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "keylistmodel.h"
#include "keycache.h"
#include "keylist.h"
#include "kleo/keygroup.h"
#include "kleo/predicates.h"
#include "kleo/keyfiltermanager.h"
#include "kleo/keyfilter.h"
#include "utils/formatting.h"
#ifdef KLEO_MODEL_TEST
# include <QAbstractItemModelTester>
#endif
#include <Libkleo/KeyFilterManager>
#include <Libkleo/KeyFilter>
#include <KLocalizedString>
#include <QFont>
#include <QColor>
#include <QHash>
#include <QIcon>
#include <QDate>
#include <gpgme++/key.h>
#ifndef Q_MOC_RUN // QTBUG-22829
#include <boost/graph/topological_sort.hpp>
#include <boost/graph/adjacency_list.hpp>
#endif
#include <algorithm>
#include <map>
#include <set>
#include <iterator>
#include <gpgme++/gpgmepp_version.h>
#if GPGMEPP_VERSION >= 0x10E00 // 1.14.0
# define GPGME_HAS_REMARKS
#endif
using namespace GpgME;
using namespace Kleo;
using namespace Kleo::KeyList;
Q_DECLARE_METATYPE(GpgME::Key)
Q_DECLARE_METATYPE(KeyGroup)
class AbstractKeyListModel::Private
{
AbstractKeyListModel *const q;
public:
explicit Private(AbstractKeyListModel *qq);
void updateFromKeyCache();
public:
int m_toolTipOptions = Formatting::Validity;
mutable QHash<const char *, QVariant> prettyEMailCache;
mutable QHash<const char *, QVariant> remarksCache;
bool m_useKeyCache = false;
KeyList::Options m_keyListOptions = AllKeys;
std::vector<GpgME::Key> m_remarkKeys;
};
AbstractKeyListModel::Private::Private(Kleo::AbstractKeyListModel *qq)
: q(qq)
{
}
void AbstractKeyListModel::Private::updateFromKeyCache()
{
if (m_useKeyCache) {
q->setKeys(m_keyListOptions == SecretKeysOnly ? KeyCache::instance()->secretKeys() : KeyCache::instance()->keys());
if (m_keyListOptions == IncludeGroups) {
q->setGroups(KeyCache::instance()->groups());
}
}
}
AbstractKeyListModel::AbstractKeyListModel(QObject *p)
: QAbstractItemModel(p), KeyListModelInterface(), d(new Private(this))
{
}
AbstractKeyListModel::~AbstractKeyListModel() {}
void AbstractKeyListModel::setToolTipOptions(int opts)
{
d->m_toolTipOptions = opts;
}
int AbstractKeyListModel::toolTipOptions() const
{
return d->m_toolTipOptions;
}
void AbstractKeyListModel::setRemarkKeys(const std::vector<GpgME::Key> &keys)
{
d->m_remarkKeys = keys;
}
std::vector<GpgME::Key> AbstractKeyListModel::remarkKeys() const
{
return d->m_remarkKeys;
}
Key AbstractKeyListModel::key(const QModelIndex &idx) const
{
if (idx.isValid()) {
return doMapToKey(idx);
} else {
return Key::null;
}
}
std::vector<Key> AbstractKeyListModel::keys(const QList<QModelIndex> &indexes) const
{
std::vector<Key> result;
result.reserve(indexes.size());
std::transform(indexes.begin(), indexes.end(),
std::back_inserter(result),
[this](const QModelIndex &idx) {
return this->key(idx);
});
result.erase(std::remove_if(result.begin(), result.end(), std::mem_fn(&GpgME::Key::isNull)), result.end());
_detail::remove_duplicates_by_fpr(result);
return result;
}
KeyGroup AbstractKeyListModel::group(const QModelIndex &idx) const
{
if (idx.isValid()) {
return doMapToGroup(idx);
} else {
return KeyGroup();
}
}
QModelIndex AbstractKeyListModel::index(const Key &key) const
{
return index(key, 0);
}
QModelIndex AbstractKeyListModel::index(const Key &key, int col) const
{
if (key.isNull() || col < 0 || col >= NumColumns) {
return {};
} else {
return doMapFromKey(key, col);
}
}
QList<QModelIndex> AbstractKeyListModel::indexes(const std::vector<Key> &keys) const
{
QList<QModelIndex> result;
result.reserve(keys.size());
std::transform(keys.begin(), keys.end(),
std::back_inserter(result),
[this](const Key &key) {
return this->index(key);
});
return result;
}
QModelIndex AbstractKeyListModel::index(const KeyGroup &group) const
{
return index(group, 0);
}
QModelIndex AbstractKeyListModel::index(const KeyGroup &group, int col) const
{
if (group.isNull() || col < 0 || col >= NumColumns) {
return {};
} else {
return doMapFromGroup(group, col);
}
}
void AbstractKeyListModel::setKeys(const std::vector<Key> &keys)
{
clear(Keys);
addKeys(keys);
}
QModelIndex AbstractKeyListModel::addKey(const Key &key)
{
const std::vector<Key> vec(1, key);
const QList<QModelIndex> l = doAddKeys(vec);
return l.empty() ? QModelIndex() : l.front();
}
void AbstractKeyListModel::removeKey(const Key &key)
{
if (key.isNull()) {
return;
}
doRemoveKey(key);
d->prettyEMailCache.remove(key.primaryFingerprint());
d->remarksCache.remove(key.primaryFingerprint());
}
QList<QModelIndex> AbstractKeyListModel::addKeys(const std::vector<Key> &keys)
{
std::vector<Key> sorted;
sorted.reserve(keys.size());
std::remove_copy_if(keys.begin(), keys.end(),
std::back_inserter(sorted),
std::mem_fn(&Key::isNull));
std::sort(sorted.begin(), sorted.end(), _detail::ByFingerprint<std::less>());
return doAddKeys(sorted);
}
void AbstractKeyListModel::setGroups(const std::vector<KeyGroup> &groups)
{
clear(Groups);
doSetGroups(groups);
}
void AbstractKeyListModel::clear(ItemTypes types)
{
beginResetModel();
doClear(types);
if (types & Keys) {
d->prettyEMailCache.clear();
d->remarksCache.clear();
}
endResetModel();
}
int AbstractKeyListModel::columnCount(const QModelIndex &) const
{
return NumColumns;
}
QVariant AbstractKeyListModel::headerData(int section, Qt::Orientation o, int role) const
{
if (o == Qt::Horizontal)
if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::ToolTipRole)
switch (section) {
case PrettyName: return i18n("Name");
case PrettyEMail: return i18n("E-Mail");
case Validity: return i18n("User-IDs");
case ValidFrom: return i18n("Valid From");
case ValidUntil: return i18n("Valid Until");
case TechnicalDetails: return i18n("Protocol");
case ShortKeyID: return i18n("Key-ID");
case KeyID: return i18n("Key-ID");
case Fingerprint: return i18n("Fingerprint");
case Issuer: return i18n("Issuer");
case SerialNumber: return i18n("Serial Number");
case Origin: return i18n("Origin");
case LastUpdate: return i18n("Last Update");
case OwnerTrust: return i18n("Certification Trust");
case Remarks: return i18n("Tags");
case NumColumns:;
}
return QVariant();
}
static QVariant returnIfValid(const QColor &t)
{
if (t.isValid()) {
return t;
} else {
return QVariant();
}
}
static QVariant returnIfValid(const QIcon &t)
{
if (!t.isNull()) {
return t;
} else {
return QVariant();
}
}
QVariant AbstractKeyListModel::data(const QModelIndex &index, int role) const
{
const Key key = this->key(index);
if (!key.isNull()) {
return data(key, index.column(), role);
}
const KeyGroup group = this->group(index);
if (!group.isNull()) {
return data(group, index.column(), role);
}
return QVariant();
}
QVariant AbstractKeyListModel::data(const Key &key, int column, int role) const
{
if (role == Qt::DisplayRole || role == Qt::EditRole) {
switch (column) {
case PrettyName:
return Formatting::prettyName(key);
case PrettyEMail:
if (const char *const fpr = key.primaryFingerprint()) {
const QHash<const char *, QVariant>::const_iterator it = d->prettyEMailCache.constFind(fpr);
if (it != d->prettyEMailCache.constEnd()) {
return *it;
} else {
return d->prettyEMailCache[fpr] = Formatting::prettyEMail(key);
}
} else {
return QVariant();
}
case Validity:
return Formatting::complianceStringShort(key);
case ValidFrom:
if (role == Qt::EditRole) {
return Formatting::creationDate(key);
} else {
return Formatting::creationDateString(key);
}
case ValidUntil:
if (role == Qt::EditRole) {
return Formatting::expirationDate(key);
} else {
return Formatting::expirationDateString(key);
}
case TechnicalDetails:
return Formatting::type(key);
case ShortKeyID:
return QString::fromLatin1(key.shortKeyID());
case KeyID:
return Formatting::prettyID(key.keyID());
case Summary:
return Formatting::summaryLine(key);
case Fingerprint:
return Formatting::prettyID(key.primaryFingerprint());
case Issuer:
return QString::fromUtf8(key.issuerName());
case Origin:
return Formatting::origin(key.origin());
case LastUpdate:
return Formatting::dateString(key.lastUpdate());
case SerialNumber:
return QString::fromUtf8(key.issuerSerial());
case OwnerTrust:
return Formatting::ownerTrustShort(key.ownerTrust());
case Remarks:
#ifdef GPGME_HAS_REMARKS
{
const char *const fpr = key.primaryFingerprint();
if (fpr && key.protocol() == GpgME::OpenPGP && key.numUserIDs() &&
d->m_remarkKeys.size()) {
if (!(key.keyListMode() & GpgME::SignatureNotations)) {
return i18n("Loading...");
}
const QHash<const char *, QVariant>::const_iterator it = d->remarksCache.constFind(fpr);
if (it != d->remarksCache.constEnd()) {
return *it;
} else {
GpgME::Error err;
const auto remarks = key.userID(0).remarks(d->m_remarkKeys, err);
if (remarks.size() == 1) {
const auto remark = QString::fromStdString(remarks[0]);
return d->remarksCache[fpr] = remark;
} else {
QStringList remarkList;
remarkList.reserve(remarks.size());
for (const auto &rem: remarks) {
remarkList << QString::fromStdString(rem);
}
const auto remark = remarkList.join(QStringLiteral("; "));
return d->remarksCache[fpr] = remark;
}
}
} else {
return QVariant();
}
}
#endif
return QVariant();
case NumColumns:
break;
}
} else if (role == Qt::ToolTipRole) {
return Formatting::toolTip(key, toolTipOptions());
} else if (role == Qt::FontRole) {
return KeyFilterManager::instance()->font(key, (column == ShortKeyID || column == KeyID || column == Fingerprint) ? QFont(QStringLiteral("monospace")) : QFont());
} else if (role == Qt::DecorationRole) {
return column == Icon ? returnIfValid(KeyFilterManager::instance()->icon(key)) : QVariant();
} else if (role == Qt::BackgroundRole) {
return returnIfValid(KeyFilterManager::instance()->bgColor(key));
} else if (role == Qt::ForegroundRole) {
return returnIfValid(KeyFilterManager::instance()->fgColor(key));
} else if (role == FingerprintRole) {
return QString::fromLatin1(key.primaryFingerprint());
} else if (role == KeyRole) {
return QVariant::fromValue(key);
}
return QVariant();
}
QVariant AbstractKeyListModel::data(const KeyGroup &group, int column, int role) const
{
if (role == Qt::DisplayRole || role == Qt::EditRole) {
switch (column) {
case PrettyName:
return group.name();
case PrettyEMail:
return QVariant();
case Validity:
return Formatting::complianceStringShort(group);
case ValidFrom:
return QString();
case ValidUntil:
return QString();
case TechnicalDetails:
return Formatting::type(group);
case ShortKeyID:
return QString();
case KeyID:
return QString();
case Summary:
return Formatting::summaryLine(group); // used for filtering
case Fingerprint:
return QString();
case Issuer:
return QString();
case Origin:
return QString();
case LastUpdate:
return QString();
case SerialNumber:
return QString();
case OwnerTrust:
return QString();
case Remarks:
return QVariant();
case NumColumns:
break;
}
} else if (role == Qt::ToolTipRole) {
return Formatting::toolTip(group, toolTipOptions());
} else if (role == Qt::FontRole) {
return QFont();
} else if (role == Qt::DecorationRole) {
return column == Icon ? QIcon::fromTheme("group") : QVariant();
} else if (role == Qt::BackgroundRole) {
} else if (role == Qt::ForegroundRole) {
} else if (role == GroupRole) {
return QVariant::fromValue(group);
}
return QVariant();
}
namespace
{
template <typename Base>
class TableModelMixin : public Base
{
public:
explicit TableModelMixin(QObject *p = nullptr) : Base(p) {}
~TableModelMixin() {}
using Base::index;
QModelIndex index(int row, int column, const QModelIndex &pidx = QModelIndex()) const override
{
return this->hasIndex(row, column, pidx) ? this->createIndex(row, column, nullptr) : QModelIndex();
}
private:
QModelIndex parent(const QModelIndex &) const override
{
return QModelIndex();
}
bool hasChildren(const QModelIndex &pidx) const override
{
return (pidx.model() == this || !pidx.isValid()) && this->rowCount(pidx) > 0 && this->columnCount(pidx) > 0;
}
};
class FlatKeyListModel
#ifndef Q_MOC_RUN
: public TableModelMixin<AbstractKeyListModel>
#else
: public AbstractKeyListModel
#endif
{
Q_OBJECT
public:
explicit FlatKeyListModel(QObject *parent = nullptr);
~FlatKeyListModel() override;
int rowCount(const QModelIndex &pidx) const override
{
return pidx.isValid() ? 0 : mKeysByFingerprint.size() + mGroups.size();
}
private:
Key doMapToKey(const QModelIndex &index) const override;
QModelIndex doMapFromKey(const Key &key, int col) const override;
QList<QModelIndex> doAddKeys(const std::vector<Key> &keys) override;
void doRemoveKey(const Key &key) override;
KeyGroup doMapToGroup(const QModelIndex &index) const override;
QModelIndex doMapFromGroup(const KeyGroup &group, int column) const override;
void doSetGroups(const std::vector<KeyGroup> &groups) override;
void doClear(ItemTypes types) override
{
if (types & Keys) {
mKeysByFingerprint.clear();
}
if (types & Groups) {
mGroups.clear();
}
}
private:
std::vector<Key> mKeysByFingerprint;
std::vector<KeyGroup> mGroups;
};
class HierarchicalKeyListModel : public AbstractKeyListModel
{
Q_OBJECT
public:
explicit HierarchicalKeyListModel(QObject *parent = nullptr);
~HierarchicalKeyListModel() override;
int rowCount(const QModelIndex &pidx) const override;
using AbstractKeyListModel::index;
QModelIndex index(int row, int col, const QModelIndex &pidx) const override;
QModelIndex parent(const QModelIndex &idx) const override;
bool hasChildren(const QModelIndex &pidx) const override
{
return rowCount(pidx) > 0;
}
private:
Key doMapToKey(const QModelIndex &index) const override;
QModelIndex doMapFromKey(const Key &key, int col) const override;
QList<QModelIndex> doAddKeys(const std::vector<Key> &keys) override;
void doRemoveKey(const Key &key) override;
KeyGroup doMapToGroup(const QModelIndex &index) const override;
QModelIndex doMapFromGroup(const KeyGroup &group, int column) const override;
void doSetGroups(const std::vector<KeyGroup> &groups) override;
void doClear(ItemTypes types) override
{
if (types & Keys) {
mTopLevels.clear();
mKeysByFingerprint.clear();
mKeysByExistingParent.clear();
mKeysByNonExistingParent.clear();
}
if (types & Groups) {
mGroups.clear();
}
}
private:
void addTopLevelKey(const Key &key);
void addKeyWithParent(const char *issuer_fpr, const Key &key);
void addKeyWithoutParent(const char *issuer_fpr, const Key &key);
private:
typedef std::map< std::string, std::vector<Key> > Map;
std::vector<Key> mKeysByFingerprint; // all keys
Map mKeysByExistingParent, mKeysByNonExistingParent; // parent->child map
std::vector<Key> mTopLevels; // all roots + parent-less
std::vector<KeyGroup> mGroups;
};
static const char *cleanChainID(const Key &key)
{
if (key.isRoot()) {
return "";
}
if (const char *chid = key.chainID()) {
return chid;
}
return "";
}
}
FlatKeyListModel::FlatKeyListModel(QObject *p)
: TableModelMixin<AbstractKeyListModel>(p)
{
}
FlatKeyListModel::~FlatKeyListModel() {}
Key FlatKeyListModel::doMapToKey(const QModelIndex &idx) const
{
Q_ASSERT(idx.isValid());
if (static_cast<unsigned>(idx.row()) < mKeysByFingerprint.size() && idx.column() < NumColumns) {
return mKeysByFingerprint[ idx.row() ];
} else {
return Key::null;
}
}
QModelIndex FlatKeyListModel::doMapFromKey(const Key &key, int col) const
{
Q_ASSERT(!key.isNull());
const std::vector<Key>::const_iterator it
= std::lower_bound(mKeysByFingerprint.begin(), mKeysByFingerprint.end(),
key, _detail::ByFingerprint<std::less>());
if (it == mKeysByFingerprint.end() || !_detail::ByFingerprint<std::equal_to>()(*it, key)) {
return {};
} else {
return createIndex(it - mKeysByFingerprint.begin(), col);
}
}
QList<QModelIndex> FlatKeyListModel::doAddKeys(const std::vector<Key> &keys)
{
Q_ASSERT(std::is_sorted(keys.begin(), keys.end(), _detail::ByFingerprint<std::less>()));
if (keys.empty()) {
return QList<QModelIndex>();
}
for (auto it = keys.begin(), end = keys.end(); it != end; ++it) {
// find an insertion point:
const std::vector<Key>::iterator pos = std::upper_bound(mKeysByFingerprint.begin(), mKeysByFingerprint.end(), *it, _detail::ByFingerprint<std::less>());
const unsigned int idx = std::distance(mKeysByFingerprint.begin(), pos);
if (idx > 0 && qstrcmp(mKeysByFingerprint[idx - 1].primaryFingerprint(), it->primaryFingerprint()) == 0) {
// key existed before - replace with new one:
mKeysByFingerprint[idx - 1] = *it;
Q_EMIT dataChanged(createIndex(idx - 1, 0), createIndex(idx - 1, NumColumns - 1));
} else {
// new key - insert:
beginInsertRows(QModelIndex(), idx, idx);
mKeysByFingerprint.insert(pos, *it);
endInsertRows();
}
}
return indexes(keys);
}
void FlatKeyListModel::doRemoveKey(const Key &key)
{
const std::vector<Key>::iterator it
= qBinaryFind(mKeysByFingerprint.begin(), mKeysByFingerprint.end(),
key, _detail::ByFingerprint<std::less>());
if (it == mKeysByFingerprint.end()) {
return;
}
const unsigned int row = std::distance(mKeysByFingerprint.begin(), it);
beginRemoveRows(QModelIndex(), row, row);
mKeysByFingerprint.erase(it);
endRemoveRows();
}
KeyGroup FlatKeyListModel::doMapToGroup(const QModelIndex &idx) const
{
Q_ASSERT(idx.isValid());
if (static_cast<unsigned>(idx.row()) >= mKeysByFingerprint.size()
&& static_cast<unsigned>(idx.row()) < mKeysByFingerprint.size() + mGroups.size()
&& idx.column() < NumColumns) {
return mGroups[ idx.row() - mKeysByFingerprint.size() ];
} else {
return KeyGroup();
}
}
QModelIndex FlatKeyListModel::doMapFromGroup(const KeyGroup &group, int column) const
{
Q_ASSERT(!group.isNull());
- const auto it = std::find_if(mGroups.begin(), mGroups.end(),
+ const auto it = std::find_if(mGroups.cbegin(), mGroups.cend(),
[group](const KeyGroup &g) {
- return g.source() == group.source()
- && g.id() == group.id()
- && g.name() == group.name();
+ return g.source() == group.source() && g.id() == group.id();
});
- if (it == mGroups.end()) {
+ if (it == mGroups.cend()) {
return QModelIndex();
} else {
- return createIndex(it - mGroups.begin() + mKeysByFingerprint.size(), column);
+ return createIndex(it - mGroups.cbegin() + mKeysByFingerprint.size(), column);
}
}
void FlatKeyListModel::doSetGroups(const std::vector<KeyGroup> &groups)
{
Q_ASSERT(mGroups.empty()); // ensure that groups have been cleared
const int first = mKeysByFingerprint.size();
const int last = first + groups.size() - 1;
beginInsertRows(QModelIndex(), first, last);
mGroups = groups;
endInsertRows();
}
HierarchicalKeyListModel::HierarchicalKeyListModel(QObject *p)
: AbstractKeyListModel(p),
mKeysByFingerprint(),
mKeysByExistingParent(),
mKeysByNonExistingParent(),
mTopLevels()
{
}
HierarchicalKeyListModel::~HierarchicalKeyListModel() {}
int HierarchicalKeyListModel::rowCount(const QModelIndex &pidx) const
{
// toplevel item:
if (!pidx.isValid()) {
return mTopLevels.size() + mGroups.size();
}
if (pidx.column() != 0) {
return 0;
}
// non-toplevel item - find the number of subjects for this issuer:
const Key issuer = this->key(pidx);
const char *const fpr = issuer.primaryFingerprint();
if (!fpr || !*fpr) {
return 0;
}
const Map::const_iterator it = mKeysByExistingParent.find(fpr);
if (it == mKeysByExistingParent.end()) {
return 0;
}
return it->second.size();
}
QModelIndex HierarchicalKeyListModel::index(int row, int col, const QModelIndex &pidx) const
{
if (row < 0 || col < 0 || col >= NumColumns) {
return {};
}
// toplevel item:
if (!pidx.isValid()) {
if (static_cast<unsigned>(row) < mTopLevels.size()) {
return index(mTopLevels[row], col);
} else if (static_cast<unsigned>(row) < mTopLevels.size() + mGroups.size()) {
return index(mGroups[row - mTopLevels.size()], col);
} else {
return QModelIndex();
}
}
// non-toplevel item - find the row'th subject of this key:
const Key issuer = this->key(pidx);
const char *const fpr = issuer.primaryFingerprint();
if (!fpr || !*fpr) {
return QModelIndex();
}
const Map::const_iterator it = mKeysByExistingParent.find(fpr);
if (it == mKeysByExistingParent.end() || static_cast<unsigned>(row) >= it->second.size()) {
return QModelIndex();
}
return index(it->second[row], col);
}
QModelIndex HierarchicalKeyListModel::parent(const QModelIndex &idx) const
{
const Key key = this->key(idx);
if (key.isNull() || key.isRoot()) {
return {};
}
const std::vector<Key>::const_iterator it
= qBinaryFind(mKeysByFingerprint.begin(), mKeysByFingerprint.end(),
cleanChainID(key), _detail::ByFingerprint<std::less>());
return it != mKeysByFingerprint.end() ? index(*it) : QModelIndex();
}
Key HierarchicalKeyListModel::doMapToKey(const QModelIndex &idx) const
{
if (!idx.isValid()) {
return Key::null;
}
const char *const issuer_fpr = static_cast<const char *>(idx.internalPointer());
if (!issuer_fpr || !*issuer_fpr) {
// top-level:
if (static_cast<unsigned>(idx.row()) >= mTopLevels.size()) {
return Key::null;
} else {
return mTopLevels[idx.row()];
}
}
// non-toplevel:
const Map::const_iterator it
= mKeysByExistingParent.find(issuer_fpr);
if (it == mKeysByExistingParent.end() || static_cast<unsigned>(idx.row()) >= it->second.size()) {
return Key::null;
}
return it->second[idx.row()];
}
QModelIndex HierarchicalKeyListModel::doMapFromKey(const Key &key, int col) const
{
if (key.isNull()) {
return {};
}
const char *issuer_fpr = cleanChainID(key);
// we need to look in the toplevels list,...
const std::vector<Key> *v = &mTopLevels;
if (issuer_fpr && *issuer_fpr) {
const std::map< std::string, std::vector<Key> >::const_iterator it
= mKeysByExistingParent.find(issuer_fpr);
// ...unless we find an existing parent:
if (it != mKeysByExistingParent.end()) {
v = &it->second;
} else {
issuer_fpr = nullptr; // force internalPointer to zero for toplevels
}
}
const std::vector<Key>::const_iterator it
= std::lower_bound(v->begin(), v->end(), key, _detail::ByFingerprint<std::less>());
if (it == v->end() || !_detail::ByFingerprint<std::equal_to>()(*it, key)) {
return QModelIndex();
}
const unsigned int row = std::distance(v->begin(), it);
return createIndex(row, col, const_cast<char * /* thanks, Trolls :/ */ >(issuer_fpr));
}
void HierarchicalKeyListModel::addKeyWithParent(const char *issuer_fpr, const Key &key)
{
Q_ASSERT(issuer_fpr); Q_ASSERT(*issuer_fpr); Q_ASSERT(!key.isNull());
std::vector<Key> &subjects = mKeysByExistingParent[issuer_fpr];
// find insertion point:
const std::vector<Key>::iterator it = std::lower_bound(subjects.begin(), subjects.end(), key, _detail::ByFingerprint<std::less>());
const int row = std::distance(subjects.begin(), it);
if (it != subjects.end() && qstricmp(it->primaryFingerprint(), key.primaryFingerprint()) == 0) {
// exists -> replace
*it = key;
Q_EMIT dataChanged(createIndex(row, 0, const_cast<char *>(issuer_fpr)), createIndex(row, NumColumns - 1, const_cast<char *>(issuer_fpr)));
} else {
// doesn't exist -> insert
const std::vector<Key>::const_iterator pos = qBinaryFind(mKeysByFingerprint.begin(), mKeysByFingerprint.end(), issuer_fpr, _detail::ByFingerprint<std::less>());
Q_ASSERT(pos != mKeysByFingerprint.end());
beginInsertRows(index(*pos), row, row);
subjects.insert(it, key);
endInsertRows();
}
}
void HierarchicalKeyListModel::addKeyWithoutParent(const char *issuer_fpr, const Key &key)
{
Q_ASSERT(issuer_fpr); Q_ASSERT(*issuer_fpr); Q_ASSERT(!key.isNull());
std::vector<Key> &subjects = mKeysByNonExistingParent[issuer_fpr];
// find insertion point:
const std::vector<Key>::iterator it = std::lower_bound(subjects.begin(), subjects.end(), key, _detail::ByFingerprint<std::less>());
if (it != subjects.end() && qstricmp(it->primaryFingerprint(), key.primaryFingerprint()) == 0)
// exists -> replace
{
*it = key;
} else
// doesn't exist -> insert
{
subjects.insert(it, key);
}
addTopLevelKey(key);
}
void HierarchicalKeyListModel::addTopLevelKey(const Key &key)
{
// find insertion point:
const std::vector<Key>::iterator it = std::lower_bound(mTopLevels.begin(), mTopLevels.end(), key, _detail::ByFingerprint<std::less>());
const int row = std::distance(mTopLevels.begin(), it);
if (it != mTopLevels.end() && qstricmp(it->primaryFingerprint(), key.primaryFingerprint()) == 0) {
// exists -> replace
*it = key;
Q_EMIT dataChanged(createIndex(row, 0), createIndex(row, NumColumns - 1));
} else {
// doesn't exist -> insert
beginInsertRows(QModelIndex(), row, row);
mTopLevels.insert(it, key);
endInsertRows();
}
}
// sorts 'keys' such that parent always come before their children:
static std::vector<Key> topological_sort(const std::vector<Key> &keys)
{
boost::adjacency_list<> graph(keys.size());
// add edges from children to parents:
for (unsigned int i = 0, end = keys.size(); i != end; ++i) {
const char *const issuer_fpr = cleanChainID(keys[i]);
if (!issuer_fpr || !*issuer_fpr) {
continue;
}
const std::vector<Key>::const_iterator it
= qBinaryFind(keys.begin(), keys.end(), issuer_fpr, _detail::ByFingerprint<std::less>());
if (it == keys.end()) {
continue;
}
add_edge(i, std::distance(keys.begin(), it), graph);
}
std::vector<int> order;
order.reserve(keys.size());
topological_sort(graph, std::back_inserter(order));
Q_ASSERT(order.size() == keys.size());
std::vector<Key> result;
result.reserve(keys.size());
for (int i : qAsConst(order)) {
result.push_back(keys[i]);
}
return result;
}
QList<QModelIndex> HierarchicalKeyListModel::doAddKeys(const std::vector<Key> &keys)
{
Q_ASSERT(std::is_sorted(keys.begin(), keys.end(), _detail::ByFingerprint<std::less>()));
if (keys.empty()) {
return QList<QModelIndex>();
}
const std::vector<Key> oldKeys = mKeysByFingerprint;
std::vector<Key> merged;
merged.reserve(keys.size() + mKeysByFingerprint.size());
std::set_union(keys.begin(), keys.end(),
mKeysByFingerprint.begin(), mKeysByFingerprint.end(),
std::back_inserter(merged), _detail::ByFingerprint<std::less>());
mKeysByFingerprint = merged;
std::set<Key, _detail::ByFingerprint<std::less> > changedParents;
const auto topologicalSortedList = topological_sort(keys);
for (const Key &key : topologicalSortedList) {
// check to see whether this key is a parent for a previously parent-less group:
const char *const fpr = key.primaryFingerprint();
if (!fpr || !*fpr) {
continue;
}
const bool keyAlreadyExisted = qBinaryFind(oldKeys.begin(), oldKeys.end(), key, _detail::ByFingerprint<std::less>()) != oldKeys.end();
const Map::iterator it = mKeysByNonExistingParent.find(fpr);
const std::vector<Key> children = it != mKeysByNonExistingParent.end() ? it->second : std::vector<Key>();
if (it != mKeysByNonExistingParent.end()) {
mKeysByNonExistingParent.erase(it);
}
// Step 1: For new keys, remove children from toplevel:
if (!keyAlreadyExisted) {
auto last = mTopLevels.begin();
auto lastFP = mKeysByFingerprint.begin();
for (const Key &k : qAsConst(children)) {
last = qBinaryFind(last, mTopLevels.end(), k, _detail::ByFingerprint<std::less>());
Q_ASSERT(last != mTopLevels.end());
const int row = std::distance(mTopLevels.begin(), last);
lastFP = qBinaryFind(lastFP, mKeysByFingerprint.end(), k, _detail::ByFingerprint<std::less>());
Q_ASSERT(lastFP != mKeysByFingerprint.end());
Q_EMIT rowAboutToBeMoved(QModelIndex(), row);
beginRemoveRows(QModelIndex(), row, row);
last = mTopLevels.erase(last);
lastFP = mKeysByFingerprint.erase(lastFP);
endRemoveRows();
}
}
// Step 2: add/update key
const char *const issuer_fpr = cleanChainID(key);
if (!issuer_fpr || !*issuer_fpr)
// root or something...
{
addTopLevelKey(key);
} else if (std::binary_search(mKeysByFingerprint.begin(), mKeysByFingerprint.end(), issuer_fpr, _detail::ByFingerprint<std::less>()))
// parent exists...
{
addKeyWithParent(issuer_fpr, key);
} else
// parent doesn't exist yet...
{
addKeyWithoutParent(issuer_fpr, key);
}
const QModelIndex key_idx = index(key);
QModelIndex key_parent = key_idx.parent();
while (key_parent.isValid()) {
changedParents.insert(doMapToKey(key_parent));
key_parent = key_parent.parent();
}
// Step 3: Add children to new parent ( == key )
if (!keyAlreadyExisted && !children.empty()) {
addKeys(children);
const QModelIndex new_parent = index(key);
// Q_EMIT the rowMoved() signals in reversed direction, so the
// implementation can use a stack for mapping.
for (int i = children.size() - 1; i >= 0; --i) {
Q_EMIT rowMoved(new_parent, i);
}
}
}
//Q_EMIT dataChanged for all parents with new children. This triggers KeyListSortFilterProxyModel to
//show a parent node if it just got children matching the proxy's filter
for (const Key &i : qAsConst(changedParents)) {
const QModelIndex idx = index(i);
if (idx.isValid()) {
Q_EMIT dataChanged(idx.sibling(idx.row(), 0), idx.sibling(idx.row(), NumColumns - 1));
}
}
return indexes(keys);
}
void HierarchicalKeyListModel::doRemoveKey(const Key &key)
{
const QModelIndex idx = index(key);
if (!idx.isValid()) {
return;
}
const char *const fpr = key.primaryFingerprint();
if (mKeysByExistingParent.find(fpr) != mKeysByExistingParent.end()) {
//handle non-leave nodes:
std::vector<Key> keys = mKeysByFingerprint;
const std::vector<Key>::iterator it = qBinaryFind(keys.begin(), keys.end(),
key, _detail::ByFingerprint<std::less>());
if (it == keys.end()) {
return;
}
keys.erase(it);
// FIXME for simplicity, we just clear the model and re-add all keys minus the removed one. This is suboptimal,
// but acceptable given that deletion of non-leave nodes is rather rare.
clear(Keys);
addKeys(keys);
return;
}
//handle leave nodes:
const std::vector<Key>::iterator it = qBinaryFind(mKeysByFingerprint.begin(), mKeysByFingerprint.end(),
key, _detail::ByFingerprint<std::less>());
Q_ASSERT(it != mKeysByFingerprint.end());
Q_ASSERT(mKeysByNonExistingParent.find(fpr) == mKeysByNonExistingParent.end());
Q_ASSERT(mKeysByExistingParent.find(fpr) == mKeysByExistingParent.end());
beginRemoveRows(parent(idx), idx.row(), idx.row());
mKeysByFingerprint.erase(it);
const char *const issuer_fpr = cleanChainID(key);
const std::vector<Key>::iterator tlIt = qBinaryFind(mTopLevels.begin(), mTopLevels.end(), key, _detail::ByFingerprint<std::less>());
if (tlIt != mTopLevels.end()) {
mTopLevels.erase(tlIt);
}
if (issuer_fpr && *issuer_fpr) {
const Map::iterator nexIt = mKeysByNonExistingParent.find(issuer_fpr);
if (nexIt != mKeysByNonExistingParent.end()) {
const std::vector<Key>::iterator eit = qBinaryFind(nexIt->second.begin(), nexIt->second.end(), key, _detail::ByFingerprint<std::less>());
if (eit != nexIt->second.end()) {
nexIt->second.erase(eit);
}
if (nexIt->second.empty()) {
mKeysByNonExistingParent.erase(nexIt);
}
}
const Map::iterator exIt = mKeysByExistingParent.find(issuer_fpr);
if (exIt != mKeysByExistingParent.end()) {
const std::vector<Key>::iterator eit = qBinaryFind(exIt->second.begin(), exIt->second.end(), key, _detail::ByFingerprint<std::less>());
if (eit != exIt->second.end()) {
exIt->second.erase(eit);
}
if (exIt->second.empty()) {
mKeysByExistingParent.erase(exIt);
}
}
}
endRemoveRows();
}
KeyGroup HierarchicalKeyListModel::doMapToGroup(const QModelIndex &idx) const
{
Q_ASSERT(idx.isValid());
if (idx.parent().isValid()) {
// groups are always top-level
return KeyGroup();
}
if (static_cast<unsigned>(idx.row()) >= mTopLevels.size()
&& static_cast<unsigned>(idx.row()) < mTopLevels.size() + mGroups.size()
&& idx.column() < NumColumns) {
return mGroups[ idx.row() - mTopLevels.size() ];
} else {
return KeyGroup();
}
}
QModelIndex HierarchicalKeyListModel::doMapFromGroup(const KeyGroup &group, int column) const
{
Q_ASSERT(!group.isNull());
- const auto it = std::find_if(mGroups.begin(), mGroups.end(),
+ const auto it = std::find_if(mGroups.cbegin(), mGroups.cend(),
[group](const KeyGroup &g) {
- return g.source() == group.source()
- && g.id() == group.id()
- && g.name() == group.name();
+ return g.source() == group.source() && g.id() == group.id();
});
- if (it == mGroups.end()) {
+ if (it == mGroups.cend()) {
return QModelIndex();
} else {
- return createIndex(it - mGroups.begin() + mTopLevels.size(), column);
+ return createIndex(it - mGroups.cbegin() + mTopLevels.size(), column);
}
}
void HierarchicalKeyListModel::doSetGroups(const std::vector<KeyGroup> &groups)
{
Q_ASSERT(mGroups.empty()); // ensure that groups have been cleared
const int first = mTopLevels.size();
const int last = first + groups.size() - 1;
beginInsertRows(QModelIndex(), first, last);
mGroups = groups;
endInsertRows();
}
void AbstractKeyListModel::useKeyCache(bool value, KeyList::Options options)
{
d->m_keyListOptions = options;
d->m_useKeyCache = value;
if (!d->m_useKeyCache) {
clear(All);
} else {
d->updateFromKeyCache();
}
connect(KeyCache::instance().get(), &KeyCache::keysMayHaveChanged,
this, [this] { d->updateFromKeyCache(); });
}
// static
AbstractKeyListModel *AbstractKeyListModel::createFlatKeyListModel(QObject *p)
{
AbstractKeyListModel *const m = new FlatKeyListModel(p);
#ifdef KLEO_MODEL_TEST
new QAbstractItemModelTester(m, p);
#endif
return m;
}
// static
AbstractKeyListModel *AbstractKeyListModel::createHierarchicalKeyListModel(QObject *p)
{
AbstractKeyListModel *const m = new HierarchicalKeyListModel(p);
#ifdef KLEO_MODEL_TEST
new QAbstractItemModelTester(m, p);
#endif
return m;
}
#include "keylistmodel.moc"
/*!
\fn AbstractKeyListModel::rowAboutToBeMoved( const QModelIndex & old_parent, int old_row )
Emitted before the removal of a row from that model. It will later
be added to the model again, in response to which rowMoved() will be
emitted. If multiple rows are moved in one go, multiple
rowAboutToBeMoved() signals are emitted before the corresponding
number of rowMoved() signals is emitted - in reverse order.
This works around the absence of move semantics in
QAbstractItemModel. Clients can maintain a stack to perform the
QModelIndex-mapping themselves, or, e.g., to preserve the selection
status of the row:
\code
std::vector<bool> mMovingRowWasSelected; // transient, used when rows are moved
// ...
void slotRowAboutToBeMoved( const QModelIndex & p, int row ) {
mMovingRowWasSelected.push_back( selectionModel()->isSelected( model()->index( row, 0, p ) ) );
}
void slotRowMoved( const QModelIndex & p, int row ) {
const bool wasSelected = mMovingRowWasSelected.back();
mMovingRowWasSelected.pop_back();
if ( wasSelected )
selectionModel()->select( model()->index( row, 0, p ), Select|Rows );
}
\endcode
A similar mechanism could be used to preserve the current item during moves.
*/
/*!
\fn AbstractKeyListModel::rowMoved( const QModelIndex & new_parent, int new_parent )
See rowAboutToBeMoved()
*/

File Metadata

Mime Type
text/x-diff
Expires
Sat, Jan 17, 2:28 AM (4 h, 20 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
3a/45/a2346bc0e50f37e221964ab893a1

Event Timeline