-
Notifications
You must be signed in to change notification settings - Fork 11
/
iconloader.cpp
196 lines (163 loc) · 4.15 KB
/
iconloader.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "iconloader.h"
#include <stdlib.h>
#include <QtCore/QFile>
#include <QtCore/QDir>
#include <QtCore/QTextStream>
struct IconDirectory
{
QString m_path;
int m_size;
bool m_scalable;
};
void IconTheme::init(const QString& themeName)
{
m_themeName = themeName;
foreach(const QString& searchPath, IconLoader::instance()->iconSearchPaths())
{
QFile file(searchPath + "/" + m_themeName + "/index.theme");
if(!file.exists())
continue;
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
continue;
QTextStream in(&file);
QString context;
while(!in.atEnd())
{
QString line = in.readLine();
if(line.isEmpty())
continue;
if(line[0] == '#')
continue;
if(line[0] == '[')
{
if(line.size() < 3)
continue;
context = line.mid(1, line.size() - 2);
if(context != "Icon Theme")
{
IconDirectory dir;
dir.m_path = context;
dir.m_size = 0;
dir.m_scalable = false;
m_iconDirs.append(dir);
}
}
QStringList list = line.split('=');
if(list.size() < 2)
continue;
QString key = list[0];
QString value = list[1];
if(key == "Inherits")
m_inheritedThemes = value.split(',');
if(key == "Size")
{
m_iconDirs.last().m_size = value.toInt();
}
if(key == "Type")
{
if(value.compare("Scalable", Qt::CaseInsensitive) == 0)
m_iconDirs.last().m_scalable = true;
}
}
}
}
bool IconTheme::loadIconFromDirectory(QImage& result, const IconDirectory& iconDir, const QString& fileName)
{
foreach(const QString& searchPath, IconLoader::instance()->iconSearchPaths())
{
QString iconFileName = searchPath + "/" + m_themeName + "/" + iconDir.m_path + "/" + fileName;
if(!QFile::exists(iconFileName))
continue;
result.load(iconFileName);
if(!result.isNull())
return true;
}
return false;
}
QImage IconTheme::loadIcon(const QString& iconName, int size)
{
QString fileName = iconName;
if(!fileName.endsWith(".png"))
fileName.append(".png");
QImage result;
foreach(const IconDirectory& iconDir, m_iconDirs)
{
if(!iconDir.m_scalable && iconDir.m_size == size)
{
if(loadIconFromDirectory(result, iconDir, fileName))
return result;
}
}
foreach(const IconDirectory& iconDir, m_iconDirs)
{
if(!iconDir.m_scalable && iconDir.m_size > size)
{
if(loadIconFromDirectory(result, iconDir, fileName))
return result;
}
}
foreach(const IconDirectory& iconDir, m_iconDirs)
{
if(!iconDir.m_scalable)
{
if(loadIconFromDirectory(result, iconDir, fileName))
return result;
}
}
return result;
}
IconLoader* IconLoader::m_instance = NULL;
IconLoader::IconLoader()
{
m_instance = this;
m_iconSearchPaths.append(QString(getenv("HOME")) + "/.icons");
QString xdgDataDirs;
char* xdgDataDirsEnv = getenv("XDG_DATA_DIRS");
if(xdgDataDirsEnv != NULL)
xdgDataDirs = xdgDataDirsEnv;
else
xdgDataDirs = "/usr/local/share/:/usr/share/";
QStringList dirs = xdgDataDirs.split(':');
foreach(const QString& dir, dirs)
{
m_iconSearchPaths.append(dir + "icons");
}
}
IconLoader::~IconLoader()
{
m_instance = NULL;
}
QImage IconLoader::loadIcon(const QString& themeName, const QString& iconName, int size)
{
QImage result = loadIconFromTheme(themeName, iconName, size);
if(result.isNull())
result.load("/usr/share/pixmaps/" + iconName);
if(result.isNull())
result.load(iconName);
if(!result.isNull() && (result.width() != size || result.height() != size))
result = result.scaled(size, size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
return result;
}
QImage IconLoader::loadIconFromTheme(const QString& themeName, const QString& iconName, int size)
{
if(!m_iconThemes.contains(themeName))
{
m_iconThemesMutex.lock();
m_iconThemes[themeName].init(themeName);
m_iconThemesMutex.unlock();
}
QImage result = m_iconThemes[themeName].loadIcon(iconName, size);
if(result.isNull() && themeName != "hicolor")
{
QStringList inheritedThemes = m_iconThemes[themeName].inheritedThemes();
if(inheritedThemes.empty())
inheritedThemes.append("hicolor");
foreach(const QString& inheritedTheme, inheritedThemes)
{
result = loadIconFromTheme(inheritedTheme, iconName, size);
if(!result.isNull())
break;
}
}
return result;
}