Commit cef028c2 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Allow updating a group of keys

Only groups of keys stored in the application configuration can be
changed/updated.

GnuPG-bug-id: 5175, 5239
parent 89cbb4ea
......@@ -29,6 +29,7 @@ public:
QString configName;
Keys keys;
Source source;
bool isImmutable = true;
};
KeyGroup::Private::Private(Id id, const QString &name, const std::vector<Key> &keys, Source source)
......@@ -104,6 +105,18 @@ 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()) {
......
......@@ -63,6 +63,9 @@ public:
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);
......
......@@ -55,6 +55,8 @@ using namespace KMime::Types;
static const unsigned int hours2ms = 1000 * 60 * 60;
static const QString groupNamePrefix = QStringLiteral("Group-");
//
//
// KeyCache
......@@ -201,10 +203,12 @@ public:
return groupKeys;
}
void addGroup(const QString &name, const std::vector<GpgME::Key> &keys, KeyGroup::Source source, const QString &configName = QString())
void addGroup(const QString &name, const std::vector<GpgME::Key> &keys, KeyGroup::Source source,
const QString &configName = QString(), bool isImmutable = true)
{
KeyGroup g(m_groups.size(), name, keys, source);
g.setConfigName(configName);
g.setIsImmutable(isImmutable);
m_groups.push_back(g);
}
......@@ -249,8 +253,6 @@ public:
void readGroupsFromGroupsConfig()
{
static const QString groupNamePrefix = QStringLiteral("Group-");
if (m_groupsConfigName.isEmpty()) {
return;
}
......@@ -274,11 +276,37 @@ public:
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;
addGroup(keyGroupName, groupKeys, KeyGroup::ApplicationConfig, keyGroupId);
const bool isImmutable = configGroup.isEntryImmutable("Keys");
addGroup(keyGroupName, groupKeys, KeyGroup::ApplicationConfig, keyGroupId, isImmutable);
}
}
}
void writeGroupToGroupsConfig(const KeyGroup &group)
{
Q_ASSERT(!group.configName().isEmpty());
if (m_groupsConfigName.isEmpty()) {
return;
}
KSharedConfigPtr groupsConfig = KSharedConfig::openConfig(m_groupsConfigName);
KConfigGroup configGroup = groupsConfig->group(groupNamePrefix + group.configName());
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);
}
void updateGroupCache()
{
// Update Group Keys
......@@ -960,6 +988,22 @@ std::vector<KeyGroup> KeyCache::groups() const
return d->m_groups;
}
void 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());
d->writeGroupToGroupsConfig(group);
d->m_groups[group.id()] = group;
Q_EMIT groupUpdated(group);
Q_EMIT keysMayHaveChanged();
}
void KeyCache::refresh(const std::vector<Key> &keys)
{
// make this better...
......
......@@ -60,6 +60,7 @@ public:
void insert(const std::vector<GpgME::Key> &keys);
void refresh(const std::vector<GpgME::Key> &keys);
void update(const KeyGroup &group);
void remove(const GpgME::Key &key);
void remove(const std::vector<GpgME::Key> &keys);
......@@ -162,6 +163,7 @@ Q_SIGNALS:
void added(const GpgME::Key &key);
void keyListingDone(const GpgME::KeyListResult &result);
void keysMayHaveChanged();
void groupUpdated(const KeyGroup &group);
private:
class RefreshKeysJob;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment