-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiconprovider.cpp
116 lines (103 loc) · 4.07 KB
/
iconprovider.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* desktop-file-memos: A desktop classification app on Linux.
*
* Copyright (C) 2019, Tianjin KYLIN Information Technology Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Yue Lan<[email protected]>
*
*/
#include "iconprovider.h"
#include <QMimeDatabase>
#include <QUrl>
#include <gio/gio.h>
#include <gio/gdesktopappinfo.h>
#include <QDebug>
IconProvider::IconProvider() {
mimeDatabase = new QMimeDatabase;
}
IconProvider::~IconProvider() {
delete mimeDatabase;
}
void IconProvider::setIconSize(int size) {
mIconSize = size;
}
int IconProvider::getIconSize() {
return mIconSize;
}
QIcon IconProvider::icon(const QFileInfo &info) const {
QIcon fileIcon;
//image file, return a thumbnail.
QPixmap tmpPixmap = QPixmap(info.filePath());
if (!tmpPixmap.isNull()){
tmpPixmap = tmpPixmap.scaled(mIconSize, mIconSize);
fileIcon = QIcon(tmpPixmap);
return fileIcon;
}
//we need do something special to '*.desktop' file.
if (mimeDatabase->mimeTypeForFile(info).iconName() == QString("application-x-desktop")) {
std::string tmp_str = info.filePath().toStdString();
const char* file_path = tmp_str.c_str();
GDesktopAppInfo *desktop_app_info = g_desktop_app_info_new_from_filename(file_path);
if (desktop_app_info != nullptr) {
char* tmp_icon_name = g_desktop_app_info_get_string(desktop_app_info, "Icon");
QIcon desktopFileIcon = QIcon::fromTheme(QString(tmp_icon_name));
if (desktopFileIcon.isNull()) {
QPixmap pixmap = QPixmap(QString (tmp_icon_name));
if (pixmap.isNull()) {
fileIcon = QIcon::fromTheme("application-x-desktop");
} else {
pixmap = pixmap.scaled(mIconSize, mIconSize);
QIcon pixmapIcon = QIcon(pixmap);
fileIcon = pixmapIcon;
}
} else {
fileIcon = desktopFileIcon;
}
if (tmp_icon_name != nullptr) {
g_free (tmp_icon_name);
}
}
return fileIcon;
}
/*
if (mimeDatabase->mimeTypeForUrl(QUrl(info.filePath())).aliases().isEmpty()) {
qDebug()<<mimeDatabase->mimeTypeForFile(info).iconName();
return QIcon::fromTheme(mimeDatabase->mimeTypeForFile(info).iconName());
} else {
*/
QString filePath = info.filePath();
std::string str = filePath.toStdString();
const char* file_path = str.c_str();
GFile *g_file = g_file_new_for_path(file_path);
GFileInfo *file_info = g_file_query_info(g_file,
G_FILE_ATTRIBUTE_STANDARD_ICON,
G_FILE_QUERY_INFO_NONE,
nullptr,
nullptr);
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));
QString iconName = QString (*icon_names);
while (iconName.contains(".") || iconName.contains("/")) {
qDebug()<<*icon_names;
icon_names++; //we need use second string, first string is not correct some times (for example, a wps-office-doc).
iconName = QString (*icon_names);
}
QIcon icon = QIcon::fromTheme(iconName);
g_object_unref(file_info);
g_object_unref(g_file);
return icon;
//}
}