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 setIcon(config.readEntry("icon"));
145 setId(config.readEntry("id", config.name()));
146 if (config.hasKey("font")) {
147 setUseFullFont(true);
148 setFont(config.readEntry("font"));
149 } else {
150 setUseFullFont(false);
151 setItalic(config.readEntry("font-italic", false));
152 setBold(config.readEntry("font-bold", false));
153 }
154 setStrikeOut(config.readEntry("font-strikeout", false));
155#ifdef SET
156#undef SET
157#endif
158#define SET(member, key) \
159 if (config.hasKey(key)) { \
160 set##member(config.readEntry(key, false) ? Set : NotSet); \
161 setSpecificity(specificity() + 1); \
162 }
163 SET(Revoked, "is-revoked");
164 SET(Expired, "is-expired");
165 SET(Disabled, "is-disabled");
166 SET(Root, "is-root-certificate");
167 SET(CanEncrypt, "can-encrypt");
168 SET(CanSign, "can-sign");
169 SET(CanCertify, "can-certify");
170 SET(CanAuthenticate, "can-authenticate");
171 SET(HasEncrypt, "has-encrypt");
172 SET(HasSign, "has-sign");
173 SET(HasCertify, "has-certify");
174 SET(HasAuthenticate, "has-authenticate");
175 SET(Qualified, "is-qualified");
176 SET(CardKey, "is-cardkey");
177 SET(HasSecret, "has-secret-key");
178 SET(IsOpenPGP, "is-openpgp-key");
179 SET(WasValidated, "was-validated");
180 SET(IsDeVs, "is-de-vs");
181#undef SET
182 static const struct {
183 const char *prefix;
184 LevelState state;
185 } prefixMap[] = {
186 {"is-", Is},
187 {"is-not-", IsNot},
188 {"is-at-least-", IsAtLeast},
189 {"is-at-most-", IsAtMost},
190 };
191 for (unsigned int i = 0; i < sizeof prefixMap / sizeof *prefixMap; ++i) {
192 const QString key = QLatin1StringView(prefixMap[i].prefix) + QLatin1String("ownertrust");
193 if (config.hasKey(key)) {
194 setOwnerTrust(prefixMap[i].state);
195 setOwnerTrustReferenceLevel(map2OwnerTrust(config.readEntry(key, QString())));
196 setSpecificity(specificity() + 1);
197 break;
198 }
199 }
200 for (unsigned int i = 0; i < sizeof prefixMap / sizeof *prefixMap; ++i) {
201 const QString key = QLatin1StringView(prefixMap[i].prefix) + QLatin1String("validity");
202 if (config.hasKey(key)) {
203 setValidity(prefixMap[i].state);
204 setValidityReferenceLevel(map2Validity(config.readEntry(key, QString())));
205 setSpecificity(specificity() + 1);
206 break;
207 }
208 }
209 static const struct {
210 const char *key;
211 MatchContext context;
212 } matchMap[] = {
213 {"any", AnyMatchContext},
214 {"appearance", Appearance},
215 {"filtering", Filtering},
216 };
217 static const QRegularExpression reg(QRegularExpression(QLatin1StringView("[^a-z!]+")));
218 const QStringList contexts = config.readEntry("match-contexts", "any").toLower().split(reg, Qt::SkipEmptyParts);
219 setMatchContexts(NoMatchContext);
220 for (const QString &ctx : contexts) {
221 bool found = false;
222 for (unsigned int i = 0; i < sizeof matchMap / sizeof *matchMap; ++i) {
223 if (ctx == QLatin1StringView(matchMap[i].key)) {
224 setMatchContexts(availableMatchContexts() |= matchMap[i].context);
225 found = true;
226 break;
227 } else if (ctx.startsWith(QLatin1Char('!')) && ctx.mid(1) == QLatin1StringView(matchMap[i].key)) {
228 setMatchContexts(availableMatchContexts() &= matchMap[i].context);
229 found = true;
230 break;
231 }
232 }
233 if (!found) {
234 qWarning() << QStringLiteral("KConfigBasedKeyFilter: found unknown match context '%1' in group '%2'").arg(ctx, config.name());
235 }
236 }
237 if (availableMatchContexts() == NoMatchContext) {
238 qWarning() << QStringLiteral(
239 "KConfigBasedKeyFilter: match context in group '%1' evaluates to NoMatchContext, "
240 "replaced by AnyMatchContext")
241 .arg(config.name());
242 setMatchContexts(AnyMatchContext);
243 }
244}
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 Tue Mar 26 2024 11:14:12 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.