| 1 | #include "icon_cache.h" |
| 2 | #include "item_model.h" |
| 3 | #if defined(Q_OS_MACOS) |
| 4 | #include "osx_helper.h" |
| 5 | #endif |
| 6 | |
| 7 | IconCache::IconCache(QObject *parent) : QObject(parent) { |
| 8 | mFileIcon = QFileIconProvider().icon(QFileIconProvider::File); |
| 9 | |
| 10 | #if defined(Q_OS_WIN32) |
| 11 | CoInitializeEx(NULL, COINIT_MULTITHREADED); |
| 12 | #endif |
| 13 | |
| 14 | mThread.start(); |
| 15 | moveToThread(&mThread); |
| 16 | } |
| 17 | |
| 18 | IconCache::~IconCache() { |
| 19 | mThread.quit(); |
| 20 | mThread.wait(); |
| 21 | |
| 22 | #if defined(Q_OS_WIN32) |
| 23 | CoUninitialize(); |
| 24 | #endif |
| 25 | } |
| 26 | |
| 27 | void IconCache::getIcon(Item *item, const QPersistentModelIndex &parent) { |
| 28 | QString ext = QFileInfo(item->name).suffix(); |
| 29 | QIcon icon; |
| 30 | auto it = mIcons.find(ext); |
| 31 | if (it == mIcons.end()) { |
| 32 | #if defined(Q_OS_WIN32) |
| 33 | SHFILEINFOW info; |
| 34 | if (SHGetFileInfoW(reinterpret_cast<LPCWSTR>(("dummy." + ext).utf16()), |
| 35 | FILE_ATTRIBUTE_NORMAL, &info, sizeof(info), |
| 36 | SHGFI_ICON | SHGFI_USEFILEATTRIBUTES) && |
| 37 | info.hIcon) { |
| 38 | icon = QtWin::fromHICON(info.hIcon); |
| 39 | DestroyIcon(info.hIcon); |
| 40 | } |
| 41 | #elif defined(Q_OS_MACOS) |
| 42 | icon = osxGetIcon(ext.toUtf8().constData()); |
| 43 | #else |
| 44 | QMimeType mime = mMimeDatabase.mimeTypeForFile( |
| 45 | item->name, QMimeDatabase::MatchExtension); |
| 46 | if (mime.isValid()) { |
| 47 | icon = QIcon::fromTheme(mime.iconName()); |
| 48 | } |
| 49 | #endif |
| 50 | if (icon.isNull()) { |
| 51 | icon = mFileIcon; |
| 52 | } |
| 53 | mIcons.insert(ext, icon); |
| 54 | } else { |
| 55 | icon = it.value(); |
| 56 | } |
| 57 | |
| 58 | emit iconReady(item, parent, icon); |
| 59 | } |