File: KoResourcePaths.cpp

package info (click to toggle)
calligra 1%3A3.2.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 260,432 kB
  • sloc: cpp: 650,911; xml: 27,662; python: 6,044; perl: 2,724; yacc: 1,817; ansic: 1,325; sh: 1,277; lex: 1,107; ruby: 1,010; javascript: 495; makefile: 24
file content (354 lines) | stat: -rw-r--r-- 13,285 bytes parent folder | download | duplicates (2)
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * Copyright (c) 2015 Boudewijn Rempt <boud@valdyas.org>
 * Copyright (c) 2015 Friedrich W. H. Kossebau <kossebau@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "KoResourcePaths.h"

#include <QGlobalStatic>
#include <QStringList>
#include <QHash>
#include <QStandardPaths>
#include <QDir>
#include <QFileInfo>
#include <QDebug>
#include <QSet>


#ifdef Q_OS_WIN
static const Qt::CaseSensitivity cs = Qt::CaseInsensitive;
#else
static const Qt::CaseSensitivity cs = Qt::CaseSensitive;
#endif

class KoResourcePathsImpl
{
public:
    static QStandardPaths::StandardLocation mapTypeToQStandardPaths(const QString &type)
    {
        return
            type == QLatin1String("data") ?    QStandardPaths::GenericDataLocation :
            type == QLatin1String("config") ?  QStandardPaths::GenericConfigLocation :
            type == QLatin1String("cache") ?   QStandardPaths::CacheLocation :
            type == QLatin1String("tmp") ?     QStandardPaths::TempLocation :
            type == QLatin1String("appdata") ? QStandardPaths::DataLocation :
            type == QLatin1String("locale") ?  QStandardPaths::GenericDataLocation :
            /* default */                      QStandardPaths::GenericDataLocation;
    }

    KoResourcePathsImpl() = default;
    ~KoResourcePathsImpl() = default;

    void addResourceTypeInternal(const QString &type, const QString &basetype,
                                 const QString &relativeName, bool priority);

    void addResourceDirInternal(const QString &type, const QString &absdir, bool priority);

    QString findResourceInternal(const QString &type, const QString &fileName);

    QStringList findDirsInternal(const QString &type, const QString &relDir);

    QStringList findAllResourcesInternal(const QString &type,
                                         const QString &filter = QString(),
                                         KoResourcePaths::SearchOptions options = KoResourcePaths::NoSearchOptions) const;

    QStringList resourceDirsInternal(const QString &type);

    QString saveLocationInternal(const QString &type, const QString &suffix = QString(), bool create = true);

    QString locateLocalInternal(const QString &type, const QString &filename, bool createDir = false);

private:
    QHash<QString, QStringList> m_absolutes; // For each resource type, the list of absolute paths, from most local (most priority) to most global
    QHash<QString, QStringList> m_relatives; // Same with relative paths
};

void KoResourcePathsImpl::addResourceTypeInternal(const QString &type, const QString &basetype,
                                                  const QString &relativename,
                                                  bool priority)
{
    if (relativename.isEmpty()) return;

    QString copy = relativename;

    Q_ASSERT(basetype == "data");

    if (!copy.endsWith(QLatin1Char('/'))) {
        copy += QLatin1Char('/');
    }

    QStringList &rels = m_relatives[type]; // find or insert

    if (!rels.contains(copy, cs)) {
        if (priority) {
            rels.prepend(copy);
        } else {
            rels.append(copy);
        }
    }

    //qDebug() << "addResourceType: type" << type << "basetype" << basetype << "relativename" << relativename << "priority" << priority << m_relatives[type];
}

void KoResourcePathsImpl::addResourceDirInternal(const QString &type, const QString &absdir, bool priority)
{
    if (absdir.isEmpty() || type.isEmpty()) return;

    // find or insert entry in the map
    QString copy = absdir;
    if (!copy.endsWith(QLatin1Char('/'))) {
        copy += QLatin1Char('/');
    }

    QStringList &paths = m_absolutes[type];
    if (!paths.contains(copy, cs)) {
        if (priority) {
            paths.prepend(copy);
        } else {
            paths.append(copy);
        }
    }

    //qDebug() << "addResourceDir: type" << type << "absdir" << absdir << "priority" << priority << m_absolutes[type];
}

