11#include <config-libkleo.h>
13#include "keygroupconfig.h"
18#include <libkleo/keycache.h>
19#include <libkleo/keyhelpers.h>
20#include <libkleo/qtstlhelpers.h>
22#include <libkleo_debug.h>
24#include <KConfigGroup>
25#include <KSharedConfig>
29#include <gpgme++/key.h>
34static const QString groupNamePrefix = QStringLiteral(
"Group-");
36class KeyGroupConfig::Private
39 explicit Private(
const QString &filename);
41 std::vector<KeyGroup> readGroups()
const;
42 KeyGroup writeGroup(
const KeyGroup &group);
43 bool removeGroup(
const KeyGroup &group);
46 KeyGroup readGroup(
const KSharedConfigPtr &groupsConfig,
const QString &groupId)
const;
52KeyGroupConfig::Private::Private(
const QString &filename)
56 qCWarning(LIBKLEO_LOG) << __func__ <<
"Warning: name of configuration file is empty";
60KeyGroup KeyGroupConfig::Private::readGroup(
const KSharedConfigPtr &groupsConfig,
const QString &groupId)
const
66 const std::vector<Key> groupKeys = KeyCache::instance()->findByFingerprint(fingerprints);
71 || std::any_of(entries.
begin(), entries.
end(), [configGroup](
const QString &entry) {
72 return configGroup.isEntryImmutable(entry);
75 KeyGroup g(groupId, groupName, groupKeys, KeyGroup::ApplicationConfig);
76 g.setIsImmutable(isImmutable);
82std::vector<KeyGroup> KeyGroupConfig::Private::readGroups()
const
84 qCDebug(LIBKLEO_LOG) << __func__ <<
"Reading groups";
85 std::vector<KeyGroup> groups;
92 const QStringList configGroups = groupsConfig->groupList();
93 for (
const QString &configGroupName : configGroups) {
95 if (configGroupName.startsWith(groupNamePrefix)) {
96 const QString keyGroupId = configGroupName.
mid(groupNamePrefix.
size());
98 qCWarning(LIBKLEO_LOG) <<
"Config group" << configGroupName <<
"has empty group id";
101 KeyGroup group = readGroup(groupsConfig, keyGroupId);
102 groups.push_back(group);
109KeyGroup KeyGroupConfig::Private::writeGroup(
const KeyGroup &group)
115 if (group.isNull()) {
116 qCDebug(LIBKLEO_LOG) << __func__ <<
"Error: group is null";
123 qCDebug(LIBKLEO_LOG) << __func__ <<
"Writing config group" << configGroup.
name();
125 configGroup.
writeEntry(
"Keys", Kleo::getFingerprints(group.keys()));
128 return readGroup(groupsConfig, group.id());
131bool KeyGroupConfig::Private::removeGroup(
const KeyGroup &group)
137 if (group.isNull()) {
138 qCDebug(LIBKLEO_LOG) << __func__ <<
"Error: group is null";
145 qCDebug(LIBKLEO_LOG) << __func__ <<
"Removing config group" << configGroup.
name();
151KeyGroupConfig::KeyGroupConfig(
const QString &filename)
152 : d{std::make_unique<Private>(filename)}
156KeyGroupConfig::~KeyGroupConfig() =
default;
158std::vector<KeyGroup> KeyGroupConfig::readGroups()
const
160 return d->readGroups();
163KeyGroup KeyGroupConfig::writeGroup(
const KeyGroup &group)
165 return d->writeGroup(group);
168void KeyGroupConfig::writeGroups(
const std::vector<KeyGroup> &groups)
170 std::for_each(std::begin(groups), std::end(groups), [
this](
const auto &group) {
171 d->writeGroup(group);
175bool KeyGroupConfig::removeGroup(
const KeyGroup &group)
177 return d->removeGroup(group);
KConfigGroup group(const QString &group)
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
void deleteGroup(const QString &group, WriteConfigFlags flags=Normal)
bool isImmutable() const override
QString readEntry(const char *key, const char *aDefault=nullptr) const
QStringList keyList() const
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
bool isEmpty() const const
QString mid(qsizetype position, qsizetype n) const const
qsizetype size() const const