-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdicontheme.cpp
More file actions
305 lines (248 loc) · 8.65 KB
/
dicontheme.cpp
File metadata and controls
305 lines (248 loc) · 8.65 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// SPDX-FileCopyrightText: 2022-2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "dicontheme.h"
#include "private/dbuiltiniconengine_p.h"
#include "private/dciiconengine_p.h"
#include "private/diconproxyengine_p.h"
#include <private/qicon_p.h>
#ifndef DTK_DISABLE_LIBXDG
#include "private/xdgiconproxyengine_p.h"
#else
#include <qpa/qplatformtheme.h>
#include <private/qguiapplication_p.h>
#include <private/qiconloader_p.h>
#endif
#include <DStandardPaths>
#include <QCache>
#include <QSet>
#include <QGuiApplication>
#include <QFileInfo>
#include <QDir>
DGUI_BEGIN_NAMESPACE
DCORE_USE_NAMESPACE
static inline QString joinPath(const QString &basePath, const QString &path)
{
if (path.isEmpty())
return basePath;
return basePath + QDir::separator() + path;
}
static inline QStringList systemDciThemePaths()
{
QStringList paths;
const auto dataPaths = DStandardPaths::paths(DStandardPaths::DSG::DataDir);
paths.reserve(dataPaths.size());
for (const auto &dataPath : dataPaths) {
paths.push_back(joinPath(dataPath, QLatin1String("icons")));
}
return paths;
}
static inline QString applicationDciThemePath()
{
return QLatin1String(":/dsg/icons");
}
static inline QString applicationBuiltInIconPath()
{
return QLatin1String(":/dsg/built-in-icons");
}
static QString findDciIconFromPath(const QString &iconName, const QString &themeName, const QString path)
{
if (path.isEmpty() || iconName.isEmpty())
return nullptr;
QString themePath = joinPath(path, themeName);
QFileInfo themeInfo(themePath);
if (!themeInfo.exists() || !themeInfo.isDir())
return nullptr;
/*
* iconName has two types, like as:
* 1. example.dci
* 2. org.deepin.app/example.dci
*
* We do not need to consider the icon name,
* just whether the file exists.
*/
QString iconNameWithSuffix(iconName);
// Focus that the icon name can not contain the dci suffix.
iconNameWithSuffix += QLatin1String(".dci");
QString iconPath = joinPath(themePath, iconNameWithSuffix);
if (!QDir::cleanPath(iconPath).startsWith(QDir::cleanPath(themePath))) // Wrongful
return nullptr;
QFileInfo iconInfo(iconPath);
if (iconInfo.exists() && iconInfo.isFile())
return iconPath;
return nullptr;
}
QIconEngine *DIconTheme::createIconEngine(const QString &iconName, Options options)
{
return new DIconProxyEngine(iconName, options);
}
QIcon DIconTheme::findQIcon(const QString &iconName, Options options)
{
if (QDir::isAbsolutePath(iconName)) {
return QIcon(iconName);
}
auto engine = createIconEngine(iconName, options);
// fallback to QIcon::fromTheme
if (!options.testFlag(DontFallbackToQIconFromTheme) && !engine)
return QIcon::fromTheme(iconName);
return QIcon(engine);
}
QIcon DIconTheme::findQIcon(const QString &iconName, const QIcon &fallback, Options options)
{
QIcon icon = findQIcon(iconName, options);
return !icon.isNull() ? icon : fallback;
}
bool DIconTheme::isBuiltinIcon(const QIcon &icon)
{
if (icon.isNull())
return false;
QIconEngine *engine = const_cast<QIcon &>(icon).data_ptr()->engine;
if (auto proxyEngine = dynamic_cast<DIconProxyEngine *>(engine))
return !proxyEngine->proxyKey().compare("DBuiltinIconEngine");
return dynamic_cast<DBuiltinIconEngine *>(engine);
}
bool DIconTheme::isXdgIcon(const QIcon &icon)
{
if (icon.isNull())
return false;
#ifdef DTK_DISABLE_LIBXDG
return false;
#else
QIconEngine *engine = const_cast<QIcon &>(icon).data_ptr()->engine;
if (auto proxyEngine = dynamic_cast<DIconProxyEngine *>(engine))
return !proxyEngine->proxyKey().compare("XdgIconProxyEngine");
return dynamic_cast<XdgIconProxyEngine *>(engine);
#endif
}
class DIconTheme::CachedData
{
public:
QCache<QString, QIcon> cache;
QCache<QString, QString> dciIconPathCache;
};
DIconTheme::Cached::Cached()
: data(new CachedData())
{
}
DIconTheme::Cached::~Cached()
{
delete data;
}
int DIconTheme::Cached::maxCost() const
{
return data->cache.maxCost();
}
void DIconTheme::Cached::setMaxCost(int cost)
{
data->cache.setMaxCost(cost);
data->dciIconPathCache.setMaxCost(cost);
}
void DIconTheme::Cached::clear()
{
data->cache.clear();
data->dciIconPathCache.clear();
}
QIcon DIconTheme::Cached::findQIcon(const QString &iconName, Options options, const QIcon &fallback)
{
const QString themeName = QIcon::themeName();
const QString cacheKey = themeName + QChar('/') + iconName + QChar('/') + QString::number(static_cast<int>(options));
if (data->cache.contains(cacheKey)) {
auto cacheIcon = data->cache.object(cacheKey);
if (cacheIcon->isNull())
return fallback;
return *cacheIcon;
}
auto newIcon = new QIcon(DIconTheme::findQIcon(iconName, options));
if (newIcon->isNull()) {
return fallback;
} else {
data->cache.insert(cacheKey, newIcon);
}
return *newIcon;
}
QString DIconTheme::Cached::findDciIconFile(const QString &iconName, const QString &themeName, const QString &fallback)
{
const QString cacheKey = themeName + QLatin1Char('/') + iconName;
if (data->dciIconPathCache.contains(cacheKey)) {
const QString *cachePath = data->dciIconPathCache.object(cacheKey);
if (cachePath->isEmpty())
return fallback;
return *cachePath;
}
QString *path = new QString(DIconTheme::findDciIconFile(iconName, themeName));
data->dciIconPathCache.insert(cacheKey, path);
if (path->isEmpty())
return fallback;
return *path;
}
static inline QStringList getDciThemePaths() {
QStringList paths = systemDciThemePaths();
paths.push_back(applicationDciThemePath());
return paths;
}
Q_GLOBAL_STATIC(DIconTheme::Cached, _globalCache)
Q_GLOBAL_STATIC_WITH_ARGS(QStringList, _dciThemePath, (getDciThemePaths()))
static void cleanGlobalCache() {
if (_globalCache.exists())
_globalCache->clear();
}
DIconTheme::Cached *DIconTheme::cached()
{
if (Q_UNLIKELY(!_globalCache.exists() && !_globalCache.isDestroyed())) {
qAddPostRoutine(cleanGlobalCache);
}
return _globalCache();
}
QString DIconTheme::findDciIconFile(const QString &iconName, const QString &themeName)
{
if (iconName.isEmpty())
return nullptr;
const QString cleanIconName = QDir::cleanPath(iconName);
if (iconName.startsWith('/') || iconName.endsWith('/')
|| cleanIconName.length() != iconName.length()
|| cleanIconName.startsWith("../")) // Wrongful
return nullptr;
const int splitCharPos = iconName.lastIndexOf('/');
QString effectiveIconName = iconName;
// If the icon name syntax is "AAA/BBB", then the "AAA" is the icon group name, the "BBB" is the actual icon
// name. eg: "org.deepin.app/accounts", hypothesis the icon search path is "/usr/share/dsg/icons" and
// icon theme name is "theme_name", then the icon files list for find is:
// "/usr/share/dsg/icons/theme_name/org.deepin.app/accounts.dci"
// "qrc:/dsg/icons/theme_name/org.deepin.app/accounts.dci" // fallback to the qrc directory
// "/usr/share/dsg/icons/theme_name/accounts.dci" // ignore the icon group name
// "qrc:/dsg/icons/theme_name/accounts.dci" // ignore the icon group name
// "/usr/share/dsg/icons/accounts.dci" // ignore the icon theme
// "qrc:/dsg/icons/accounts.dci" // ignore the icon theme
// "qrc:/dsg/built-in-icons/accounts.dci" // fallback to built-in icons
const auto &searchPaths = DIconTheme::dciThemeSearchPaths();
for (const QString &themePath : searchPaths) {
QString iconPath = findDciIconFromPath(effectiveIconName, themeName, themePath);
if (!iconPath.isEmpty())
return iconPath;
}
if (splitCharPos > 0) {
effectiveIconName = iconName.mid(splitCharPos + 1);
Q_ASSERT(!effectiveIconName.isEmpty());
for (const QString &themePath : searchPaths) {
QString iconPath = findDciIconFromPath(effectiveIconName, themeName, themePath);
if (!iconPath.isEmpty())
return iconPath;
}
}
// fallback to without theme directory
for (const QString &themePath : searchPaths) {
QString iconPath = findDciIconFromPath(effectiveIconName, nullptr, themePath);
if (!iconPath.isEmpty())
return iconPath;
}
return findDciIconFromPath(effectiveIconName, nullptr, applicationBuiltInIconPath());
}
QStringList DIconTheme::dciThemeSearchPaths()
{
return *_dciThemePath;
}
void DIconTheme::setDciThemeSearchPaths(const QStringList &path)
{
*_dciThemePath = path;
}
DGUI_END_NAMESPACE