QString KoResourcePathsImpl::findResourceInternal(const QString &type, const QString &fileName)
{
    const QStandardPaths::StandardLocation location = mapTypeToQStandardPaths(type);
    QString resource = QStandardPaths::locate(location, fileName, QStandardPaths::LocateFile);
    if (resource.isEmpty()) {
        foreach(const QString &relative, m_relatives.value(type)) {
            resource = QStandardPaths::locate(location, relative + fileName, QStandardPaths::LocateFile);
            if (!resource.isEmpty()) {
                break;
            }
        }
    }
    if (resource.isEmpty()) {
        foreach(const QString &absolute, m_absolutes.value(type)) {
            const QString filePath = absolute + fileName;
            if (QFileInfo::exists(filePath)) {
                resource = filePath;
                break;
            }
        }
    }
    //Q_ASSERT(!resource.isEmpty());
    //qDebug() << "findResource: type" << type << "filename" << fileName << "resource" << resource;
    return resource;
}

QStringList KoResourcePathsImpl::findDirsInternal(const QString &type, const QString &relDir)
{
    const QStandardPaths::StandardLocation location = mapTypeToQStandardPaths(type);

    QStringList dirs = QStandardPaths::locateAll(location, relDir, QStandardPaths::LocateDirectory);

    foreach(const QString &relative, m_relatives.value(type)) {
        dirs << QStandardPaths::locateAll(location, relative + relDir, QStandardPaths::LocateDirectory);
    }

    foreach(const QString &absolute, m_absolutes.value(type)) {
        const QString dirPath = absolute + relDir;
        if (QDir(dirPath).exists()) {
            dirs << dirPath;
        }
    }

    //Q_ASSERT(!dirs.isEmpty());
    //qDebug() << "findDirs: type" << type << "relDir" << relDir<< "resource" << dirs;
    return dirs;
}


QStringList filesInDir(const QString &startdir, const QString & filter, bool noduplicates, bool recursive)
{
    //qDebug() << "filesInDir: startdir" << startdir << "filter" << filter << "noduplicates" << noduplicates << "recursive" << recursive;
    QStringList result;

    // First the entries in this path
    QStringList nameFilters;
    nameFilters << filter;
    const QStringList fileNames = QDir(startdir).entryList(nameFilters, QDir::Files | QDir::CaseSensitive, QDir::Name);
    //qDebug() << "\tFound:" << fileNames.size() << ":" << fileNames;
    Q_FOREACH (const QString &fileName, fileNames) {
        QString file = startdir + '/' + fileName;
        result << file;
    }

    // And then everything underneath, if recursive is specified
    if (recursive) {
        const QStringList entries = QDir(startdir).entryList(QDir::Dirs | QDir::NoDotAndDotDot);
        Q_FOREACH (const QString &subdir, entries) {
            //qDebug() << "\tGoing to look in subdir" << subdir << "of" << startdir;
            result << filesInDir(startdir + '/' + subdir, filter, noduplicates, recursive);
        }
    }
    return result;
}

QStringList KoResourcePathsImpl::findAllResourcesInternal(const QString &type,
                                                          const QString &_filter,
                                                          KoResourcePaths::SearchOptions options) const
{
    //qDebug() << "=====================================================";

    bool noDuplicates = options & KoResourcePaths::NoDuplicates;
    bool recursive = options & KoResourcePaths::Recursive;

    //qDebug() << "findAllResources: type" << type << "filter" << _filter << "no dups" << noDuplicates << "recursive" << recursive;

    const QStandardPaths::StandardLocation location = mapTypeToQStandardPaths(type);

    const QStringList relatives = m_relatives.value(type);
    QString filter = _filter;
    QString prefix;

    // In cases where the filter  is like "color-schemes/*.colors" instead of "*.kpp", used with unregistgered resource types
    if (filter.indexOf('*') > 0) {
        prefix = filter.split('*').first();
        filter = '*' + filter.split('*')[1];
        //qDebug() << "Split up alias" << relatives << "filter" << filter;
    }

    QStringList resources;
    if (relatives.isEmpty()) {
        resources << QStandardPaths::locateAll(location, prefix + filter, QStandardPaths::LocateFile);
    }

    ////qDebug() << "\tresources from qstandardpaths:" << resources.size();


    foreach(const QString &relative, relatives) {
        //qDebug() << "\t\relative:" << relative;
        const QStringList dirs = QStandardPaths::locateAll(location, relative + prefix, QStandardPaths::LocateDirectory);
        QSet<QString> s = QSet<QString>::fromList(dirs);

        //qDebug() << "\t\tdirs:" << dirs;
        Q_FOREACH (const QString &dir, s) {
            resources << filesInDir(dir, filter, noDuplicates, recursive);
        }
    }

    foreach(const QString &absolute, m_absolutes.value(type)) {
        const QString dir = absolute + prefix;
        if (QDir(dir).exists()) {
            resources << filesInDir(dir, filter, noDuplicates, recursive);
        }
    }

    if (noDuplicates) {
        QSet<QString> s = QSet<QString>::fromList(resources);
        resources = s.toList();
    }

    //qDebug() << "\tresources also from aliases:" << resources.size();
    //qDebug() << "=====================================================";

    //Q_ASSERT(!resources.isEmpty());

    return resources;
}

QStringList KoResourcePathsImpl::resourceDirsInternal(const QString &type)
{
    //return KGlobal::dirs()->resourceDirs(type.toLatin1());
    QStringList resourceDirs;

    const QStandardPaths::StandardLocation location = mapTypeToQStandardPaths(type);
    foreach(const QString &relative, m_relatives.value(type)) {
        resourceDirs << QStandardPaths::locateAll(location, relative, QStandardPaths::LocateDirectory);
    }
    foreach(const QString &absolute, m_absolutes.value(type)) {
        if (QDir(absolute).exists()) {
            resourceDirs << absolute;
        }
    }
    //qDebug() << "resourceDirs: type" << type << resourceDirs;

    return resourceDirs;
}

QString KoResourcePathsImpl::saveLocationInternal(const QString &type, const QString &suffix, bool create)
{
    QString path = QStandardPaths::writableLocation(mapTypeToQStandardPaths(type)) + '/' + suffix;
    QDir d(path);
    if (!d.exists() && create) {
        d.mkpath(path);
    }
    //qDebug() << "saveLocation: type" << type << "suffix" << suffix << "create" << create << "path" << path;

    return path;
}

QString KoResourcePathsImpl::locateLocalInternal(const QString &type, const QString &filename, bool createDir)
{
    QString path = saveLocationInternal(type, "", createDir);
    //qDebug() << "locateLocal: type" << type << "filename" << filename << "CreateDir" << createDir << "path" << path;
    return path + '/' + filename;
}

Q_GLOBAL_STATIC(KoResourcePathsImpl, s_instance);


void KoResourcePaths::addResourceType(const char *type, const char *basetype,
                                      const QString &relativeName, bool priority)
{
    s_instance->addResourceTypeInternal(QString::fromLatin1(type), QString::fromLatin1(basetype), relativeName, priority);
}

void KoResourcePaths::addResourceDir(const char *type, const QString &dir, bool priority)
{
    s_instance->addResourceDirInternal(QString::fromLatin1(type), dir, priority);
}

QString KoResourcePaths::findResource(const char *type, const QString &fileName)
{
    return s_instance->findResourceInternal(QString::fromLatin1(type), fileName);
}

QStringList KoResourcePaths::findDirs(const char *type, const QString &reldir)
{
    return s_instance->findDirsInternal(QString::fromLatin1(type), reldir);
}

QStringList KoResourcePaths::findAllResources(const char *type,
                                              const QString &filter,
                                              SearchOptions options)
{
    return s_instance->findAllResourcesInternal(QString::fromLatin1(type), filter, options);
}

QStringList KoResourcePaths::resourceDirs(const char *type)
{
    return s_instance->resourceDirsInternal(QString::fromLatin1(type));
}

QString KoResourcePaths::saveLocation(const char *type, const QString &suffix, bool create)
{
    return s_instance->saveLocationInternal(QString::fromLatin1(type), suffix, create);
}

QString KoResourcePaths::locate(const char *type, const QString &filename)
{
    return s_instance->findResourceInternal(QString::fromLatin1(type), filename);
}

QString KoResourcePaths::locateLocal(const char *type, const QString &filename, bool createDir)
{
    return s_instance->locateLocalInternal(QString::fromLatin1(type), filename, createDir);
}