-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfmfileiconprovider.cpp
80 lines (68 loc) · 2.62 KB
/
fmfileiconprovider.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "fmfileiconprovider.h"
#include <QFileInfo>
#include <QPixmap>
#include <QFile>
#include <QUrl>
#include <QDebug>
#include <QSettings>
#include <QDesktopServices>
#include <gio/gio.h>
FMFileIconProvider::FMFileIconProvider()
{
mimeDataBase = new QMimeDatabase;
}
FMFileIconProvider::~FMFileIconProvider() {
delete &mimeDataBase;
}
void FMFileIconProvider::setIconSize(int size) {
if (size > 0)
mIconSize = size;
}
//Qt can't get theme icon directly. use QIcon::fromTheme can, but we need setThemeName first.
QIcon FMFileIconProvider::icon(const QFileInfo &info) const {
/*
const QString filePath = info.filePath();
std::string str = filePath.toStdString();
const char* file_path = str.c_str();
qDebug()<<file_path;
GFile *g_file = g_file_new_for_path(file_path);
GFileInfo *file_info = g_file_query_info(g_file,
"standard::*",
G_FILE_QUERY_INFO_NONE,
NULL,
NULL);
GIcon *g_icon = g_file_info_get_icon (file_info);
const gchar* const* icon_names = g_themed_icon_get_names(G_THEMED_ICON (g_icon));
qDebug()<<*icon_names;
qDebug()<<g_icon_to_string(g_icon);
QString iconName = QString (*icon_names);
qDebug()<<iconName;
g_object_unref(g_icon);
g_object_unref(g_file);
//return QIcon::fromTheme(iconName);
*/
//image file, return a thumbnail.
QPixmap tmpPixmap = QPixmap(info.filePath());
if (!tmpPixmap.isNull()){
tmpPixmap = tmpPixmap.scaled(mIconSize, mIconSize);
return QIcon(tmpPixmap);
}
//this is not a good way.
if (mimeDataBase->mimeTypeForFile(info).iconName() == QString("application-x-desktop")) {
qDebug()<<"is .desktop file";
QSettings desktopFile(info.filePath(), QSettings::IniFormat);
desktopFile.setIniCodec("UTF-8");
QPixmap desktopFileIconPixmap = QPixmap(desktopFile.value("Desktop Entry/Icon").toString());
if (!desktopFileIconPixmap.isNull()) {
desktopFileIconPixmap = desktopFileIconPixmap.scaled(mIconSize,mIconSize);
return QIcon(desktopFileIconPixmap);
}
QIcon icon = QIcon::fromTheme(desktopFile.value("Desktop Entry/Icon").toString());
qDebug()<<icon.name();
return icon;
}
if (mimeDataBase->mimeTypeForUrl(QUrl(info.filePath())).aliases().isEmpty()) {
return QIcon::fromTheme(mimeDataBase->mimeTypeForFile(info).iconName());
}
return QIcon::fromTheme((mimeDataBase->mimeTypeForUrl(QUrl(info.filePath())).iconName()));
}