diff --git a/src/core/global.h b/src/core/global.h --- a/src/core/global.h +++ b/src/core/global.h @@ -306,6 +306,31 @@ KIOCORE_EXPORT QString favIconForUrl(const QUrl &url); /** + * Returns the icon in the .directory file that corresponds to the folder at + * the given path, if any. + * + * If unavailable, returns QString(). + * + * @param path the path to a folder + * @return the icon listed in the folder's .directory file, or QString() + * + * @since 5.47 + */ +KIOCORE_EXPORT QString iconFromDirectoryFile(const QString &path); + +/** + * Returns the icon for the .desktop file at the given path, if any. + * + * If unavailable, returns QString(). + * + * @param path the path to a .desktop file + * @return the icon listed in the .desktop file, or QString() + * + * @since 5.47 + */ +KIOCORE_EXPORT QString iconFromDesktopFile(const QString &path); + +/** * Converts KIO file permissions from mode_t to QFile::Permissions format. * * This is a convenience function for converting KIO permissions parameter from diff --git a/src/core/global.cpp b/src/core/global.cpp --- a/src/core/global.cpp +++ b/src/core/global.cpp @@ -23,9 +23,12 @@ #include #include #include +#include #include #include #include + +#include #include #include #include @@ -299,6 +302,65 @@ return FavIconsCache::instance()->iconForUrl(url); } +QString KIO::iconFromDirectoryFile(const QString &path) +{ + const QString filePath = path + QLatin1String("/.directory"); + if (!QFileInfo(filePath).isFile()) { // exists -and- is a file + return QString(); + } + + KDesktopFile cfg(filePath); + QString icon = cfg.readIcon(); + + const KConfigGroup group = cfg.desktopGroup(); + const QString emptyIcon = group.readEntry("EmptyIcon"); + if (!emptyIcon.isEmpty()) { + bool isDirEmpty = true; + QDirIterator dirIt(path, QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); + while (dirIt.hasNext()) { + dirIt.next(); + if (dirIt.fileName() != QLatin1String(".directory")) { + isDirEmpty = false; + break; + } + } + if (isDirEmpty) { + icon = emptyIcon; + } + } + + if (icon.startsWith(QLatin1String("./"))) { + // path is relative with respect to the location + // of the .directory file (#73463) + return path + icon.mid(1); + } + return icon; +} + +QString KIO::iconFromDesktopFile(const QString &path) +{ + KDesktopFile cfg(path); + const QString icon = cfg.readIcon(); + if (cfg.hasLinkType()) { + const KConfigGroup group = cfg.desktopGroup(); + const QString emptyIcon = group.readEntry("EmptyIcon"); + const QString type = cfg.readPath(); + if (!emptyIcon.isEmpty()) { + const QString u = cfg.readUrl(); + const QUrl url(u); + if (url.scheme() == QLatin1String("trash")) { + // We need to find if the trash is empty, preferably without using a KIO job. + // So instead kio_trash leaves an entry in its config file for us. + KConfig trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig); + if (trashConfig.group("Status").readEntry("Empty", true)) { + return emptyIcon; + } + } + } + } + return icon; +} + QString KIO::iconNameForUrl(const QUrl &url) { const QLatin1String unknown("unknown"); diff --git a/src/core/kfileitem.cpp b/src/core/kfileitem.cpp --- a/src/core/kfileitem.cpp +++ b/src/core/kfileitem.cpp @@ -26,7 +26,6 @@ #include #include -#include #include #include #include @@ -832,65 +831,6 @@ } } -static QString iconFromDirectoryFile(const QString &path) -{ - const QString filePath = path + QLatin1String("/.directory"); - if (!QFileInfo(filePath).isFile()) { // exists -and- is a file - return QString(); - } - - KDesktopFile cfg(filePath); - QString icon = cfg.readIcon(); - - const KConfigGroup group = cfg.desktopGroup(); - const QString emptyIcon = group.readEntry("EmptyIcon"); - if (!emptyIcon.isEmpty()) { - bool isDirEmpty = true; - QDirIterator dirIt(path, QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); - while (dirIt.hasNext()) { - dirIt.next(); - if (dirIt.fileName() != QLatin1String(".directory")) { - isDirEmpty = false; - break; - } - } - if (isDirEmpty) { - icon = emptyIcon; - } - } - - if (icon.startsWith(QLatin1String("./"))) { - // path is relative with respect to the location - // of the .directory file (#73463) - return path + icon.mid(1); - } - return icon; -} - -static QString iconFromDesktopFile(const QString &path) -{ - KDesktopFile cfg(path); - const QString icon = cfg.readIcon(); - if (cfg.hasLinkType()) { - const KConfigGroup group = cfg.desktopGroup(); - const QString emptyIcon = group.readEntry("EmptyIcon"); - const QString type = cfg.readPath(); - if (!emptyIcon.isEmpty()) { - const QString u = cfg.readUrl(); - const QUrl url(u); - if (url.scheme() == QLatin1String("trash")) { - // We need to find if the trash is empty, preferably without using a KIO job. - // So instead kio_trash leaves an entry in its config file for us. - KConfig trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig); - if (trashConfig.group("Status").readEntry("Empty", true)) { - return emptyIcon; - } - } - } - } - return icon; -} - QString KFileItem::iconName() const { if (!d) { @@ -922,16 +862,16 @@ const bool delaySlowOperations = d->m_delayedMimeTypes; if (isLocalUrl && !delaySlowOperations && mime.inherits(QStringLiteral("application/x-desktop"))) { - d->m_iconName = iconFromDesktopFile(url.toLocalFile()); + d->m_iconName = KIO::iconFromDesktopFile(url.toLocalFile()); if (!d->m_iconName.isEmpty()) { d->m_useIconNameCache = d->m_bMimeTypeKnown; return d->m_iconName; } } if (isLocalUrl && !delaySlowOperations && isDir()) { if (isDirectoryMounted(url)) { - d->m_iconName = iconFromDirectoryFile(url.toLocalFile()); + d->m_iconName = KIO::iconFromDirectoryFile(url.toLocalFile()); if (!d->m_iconName.isEmpty()) { d->m_useIconNameCache = d->m_bMimeTypeKnown; return d->m_iconName;