Browse Source

Add QFileIconProvider implementation using mime database

If built-in QFileIconProvider does not seem to work, use custom
implementation which queries mime database.
adaptive-webui-19844
Eugene Shalygin 8 years ago
parent
commit
57493c5556
  1. 49
      src/gui/torrentcontentmodel.cpp

49
src/gui/torrentcontentmodel.cpp

@ -37,6 +37,9 @@
#include <Windows.h> #include <Windows.h>
#include <Shellapi.h> #include <Shellapi.h>
#include <QtWin> #include <QtWin>
#else
#include <QMimeDatabase>
#include <QMimeType>
#endif #endif
#include "guiiconprovider.h" #include "guiiconprovider.h"
@ -85,6 +88,49 @@ namespace
return QIcon(iconPixmap); return QIcon(iconPixmap);
} }
}; };
#else
/**
* @brief Tests whether QFileIconProvider actually works
*
* Some QPA plugins do not implement QPlatformTheme::fileIcon(), and
* QFileIconProvider::icon() returns empty icons as the result. Here we ask it for
* two icons for probably absent files and when both icons are null, we assume that
* the current QPA plugin does not implement QPlatformTheme::fileIcon().
*/
bool doesQFileIconProviderWork()
{
QFileIconProvider provider;
const char PSEUDO_UNIQUE_FILE_NAME[] = "/tmp/qBittorrent-test-QFileIconProvider-845eb448-7ad5-4cdb-b764-b3f322a266a9";
QIcon testIcon1 = provider.icon(QFileInfo(
QLatin1String(PSEUDO_UNIQUE_FILE_NAME) + QLatin1String(".pdf")));
QIcon testIcon2 = provider.icon(QFileInfo(
QLatin1String(PSEUDO_UNIQUE_FILE_NAME) + QLatin1String(".png")));
return (!testIcon1.isNull() || !testIcon2.isNull());
}
class MimeFileIconProvider: public UnifiedFileIconProvider
{
using QFileIconProvider::icon;
QIcon icon(const QFileInfo &info) const override
{
const QMimeType mimeType = m_db.mimeTypeForFile(info, QMimeDatabase::MatchExtension);
QIcon res = QIcon::fromTheme(mimeType.iconName());
if (!res.isNull()) {
return res;
}
res = QIcon::fromTheme(mimeType.genericIconName());
if (!res.isNull()) {
return res;
}
return UnifiedFileIconProvider::icon(info);
}
private:
QMimeDatabase m_db;
};
#endif #endif
} }
@ -95,7 +141,8 @@ TorrentContentModel::TorrentContentModel(QObject *parent)
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
m_fileIconProvider = new WinShellFileIconProvider(); m_fileIconProvider = new WinShellFileIconProvider();
#else #else
m_fileIconProvider = new QFileIconProvider(); static bool doesBuiltInProviderWork = doesQFileIconProviderWork();
m_fileIconProvider = doesBuiltInProviderWork ? new QFileIconProvider() : new MimeFileIconProvider();
#endif #endif
} }

Loading…
Cancel
Save