diff --git a/autotests/kfileitemtest.cpp b/autotests/kfileitemtest.cpp --- a/autotests/kfileitemtest.cpp +++ b/autotests/kfileitemtest.cpp @@ -30,6 +30,7 @@ #include "kiotesthelper.h" #include +#include QTEST_MAIN(KFileItemTest) @@ -620,7 +621,18 @@ QTest::newRow("relative") << QUrl("foo") << "unknown"; QTest::newRow("tilde") << QUrl("~") << "unknown"; - // TODO more tests + QTest::newRow("unknownscheme folder") << QUrl("unknownscheme:/") << "inode-directory"; + QTest::newRow("unknownscheme file") << QUrl("unknownscheme:/test") << "application-octet-stream"; + + QTest::newRow("trash folder") << QUrl("trash:/") << "user-trash-full"; + QTest::newRow("trash file") << QUrl("trash:/test") << "user-trash-full"; + + QTest::newRow("https scheme") << QUrl("https://kde.org/") << "text-html"; + + if (KProtocolInfo::isKnownProtocol("smb")) { + QTest::newRow("smb folder") << QUrl("smb:/") << "network-workgroup"; + QTest::newRow("smb file") << QUrl("smb:/test") << "network-workgroup"; + } } void KFileItemTest::testIconNameForUrl() diff --git a/src/core/global.cpp b/src/core/global.cpp --- a/src/core/global.cpp +++ b/src/core/global.cpp @@ -223,55 +223,51 @@ QString KIO::iconNameForUrl(const QUrl &url) { - const QLatin1String unknown("unknown"); if (url.scheme().isEmpty()) { // empty URL or relative URL (e.g. '~') - return unknown; + return QStringLiteral("unknown"); } QMimeDatabase db; const QMimeType mt = db.mimeTypeForUrl(url); - const QString mimeTypeIcon = mt.iconName(); - QString i = mimeTypeIcon; + QString iconName; if (url.isLocalFile()) { // Check to see whether it's an xdg location (e.g. Pictures folder) if (mt.inherits(QStringLiteral("inode/directory"))) { - i = KIOPrivate::iconForStandardPath(url.toLocalFile()); + iconName = KIOPrivate::iconForStandardPath(url.toLocalFile()); } // Let KFileItem::iconName handle things for us - if (i == unknown || i.isEmpty() || mt.isDefault()) { + if (iconName.isEmpty()) { const KFileItem item(url, mt.name()); - i = item.iconName(); + iconName = item.iconName(); } } else { // It's non-local and maybe on a slow filesystem // Look for a favicon if (url.scheme().startsWith(QLatin1String("http"))) { - i = favIconForUrl(url); + iconName = favIconForUrl(url); } // Then handle the trash else if (url.scheme() == QLatin1String("trash") && url.path().length() <= 1) { KConfig trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig); if (trashConfig.group("Status").readEntry("Empty", true)) { - i = QStringLiteral("user-trash"); + iconName = QStringLiteral("user-trash"); } else { - i = QStringLiteral("user-trash-full"); + iconName = QStringLiteral("user-trash-full"); } } - if (i.isEmpty()) { - i = KProtocolInfo::icon(url.scheme()); - } - - // root of protocol: if we found nothing, revert to mimeTypeIcon (which is usually "folder") - if (url.path().length() <= 1 && (i == unknown || i.isEmpty())) { - i = mimeTypeIcon; + // and other protocols + if (iconName.isEmpty()) { + iconName = KProtocolInfo::icon(url.scheme()); } } - return !i.isEmpty() ? i : unknown; + // if we found nothing, return QMimeType.iconName() + // (which fallbacks to "application-octet-stream" when no mimetype could be determined) + return !iconName.isEmpty() ? iconName : mt.iconName(); } QUrl KIO::upUrl(const QUrl &url)