Libkleo

kconfigbasedkeyfilter.cpp
1/*
2 kconfigbasedkeyfilter.cpp
3
4 This file is part of libkleopatra, the KDE keymanagement library
5 SPDX-FileCopyrightText: 2004 Klarälvdalens Datakonsult AB
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9
10#include <config-libkleo.h>
11
12#include "kconfigbasedkeyfilter.h"
13
14#include <KConfigBase>
15#include <KConfigGroup>
16
17#include <QDebug>
18#include <QRegularExpression>
19
20#include <algorithm>
21
22using namespace Kleo;
23using namespace GpgME;
24
25//
26//
27// FontDescription - intuitive font property resolving
28// (QFont::resolve doesn't work for us)
29//
30//
31struct KeyFilter::FontDescription::Private {
32 bool bold, italic, strikeOut, fullFont;
33 QFont font;
34};
35
36KeyFilter::FontDescription::FontDescription()
37 : d(new Private)
38{
39 d->bold = d->italic = d->strikeOut = d->fullFont = false;
40}
41
42KeyFilter::FontDescription::FontDescription(const FontDescription &other)
43 : d(new Private(*other.d))
44{
45}
46
47KeyFilter::FontDescription::~FontDescription() = default;
48
49KeyFilter::FontDescription KeyFilter::FontDescription::create(bool b, bool i, bool s)
50{
51 FontDescription fd;
52 fd.d->bold = b;
53 fd.d->italic = i;
54 fd.d->strikeOut = s;
55 return fd;
56}
57
58KeyFilter::FontDescription KeyFilter::FontDescription::create(const QFont &f, bool b, bool i, bool s)
59{
60 FontDescription fd;
61 fd.d->fullFont = true;
62 fd.d->font = f;
63 fd.d->bold = b;
64 fd.d->italic = i;
65 fd.d->strikeOut = s;
66 return fd;
67}
68
69QFont KeyFilter::FontDescription::font(const QFont &base) const
70{
71 QFont font;
72 if (d->fullFont) {
73 font = d->font;
74 font.setPointSize(base.pointSize());
75 } else {
76 font = base;
77 }
78 if (d->bold) {
79 font.setBold(true);
80 }
81 if (d->italic) {
82 font.setItalic(true);
83 }
84 if (d->strikeOut) {
85 font.setStrikeOut(true);
86 }
87 return font;
88}
89
90KeyFilter::FontDescription KeyFilter::FontDescription::resolve(const FontDescription &other) const
91{
92 FontDescription fd;
93 fd.d->fullFont = this->d->fullFont || other.d->fullFont;
94 if (fd.d->fullFont) {
95 fd.d->font = this->d->fullFont ? this->d->font : other.d->font;
96 }
97 fd.d->bold = this->d->bold || other.d->bold;
98 fd.d->italic = this->d->italic || other.d->italic;
99 fd.d->strikeOut = this->d->strikeOut || other.d->strikeOut;
100 return fd;
101}
102
103static const struct {
104 const char *name;
105 Key::OwnerTrust trust;
106 UserID::Validity validity;
107} ownerTrustAndValidityMap[] = {
108 // clang-format off
109 {"unknown", Key::Unknown, UserID::Unknown },
110 {"undefined", Key::Undefined, UserID::Undefined},
111 {"never", Key::Never, UserID::Never },
112 {"marginal", Key::Marginal, UserID::Marginal },
113 {"full", Key::Full, UserID::Full },
114 {"ultimate", Key::Ultimate, UserID::Ultimate },
115 // clang-format on
116};
117
118static Key::OwnerTrust map2OwnerTrust(const QString &s)
119{
120 for (unsigned int i = 0; i < sizeof ownerTrustAndValidityMap / sizeof *ownerTrustAndValidityMap; ++i) {
121 if (s.toLower() == QLatin1StringView(ownerTrustAndValidityMap[i].name)) {
122 return ownerTrustAndValidityMap[i].trust;
123 }
124 }
125 return ownerTrustAndValidityMap[0].trust;
126}
127
128static UserID::Validity map2Validity(const QString &s)
129{
130 for (unsigned int i = 0; i < sizeof ownerTrustAndValidityMap / sizeof *ownerTrustAndValidityMap; ++i) {
131 if (s.toLower() == QLatin1StringView(ownerTrustAndValidityMap[i].name)) {
132 return ownerTrustAndValidityMap[i].validity;
133 }
134 }
135 return ownerTrustAndValidityMap[0].validity;
136}
137
138KConfigBasedKeyFilter::KConfigBasedKeyFilter(const KConfigGroup &config)
140{
141 setFgColor(config.readEntry<QColor>("foreground-color", QColor()));
142 setBgColor(config.readEntry<QColor>("background-color", QColor()));
143 setName(config.readEntry("Name", config.name()));
144 setDescription(config.readEntry("Description", QString()));
145 setIcon(config.readEntry("icon"));
146 setId(config.readEntry("id", config.name()));
147 if (config.hasKey("font")) {
148 setUseFullFont(true);
149 setFont(config.readEntry("font"));
150 } else {
151 setUseFullFont(false);
152 setItalic(config.readEntry("font-italic", false));
153 setBold(config.readEntry("font-bold", false));
154 }
155 setStrikeOut(config.readEntry("font-strikeout", false));
156#ifdef SET
157#undef SET
158#endif
159#define SET(member, key) \
160 if (config.hasKey(key)) { \
161 set##member(config.readEntry(key, false) ? Set : NotSet); \
162 setSpecificity(specificity() + 1); \
163 }
164 SET(Revoked, "is-revoked");
165 SET(Expired, "is-expired");
166 SET(Disabled, "is-disabled");
167 SET(Root, "is-root-certificate");
168 SET(CanEncrypt, "can-encrypt");
169 SET(CanSign, "can-sign");
170 SET(CanCertify, "can-certify");
171 SET(CanAuthenticate, "can-authenticate");
172 SET(HasEncrypt, "has-encrypt");
173 SET(HasSign, "has-sign");
174 SET(HasCertify, "has-certify");
175 SET(HasAuthenticate, "has-authenticate");
176 SET(Qualified, "is-qualified");
177 SET(CardKey, "is-cardkey");
178 SET(HasSecret, "has-secret-key");
179 SET(IsOpenPGP, "is-openpgp-key");
180 SET(WasValidated, "was-validated");
181 SET(IsDeVs, "is-de-vs");
182#undef SET
183 static const struct {
184 const char *prefix;
185 LevelState state;
186 } prefixMap[] = {
187 {"is-", Is},
188 {"is-not-", IsNot},
189 {"is-at-least-", IsAtLeast},
190 {"is-at-most-", IsAtMost},
191 };
192 for (unsigned int i = 0; i < sizeof prefixMap / sizeof *prefixMap; ++i) {
193 const QString key = QLatin1StringView(prefixMap[i].prefix) + QLatin1String("ownertrust");
194 if (config.hasKey(key)) {
195 setOwnerTrust(prefixMap[i].state);
196 setOwnerTrustReferenceLevel(map2OwnerTrust(config.readEntry(key, QString())));
197 setSpecificity(specificity() + 1);
198 break;
199 }
200 }
201 for (unsigned int i = 0; i < sizeof prefixMap / sizeof *prefixMap; ++i) {
202 const QString key = QLatin1StringView(prefixMap[i].prefix) + QLatin1String("validity");
203 if (config.hasKey(key)) {
204 setValidity(prefixMap[i].state);
205 setValidityReferenceLevel(map2Validity(config.readEntry(key, QString())));
206 setSpecificity(specificity() + 1);
207 break;
208 }
209 }
210 static const struct {
211 const char *key;
212 MatchContext context;
213 } matchMap[] = {
214 {"any", AnyMatchContext},
215 {"appearance", Appearance},
216 {"filtering", Filtering},
217 };
218 static const QRegularExpression reg(QRegularExpression(QLatin1StringView("[^a-z!]+")));
219 const QStringList contexts = config.readEntry("match-contexts", "any").toLower().split(reg, Qt::SkipEmptyParts);
220 setMatchContexts(NoMatchContext);
221 for (const QString &ctx : contexts) {
222 bool found = false;
223 for (unsigned int i = 0; i < sizeof matchMap / sizeof *matchMap; ++i) {
224 if (ctx == QLatin1StringView(matchMap[i].key)) {
225 setMatchContexts(availableMatchContexts() |= matchMap[i].context);
226 found = true;
227 break;
228 } else if (ctx.startsWith(QLatin1Char('!')) && ctx.mid(1) == QLatin1StringView(matchMap[i].key)) {
229 setMatchContexts(availableMatchContexts() &= matchMap[i].context);
230 found = true;
231 break;
232 }
233 }
234 if (!found) {
235 qWarning() << QStringLiteral("KConfigBasedKeyFilter: found unknown match context '%1' in group '%2'").arg(ctx, config.name());
236 }
237 }
238 if (availableMatchContexts() == NoMatchContext) {
239 qWarning() << QStringLiteral(
240 "KConfigBasedKeyFilter: match context in group '%1' evaluates to NoMatchContext, "
241 "replaced by AnyMatchContext")
242 .arg(config.name());
243 setMatchContexts(AnyMatchContext);
244 }
245}
QString name() const
bool hasKey(const char *key) const
QString readEntry(const char *key, const char *aDefault=nullptr) const
Default implementation of key filter class.
bool bold() const const
int pointSize() const const
void setBold(bool enable)
void setItalic(bool enable)
void setPointSize(int pointSize)
void setStrikeOut(bool enable)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString toLower() const const
SkipEmptyParts
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:50:31 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.