Sort keyselectioncombo

This puts valid keys or the newest valid key to the top
of the model for better default selection and selection.
parent cd827e98
......@@ -38,7 +38,6 @@ Q_DECLARE_METATYPE(GpgME::Key)
namespace
{
class ProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
......@@ -61,6 +60,44 @@ public:
qDeleteAll(mBackItems);
}
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
{
const auto leftKey = sourceModel()->data(left, Kleo::KeyListModelInterface::KeyRole).value<GpgME::Key>();
const auto rightKey = sourceModel()->data(right, Kleo::KeyListModelInterface::KeyRole).value<GpgME::Key>();
if (leftKey.isNull()) {
return false;
}
if (rightKey.isNull()) {
return true;
}
// As we display UID(0) this is ok. We probably need a get Best UID at some point.
const auto lUid = leftKey.userID(0);
const auto rUid = rightKey.userID(0);
if (lUid.validity() == rUid.validity()) {
/* Both are the same check which one is newer. */
time_t oldTime = 0;
for (const GpgME::Subkey s: leftKey.subkeys()) {
if (s.isRevoked() || s.isInvalid() || s.isDisabled()) {
continue;
}
if (s.creationTime() > oldTime) {
oldTime= s.creationTime();
}
}
time_t newTime = 0;
for (const GpgME::Subkey s: rightKey.subkeys()) {
if (s.isRevoked() || s.isInvalid() || s.isDisabled()) {
continue;
}
if (s.creationTime() > newTime) {
newTime = s.creationTime();
}
}
return newTime < oldTime;
}
return lUid.validity() > rUid.validity();
}
bool isCustomItem(const int row) const
{
return row < mFrontItems.count() || row >= mFrontItems.count() + QSortFilterProxyModel::rowCount();
......@@ -303,6 +340,7 @@ void KeySelectionCombo::init()
void KeySelectionCombo::setKeyFilter(const std::shared_ptr<const KeyFilter> &kf)
{
d->sortFilterProxy->setKeyFilter(kf);
d->proxyModel->sort(0);
setCurrentKey(d->defaultKey);
}
......
Markdown is supported
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