Libkleo

useridselectioncombo.cpp
1/* This file is part of Kleopatra, the KDE keymanager
2 SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include <config-libkleo.h>
8
9#include "useridselectioncombo.h"
10
11#include "progressbar.h"
12
13#include <libkleo/defaultkeyfilter.h>
14#include <libkleo/dn.h>
15#include <libkleo/formatting.h>
16#include <libkleo/keycache.h>
17#include <libkleo/keyfiltermanager.h>
18#include <libkleo/keyhelpers.h>
19#include <libkleo/keylist.h>
20#include <libkleo/keylistmodel.h>
21#include <libkleo/keylistsortfilterproxymodel.h>
22#include <libkleo/useridproxymodel.h>
23
24#include <kleo_ui_debug.h>
25
26#include <KLocalizedString>
27
28#include <QHBoxLayout>
29#include <QList>
30#include <QSortFilterProxyModel>
31#include <QTimer>
32#include <QToolButton>
33
34#include <gpgme++/key.h>
35
36using namespace Kleo;
37
38#if !UNITY_BUILD
39Q_DECLARE_METATYPE(GpgME::Key)
40#endif
41namespace
42{
43class SortFilterProxyModel : public KeyListSortFilterProxyModel
44{
45 Q_OBJECT
46
47public:
48 using KeyListSortFilterProxyModel::KeyListSortFilterProxyModel;
49
50 void setAlwaysAcceptedKey(const QString &fingerprint)
51 {
52 if (fingerprint == mFingerprint) {
53 return;
54 }
55 mFingerprint = fingerprint;
56 invalidate();
57 }
58
59protected:
60 bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
61 {
62 if (!mFingerprint.isEmpty()) {
63 const QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
64 const auto fingerprint = sourceModel()->data(index, KeyList::FingerprintRole).toString();
65 if (fingerprint == mFingerprint) {
66 return true;
67 }
68 }
69
70 return KeyListSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
71 }
72
73private:
74 QString mFingerprint;
75};
76
77static QString formatUserID(const GpgME::UserID &userID)
78{
79 QString name;
80 QString email;
81
82 if (userID.parent().protocol() == GpgME::OpenPGP) {
83 name = QString::fromUtf8(userID.name());
84 email = QString::fromUtf8(userID.email());
85 } else {
86 const Kleo::DN dn(userID.id());
87 name = dn[QStringLiteral("CN")];
88 email = dn[QStringLiteral("EMAIL")];
89 if (name.isEmpty()) {
90 name = Kleo::DN(userID.parent().userID(0).id())[QStringLiteral("CN")];
91 }
92 }
93 return email.isEmpty() ? name : name.isEmpty() ? email : i18nc("Name <email>", "%1 <%2>", name, email);
94}
95
96class SortAndFormatCertificatesProxyModel : public QSortFilterProxyModel
97{
98 Q_OBJECT
99
100public:
101 SortAndFormatCertificatesProxyModel(KeyUsage::Flags usageFlags, QObject *parent = nullptr)
102 : QSortFilterProxyModel{parent}
103 , mIconProvider{usageFlags}
104 {
105 }
106
107private:
108 bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
109 {
110 const auto leftUserId = sourceModel()->data(left, KeyList::UserIDRole).value<GpgME::UserID>();
111 const auto rightUserId = sourceModel()->data(right, KeyList::UserIDRole).value<GpgME::UserID>();
112 if (leftUserId.isNull()) {
113 return false;
114 }
115 if (rightUserId.isNull()) {
116 return true;
117 }
118 const auto leftNameAndEmail = formatUserID(leftUserId);
119 const auto rightNameAndEmail = formatUserID(rightUserId);
120 const int cmp = QString::localeAwareCompare(leftNameAndEmail, rightNameAndEmail);
121 if (cmp) {
122 return cmp < 0;
123 }
124
125 if (leftUserId.validity() != rightUserId.validity()) {
126 return leftUserId.validity() > rightUserId.validity();
127 }
128
129 /* Both have the same validity, check which one is newer. */
130 time_t leftTime = 0;
131 for (const GpgME::Subkey &s : leftUserId.parent().subkeys()) {
132 if (s.isBad()) {
133 continue;
134 }
135 if (s.creationTime() > leftTime) {
136 leftTime = s.creationTime();
137 }
138 }
139 time_t rightTime = 0;
140 for (const GpgME::Subkey &s : rightUserId.parent().subkeys()) {
141 if (s.isBad()) {
142 continue;
143 }
144 if (s.creationTime() > rightTime) {
145 rightTime = s.creationTime();
146 }
147 }
148 if (rightTime != leftTime) {
149 return leftTime > rightTime;
150 }
151
152 // as final resort we compare the fingerprints
153 return strcmp(leftUserId.parent().primaryFingerprint(), rightUserId.parent().primaryFingerprint()) < 0;
154 }
155
156protected:
157 QVariant data(const QModelIndex &index, int role) const override
158 {
159 if (!index.isValid()) {
160 return QVariant();
161 }
162
163 const auto userId = QSortFilterProxyModel::data(index, KeyList::UserIDRole).value<GpgME::UserID>();
164 Q_ASSERT(!userId.isNull());
165 if (userId.isNull()) {
166 return QVariant();
167 }
168
169 switch (role) {
170 case Qt::DisplayRole:
172 return Formatting::summaryLine(userId);
173 }
174 case Qt::ToolTipRole: {
175 using namespace Kleo::Formatting;
176 return Kleo::Formatting::toolTip(userId, Validity | Issuer | Subject | Fingerprint | ExpiryDates | UserIDs);
177 }
178 case Qt::DecorationRole: {
179 return mIconProvider.icon(userId.parent());
180 }
181 case Qt::FontRole: {
182 return KeyFilterManager::instance()->font(userId.parent(), QFont());
183 }
184 default:
185 return QSortFilterProxyModel::data(index, role);
186 }
187 }
188
189private:
190 Formatting::IconProvider mIconProvider;
191};
192
193class CustomItemsProxyModel : public QSortFilterProxyModel
194{
195 Q_OBJECT
196
197private:
198 struct CustomItem {
199 QIcon icon;
200 QString text;
201 QVariant data;
202 QString toolTip;
203 };
204
205public:
206 CustomItemsProxyModel(QObject *parent = nullptr)
207 : QSortFilterProxyModel(parent)
208 {
209 }
210
211 ~CustomItemsProxyModel() override
212 {
213 qDeleteAll(mFrontItems);
214 qDeleteAll(mBackItems);
215 }
216
217 bool isCustomItem(const int row) const
218 {
219 return row < mFrontItems.count() || row >= mFrontItems.count() + QSortFilterProxyModel::rowCount();
220 }
221
222 void prependItem(const QIcon &icon, const QString &text, const QVariant &data, const QString &toolTip)
223 {
224 beginInsertRows(QModelIndex(), 0, 0);
225 mFrontItems.push_front(new CustomItem{icon, text, data, toolTip});
226 endInsertRows();
227 }
228
229 void appendItem(const QIcon &icon, const QString &text, const QVariant &data, const QString &toolTip)
230 {
231 beginInsertRows(QModelIndex(), rowCount(), rowCount());
232 mBackItems.push_back(new CustomItem{icon, text, data, toolTip});
233 endInsertRows();
234 }
235
236 void removeCustomItem(const QVariant &data)
237 {
238 for (int i = 0; i < mFrontItems.count(); ++i) {
239 if (mFrontItems[i]->data == data) {
240 beginRemoveRows(QModelIndex(), i, i);
241 delete mFrontItems.takeAt(i);
242 endRemoveRows();
243 return;
244 }
245 }
246 for (int i = 0; i < mBackItems.count(); ++i) {
247 if (mBackItems[i]->data == data) {
248 const int index = mFrontItems.count() + QSortFilterProxyModel::rowCount() + i;
249 beginRemoveRows(QModelIndex(), index, index);
250 delete mBackItems.takeAt(i);
251 endRemoveRows();
252 return;
253 }
254 }
255 }
256
257 int rowCount(const QModelIndex &parent = QModelIndex()) const override
258 {
259 return mFrontItems.count() + QSortFilterProxyModel::rowCount(parent) + mBackItems.count();
260 }
261
262 int columnCount(const QModelIndex &parent = QModelIndex()) const override
263 {
264 Q_UNUSED(parent)
265 // pretend that there is only one column to workaround a bug in
266 // QAccessibleTable which provides the accessibility interface for the
267 // pop-up of the combo box
268 return 1;
269 }
270
271 QModelIndex mapToSource(const QModelIndex &index) const override
272 {
273 if (!index.isValid()) {
274 return {};
275 }
276 if (!isCustomItem(index.row())) {
277 const int sourceRow = index.row() - mFrontItems.count();
278 return QSortFilterProxyModel::mapToSource(createIndex(sourceRow, index.column(), index.internalPointer()));
279 }
280 return {};
281 }
282
283 QModelIndex mapFromSource(const QModelIndex &source_index) const override
284 {
285 const QModelIndex idx = QSortFilterProxyModel::mapFromSource(source_index);
286 return createIndex(mFrontItems.count() + idx.row(), idx.column(), idx.internalPointer());
287 }
288
289 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override
290 {
291 if (row < 0 || row >= rowCount()) {
292 return {};
293 }
294 if (row < mFrontItems.count()) {
295 return createIndex(row, column, mFrontItems[row]);
296 } else if (row >= mFrontItems.count() + QSortFilterProxyModel::rowCount()) {
297 return createIndex(row, column, mBackItems[row - mFrontItems.count() - QSortFilterProxyModel::rowCount()]);
298 } else {
299 const QModelIndex mi = QSortFilterProxyModel::index(row - mFrontItems.count(), column, parent);
300 return createIndex(row, column, mi.internalPointer());
301 }
302 }
303
304 Qt::ItemFlags flags(const QModelIndex &index) const override
305 {
306 Q_UNUSED(index)
308 }
309
310 QModelIndex parent(const QModelIndex &) const override
311 {
312 // Flat list
313 return {};
314 }
315
316 QVariant data(const QModelIndex &index, int role) const override
317 {
318 if (!index.isValid()) {
319 return QVariant();
320 }
321
322 if (isCustomItem(index.row())) {
323 Q_ASSERT(!mFrontItems.isEmpty() || !mBackItems.isEmpty());
324 auto ci = static_cast<CustomItem *>(index.internalPointer());
325 switch (role) {
326 case Qt::DisplayRole:
327 return ci->text;
329 return ci->icon;
330 case Qt::UserRole:
331 case KeyList::UserIDRole:
332 return ci->data;
333 case Qt::ToolTipRole:
334 return ci->toolTip;
335 default:
336 return QVariant();
337 }
338 }
339
340 return QSortFilterProxyModel::data(index, role);
341 }
342
343private:
344 QList<CustomItem *> mFrontItems;
345 QList<CustomItem *> mBackItems;
346};
347
348} // anonymous namespace
349
350namespace Kleo
351{
352class UserIDSelectionComboPrivate
353{
354public:
355 UserIDSelectionComboPrivate(UserIDSelectionCombo *parent, bool secretOnly_, KeyUsage::Flags usage)
356 : wasEnabled(true)
357 , secretOnly{secretOnly_}
358 , usageFlags{usage}
359 , q{parent}
360 {
361 }
362
363 /* Selects the first key with a UID addrSpec that matches
364 * the mPerfectMatchMbox variable.
365 *
366 * The idea here is that if there are keys like:
367 *
368 * tom-store@abc.com
369 * susi-store@abc.com
370 * store@abc.com
371 *
372 * And the user wants to send a mail to "store@abc.com"
373 * the filter should still show tom and susi (because they
374 * both are part of store) but the key for "store" should
375 * be preselected.
376 *
377 * Returns true if one was selected. False otherwise. */
378 bool selectPerfectIdMatch() const
379 {
380 if (mPerfectMatchMbox.isEmpty()) {
381 return false;
382 }
383
384 for (int i = 0; i < proxyModel->rowCount(); ++i) {
385 const auto idx = proxyModel->index(i, 0, QModelIndex());
386 const auto userID = idx.data(KeyList::UserIDRole).value<GpgME::UserID>();
387 if (userID.isNull()) {
388 // WTF?
389 continue;
390 }
391 if (QString::fromStdString(userID.addrSpec()) == mPerfectMatchMbox) {
392 combo->setCurrentIndex(i);
393 return true;
394 }
395 }
396 return false;
397 }
398
399 /* Updates the current key with the default key if the key matches
400 * the current key filter. */
401 void updateWithDefaultKey()
402 {
403 GpgME::Protocol filterProto = GpgME::UnknownProtocol;
404
405 const auto filter = dynamic_cast<const DefaultKeyFilter *>(sortFilterProxy->keyFilter().get());
406 if (filter && filter->isOpenPGP() == DefaultKeyFilter::Set) {
407 filterProto = GpgME::OpenPGP;
408 } else if (filter && filter->isOpenPGP() == DefaultKeyFilter::NotSet) {
409 filterProto = GpgME::CMS;
410 }
411
412 QString defaultKey = defaultKeys.value(filterProto);
413 if (defaultKey.isEmpty()) {
414 // Fallback to unknown protocol
415 defaultKey = defaultKeys.value(GpgME::UnknownProtocol);
416 }
417 // make sure that the default key is not filtered out unless it has the wrong protocol
418 if (filterProto == GpgME::UnknownProtocol) {
419 sortFilterProxy->setAlwaysAcceptedKey(defaultKey);
420 } else {
421 const auto key = KeyCache::instance()->findByFingerprint(defaultKey.toLatin1().constData());
422 if (!key.isNull() && key.protocol() == filterProto) {
423 sortFilterProxy->setAlwaysAcceptedKey(defaultKey);
424 } else {
425 sortFilterProxy->setAlwaysAcceptedKey({});
426 }
427 }
428 q->setCurrentKey(defaultKey);
429 }
430
431 void storeCurrentSelectionBeforeModelChange()
432 {
433 userIDBeforeModelChange = q->currentUserID();
434 customItemBeforeModelChange = combo->currentData();
435 }
436
437 void restoreCurrentSelectionAfterModelChange()
438 {
439 if (!userIDBeforeModelChange.isNull()) {
440 q->setCurrentUserID(userIDBeforeModelChange);
441 } else if (customItemBeforeModelChange.isValid()) {
442 const auto index = combo->findData(customItemBeforeModelChange);
443 if (index != -1) {
444 combo->setCurrentIndex(index);
445 } else {
446 updateWithDefaultKey();
447 }
448 }
449 }
450
451 Kleo::AbstractKeyListModel *model = nullptr;
452 UserIDProxyModel *userIdProxy = nullptr;
453 SortFilterProxyModel *sortFilterProxy = nullptr;
454 SortAndFormatCertificatesProxyModel *sortAndFormatProxy = nullptr;
455 CustomItemsProxyModel *proxyModel = nullptr;
456 QComboBox *combo = nullptr;
457 QToolButton *button = nullptr;
458 std::shared_ptr<Kleo::KeyCache> cache;
459 QMap<GpgME::Protocol, QString> defaultKeys;
460 bool wasEnabled = false;
461 bool useWasEnabled = false;
462 bool secretOnly = false;
463 bool initialKeyListingDone = false;
464 QString mPerfectMatchMbox;
465 GpgME::UserID userIDBeforeModelChange;
466 QVariant customItemBeforeModelChange;
467 KeyUsage::Flags usageFlags;
468
469private:
470 UserIDSelectionCombo *const q;
471};
472
473}
474
475using namespace Kleo;
476
477UserIDSelectionCombo::UserIDSelectionCombo(QWidget *parent)
478 : UserIDSelectionCombo(true, KeyUsage::None, parent)
479{
480}
481
482UserIDSelectionCombo::UserIDSelectionCombo(bool secretOnly, QWidget *parent)
483 : UserIDSelectionCombo(secretOnly, KeyUsage::None, parent)
484{
485}
486
487UserIDSelectionCombo::UserIDSelectionCombo(KeyUsage::Flags usage, QWidget *parent)
488 : UserIDSelectionCombo{false, usage, parent}
489{
490}
491
492UserIDSelectionCombo::UserIDSelectionCombo(KeyUsage::Flag usage, QWidget *parent)
493 : UserIDSelectionCombo{false, usage, parent}
494{
495}
496
497UserIDSelectionCombo::UserIDSelectionCombo(bool secretOnly, KeyUsage::Flags usage, QWidget *parent)
498 : QWidget(parent)
499 , d(new UserIDSelectionComboPrivate(this, secretOnly, usage))
500{
501 // set a non-empty string as accessible description to prevent screen readers
502 // from reading the tool tip which isn't meant for screen readers
503 setAccessibleDescription(QStringLiteral(" "));
504 d->model = Kleo::AbstractKeyListModel::createFlatKeyListModel(this);
505
506 d->userIdProxy = new UserIDProxyModel(this);
507 d->userIdProxy->setSourceModel(d->model);
508
509 d->sortFilterProxy = new SortFilterProxyModel(this);
510 d->sortFilterProxy->setSourceModel(d->userIdProxy);
511
512 d->sortAndFormatProxy = new SortAndFormatCertificatesProxyModel{usage, this};
513 d->sortAndFormatProxy->setSourceModel(d->sortFilterProxy);
514 // initialize dynamic sorting
515 d->sortAndFormatProxy->sort(0);
516
517 d->proxyModel = new CustomItemsProxyModel{this};
518 d->proxyModel->setSourceModel(d->sortAndFormatProxy);
519
520 auto layout = new QHBoxLayout(this);
521 layout->setContentsMargins({});
522
523 d->combo = new QComboBox(parent);
524 layout->addWidget(d->combo);
525
526 d->button = new QToolButton(parent);
527 d->button->setIcon(QIcon::fromTheme(QStringLiteral("resource-group-new")));
528 d->button->setToolTip(i18nc("@info:tooltip", "Show certificate list"));
529 d->button->setAccessibleName(i18n("Show certificate list"));
530 layout->addWidget(d->button);
531
532 connect(d->button, &QToolButton::clicked, this, &UserIDSelectionCombo::certificateSelectionRequested);
533
534 d->combo->setModel(d->proxyModel);
535 connect(d->combo, &QComboBox::currentIndexChanged, this, [this](int row) {
536 if (row >= 0 && row < d->proxyModel->rowCount()) {
537 if (d->proxyModel->isCustomItem(row)) {
538 Q_EMIT customItemSelected(d->combo->currentData(Qt::UserRole));
539 } else {
540 Q_EMIT currentKeyChanged(currentKey());
541 }
542 }
543 });
544
545 d->cache = Kleo::KeyCache::mutableInstance();
546
547 connect(d->combo->model(), &QAbstractItemModel::rowsAboutToBeInserted, this, [this]() {
548 d->storeCurrentSelectionBeforeModelChange();
549 });
550 connect(d->combo->model(), &QAbstractItemModel::rowsInserted, this, [this]() {
551 d->restoreCurrentSelectionAfterModelChange();
552 });
553 connect(d->combo->model(), &QAbstractItemModel::rowsAboutToBeRemoved, this, [this]() {
554 d->storeCurrentSelectionBeforeModelChange();
555 });
556 connect(d->combo->model(), &QAbstractItemModel::rowsRemoved, this, [this]() {
557 d->restoreCurrentSelectionAfterModelChange();
558 });
559 connect(d->combo->model(), &QAbstractItemModel::modelAboutToBeReset, this, [this]() {
560 d->storeCurrentSelectionBeforeModelChange();
561 });
562 connect(d->combo->model(), &QAbstractItemModel::modelReset, this, [this]() {
563 d->restoreCurrentSelectionAfterModelChange();
564 });
565
566 QTimer::singleShot(0, this, &UserIDSelectionCombo::init);
567}
568
569UserIDSelectionCombo::~UserIDSelectionCombo() = default;
570
571void UserIDSelectionCombo::init()
572{
573 connect(d->cache.get(), &Kleo::KeyCache::keyListingDone, this, [this]() {
574 // Set useKeyCache ensures that the cache is populated
575 // so this can be a blocking call if the cache is not initialized
576 if (!d->initialKeyListingDone) {
577 d->model->useKeyCache(true, d->secretOnly ? KeyList::SecretKeysOnly : KeyList::AllKeys);
578 }
579 d->proxyModel->removeCustomItem(QStringLiteral("-libkleo-loading-keys"));
580
581 // We use the useWasEnabled state variable to decide if we should
582 // change the enable / disable state based on the keylist done signal.
583 // If we triggered the refresh useWasEnabled is true and we want to
584 // enable / disable again after our refresh, as the refresh disabled it.
585 //
586 // But if a keyListingDone signal comes from just a generic refresh
587 // triggered by someone else we don't want to change the enable / disable
588 // state.
589 if (d->useWasEnabled) {
590 setEnabled(d->wasEnabled);
591 d->useWasEnabled = false;
592 }
593 Q_EMIT keyListingFinished();
594 });
595
596 connect(this, &UserIDSelectionCombo::keyListingFinished, this, [this]() {
597 if (!d->initialKeyListingDone) {
598 d->updateWithDefaultKey();
599 d->initialKeyListingDone = true;
600 }
601 });
602
603 if (!d->cache->initialized()) {
604 refreshKeys();
605 } else {
606 d->model->useKeyCache(true, d->secretOnly ? KeyList::SecretKeysOnly : KeyList::AllKeys);
607 Q_EMIT keyListingFinished();
608 }
609
610 connect(d->combo, &QComboBox::currentIndexChanged, this, [this]() {
611 setToolTip(d->combo->currentData(Qt::ToolTipRole).toString());
612 });
613}
614
615void UserIDSelectionCombo::setKeyFilter(const std::shared_ptr<const KeyFilter> &kf)
616{
617 d->sortFilterProxy->setKeyFilter(kf);
618 d->updateWithDefaultKey();
619}
620
621std::shared_ptr<const KeyFilter> UserIDSelectionCombo::keyFilter() const
622{
623 return d->sortFilterProxy->keyFilter();
624}
625
626void UserIDSelectionCombo::setIdFilter(const QString &id)
627{
628 d->sortFilterProxy->setFilterRegularExpression(id);
629 d->mPerfectMatchMbox = id;
630 d->updateWithDefaultKey();
631}
632
633QString UserIDSelectionCombo::idFilter() const
634{
635 return d->sortFilterProxy->filterRegularExpression().pattern();
636}
637
638GpgME::Key Kleo::UserIDSelectionCombo::currentKey() const
639{
640 return d->combo->currentData(KeyList::KeyRole).value<GpgME::Key>();
641}
642
643void Kleo::UserIDSelectionCombo::setCurrentKey(const GpgME::Key &key)
644{
645 const int idx = d->combo->findData(QString::fromLatin1(key.primaryFingerprint()), KeyList::FingerprintRole, Qt::MatchExactly);
646 if (idx > -1) {
647 d->combo->setCurrentIndex(idx);
648 } else if (!d->selectPerfectIdMatch()) {
649 d->updateWithDefaultKey();
650 }
651 setToolTip(d->combo->currentData(Qt::ToolTipRole).toString());
652}
653
654void Kleo::UserIDSelectionCombo::setCurrentKey(const QString &fingerprint)
655{
656 const auto cur = currentKey();
657 if (!cur.isNull() && !fingerprint.isEmpty() && fingerprint == QLatin1StringView(cur.primaryFingerprint())) {
658 // already set; still emit a changed signal because the current key may
659 // have become the item at the current index by changes in the underlying model
660 Q_EMIT currentKeyChanged(cur);
661 return;
662 }
663 const int idx = d->combo->findData(fingerprint, KeyList::FingerprintRole, Qt::MatchExactly);
664 if (idx > -1) {
665 d->combo->setCurrentIndex(idx);
666 } else if (!d->selectPerfectIdMatch()) {
667 d->combo->setCurrentIndex(0);
668 }
669 setToolTip(d->combo->currentData(Qt::ToolTipRole).toString());
670}
671
672GpgME::UserID Kleo::UserIDSelectionCombo::currentUserID() const
673{
674 return d->combo->currentData(KeyList::UserIDRole).value<GpgME::UserID>();
675}
676
677void Kleo::UserIDSelectionCombo::setCurrentUserID(const GpgME::UserID &userID)
678{
679 for (auto i = 0; i < d->combo->count(); i++) {
680 const auto &other = d->combo->itemData(i, KeyList::UserIDRole).value<GpgME::UserID>();
681 if (!qstrcmp(userID.id(), other.id()) && !qstrcmp(userID.parent().primaryFingerprint(), other.parent().primaryFingerprint())) {
682 d->combo->setCurrentIndex(i);
683 setToolTip(d->combo->currentData(Qt::ToolTipRole).toString());
684 return;
685 }
686 }
687 if (!d->selectPerfectIdMatch()) {
688 d->updateWithDefaultKey();
689 setToolTip(d->combo->currentData(Qt::ToolTipRole).toString());
690 }
691}
692
693void UserIDSelectionCombo::refreshKeys()
694{
695 d->wasEnabled = isEnabled();
696 d->useWasEnabled = true;
697 setEnabled(false);
698 const bool wasBlocked = blockSignals(true);
699 prependCustomItem(QIcon(), i18n("Loading keys ..."), QStringLiteral("-libkleo-loading-keys"));
700 d->combo->setCurrentIndex(0);
701 blockSignals(wasBlocked);
702 d->cache->startKeyListing();
703}
704
705void UserIDSelectionCombo::appendCustomItem(const QIcon &icon, const QString &text, const QVariant &data, const QString &toolTip)
706{
707 d->proxyModel->appendItem(icon, text, data, toolTip);
708}
709
710void UserIDSelectionCombo::appendCustomItem(const QIcon &icon, const QString &text, const QVariant &data)
711{
712 appendCustomItem(icon, text, data, QString());
713}
714
715void UserIDSelectionCombo::prependCustomItem(const QIcon &icon, const QString &text, const QVariant &data, const QString &toolTip)
716{
717 d->proxyModel->prependItem(icon, text, data, toolTip);
718}
719
720void UserIDSelectionCombo::prependCustomItem(const QIcon &icon, const QString &text, const QVariant &data)
721{
722 prependCustomItem(icon, text, data, QString());
723}
724
725void UserIDSelectionCombo::removeCustomItem(const QVariant &data)
726{
727 d->proxyModel->removeCustomItem(data);
728}
729
730void Kleo::UserIDSelectionCombo::setDefaultKey(const QString &fingerprint, GpgME::Protocol proto)
731{
732 d->defaultKeys.insert(proto, fingerprint);
733 d->updateWithDefaultKey();
734}
735
736void Kleo::UserIDSelectionCombo::setDefaultKey(const QString &fingerprint)
737{
738 setDefaultKey(fingerprint, GpgME::UnknownProtocol);
739}
740
741QString Kleo::UserIDSelectionCombo::defaultKey(GpgME::Protocol proto) const
742{
743 return d->defaultKeys.value(proto);
744}
745
746QString Kleo::UserIDSelectionCombo::defaultKey() const
747{
748 return defaultKey(GpgME::UnknownProtocol);
749}
750
751QComboBox *Kleo::UserIDSelectionCombo::combo() const
752{
753 return d->combo;
754}
755
756int Kleo::UserIDSelectionCombo::findUserId(const GpgME::UserID &userId) const
757{
758 for (int i = 0; i < combo()->model()->rowCount(); i++) {
759 if (Kleo::userIDsAreEqual(userId, combo()->model()->index(i, 0).data(KeyList::UserIDRole).value<GpgME::UserID>())) {
760 return i;
761 }
762 }
763 return -1;
764}
765
766#include "useridselectioncombo.moc"
767
768#include "moc_useridselectioncombo.cpp"
DN parser and reorderer.
Definition dn.h:27
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
KIOCORE_EXPORT TransferJob * get(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
void clicked(bool checked)
void modelAboutToBeReset()
void rowsAboutToBeInserted(const QModelIndex &parent, int start, int end)
void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
void rowsInserted(const QModelIndex &parent, int first, int last)
void rowsRemoved(const QModelIndex &parent, int first, int last)
const char * constData() const const
void currentIndexChanged(int index)
QIcon fromTheme(const QString &name)
int column() const const
void * internalPointer() const const
bool isValid() const const
int row() const const
Q_EMITQ_EMIT
bool blockSignals(bool block)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual QVariant data(const QModelIndex &index, int role) const const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const const override
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const override
virtual int rowCount(const QModelIndex &parent) const const override
QChar * data()
QString fromLatin1(QByteArrayView str)
QString fromStdString(const std::string &str)
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
int localeAwareCompare(QStringView s1, QStringView s2)
QByteArray toLatin1() const const
DisplayRole
typedef ItemFlags
MatchExactly
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
T value() const const
bool isEnabled() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:50:12 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.