diff --git a/autotests/kfileitemtest.h b/autotests/kfileitemtest.h --- a/autotests/kfileitemtest.h +++ b/autotests/kfileitemtest.h @@ -36,6 +36,7 @@ void testHiddenFile(); void testMimeTypeOnDemand(); void testCmp(); + void testCmpByUrl(); void testRename(); void testRefresh(); void testDotDirectory(); diff --git a/autotests/kfileitemtest.cpp b/autotests/kfileitemtest.cpp --- a/autotests/kfileitemtest.cpp +++ b/autotests/kfileitemtest.cpp @@ -294,6 +294,27 @@ QVERIFY(fileItem.cmp(fileItem2)); } +void KFileItemTest::testCmpByUrl() +{ + KFileItem nulFileItem; + KFileItem fileItem(QUrl::fromLocalFile(QStringLiteral("1foo"))); + KFileItem fileItem2(QUrl::fromLocalFile(QStringLiteral("fo1"))); + KFileItem fileItem3(QUrl::fromLocalFile(QStringLiteral("foo"))); + + // a KFileItem without url is considered < than anything. + QVERIFY(nulFileItem < nulFileItem); + QVERIFY(nulFileItem < fileItem); + + QVERIFY(!(fileItem < nulFileItem)); + QVERIFY(fileItem < fileItem2); + QVERIFY(!(fileItem2 < fileItem)); + QVERIFY(fileItem2 < fileItem3); + QVERIFY(!(fileItem3 < fileItem2)); + QVERIFY(!(fileItem3 < fileItem)); + // This should be false as they are equal + QVERIFY(!(fileItem < fileItem)); +} + void KFileItemTest::testRename() { KIO::UDSEntry entry; diff --git a/src/core/kfileitem.h b/src/core/kfileitem.h --- a/src/core/kfileitem.h +++ b/src/core/kfileitem.h @@ -486,6 +486,18 @@ */ bool operator!=(const KFileItem &other) const; + /** + * Returns true if other's URL is lexically greater than this url; otherwise returns false + * @since 5.47 + */ + bool operator<(const KFileItem &other) const; + + /** + * Returns true if url other is lexically greater than this url; otherwise returns false + * @since 5.47 + */ + bool operator<(const QUrl &other) const; + /** * Converts this KFileItem to a QVariant, this allows to use KFileItem * in QVariant() constructor diff --git a/src/core/kfileitem.cpp b/src/core/kfileitem.cpp --- a/src/core/kfileitem.cpp +++ b/src/core/kfileitem.cpp @@ -1236,6 +1236,25 @@ return !operator==(other); } +bool KFileItem::operator<(const KFileItem &other) const +{ + if (!d) { + return true; + } + if (!other.d) { + return false; + } + return d->m_url < other.d->m_url; +} + +bool KFileItem::operator<(const QUrl &other) const +{ + if (!d) { + return true; + } + return d->m_url < other; +} + KFileItem::operator QVariant() const { return qVariantFromValue(*this);