Changeset View
Changeset View
Standalone View
Standalone View
src/kleo/keyfiltermanager.cpp
Show All 31 Lines | |||||
32 | 32 | | |||
33 | #include "keyfiltermanager.h" | 33 | #include "keyfiltermanager.h" | ||
34 | #include "kconfigbasedkeyfilter.h" | 34 | #include "kconfigbasedkeyfilter.h" | ||
35 | #include "defaultkeyfilter.h" | 35 | #include "defaultkeyfilter.h" | ||
36 | 36 | | |||
37 | #include "stl_util.h" | 37 | #include "stl_util.h" | ||
38 | 38 | | |||
39 | #include "libkleo_debug.h" | 39 | #include "libkleo_debug.h" | ||
40 | #include "utils/formatting.h" | ||||
40 | 41 | | |||
41 | #include <kconfig.h> | 42 | #include <kconfig.h> | ||
42 | #include <kconfiggroup.h> | 43 | #include <kconfiggroup.h> | ||
43 | #include <KSharedConfig> | 44 | #include <KSharedConfig> | ||
44 | #include <KLocalizedString> | 45 | #include <KLocalizedString> | ||
45 | #include <QIcon> | 46 | #include <QIcon> | ||
46 | #include <QDebug> | 47 | #include <QDebug> | ||
47 | 48 | | |||
▲ Show 20 Lines • Show All 100 Lines • ▼ Show 20 Line(s) | 146 | { | |||
148 | setValidityReferenceLevel(UserID::Never); | 149 | setValidityReferenceLevel(UserID::Never); | ||
149 | setSpecificity(UINT_MAX - 4); // overly high for ordering | 150 | setSpecificity(UINT_MAX - 4); // overly high for ordering | ||
150 | 151 | | |||
151 | setName(i18n("Other Certificates")); | 152 | setName(i18n("Other Certificates")); | ||
152 | setId(QStringLiteral("other-certificates")); | 153 | setId(QStringLiteral("other-certificates")); | ||
153 | setMatchContexts(Filtering); | 154 | setMatchContexts(Filtering); | ||
154 | } | 155 | } | ||
155 | }; | 156 | }; | ||
157 | | ||||
158 | /* This filter selects only VS-compliant keys if Kleopatra is used in | ||||
159 | * CO_DE_VS mode. */ | ||||
160 | class DeVsCompliantKeyFilter : public DefaultKeyFilter | ||||
161 | { | ||||
162 | public: | ||||
163 | DeVsCompliantKeyFilter() | ||||
164 | : DefaultKeyFilter() | ||||
165 | { | ||||
166 | setName(i18n("VS-compliant Certificates")); | ||||
167 | setId(QStringLiteral("vs-compliant-certificates")); | ||||
168 | setSpecificity(UINT_MAX - 5); // overly high for ordering | ||||
169 | } | ||||
170 | bool matches (const Key &key, MatchContexts contexts) const override | ||||
171 | { | ||||
172 | return (contexts & Filtering) && Formatting::isKeyDeVs(key); | ||||
173 | } | ||||
174 | }; | ||||
175 | | ||||
176 | /* This filter gives VS-compliant keys a light green background if | ||||
177 | * Kleopatra is used in CO_DE_VS mode. */ | ||||
178 | class DeVsCompliantKeyAppearanceFilter : public DefaultKeyFilter | ||||
179 | { | ||||
180 | public: | ||||
181 | DeVsCompliantKeyAppearanceFilter() | ||||
182 | : DefaultKeyFilter() | ||||
183 | { | ||||
184 | setBgColor(QColor("lightgreen")); | ||||
185 | } | ||||
186 | bool matches (const Key &key, MatchContexts contexts) const override | ||||
187 | { | ||||
188 | return (contexts & Appearance) && Formatting::isKeyDeVs(key); | ||||
189 | } | ||||
190 | }; | ||||
191 | | ||||
192 | /* This filter gives non-VS-compliant keys a light red background if | ||||
193 | * Kleopatra is used in CO_DE_VS mode. */ | ||||
194 | class NotDeVsCompliantKeyAppearanceFilter : public DefaultKeyFilter | ||||
195 | { | ||||
196 | public: | ||||
197 | NotDeVsCompliantKeyAppearanceFilter() | ||||
198 | : DefaultKeyFilter() | ||||
199 | { | ||||
200 | setBgColor(QColor("lightpink")); | ||||
201 | } | ||||
202 | bool matches (const Key &key, MatchContexts contexts) const override | ||||
203 | { | ||||
204 | return (contexts & Appearance) && ! Formatting::isKeyDeVs(key); | ||||
205 | } | ||||
206 | }; | ||||
207 | | ||||
156 | } | 208 | } | ||
157 | 209 | | |||
158 | static std::vector<std::shared_ptr<KeyFilter>> defaultFilters() | 210 | static std::vector<std::shared_ptr<KeyFilter>> defaultFilters() | ||
159 | { | 211 | { | ||
160 | std::vector<std::shared_ptr<KeyFilter> > result; | 212 | std::vector<std::shared_ptr<KeyFilter> > result; | ||
161 | result.reserve(5); | 213 | result.reserve(5); | ||
162 | result.push_back(std::shared_ptr<KeyFilter>(new MyCertificatesKeyFilter)); | 214 | result.push_back(std::shared_ptr<KeyFilter>(new MyCertificatesKeyFilter)); | ||
163 | result.push_back(std::shared_ptr<KeyFilter>(new TrustedCertificatesKeyFilter)); | 215 | result.push_back(std::shared_ptr<KeyFilter>(new TrustedCertificatesKeyFilter)); | ||
164 | result.push_back(std::shared_ptr<KeyFilter>(new FullCertificatesKeyFilter)); | 216 | result.push_back(std::shared_ptr<KeyFilter>(new FullCertificatesKeyFilter)); | ||
165 | result.push_back(std::shared_ptr<KeyFilter>(new OtherCertificatesKeyFilter)); | 217 | result.push_back(std::shared_ptr<KeyFilter>(new OtherCertificatesKeyFilter)); | ||
166 | result.push_back(std::shared_ptr<KeyFilter>(new AllCertificatesKeyFilter)); | 218 | result.push_back(std::shared_ptr<KeyFilter>(new AllCertificatesKeyFilter)); | ||
219 | if (Formatting::complianceMode() == QStringLiteral("de-vs")) { | ||||
220 | result.push_back(std::shared_ptr<KeyFilter>(new DeVsCompliantKeyFilter)); | ||||
221 | } | ||||
222 | return result; | ||||
223 | } | ||||
224 | | ||||
225 | static std::vector<std::shared_ptr<KeyFilter>> defaultAppearanceFilters() | ||||
226 | { | ||||
227 | std::vector<std::shared_ptr<KeyFilter> > result; | ||||
228 | if (Formatting::complianceMode() == QStringLiteral("de-vs")) { | ||||
229 | result.reserve(2); | ||||
230 | result.push_back(std::shared_ptr<KeyFilter>(new DeVsCompliantKeyAppearanceFilter)); | ||||
231 | result.push_back(std::shared_ptr<KeyFilter>(new NotDeVsCompliantKeyAppearanceFilter)); | ||||
232 | } | ||||
167 | return result; | 233 | return result; | ||
168 | } | 234 | } | ||
169 | 235 | | |||
170 | class KeyFilterManager::Private | 236 | class KeyFilterManager::Private | ||
171 | { | 237 | { | ||
172 | public: | 238 | public: | ||
173 | Private() : filters(), model(this) {} | 239 | Private() : filters(), appearanceFilters(), model(this) {} | ||
174 | void clear() | 240 | void clear() | ||
175 | { | 241 | { | ||
176 | filters.clear(); | 242 | filters.clear(); | ||
243 | appearanceFilters.clear(); | ||||
177 | model.reset(); | 244 | model.reset(); | ||
178 | } | 245 | } | ||
179 | 246 | | |||
180 | std::vector<std::shared_ptr<KeyFilter>> filters; | 247 | std::vector<std::shared_ptr<KeyFilter>> filters; | ||
248 | std::vector<std::shared_ptr<KeyFilter>> appearanceFilters; | ||||
181 | Model model; | 249 | Model model; | ||
182 | }; | 250 | }; | ||
183 | 251 | | |||
184 | KeyFilterManager *KeyFilterManager::mSelf = nullptr; | 252 | KeyFilterManager *KeyFilterManager::mSelf = nullptr; | ||
185 | 253 | | |||
186 | KeyFilterManager::KeyFilterManager(QObject *parent) | 254 | KeyFilterManager::KeyFilterManager(QObject *parent) | ||
187 | : QObject(parent), d(new Private) | 255 | : QObject(parent), d(new Private) | ||
188 | { | 256 | { | ||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Line(s) | |||||
246 | }; | 314 | }; | ||
247 | } | 315 | } | ||
248 | 316 | | |||
249 | void KeyFilterManager::reload() | 317 | void KeyFilterManager::reload() | ||
250 | { | 318 | { | ||
251 | d->clear(); | 319 | d->clear(); | ||
252 | 320 | | |||
253 | d->filters = defaultFilters(); | 321 | d->filters = defaultFilters(); | ||
322 | d->appearanceFilters = defaultAppearanceFilters(); | ||||
254 | KSharedConfigPtr config = KSharedConfig::openConfig(QStringLiteral("libkleopatrarc")); | 323 | KSharedConfigPtr config = KSharedConfig::openConfig(QStringLiteral("libkleopatrarc")); | ||
255 | 324 | | |||
256 | const QStringList groups = config->groupList().filter(QRegularExpression(QStringLiteral("^Key Filter #\\d+$"))); | 325 | const QStringList groups = config->groupList().filter(QRegularExpression(QStringLiteral("^Key Filter #\\d+$"))); | ||
257 | for (QStringList::const_iterator it = groups.begin(); it != groups.end(); ++it) { | 326 | for (QStringList::const_iterator it = groups.begin(); it != groups.end(); ++it) { | ||
258 | const KConfigGroup cfg(config, *it); | 327 | const KConfigGroup cfg(config, *it); | ||
259 | d->filters.push_back(std::shared_ptr<KeyFilter>(new KConfigBasedKeyFilter(cfg))); | 328 | d->filters.push_back(std::shared_ptr<KeyFilter>(new KConfigBasedKeyFilter(cfg))); | ||
260 | } | 329 | } | ||
261 | std::stable_sort(d->filters.begin(), d->filters.end(), ByDecreasingSpecificity()); | 330 | std::stable_sort(d->filters.begin(), d->filters.end(), ByDecreasingSpecificity()); | ||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Line(s) | 382 | { | |||
319 | } | 388 | } | ||
320 | if (role == Qt::DecorationRole) { | 389 | if (role == Qt::DecorationRole) { | ||
321 | return m_keyFilterManagerPrivate->filters[idx.row()]->icon(); | 390 | return m_keyFilterManagerPrivate->filters[idx.row()]->icon(); | ||
322 | } else { | 391 | } else { | ||
323 | return m_keyFilterManagerPrivate->filters[idx.row()]->name(); | 392 | return m_keyFilterManagerPrivate->filters[idx.row()]->name(); | ||
324 | } | 393 | } | ||
325 | } | 394 | } | ||
326 | 395 | | |||
327 | QFont KeyFilterManager::font(const Key &key, const QFont &baseFont) const | 396 | static KeyFilter::FontDescription get_fontdescription(const std::vector<std::shared_ptr<KeyFilter>> &filters, const Key &key, const KeyFilter::FontDescription &initial) | ||
328 | { | 397 | { | ||
329 | return kdtools::accumulate_if(d->filters.begin(), d->filters.end(), | 398 | return kdtools::accumulate_if(filters.begin(), filters.end(), | ||
330 | [&key](const std::shared_ptr<KeyFilter> &filter) { | 399 | [&key](const std::shared_ptr<KeyFilter> &filter) { | ||
331 | return filter->matches(key, KeyFilter::Appearance); | 400 | return filter->matches(key, KeyFilter::Appearance); | ||
332 | }, | 401 | }, | ||
333 | KeyFilter::FontDescription(), | 402 | initial, | ||
334 | [](const KeyFilter::FontDescription &lhs, | 403 | [](const KeyFilter::FontDescription &lhs, | ||
335 | const std::shared_ptr<KeyFilter> &rhs) { | 404 | const std::shared_ptr<KeyFilter> &rhs) { | ||
336 | return lhs.resolve(rhs->fontDescription()); | 405 | return lhs.resolve(rhs->fontDescription()); | ||
337 | }).font(baseFont); | 406 | }); | ||
407 | } | ||||
408 | | ||||
409 | QFont KeyFilterManager::font(const Key &key, const QFont &baseFont) const | ||||
410 | { | ||||
411 | KeyFilter::FontDescription fd; | ||||
412 | | ||||
413 | fd = get_fontdescription(d->appearanceFilters, key, KeyFilter::FontDescription()); | ||||
414 | fd = get_fontdescription(d->filters, key, fd); | ||||
415 | | ||||
416 | return fd.font(baseFont); | ||||
338 | } | 417 | } | ||
339 | 418 | | |||
340 | static QColor get_color(const std::vector<std::shared_ptr<KeyFilter>> &filters, const Key &key, QColor(KeyFilter::*fun)() const) | 419 | static QColor get_color(const std::vector<std::shared_ptr<KeyFilter>> &filters, const Key &key, QColor(KeyFilter::*fun)() const) | ||
341 | { | 420 | { | ||
342 | const auto it = std::find_if(filters.cbegin(), filters.cend(), | 421 | const auto it = std::find_if(filters.cbegin(), filters.cend(), | ||
343 | [&fun, &key](const std::shared_ptr<KeyFilter> &filter) { | 422 | [&fun, &key](const std::shared_ptr<KeyFilter> &filter) { | ||
344 | return filter->matches(key, KeyFilter::Appearance) | 423 | return filter->matches(key, KeyFilter::Appearance) | ||
345 | && !(filter.get()->*fun)().isValid(); | 424 | && (filter.get()->*fun)().isValid(); | ||
346 | }); | 425 | }); | ||
347 | if (it == filters.cend()) { | 426 | if (it == filters.cend()) { | ||
348 | return QColor(); | 427 | return QColor(); | ||
349 | } else { | 428 | } else { | ||
350 | return (it->get()->*fun)(); | 429 | return (it->get()->*fun)(); | ||
351 | } | 430 | } | ||
352 | } | 431 | } | ||
353 | 432 | | |||
354 | static QString get_string(const std::vector<std::shared_ptr<KeyFilter>> &filters, const Key &key, QString(KeyFilter::*fun)() const) | 433 | static QString get_string(const std::vector<std::shared_ptr<KeyFilter>> &filters, const Key &key, QString(KeyFilter::*fun)() const) | ||
355 | { | 434 | { | ||
356 | const auto it = std::find_if(filters.cbegin(), filters.cend(), | 435 | const auto it = std::find_if(filters.cbegin(), filters.cend(), | ||
357 | [&fun, &key](const std::shared_ptr<KeyFilter> &filter) { | 436 | [&fun, &key](const std::shared_ptr<KeyFilter> &filter) { | ||
358 | return filter->matches(key, KeyFilter::Appearance) | 437 | return filter->matches(key, KeyFilter::Appearance) | ||
359 | && !(filter.get()->*fun)().isEmpty(); | 438 | && !(filter.get()->*fun)().isEmpty(); | ||
360 | }); | 439 | }); | ||
361 | if (it == filters.cend()) { | 440 | if (it == filters.cend()) { | ||
362 | return QString(); | 441 | return QString(); | ||
363 | } else { | 442 | } else { | ||
364 | return (*it)->icon(); | 443 | return (*it)->icon(); | ||
365 | } | 444 | } | ||
366 | } | 445 | } | ||
367 | 446 | | |||
368 | QColor KeyFilterManager::bgColor(const Key &key) const | 447 | QColor KeyFilterManager::bgColor(const Key &key) const | ||
369 | { | 448 | { | ||
370 | return get_color(d->filters, key, &KeyFilter::bgColor); | 449 | QColor color; | ||
450 | | ||||
451 | color = get_color(d->appearanceFilters, key, &KeyFilter::bgColor); | ||||
452 | if (! color.isValid()) | ||||
453 | color = get_color(d->filters, key, &KeyFilter::bgColor); | ||||
454 | | ||||
455 | return color; | ||||
371 | } | 456 | } | ||
372 | 457 | | |||
373 | QColor KeyFilterManager::fgColor(const Key &key) const | 458 | QColor KeyFilterManager::fgColor(const Key &key) const | ||
374 | { | 459 | { | ||
375 | return get_color(d->filters, key, &KeyFilter::fgColor); | 460 | QColor color; | ||
461 | | ||||
462 | color = get_color(d->appearanceFilters, key, &KeyFilter::fgColor); | ||||
463 | if (! color.isValid()) | ||||
464 | color = get_color(d->filters, key, &KeyFilter::fgColor); | ||||
465 | | ||||
466 | return color; | ||||
376 | } | 467 | } | ||
377 | 468 | | |||
378 | QIcon KeyFilterManager::icon(const Key &key) const | 469 | QIcon KeyFilterManager::icon(const Key &key) const | ||
379 | { | 470 | { | ||
380 | const QString icon = get_string(d->filters, key, &KeyFilter::icon); | 471 | QString icon; | ||
472 | | ||||
473 | icon = get_string(d->appearanceFilters, key, &KeyFilter::icon); | ||||
474 | if (icon.isEmpty()) | ||||
475 | icon = get_string(d->filters, key, &KeyFilter::icon); | ||||
476 | | ||||
381 | return icon.isEmpty() ? QIcon() : QIcon::fromTheme(icon); | 477 | return icon.isEmpty() ? QIcon() : QIcon::fromTheme(icon); | ||
382 | } | 478 | } | ||
383 | 479 | |