Skip to content

Commit

Permalink
Use Windows native file icons as a fallback in icon display
Browse files Browse the repository at this point in the history
  • Loading branch information
jlu5 committed May 22, 2017
1 parent 1b1761b commit 4479f8d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
23 changes: 19 additions & 4 deletions fileinsight.cpp
Expand Up @@ -254,13 +254,13 @@ void FileInsight::openFile(QString filename, bool overwrite)
QString mimetype = this->getMimeType(filename);
currentTab->ui->mimeOutput->setPlainText(mimetype);

QIcon icon = this->getIcon(mimetype);
QIcon icon = this->getIcon(mimetype, filename);

// TODO: consider scaling the icon display based on the window size
currentTab->ui->iconDisplay->setPixmap(icon.pixmap(128,128));
}

QIcon FileInsight::getIcon(QString mimetype) {
QIcon FileInsight::getIcon(QString mimetype, QString filename) {
/* Fetch an icon based on the MIME type string given. This uses freedesktop.org
* comptible icon themes, which are native to Linux but can be ported to Windows
* by bundling a theme.
Expand Down Expand Up @@ -294,12 +294,27 @@ QIcon FileInsight::getIcon(QString mimetype) {
/* Otherwise, fall back to the following in order:
* 1) The icon for the generic type (e.g. a "video" icon for .mp4 files)
* 2) The "unknown file type" icon in the icon theme used.
* 3) Qt's (small and out of place) generic file icon.
* 3) IF on Windows, the shell file type icon for the given file.
* 4) Qt's (small and out of place) generic file icon.
*/
if (QIcon::hasThemeIcon(generic_type)) {
icon = QIcon::fromTheme(generic_type);
} else {
icon = QIcon::fromTheme("unknown", this->iconprovider.icon(QFileIconProvider::File));
#ifdef Q_OS_WIN
// Fetch the Windows icon using shell32.SHGetFileInfo:
// https://msdn.microsoft.com/en-us/library/windows/desktop/bb762179(v=vs.85).aspx
SHFILEINFOW shellfileinfo;
SHGetFileInfo((LPCTSTR) filename.utf16(),
FILE_ATTRIBUTE_NORMAL, &shellfileinfo, sizeof(shellfileinfo),
SHGFI_ICON | SHGFI_LARGEICON | SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES);
// Then, convert it into a QIcon.
QPixmap pixmap = QtWin::fromHICON(shellfileinfo.hIcon);
icon = QIcon(pixmap);
#endif

if (icon.isNull()) {
icon = QIcon::fromTheme("unknown", this->iconprovider.icon(QFileIconProvider::File));
}
}
}
return icon;
Expand Down
7 changes: 6 additions & 1 deletion fileinsight.h
Expand Up @@ -24,6 +24,11 @@
#include <QList>
#include <QDebug>

#ifdef Q_OS_WIN
#include <windows.h>
#include <QtWinExtras>
#endif

enum FileInsightBackend { BACKEND_MAGIC, BACKEND_TRID, BACKEND_QT, BACKEND_QT_FILEONLY};

namespace Ui {
Expand All @@ -40,7 +45,7 @@ class FileInsight : public QMainWindow
void chooseFile();
FileInsightBackend getBackend();
FileInsightSubdialog * getCurrentTab();
QIcon getIcon(QString mimetype);
QIcon getIcon(QString mimetype, QString filename);
QString getMagicInfo(QString filename);
QString getMimeType(QString filename);
QString getTridInfo(QString filename);
Expand Down
1 change: 1 addition & 0 deletions fileinsight.pro
Expand Up @@ -24,6 +24,7 @@ FORMS += fileinsight.ui \
win32:INCLUDEPATH += $$PWD\thirdparty\include
win32:LIBPATH += $$PWD\thirdparty\bin
win32:LIBPATH += $$PWD\thirdparty\lib
win32:QT += winextras

# Link to libmagic for file type detection
LIBS += -lmagic

0 comments on commit 4479f8d

Please sign in to comment.