Commit 956b7f06 authored by Daniel Vrátil's avatar Daniel Vrátil 🤖
Browse files

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.
parent 7ad4d6dc
......@@ -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();
......
......@@ -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;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment