| 1 |
// SPDX-FileCopyrightText: 2022-2023 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include "dicontheme.h" |
| 6 |
#include "private/dbuiltiniconengine_p.h" |
| 7 |
#include "private/dciiconengine_p.h" |
| 8 |
#include "private/diconproxyengine_p.h" |
| 9 |
#ifndef DTK_DISABLE_LIBXDG |
| 10 |
#include "private/xdgiconproxyengine_p.h" |
| 11 |
#else |
| 12 |
#include <qpa/qplatformtheme.h> |
| 13 |
#include <private/qguiapplication_p.h> |
| 14 |
#include <private/qiconloader_p.h> |
| 15 |
#endif |
| 16 |
|
| 17 |
#include <DStandardPaths> |
| 18 |
|
| 19 |
#include <QCache> |
| 20 |
#include <QSet> |
| 21 |
#include <QGuiApplication> |
| 22 |
#include <QFileInfo> |
| 23 |
#include <QDir> |
| 24 |
|
| 25 |
DGUI_BEGIN_NAMESPACE |
| 26 |
DCORE_USE_NAMESPACE |
| 27 |
|
| 28 |
static inline QString joinPath(const QString &basePath, const QString &path) |
| 29 |
{ |
| 30 |
if (path.isEmpty()) |
| 31 |
return basePath; |
| 32 |
|
| 33 |
return basePath + QDir::separator() + path; |
| 34 |
} |
| 35 |
|
| 36 |
static inline QStringList systemDciThemePaths() |
| 37 |
{ |
| 38 |
QStringList paths; |
| 39 |
const auto dataPaths = DStandardPaths::paths(DStandardPaths::DSG::DataDir); |
| 40 |
paths.reserve(dataPaths.size()); |
| 41 |
|
| 42 |
for (const auto &dataPath : dataPaths) { |
| 43 |
paths.push_back(joinPath(dataPath, QLatin1String("icons"))); |
| 44 |
} |
| 45 |
|
| 46 |
return paths; |
| 47 |
} |
| 48 |
|
| 49 |
static inline QString applicationDciThemePath() |
| 50 |
{ |
| 51 |
return QLatin1String(":/dsg/icons"); |
| 52 |
} |
| 53 |
|
| 54 |
static inline QString applicationBuiltInIconPath() |
| 55 |
{ |
| 56 |
return QLatin1String(":/dsg/built-in-icons"); |
| 57 |
} |
| 58 |
|
| 59 |
static QString findDciIconFromPath(const QString &iconName, const QString &themeName, const QString path) |
| 60 |
{ |
| 61 |
if (path.isEmpty() || iconName.isEmpty()) |
| 62 |
return nullptr; |
| 63 |
|
| 64 |
QString themePath = joinPath(path, themeName); |
| 65 |
QFileInfo themeInfo(themePath); |
| 66 |
if (!themeInfo.exists() || !themeInfo.isDir()) |
| 67 |
return nullptr; |
| 68 |
|
| 69 |
/* |
| 70 |
* iconName has two types, like as: |
| 71 |
* 1. example.dci |
| 72 |
* 2. org.deepin.app/example.dci |
| 73 |
* |
| 74 |
* We do not need to consider the icon name, |
| 75 |
* just whether the file exists. |
| 76 |
*/ |
| 77 |
QString iconNameWithSuffix(iconName); |
| 78 |
// Focus that the icon name can not contain the dci suffix. |
| 79 |
iconNameWithSuffix += QLatin1String(".dci"); |
| 80 |
QString iconPath = joinPath(themePath, iconNameWithSuffix); |
| 81 |
if (!QDir::cleanPath(iconPath).startsWith(QDir::cleanPath(themePath))) // Wrongful |
| 82 |
return nullptr; |
| 83 |
|
| 84 |
QFileInfo iconInfo(iconPath); |
| 85 |
if (iconInfo.exists() && iconInfo.isFile()) |
| 86 |
return iconPath; |
| 87 |
|
| 88 |
return nullptr; |
| 89 |
} |
| 90 |
|
| 91 |
QIconEngine *DIconTheme::createIconEngine(const QString &iconName, Options options) |
| 92 |
{ |
| 93 |
return new DIconProxyEngine(iconName, options); |
| 94 |
} |
| 95 |
|
| 96 |
QIcon DIconTheme::findQIcon(const QString &iconName, Options options) |
| 97 |
{ |
| 98 |
if (QDir::isAbsolutePath(iconName)) { |
| 99 |
return QIcon(iconName); |
| 100 |
} |
| 101 |
auto engine = createIconEngine(iconName, options); |
| 102 |
// fallback to QIcon::fromTheme |
| 103 |
if (!options.testFlag(DontFallbackToQIconFromTheme) && !engine) |
| 104 |
return QIcon::fromTheme(iconName); |
| 105 |
|
| 106 |
return QIcon(engine); |
| 107 |
} |
| 108 |
|
| 109 |
QIcon DIconTheme::findQIcon(const QString &iconName, const QIcon &fallback, Options options) |
| 110 |
{ |
| 111 |
QIcon icon = findQIcon(iconName, options); |
| 112 |
return !icon.isNull() ? icon : fallback; |
| 113 |
} |
| 114 |
|
| 115 |
bool DIconTheme::isBuiltinIcon(const QIcon &icon) |
| 116 |
{ |
| 117 |
if (icon.isNull()) |
| 118 |
return false; |
| 119 |
QIconEngine *engine = const_cast<QIcon &>(icon).data_ptr()->engine; |
| 120 |
if (auto proxyEngine = dynamic_cast<DIconProxyEngine *>(engine)) |
| 121 |
return !proxyEngine->proxyKey().compare("DBuiltinIconEngine"); |
| 122 |
|
| 123 |
return dynamic_cast<DBuiltinIconEngine *>(engine); |
| 124 |
} |
| 125 |
|
| 126 |
bool DIconTheme::isXdgIcon(const QIcon &icon) |
| 127 |
{ |
| 128 |
#ifdef DTK_DISABLE_LIBXDG |
| 129 |
return false; |
| 130 |
#else |
| 131 |
if (icon.isNull()) |
| 132 |
return false; |
| 133 |
|
| 134 |
QIconEngine *engine = const_cast<QIcon &>(icon).data_ptr()->engine; |
| 135 |
if (auto proxyEngine = dynamic_cast<DIconProxyEngine *>(engine)) |
| 136 |
return !proxyEngine->proxyKey().compare("XdgIconProxyEngine"); |
| 137 |
|
| 138 |
return dynamic_cast<XdgIconProxyEngine *>(engine); |
| 139 |
#endif |
| 140 |
} |
| 141 |
|
| 142 |
class DIconTheme::CachedData |
| 143 |
{ |
| 144 |
public: |
| 145 |
QCache<QString, QIcon> cache; |
| 146 |
QCache<QString, QString> dciIconPathCache; |
| 147 |
}; |
| 148 |
|
| 149 |
DIconTheme::Cached::Cached() |
| 150 |
: data(new CachedData()) |
| 151 |
{ |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
DIconTheme::Cached::~Cached() |
| 156 |
{ |
| 157 |
delete data; |
| 158 |
} |
| 159 |
|
| 160 |
int DIconTheme::Cached::maxCost() const |
| 161 |
{ |
| 162 |
return data->cache.maxCost(); |
| 163 |
} |
| 164 |
|
| 165 |
void DIconTheme::Cached::setMaxCost(int cost) |
| 166 |
{ |
| 167 |
data->cache.setMaxCost(cost); |
| 168 |
data->dciIconPathCache.setMaxCost(cost); |
| 169 |
} |
| 170 |
|
| 171 |
void DIconTheme::Cached::clear() |
| 172 |
{ |
| 173 |
data->cache.clear(); |
| 174 |
data->dciIconPathCache.clear(); |
| 175 |
} |
| 176 |
|
| 177 |
QIcon DIconTheme::Cached::findQIcon(const QString &iconName, Options options, const QIcon &fallback) |
| 178 |
{ |
| 179 |
const QString themeName = QIcon::themeName(); |
| 180 |
const QString cacheKey = themeName + QChar('/') + iconName + QChar('/') + QString::number(static_cast<int>(options)); |
| 181 |
if (data->cache.contains(cacheKey)) { |
| 182 |
auto cacheIcon = data->cache.object(cacheKey); |
| 183 |
if (cacheIcon->isNull()) |
| 184 |
return fallback; |
| 185 |
return *cacheIcon; |
| 186 |
} |
| 187 |
|
| 188 |
auto newIcon = new QIcon(DIconTheme::findQIcon(iconName, options)); |
| 189 |
|
| 190 |
if (newIcon->isNull()) { |
| 191 |
return fallback; |
| 192 |
} else { |
| 193 |
data->cache.insert(cacheKey, newIcon); |
| 194 |
} |
| 195 |
|
| 196 |
return *newIcon; |
| 197 |
} |
| 198 |
|
| 199 |
QString DIconTheme::Cached::findDciIconFile(const QString &iconName, const QString &themeName, const QString &fallback) |
| 200 |
{ |
| 201 |
const QString cacheKey = themeName + QLatin1Char('/') + iconName; |
| 202 |
if (data->dciIconPathCache.contains(cacheKey)) { |
| 203 |
const QString *cachePath = data->dciIconPathCache.object(cacheKey); |
| 204 |
if (cachePath->isEmpty()) |
| 205 |
return fallback; |
| 206 |
return *cachePath; |
| 207 |
} |
| 208 |
|
| 209 |
QString *path = new QString(DIconTheme::findDciIconFile(iconName, themeName)); |
| 210 |
data->dciIconPathCache.insert(cacheKey, path); |
| 211 |
|
| 212 |
if (path->isEmpty()) |
| 213 |
return fallback; |
| 214 |
|
| 215 |
return *path; |
| 216 |
} |
| 217 |
|
| 218 |
static inline QStringList getDciThemePaths() { |
| 219 |
QStringList paths = systemDciThemePaths(); |
| 220 |
paths.push_back(applicationDciThemePath()); |
| 221 |
return paths; |
| 222 |
} |
| 223 |
|
| 224 |
Q_GLOBAL_STATIC(DIconTheme::Cached, _globalCache) |
| 225 |
Q_GLOBAL_STATIC_WITH_ARGS(QStringList, _dciThemePath, (getDciThemePaths())) |
| 226 |
|
| 227 |
static void cleanGlobalCache() { |
| 228 |
if (_globalCache.exists()) |
| 229 |
_globalCache->clear(); |
| 230 |
} |
| 231 |
|
| 232 |
DIconTheme::Cached *DIconTheme::cached() |
| 233 |
{ |
| 234 |
if (Q_UNLIKELY(!_globalCache.exists() && !_globalCache.isDestroyed())) { |
| 235 |
qAddPostRoutine(cleanGlobalCache); |
| 236 |
} |
| 237 |
return _globalCache(); |
| 238 |
} |
| 239 |
|
| 240 |
QString DIconTheme::findDciIconFile(const QString &iconName, const QString &themeName) |
| 241 |
{ |
| 242 |
if (iconName.isEmpty()) |
| 243 |
return nullptr; |
| 244 |
|
| 245 |
const QString cleanIconName = QDir::cleanPath(iconName); |
| 246 |
if (iconName.startsWith('/') || iconName.endsWith('/') |
| 247 |
|| cleanIconName.length() != iconName.length() |
| 248 |
|| cleanIconName.startsWith("../")) // Wrongful |
| 249 |
return nullptr; |
| 250 |
|
| 251 |
const int splitCharPos = iconName.lastIndexOf('/'); |
| 252 |
QString effectiveIconName = iconName; |
| 253 |
// If the icon name syntax is "AAA/BBB", then the "AAA" is the icon group name, the "BBB" is the actual icon |
| 254 |
// name. eg: "org.deepin.app/accounts", hypothesis the icon search path is "/usr/share/dsg/icons" and |
| 255 |
// icon theme name is "theme_name", then the icon files list for find is: |
| 256 |
// "/usr/share/dsg/icons/theme_name/org.deepin.app/accounts.dci" |
| 257 |
// "qrc:/dsg/icons/theme_name/org.deepin.app/accounts.dci" // fallback to the qrc directory |
| 258 |
|
| 259 |
// "/usr/share/dsg/icons/theme_name/accounts.dci" // ignore the icon group name |
| 260 |
// "qrc:/dsg/icons/theme_name/accounts.dci" // ignore the icon group name |
| 261 |
|
| 262 |
// "/usr/share/dsg/icons/accounts.dci" // ignore the icon theme |
| 263 |
// "qrc:/dsg/icons/accounts.dci" // ignore the icon theme |
| 264 |
|
| 265 |
// "qrc:/dsg/built-in-icons/accounts.dci" // fallback to built-in icons |
| 266 |
|
| 267 |
const auto &searchPaths = DIconTheme::dciThemeSearchPaths(); |
| 268 |
for (const QString &themePath : searchPaths) { |
| 269 |
QString iconPath = findDciIconFromPath(effectiveIconName, themeName, themePath); |
| 270 |
if (!iconPath.isEmpty()) |
| 271 |
return iconPath; |
| 272 |
} |
| 273 |
|
| 274 |
if (splitCharPos > 0) { |
| 275 |
effectiveIconName = iconName.mid(splitCharPos + 1); |
| 276 |
Q_ASSERT(!effectiveIconName.isEmpty()); |
| 277 |
for (const QString &themePath : searchPaths) { |
| 278 |
QString iconPath = findDciIconFromPath(effectiveIconName, themeName, themePath); |
| 279 |
if (!iconPath.isEmpty()) |
| 280 |
return iconPath; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
// fallback to without theme directory |
| 285 |
for (const QString &themePath : searchPaths) { |
| 286 |
QString iconPath = findDciIconFromPath(effectiveIconName, nullptr, themePath); |
| 287 |
if (!iconPath.isEmpty()) |
| 288 |
return iconPath; |
| 289 |
} |
| 290 |
|
| 291 |
return findDciIconFromPath(effectiveIconName, nullptr, applicationBuiltInIconPath()); |
| 292 |
} |
| 293 |
|
| 294 |
QStringList DIconTheme::dciThemeSearchPaths() |
| 295 |
{ |
| 296 |
return *_dciThemePath; |
| 297 |
} |
| 298 |
|
| 299 |
void DIconTheme::setDciThemeSearchPaths(const QStringList &path) |
| 300 |
{ |
| 301 |
*_dciThemePath = path; |
| 302 |
} |
| 303 |
|
| 304 |
DGUI_END_NAMESPACE |