1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-23 00:04:06 +00:00
transmission/qt/IconCache.cc

158 lines
3.2 KiB
C++

/*
* This file Copyright (C) 2009-2015 Mnemosyne LLC
*
* It may be used under the GNU GPL versions 2 or 3
* or any future license endorsed by Mnemosyne LLC.
*
*/
#include "IconCache.h"
#ifdef _WIN32
#include <windows.h>
#include <shellapi.h>
#endif
#include <QFile>
#include <QFileIconProvider>
#include <QFileInfo>
#include <QIcon>
#include <QObject>
#ifdef _WIN32
#include <QPixmapCache>
#include <QtWin>
#else
#include <QMimeDatabase>
#include <QMimeType>
#endif
#include <libtransmission/transmission.h>
/***
****
***/
IconCache& IconCache::get()
{
static auto& singleton = *new IconCache();
return singleton;
}
IconCache::IconCache() :
folder_icon_(QFileIconProvider().icon(QFileIconProvider::Folder)),
file_icon_(QFileIconProvider().icon(QFileIconProvider::File))
{
}
QIcon IconCache::guessMimeIcon(QString const& filename, QIcon fallback) const
{
QIcon icon;
#ifdef _WIN32
if (!filename.isEmpty())
{
QFileInfo const file_info(filename);
addAssociatedFileIcon(file_info, SHGFI_SMALLICON, icon);
addAssociatedFileIcon(file_info, 0, icon);
addAssociatedFileIcon(file_info, SHGFI_LARGEICON, icon);
}
#else
icon = getMimeIcon(filename);
#endif
if (icon.isNull())
{
icon = fallback;
}
return icon;
}
/***
****
***/
#ifdef _WIN32
void IconCache::addAssociatedFileIcon(QFileInfo const& file_info, UINT icon_size, QIcon& icon) const
{
QString const pixmap_cache_key = QStringLiteral("tr_file_ext_") + QString::number(icon_size) + QLatin1Char('_') +
file_info.suffix();
QPixmap pixmap;
if (!QPixmapCache::find(pixmap_cache_key, &pixmap))
{
auto const filename = file_info.fileName().toStdWString();
SHFILEINFO shell_file_info;
if (::SHGetFileInfoW(filename.data(), FILE_ATTRIBUTE_NORMAL, &shell_file_info,
sizeof(shell_file_info), SHGFI_ICON | icon_size | SHGFI_USEFILEATTRIBUTES) != 0)
{
if (shell_file_info.hIcon != nullptr)
{
pixmap = QtWin::fromHICON(shell_file_info.hIcon);
::DestroyIcon(shell_file_info.hIcon);
}
}
QPixmapCache::insert(pixmap_cache_key, pixmap);
}
if (!pixmap.isNull())
{
icon.addPixmap(pixmap);
}
}
#else
QIcon IconCache::getMimeIcon(QString const& filename) const
{
if (suffixes_.empty())
{
for (auto const& type : QMimeDatabase().allMimeTypes())
{
auto const tmp = type.suffixes();
suffixes_.insert(tmp.begin(), tmp.end());
}
}
auto const ext = QFileInfo(filename).suffix();
if (suffixes_.count(ext) == 0)
{
return {};
}
QIcon& icon = icon_cache_[ext];
if (icon.isNull()) // cache miss
{
QMimeDatabase mime_db;
QMimeType type = mime_db.mimeTypeForFile(filename, QMimeDatabase::MatchExtension);
if (icon.isNull())
{
icon = QIcon::fromTheme(type.iconName());
}
if (icon.isNull())
{
icon = QIcon::fromTheme(type.genericIconName());
}
if (icon.isNull())
{
icon = {};
}
}
return icon;
}
#endif