Skip to content

Commit

Permalink
Fix sorting of advanced attributes
Browse files Browse the repository at this point in the history
Advanced attributes are sorted using locale aware sort.

Fixes keepassxreboot#6175
  • Loading branch information
marco-langer committed Dec 10, 2023
1 parent f7fd388 commit 557f63a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/gui/entry/EntryAttributesModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@

#include "core/EntryAttributes.h"

#include <QCollator>

namespace
{
void sortLocaleAware(QList<QString>& strings)
{
QCollator collator;
collator.setNumericMode(true);
std::sort(strings.begin(), strings.end(), collator);
}
} // namespace

EntryAttributesModel::EntryAttributesModel(QObject* parent)
: QAbstractListModel(parent)
, m_entryAttributes(nullptr)
Expand Down Expand Up @@ -150,7 +162,7 @@ void EntryAttributesModel::attributeAboutToAdd(const QString& key)
{
QList<QString> rows = m_attributes;
rows.append(key);
std::sort(rows.begin(), rows.end());
sortLocaleAware(rows);
int row = rows.indexOf(key);
beginInsertRows(QModelIndex(), row, row);
}
Expand Down Expand Up @@ -180,7 +192,7 @@ void EntryAttributesModel::attributeAboutToRename(const QString& oldKey, const Q
QList<QString> rows = m_attributes;
rows.removeOne(oldKey);
rows.append(newKey);
std::sort(rows.begin(), rows.end());
sortLocaleAware(rows);
int newRow = rows.indexOf(newKey);
if (newRow > oldRow) {
newRow++;
Expand Down Expand Up @@ -232,4 +244,5 @@ void EntryAttributesModel::updateAttributes()
m_attributes.append(key);
}
}
sortLocaleAware(m_attributes);
}
13 changes: 13 additions & 0 deletions tests/TestEntryModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ void TestEntryModel::testAttributesModel()

// make sure these don't generate messages
entryAttributes->set("Title", "test");
entryAttributes->set("UserName", "test");
entryAttributes->set("Password", "test");
entryAttributes->set("URL", "test");
entryAttributes->set("Notes", "test");

QCOMPARE(spyDataChanged.count(), 1);
Expand All @@ -214,6 +217,16 @@ void TestEntryModel::testAttributesModel()
entryAttributes->set("2nd", value, true);
QVERIFY(entryAttributes->isProtected("2nd"));
QCOMPARE(entryAttributes->value("2nd"), value);
entryAttributes->clear();

// test attribute sorting
entryAttributes->set("Test1", "1");
entryAttributes->set("Test11", "11");
entryAttributes->set("Test2", "2");
QCOMPARE(model->rowCount(), 3);
QCOMPARE(model->data(model->index(0, 0)).toString(), QString("Test1"));
QCOMPARE(model->data(model->index(1, 0)).toString(), QString("Test2"));
QCOMPARE(model->data(model->index(2, 0)).toString(), QString("Test11"));

QSignalSpy spyReset(model, SIGNAL(modelReset()));
entryAttributes->clear();
Expand Down

0 comments on commit 557f63a

Please sign in to comment.