summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Vrátil <dvratil@kde.org>2016-10-03 09:44:44 (GMT)
committerDaniel Vrátil <dvratil@kde.org>2016-10-03 09:44:44 (GMT)
commit956b7f064e7dac515c3cf440b13f6b5fce349f9e (patch)
treece69ff5b5f69b3448d5ef1e55768b30464d7d96d
parent7ad4d6dc2a0b3a3b0ea604f9ad2c8dd3a4191310 (diff)
KeySelectionCombo: fix key pre-selection after applying filter
Among other things this fixes crash in KMail when switching to the Cryptograpy tab in Identity dialog.
-rw-r--r--src/ui/keyselectioncombo.cpp40
-rw-r--r--src/ui/keyselectioncombo.h1
2 files changed, 24 insertions, 17 deletions
diff --git a/src/ui/keyselectioncombo.cpp b/src/ui/keyselectioncombo.cpp
index aa21326..6e8a459 100644
--- a/src/ui/keyselectioncombo.cpp
+++ b/src/ui/keyselectioncombo.cpp
@@ -95,7 +95,7 @@ public:
}
for (int i = 0; i < mBackItems.count(); ++i) {
if (mBackItems[i]->data == data) {
- const int index = mFrontItems.count() + rowCount() + i;
+ const int index = mFrontItems.count() + QSortFilterProxyModel::rowCount() + i;
beginRemoveRows(QModelIndex(), index, index);
delete mBackItems.takeAt(i);
endRemoveRows();
@@ -257,10 +257,12 @@ KeySelectionCombo::KeySelectionCombo(QWidget* parent)
setModel(d->proxyModel);
connect(this, static_cast<void(KeySelectionCombo::*)(int)>(&KeySelectionCombo::currentIndexChanged),
this, [this](int row) {
- if (d->proxyModel->isCustomItem(row)) {
- Q_EMIT customItemSelected(d->proxyModel->index(row, 0).data(Qt::UserRole));
- } else {
- Q_EMIT currentKeyChanged(currentKey());
+ if (row >= 0 && row < d->proxyModel->rowCount()) {
+ if (d->proxyModel->isCustomItem(row)) {
+ Q_EMIT customItemSelected(d->proxyModel->index(row, 0).data(Qt::UserRole));
+ } else {
+ Q_EMIT currentKeyChanged(currentKey());
+ }
}
});
@@ -289,18 +291,7 @@ void KeySelectionCombo::init()
connect(this, &KeySelectionCombo::keyListingFinished,
this, [this]() {
- bool match = false;
- for (int i = 0; i < count(); ++i) {
- const GpgME::Key key = itemData(i, Kleo::AbstractKeyListModel::KeyRole).value<GpgME::Key>();
- if (d->defaultKey == key.primaryFingerprint()) {
- setCurrentIndex(i);
- match = true;
- break;
- }
- }
- if (!match) {
- setCurrentIndex(0);
- }
+ setCurrentKey(d->defaultKey);
});
if (!d->cache->initialized()) {
@@ -315,6 +306,7 @@ void KeySelectionCombo::init()
void KeySelectionCombo::setKeyFilter(const boost::shared_ptr<const KeyFilter> &kf)
{
d->sortFilterProxy->setKeyFilter(kf);
+ setCurrentKey(d->defaultKey);
}
boost::shared_ptr<const KeyFilter> KeySelectionCombo::keyFilter() const
@@ -325,6 +317,7 @@ boost::shared_ptr<const KeyFilter> KeySelectionCombo::keyFilter() const
void KeySelectionCombo::setIdFilter(const QString &id)
{
d->sortFilterProxy->setFilterRegExp(id);
+ setCurrentKey(d->defaultKey);
}
QString KeySelectionCombo::idFilter() const
@@ -345,6 +338,19 @@ void Kleo::KeySelectionCombo::setCurrentKey(const GpgME::Key &key)
}
}
+void Kleo::KeySelectionCombo::setCurrentKey(const QString &fingerprint)
+{
+ for (int i = 0; i < d->proxyModel->rowCount(); ++i) {
+ const auto idx = d->proxyModel->index(i, 0, QModelIndex());
+ const auto key = d->proxyModel->data(idx, Kleo::KeyListModelInterface::KeyRole).value<GpgME::Key>();
+ if (!key.isNull() && fingerprint == key.primaryFingerprint()) {
+ setCurrentIndex(i);
+ return;
+ }
+ }
+ setCurrentIndex(0);
+}
+
void KeySelectionCombo::refreshKeys()
{
d->wasEnabled = isEnabled();
diff --git a/src/ui/keyselectioncombo.h b/src/ui/keyselectioncombo.h
index cbf16d4..402e81f 100644
--- a/src/ui/keyselectioncombo.h
+++ b/src/ui/keyselectioncombo.h
@@ -56,6 +56,7 @@ public:
GpgME::Key currentKey() const;
void setCurrentKey(const GpgME::Key &key);
+ void setCurrentKey(const QString &fingerprint);
void setDefaultKey(const QString &fingerprint);
QString defaultKey() const;