mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-01-10 23:07:59 +00:00
Add QFileIconProvider implementation using mime database
If built-in QFileIconProvider does not seem to work, use custom implementation which queries mime database.
This commit is contained in:
parent
30bf6e958f
commit
57493c5556
@ -37,6 +37,9 @@
|
||||
#include <Windows.h>
|
||||
#include <Shellapi.h>
|
||||
#include <QtWin>
|
||||
#else
|
||||
#include <QMimeDatabase>
|
||||
#include <QMimeType>
|
||||
#endif
|
||||
|
||||
#include "guiiconprovider.h"
|
||||
@ -85,6 +88,49 @@ namespace
|
||||
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
|
||||
}
|
||||
|
||||
@ -95,7 +141,8 @@ TorrentContentModel::TorrentContentModel(QObject *parent)
|
||||
#ifdef Q_OS_WIN
|
||||
m_fileIconProvider = new WinShellFileIconProvider();
|
||||
#else
|
||||
m_fileIconProvider = new QFileIconProvider();
|
||||
static bool doesBuiltInProviderWork = doesQFileIconProviderWork();
|
||||
m_fileIconProvider = doesBuiltInProviderWork ? new QFileIconProvider() : new MimeFileIconProvider();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user