Debian Codegraph @

src/libs/widgets/KoResourcePaths.cpp

raw
1 /*
2 * SPDX-FileCopyrightText: 2015 Boudewijn Rempt <[email protected]>
3 * SPDX-FileCopyrightText: 2015 Friedrich W. H. Kossebau <[email protected]>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-or-later
6 */
7
8 // clazy:excludeall=qstring-arg
9 #include "KoResourcePaths.h"
10
11 #include <QGlobalStatic>
12 #include <QStringList>
13 #include <QHash>
14 #include <QStandardPaths>
15 #include <QDir>
16 #include <QFileInfo>
17 #include <QDebug>
18 #include <QSet>
19
20
21 #ifdef Q_OS_WIN
22 static const Qt::CaseSensitivity cs = Qt::CaseInsensitive;
23 #else
24 static const Qt::CaseSensitivity cs = Qt::CaseSensitive;
25 #endif
26
27 class KoResourcePathsImpl
28 {
29 public:
30 static QStandardPaths::StandardLocation mapTypeToQStandardPaths(const QString &type)
31 {
32 return
33 type == QStringLiteral("data") ? QStandardPaths::GenericDataLocation :
34 type == QStringLiteral("config") ? QStandardPaths::GenericConfigLocation :
35 type == QStringLiteral("cache") ? QStandardPaths::CacheLocation :
36 type == QStringLiteral("tmp") ? QStandardPaths::TempLocation :
37 type == QStringLiteral("appdata") ? QStandardPaths::AppLocalDataLocation :
38 type == QStringLiteral("locale") ? QStandardPaths::GenericDataLocation :
39 /* default */ QStandardPaths::GenericDataLocation;
40 }
41
42 KoResourcePathsImpl();
43 ~KoResourcePathsImpl();
44
45 void addResourceTypeInternal(const QString &type, const QString &basetype,
46 const QString &relativeName, bool priority);
47
48 void addResourceDirInternal(const QString &type, const QString &absdir, bool priority);
49
50 QString findResourceInternal(const QString &type, const QString &fileName);
51
52 QStringList findDirsInternal(const QString &type, const QString &relDir);
53
54 QStringList findAllResourcesInternal(const QString &type,
55 const QString &filter = QString(),
56 KoResourcePaths::SearchOptions options = KoResourcePaths::NoSearchOptions) const;
57
58 QStringList resourceDirsInternal(const QString &type);
59
60 QString saveLocationInternal(const QString &type, const QString &suffix = QString(), bool create = true);
61
62 QString locateLocalInternal(const QString &type, const QString &filename, bool createDir = false);
63
64 private:
65 QHash<QString, QStringList> m_absolutes; // For each resource type, the list of absolute paths, from most local (most priority) to most global
66 QHash<QString, QStringList> m_relatives; // Same with relative paths
67 };
68
69 KoResourcePathsImpl::KoResourcePathsImpl()
70 {
71 }
72
73 KoResourcePathsImpl::~KoResourcePathsImpl()
74 {
75 }
76
77
78 void KoResourcePathsImpl::addResourceTypeInternal(const QString &type, const QString &basetype,
79 const QString &relativename,
80 bool priority)
81 {
82 if (relativename.isEmpty()) return;
83
84 QString copy = relativename;
85
86 Q_ASSERT(basetype == QStringLiteral("data"));
87
88 if (!copy.endsWith(QLatin1Char(QLatin1Char('/')))) {
89 copy += QLatin1Char(QLatin1Char('/'));
90 }
91
92 QStringList &rels = m_relatives[type]; // find or insert
93
94 if (!rels.contains(copy, cs)) {
95 if (priority) {
96 rels.prepend(copy);
97 } else {
98 rels.append(copy);
99 }
100 }
101
102 //qDebug() << "addResourceType: type" << type << "basetype" << basetype << "relativename" << relativename << "priority" << priority << m_relatives[type];
103 }
104
105 void KoResourcePathsImpl::addResourceDirInternal(const QString &type, const QString &absdir, bool priority)
106 {
107 if (absdir.isEmpty() || type.isEmpty()) return;
108
109 // find or insert entry in the map
110 QString copy = absdir;
111 if (!copy.endsWith(QLatin1Char(QLatin1Char('/')))) {
112 copy += QLatin1Char(QLatin1Char('/'));
113 }
114
115 QStringList &paths = m_absolutes[type];
116 if (!paths.contains(copy, cs)) {
117 if (priority) {
118 paths.prepend(copy);
119 } else {
120 paths.append(copy);
121 }
122 }
123
124 //qDebug() << "addResourceDir: type" << type << "absdir" << absdir << "priority" << priority << m_absolutes[type];
125 }
126
127 QString KoResourcePathsImpl::findResourceInternal(const QString &type, const QString &fileName)
128 {
129 const QStandardPaths::StandardLocation location = mapTypeToQStandardPaths(type);
130 QString resource = QStandardPaths::locate(location, fileName, QStandardPaths::LocateFile);
131 if (resource.isEmpty()) {
132 const QStringList relatives = m_relatives.value(type);
133 for (const QString &relative : relatives) {
134 resource = QStandardPaths::locate(location, relative + fileName, QStandardPaths::LocateFile);
135 if (!resource.isEmpty()) {
136 break;
137 }
138 }
139 }
140 if (resource.isEmpty()) {
141 const QStringList absolutes = m_absolutes.value(type);
142 for (const QString &absolute : absolutes) {
143 const QString filePath = absolute + fileName;
144 if (QFileInfo::exists(filePath)) {
145 resource = filePath;
146 break;
147 }
148 }
149 }
150 //Q_ASSERT(!resource.isEmpty());
151 //qDebug() << "findResource: type" << type << "filename" << fileName << "resource" << resource;
152 return resource;
153 }
154
155 QStringList KoResourcePathsImpl::findDirsInternal(const QString &type, const QString &relDir)
156 {
157 const QStandardPaths::StandardLocation location = mapTypeToQStandardPaths(type);
158
159 QStringList dirs = QStandardPaths::locateAll(location, relDir, QStandardPaths::LocateDirectory);
160 const QStringList relatives = m_relatives.value(type);
161 for (const QString &relative : relatives) {
162 dirs << QStandardPaths::locateAll(location, relative + relDir, QStandardPaths::LocateDirectory);
163 }
164 const QStringList absolutes = m_absolutes.value(type);
165 for (const QString &absolute : absolutes) {
166 const QString dirPath = absolute + relDir;
167 if (QDir(dirPath).exists()) {
168 dirs << dirPath;
169 }
170 }
171
172 //Q_ASSERT(!dirs.isEmpty());
173 //qDebug() << "findDirs: type" << type << "relDir" << relDir<< "resource" << dirs;
174 return dirs;
175 }
176
177
178 QStringList filesInDir(const QString &startdir, const QString & filter, bool noduplicates, bool recursive)
179 {
180 //qDebug() << "filesInDir: startdir" << startdir << "filter" << filter << "noduplicates" << noduplicates << "recursive" << recursive;
181 QStringList result;
182
183 // First the entries in this path
184 QStringList nameFilters;
185 nameFilters << filter;
186 const QStringList fileNames = QDir(startdir).entryList(nameFilters, QDir::Files | QDir::CaseSensitive, QDir::Name);
187 //qDebug() << "\tFound:" << fileNames.size() << ":" << fileNames;
188 for (const QString &fileName : fileNames) {
189 QString file = startdir + QLatin1Char('/') + fileName;
190 result << file;
191 }
192
193 // And then everything underneath, if recursive is specified
194 if (recursive) {
195 const QStringList entries = QDir(startdir).entryList(QDir::Dirs | QDir::NoDotAndDotDot);
196 for (const QString &subdir : entries) {
197 //qDebug() << "\tGoing to look in subdir" << subdir << "of" << startdir;
198 result << filesInDir(startdir + QLatin1Char('/') + subdir, filter, noduplicates, recursive);
199 }
200 }
201 return result;
202 }
203
204 QStringList KoResourcePathsImpl::findAllResourcesInternal(const QString &type,
205 const QString &_filter,
206 KoResourcePaths::SearchOptions options) const
207 {
208 //qDebug() << "=====================================================";
209
210 bool noDuplicates = options & KoResourcePaths::NoDuplicates;
211 bool recursive = options & KoResourcePaths::Recursive;
212
213 //qDebug() << "findAllResources: type" << type << "filter" << _filter << "no dups" << noDuplicates << "recursive" << recursive;
214
215 const QStandardPaths::StandardLocation location = mapTypeToQStandardPaths(type);
216
217 const QStringList relatives = m_relatives.value(type);
218 QString filter = _filter;
219 QString prefix;
220
221 // In cases where the filter is like "color-schemes/*.colors" instead of "*.kpp", used with unregistgered resource types
222 if (filter.indexOf(QLatin1Char('*')) > 0) {
223 prefix = filter.split(QLatin1Char('*')).first();
224 filter = QLatin1Char('*') + filter.split(QLatin1Char('*'))[1];
225 //qDebug() << "Split up alias" << relatives << "filter" << filter;
226 }
227
228 QStringList resources;
229 if (relatives.isEmpty()) {
230 resources << QStandardPaths::locateAll(location, prefix + filter, QStandardPaths::LocateFile);
231 }
232
233 ////qDebug() << "\tresources from qstandardpaths:" << resources.size();
234
235 for (const QString &relative : relatives) {
236 //qDebug() << "\t\relative:" << relative;
237 const QStringList dirs = QStandardPaths::locateAll(location, relative + prefix, QStandardPaths::LocateDirectory);
238 const QSet<QString> s = QSet<QString>(dirs.begin(), dirs.end());
239
240 //qDebug() << "\t\tdirs:" << dirs;
241 for (const QString &dir : s) {
242 resources << filesInDir(dir, filter, noDuplicates, recursive);
243 }
244 }
245 const QStringList absolutes = m_absolutes.value(type);
246 for (const QString &absolute : absolutes) {
247 const QString dir = absolute + prefix;
248 if (QDir(dir).exists()) {
249 resources << filesInDir(dir, filter, noDuplicates, recursive);
250 }
251 }
252
253 if (noDuplicates) {
254 QSet<QString> s = QSet<QString>(resources.begin(), resources.end());
255 resources = s.values();
256 }
257
258 //qDebug() << "\tresources also from aliases:" << resources.size();
259 //qDebug() << "=====================================================";
260
261 //Q_ASSERT(!resources.isEmpty());
262
263 return resources;
264 }
265
266 QStringList KoResourcePathsImpl::resourceDirsInternal(const QString &type)
267 {
268 //return KGlobal::dirs()->resourceDirs(type.toLatin1());
269 QStringList resourceDirs;
270
271 const QStandardPaths::StandardLocation location = mapTypeToQStandardPaths(type);
272 const QStringList relatives = m_relatives.value(type);
273 for (const QString &relative : relatives) {
274 resourceDirs << QStandardPaths::locateAll(location, relative, QStandardPaths::LocateDirectory);
275 }
276 const QStringList absolutes = m_absolutes.value(type);
277 for (const QString &absolute : absolutes) {
278 if (QDir(absolute).exists()) {
279 resourceDirs << absolute;
280 }
281 }
282 //qDebug() << "resourceDirs: type" << type << resourceDirs;
283
284 return resourceDirs;
285 }
286
287 QString KoResourcePathsImpl::saveLocationInternal(const QString &type, const QString &suffix, bool create)
288 {
289 QString path = QStandardPaths::writableLocation(mapTypeToQStandardPaths(type)) + QLatin1Char('/') + suffix;
290 QDir d(path);
291 if (!d.exists() && create) {
292 d.mkpath(path);
293 }
294 //qDebug() << "saveLocation: type" << type << "suffix" << suffix << "create" << create << "path" << path;
295
296 return path;
297 }
298
299 QString KoResourcePathsImpl::locateLocalInternal(const QString &type, const QString &filename, bool createDir)
300 {
301 QString path = saveLocationInternal(type, QString(), createDir);
302 //qDebug() << "locateLocal: type" << type << "filename" << filename << "CreateDir" << createDir << "path" << path;
303 return path + QLatin1Char('/') + filename;
304 }
305
306 Q_GLOBAL_STATIC(KoResourcePathsImpl, s_instance)
307
308
309 void KoResourcePaths::addResourceType(const char *type, const char *basetype,
310 const QString &relativeName, bool priority)
311 {
312 s_instance->addResourceTypeInternal(QString::fromLatin1(type), QString::fromLatin1(basetype), relativeName, priority);
313 }
314
315 void KoResourcePaths::addResourceDir(const char *type, const QString &dir, bool priority)
316 {
317 s_instance->addResourceDirInternal(QString::fromLatin1(type), dir, priority);
318 }
319
320 QString KoResourcePaths::findResource(const char *type, const QString &fileName)
321 {
322 return s_instance->findResourceInternal(QString::fromLatin1(type), fileName);
323 }
324
325 QStringList KoResourcePaths::findDirs(const char *type, const QString &reldir)
326 {
327 return s_instance->findDirsInternal(QString::fromLatin1(type), reldir);
328 }
329
330 QStringList KoResourcePaths::findAllResources(const char *type,
331 const QString &filter,
332 SearchOptions options)
333 {
334 return s_instance->findAllResourcesInternal(QString::fromLatin1(type), filter, options);
335 }
336
337 QStringList KoResourcePaths::resourceDirs(const char *type)
338 {
339 return s_instance->resourceDirsInternal(QString::fromLatin1(type));
340 }
341
342 QString KoResourcePaths::saveLocation(const char *type, const QString &suffix, bool create)
343 {
344 return s_instance->saveLocationInternal(QString::fromLatin1(type), suffix, create);
345 }
346
347 QString KoResourcePaths::locate(const char *type, const QString &filename)
348 {
349 return s_instance->findResourceInternal(QString::fromLatin1(type), filename);
350 }
351
352 QString KoResourcePaths::locateLocal(const char *type, const QString &filename, bool createDir)
353 {
354 return s_instance->locateLocalInternal(QString::fromLatin1(type), filename, createDir);
355 }