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,42 @@ QVERIFY(fileItem.cmp(fileItem2)); } +void KFileItemTest::testCmpByUrl() +{ + const QUrl nulUrl; + const QUrl url = QUrl::fromLocalFile(QStringLiteral("1foo")); + const QUrl url2 = QUrl::fromLocalFile(QStringLiteral("fo1")); + const QUrl url3 = QUrl::fromLocalFile(QStringLiteral("foo")); + KFileItem nulFileItem; + KFileItem nulFileItem2(nulUrl); + KFileItem fileItem(url); + KFileItem fileItem2(url2); + KFileItem fileItem3(url3); + + // an invalid KFileItem is considered equal to any other invalid KFileItem or invalid QUrl. + QVERIFY(!(nulFileItem < nulFileItem)); + QVERIFY(!(nulFileItem < nulFileItem2)); + QVERIFY(!(nulFileItem2 < nulFileItem)); + QVERIFY(!(nulFileItem < nulUrl)); + // an invalid KFileItem is considered less than any valid KFileItem. + QVERIFY(nulFileItem < fileItem); + // a valid KFileItem is not less than an invalid KFileItem or invalid QUrl + QVERIFY(!(fileItem < nulUrl)); + QVERIFY(!(fileItem < nulFileItem)); + QVERIFY(!(fileItem < nulFileItem2)); + + QVERIFY(fileItem < fileItem2); + QVERIFY(fileItem < url2); + QVERIFY(!(fileItem2 < fileItem)); + QVERIFY(fileItem2 < fileItem3); + QVERIFY(fileItem < url3); + QVERIFY(!(fileItem3 < fileItem2)); + QVERIFY(!(fileItem3 < fileItem)); + // Must be false as they are considered equal + QVERIFY(!(fileItem < fileItem)); + QVERIFY(!(fileItem < url)); +} + 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 this item's URL is lexically less than other's URL; otherwise returns false + * @since 5.48 + */ + bool operator<(const KFileItem &other) const; + + /** + * Returns true if this item's URL is lexically less than url other; otherwise returns false + * @since 5.48 + */ + 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 @@ -1240,6 +1240,25 @@ return !operator==(other); } +bool KFileItem::operator<(const KFileItem &other) const +{ + if (!other.d) { + return false; + } + if (!d) { + return other.d->m_url.isValid(); + } + return d->m_url < other.d->m_url; +} + +bool KFileItem::operator<(const QUrl &other) const +{ + if (!d) { + return other.isValid(); + } + return d->m_url < other; +} + KFileItem::operator QVariant() const { return qVariantFromValue(*this);