Krita Source Code Documentation
Loading...
Searching...
No Matches
KoResourcePaths Class Reference

#include <KoResourcePaths.h>

+ Inheritance diagram for KoResourcePaths:

Public Types

enum  SearchOption { NoSearchOptions = 0 , Recursive = 1 , IgnoreExecBit = 4 }
 

Public Member Functions

QStringList aliases (const QString &type)
 
 KoResourcePaths ()
 
QStandardPaths::StandardLocation mapTypeToQStandardPaths (const QString &type)
 
virtual ~KoResourcePaths ()
 

Static Public Member Functions

static void addAssetDir (const QString &type, const QString &dir, bool priority=true)
 
static void addAssetType (const QString &type, const char *basetype, const QString &relativeName, bool priority=true)
 
static QStringList assetDirs (const QString &type)
 
static QStringList findAllAssets (const QString &type, const QString &filter=QString(), SearchOptions options=NoSearchOptions)
 
static QString findAsset (const QString &type, const QString &fileName)
 
static QStringList findDirs (const QString &type)
 
static void getAllUserResourceFoldersLocationsForWindowsStore (QString &standardLocation, QString &privateLocation)
 getAllAppDataLocationsForWindowsStore Use this to get both private and general appdata folders which also considers user's choice of custom resource folder Used in GeneralTab in kis_dlg_preferences, and KisViewManager::openResourceDirectory().
 
static QString getAppDataLocation ()
 
static QString getApplicationRoot ()
 
static QString locate (const QString &type, const QString &filename)
 
static QString locateLocal (const QString &type, const QString &filename, bool createDir=false)
 
static QString saveLocation (const QString &type, const QString &suffix=QString(), bool create=true)
 

Public Attributes

QMap< QString, QStringListabsolutes
 
QMutex absolutesMutex
 
QMap< QString, QStringListrelatives
 
QMutex relativesMutex
 

Static Public Attributes

static QString s_overrideAppDataLocation
 getAppDataLocation Use this instead of QStandardPaths::AppDataLocation! The user can configure the location where resources and other user writable items are stored now.
 

Private Member Functions

void addResourceDirInternal (const QString &type, const QString &absdir, bool priority)
 
void addResourceTypeInternal (const QString &type, const QString &basetype, const QString &relativeName, bool priority)
 
QStringList findAllResourcesInternal (const QString &type, const QString &filter=QString(), SearchOptions options=NoSearchOptions) const
 
QStringList findDirsInternal (const QString &type)
 
QStringList findExtraResourceDirs () const
 
QString findResourceInternal (const QString &type, const QString &fileName)
 
QString locateInternal (const QString &type, const QString &filename)
 
QString locateLocalInternal (const QString &type, const QString &filename, bool createDir=false)
 
QStringList resourceDirsInternal (const QString &type)
 
QString saveLocationInternal (const QString &type, const QString &suffix=QString(), bool create=true)
 
- Private Member Functions inherited from Private
 Private (KisCanvas2 *c)
 

Private Attributes

QScopedPointer< Privated
 
- Private Attributes inherited from Private
KisCanvas2canvas
 
int displayedFrame
 
int intendedFrame
 

Detailed Description

The usual place to look for assets is Qt's AppDataLocation. This corresponds to XDG_DATA_DIRS on Linux. To ensure your installation and path are configured correctly, ensure your files are located in the directories contained in this variable:

QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);

This can be overridden in Krita's configuration.

Unfortunately, we are mixing up two things in the appdatalocation:

  • resources: brushes, presets and so on
  • assets: color themes, icc profiles and other weird stuff

There are many debug lines that can be uncommented for more specific installation checks. In the future these should be converted to qloggingcategory to enable convenient enable/disable functionality.

Note: DO NOT USE THIS CLASS WHEN LOCATING RESOURCES LIKE BRUSHES OR GRADIENTS. Use KisResourceLocator instead.

Definition at line 157 of file KoResourcePaths.cpp.

Member Enumeration Documentation

◆ SearchOption

Enumerator
NoSearchOptions 
Recursive 
IgnoreExecBit 

Definition at line 45 of file KoResourcePaths.h.

Constructor & Destructor Documentation

◆ KoResourcePaths()

KoResourcePaths::KoResourcePaths ( )

Definition at line 206 of file KoResourcePaths.cpp.

207 : d(new Private)
208{
209}
QScopedPointer< Private > d

◆ ~KoResourcePaths()

KoResourcePaths::~KoResourcePaths ( )
virtual

Definition at line 211 of file KoResourcePaths.cpp.

212{
213}

Member Function Documentation

◆ addAssetDir()

void KoResourcePaths::addAssetDir ( const QString & type,
const QString & dir,
bool priority = true )
static

Adds absolute path at the beginning of the search path for particular types (for example in case of icons where the user specifies extra paths).

You shouldn't need this function in 99% of all cases besides adding user-given paths.

Parameters
typeSpecifies a short descriptive string to access files of this type.
absdirPoints to directory where to look for this specific type. Nonexistent directories may be saved but pruned.
priorityif true, the directory is added before any other, otherwise after

Definition at line 345 of file KoResourcePaths.cpp.

346{
347 s_instance->addResourceDirInternal(type, dir, priority);
348}

◆ addAssetType()

void KoResourcePaths::addAssetType ( const QString & type,
const char * basetype,
const QString & relativeName,
bool priority = true )
static

Adds suffixes for asset types.

You may add as many as you need, but it is advised that there is exactly one to make writing definite.

The later a suffix is added, the higher its priority. Note, that the suffix should end with / but doesn't have to start with one (as prefixes should end with one). So adding a suffix for app_pics would look like KoStandardPaths::addResourceType("app_pics", "data", "app/pics");

Parameters
typeSpecifies a short descriptive string to access files of this type.
basetypeSpecifies an already known type, or 0 if none
relativenameSpecifies a directory relative to the basetype
priorityif true, the directory is added before any other, otherwise after

Definition at line 339 of file KoResourcePaths.cpp.

341{
342 s_instance->addResourceTypeInternal(type, QString::fromLatin1(basetype), relativeName, priority);
343}

◆ addResourceDirInternal()

void KoResourcePaths::addResourceDirInternal ( const QString & type,
const QString & absdir,
bool priority )
private

Definition at line 417 of file KoResourcePaths.cpp.

418{
419 if (absdir.isEmpty() || type.isEmpty()) return;
420
421 // find or insert entry in the map
422 QString copy = absdir;
423 if (copy.at(copy.length() - 1) != QLatin1Char('/')) {
424 copy += QLatin1Char('/');
425 }
426
427 d->absolutesMutex.lock();
428 QStringList &paths = d->absolutes[type];
429 if (!paths.contains(copy, cs)) {
430 if (priority) {
431 paths.prepend(copy);
432 } else {
433 paths.append(copy);
434 }
435 }
436 d->absolutesMutex.unlock();
437
438 dbgResources << "addResourceDir: type" << type << "absdir" << absdir << "priority" << priority << d->absolutes[type];
439}
#define dbgResources
Definition kis_debug.h:43

References d, and dbgResources.

◆ addResourceTypeInternal()

void KoResourcePaths::addResourceTypeInternal ( const QString & type,
const QString & basetype,
const QString & relativeName,
bool priority )
private

Definition at line 387 of file KoResourcePaths.cpp.

390{
391 Q_UNUSED(basetype);
392 if (relativename.isEmpty()) return;
393
394 QString copy = relativename;
395
396 Q_ASSERT(basetype == "data");
397
398 if (!copy.endsWith(QLatin1Char('/'))) {
399 copy += QLatin1Char('/');
400 }
401
402 d->relativesMutex.lock();
403 QStringList &rels = d->relatives[type]; // find or insert
404
405 if (!rels.contains(copy, cs)) {
406 if (priority) {
407 rels.prepend(copy);
408 } else {
409 rels.append(copy);
410 }
411 }
412 d->relativesMutex.unlock();
413
414 dbgResources << "addResourceType: type" << type << "basetype" << basetype << "relativename" << relativename << "priority" << priority << d->relatives[type];
415}

References d, and dbgResources.

◆ aliases()

QStringList KoResourcePaths::aliases ( const QString & type)
inline

Definition at line 165 of file KoResourcePaths.cpp.

166 {
168 QStringList a;
169 relativesMutex.lock();
170 if (relatives.contains(type)) {
171 r += relatives[type];
172 }
173 relativesMutex.unlock();
174 absolutesMutex.lock();
175 if (absolutes.contains(type)) {
176 a += absolutes[type];
177 }
178 absolutesMutex.unlock();
179
180 return r + a;
181 }
QMap< QString, QStringList > absolutes
QMap< QString, QStringList > relatives

◆ assetDirs()

QStringList KoResourcePaths::assetDirs ( const QString & type)
static
Parameters
typeThe type of resource
Returns
The list of possible directories for the specified type. The function updates the cache if possible. If the resource type specified is unknown, it will return an empty list. Note, that the directories are assured to exist beside the save location, which may not exist, but is returned anyway.

Definition at line 367 of file KoResourcePaths.cpp.

368{
369 return cleanupDirs(s_instance->resourceDirsInternal(type));
370}

◆ findAllAssets()

QStringList KoResourcePaths::findAllAssets ( const QString & type,
const QString & filter = QString(),
SearchOptions options = NoSearchOptions )
static

Tries to find all resources with the specified type.

The function will look into all specified directories and return all filenames in these directories.

The "most local" files are returned before the "more global" files.

Parameters
typeThe type of resource to locate directories for.
filterOnly accept filenames that fit to filter. The filter may consist of an optional directory and a QRegExp wildcard expression. E.g. "images\*.jpg". Use QString() if you do not want a filter.
optionsif the flags passed include Recursive, subdirectories will also be search.
Returns
List of all the files whose filename matches the specified filter.

Definition at line 360 of file KoResourcePaths.cpp.

363{
364 return cleanup(s_instance->findAllResourcesInternal(type, filter, options));
365}

◆ findAllResourcesInternal()

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

Definition at line 573 of file KoResourcePaths.cpp.

576{
577 dbgResources << "=====================================================";
578 dbgResources << type << _filter << QStandardPaths::standardLocations(d->mapTypeToQStandardPaths(type));
579
580 bool recursive = options & KoResourcePaths::Recursive;
581
582 dbgResources << "findAllResources: type" << type << "filter" << _filter << "recursive" << recursive;
583
584 QStringList aliases = d->aliases(type);
585 QString filter = _filter;
586
587 // In cases where the filter is like "color-schemes/*.colors" instead of "*.kpp", used with unregistered resource types
588 if (filter.indexOf('*') > 0) {
589 aliases << filter.split('*').first();
590 filter = '*' + filter.split('*')[1];
591 dbgResources << "Split up alias" << aliases << "filter" << filter;
592 }
593
594 QStringList resources;
595 if (aliases.isEmpty()) {
596 QStringList standardResources =
597 QStandardPaths::locateAll(d->mapTypeToQStandardPaths(type),
598 filter, QStandardPaths::LocateFile);
599 dbgResources << "standardResources" << standardResources;
600 appendResources(&resources, standardResources, true);
601 dbgResources << "1" << resources;
602 }
603
604 QStringList extraResourceDirs = findExtraResourceDirs();
605
606 if (!extraResourceDirs.isEmpty()) {
607 Q_FOREACH(const QString &extraResourceDir, extraResourceDirs) {
608 if (aliases.isEmpty()) {
609 appendResources(&resources, filesInDir(extraResourceDir + '/' + type, filter, recursive), true);
610 }
611 else {
612 Q_FOREACH (const QString &alias, aliases) {
613 appendResources(&resources, filesInDir(extraResourceDir + '/' + alias + '/', filter, recursive), true);
614 }
615 }
616 }
617
618 }
619
620 dbgResources << "\tresources from qstandardpaths:" << resources.size();
621
622 Q_FOREACH (const QString &alias, aliases) {
623 dbgResources << "\t\talias:" << alias;
624 QStringList dirs;
625
626 QFileInfo dirInfo(alias);
627 if (dirInfo.exists() && dirInfo.isDir() && dirInfo.isAbsolute()) {
628 dirs << alias;
629 } else {
630 dirs << QStandardPaths::locateAll(d->mapTypeToQStandardPaths(type), alias, QStandardPaths::LocateDirectory)
631 << getInstallationPrefix() + "share/" + alias + "/"
632 << getInstallationPrefix() + "share/krita/" + alias + "/";
633 }
634
635 Q_FOREACH (const QString &dir, dirs) {
636 appendResources(&resources,
637 filesInDir(dir, filter, recursive),
638 true);
639 }
640 }
641
642 dbgResources << "\tresources also from aliases:" << resources.size();
643
644 // if the original filter is "input/*", we only want share/input/* and share/krita/input/* here, but not
645 // share/*. therefore, use _filter here instead of filter which was split into alias and "*".
646 QFileInfo fi(_filter);
647
648 QStringList prefixResources;
649 prefixResources << filesInDir(getInstallationPrefix() + "share/" + fi.path(), fi.fileName(), false);
650 prefixResources << filesInDir(getInstallationPrefix() + "share/krita/" + fi.path(), fi.fileName(), false);
651 appendResources(&resources, prefixResources, true);
652
653 dbgResources << "\tresources from installation:" << resources.size();
654 dbgResources << "=====================================================";
655
656 return resources;
657}
QStringList filesInDir(const QString &startdir, const QString &filter, bool recursive)
QStringList aliases(const QString &type)
QStringList findExtraResourceDirs() const

References aliases(), d, dbgResources, filesInDir(), findExtraResourceDirs(), and Recursive.

◆ findAsset()

QString KoResourcePaths::findAsset ( const QString & type,
const QString & fileName )
static

Tries to find a resource in the following order:

  • All PREFIX/<relativename> paths (most recent first).
  • All absolute paths (most recent first).

The filename should be a filename relative to the base dir for resources. So it's a way to get the path to libkdecore.la to findResource("lib", "libkdecore.la"). KStandardDirs will then look into the subdir lib of all elements of all prefixes ($KDEDIRS) for a file libkdecore.la and return the path to the first one it finds (e.g. /opt/kde/lib/libkdecore.la).

Example:

QString iconfilename = KStandardPaths::findResource("icon",QString("oxygen/22x22/apps/ktip.png"));
Parameters
typeThe type of the wanted resource
filenameA relative filename of the resource.
Returns
A full path to the filename specified in the second argument, or QString() if not found.

Definition at line 350 of file KoResourcePaths.cpp.

351{
352 return cleanup(s_instance->findResourceInternal(type, fileName));
353}

◆ findDirs()

QStringList KoResourcePaths::findDirs ( const QString & type)
static

Tries to find all directories whose names consist of the specified type and a relative path. So findDirs("xdgdata-apps", "Settings") would return

  • /home/joe/.local/share/applications/Settings/
  • /usr/share/applications/Settings/

(from the most local to the most global)

Note that it appends / to the end of the directories, so you can use this right away as directory names.

Parameters
typeThe type of the base directory.
reldirRelative directory.
Returns
A list of matching directories, or an empty list if the resource specified is not found.

Definition at line 355 of file KoResourcePaths.cpp.

356{
357 return cleanupDirs(s_instance->findDirsInternal(type));
358}

◆ findDirsInternal()

QStringList KoResourcePaths::findDirsInternal ( const QString & type)
private

Definition at line 531 of file KoResourcePaths.cpp.

532{
533 QStringList aliases = d->aliases(type);
534 dbgResources << type << aliases << d->mapTypeToQStandardPaths(type);
535
536 QStringList dirs;
537 QStringList standardDirs =
538 QStandardPaths::locateAll(d->mapTypeToQStandardPaths(type), "", QStandardPaths::LocateDirectory);
539
540 appendResources(&dirs, standardDirs, true);
541
542 Q_FOREACH (const QString &alias, aliases) {
543 QStringList aliasDirs =
544 QStandardPaths::locateAll(d->mapTypeToQStandardPaths(type), alias + '/', QStandardPaths::LocateDirectory);
545 appendResources(&dirs, aliasDirs, true);
546
547#ifdef Q_OS_MACOS
548 dbgResources << "MAC:" << getApplicationRoot();
549 QStringList bundlePaths;
550 bundlePaths << getApplicationRoot() + "/share/krita/" + alias;
551 bundlePaths << getApplicationRoot() + "/../share/krita/" + alias;
552 dbgResources << "bundlePaths" << bundlePaths;
553 appendResources(&dirs, bundlePaths, true);
554 Q_ASSERT(!dirs.isEmpty());
555#endif
556
557 QStringList fallbackPaths;
558 fallbackPaths << getApplicationRoot() + "/share/" + alias;
559 fallbackPaths << getApplicationRoot() + "/share/krita/" + alias;
560 appendResources(&dirs, fallbackPaths, true);
561
562 }
563
564 QStringList saveLocationList;
565 saveLocationList << saveLocation(type, QString(), true);
566 appendResources(&dirs, saveLocationList, true);
567
568 dbgResources << "findDirs: type" << type << "resource" << dirs;
569 return dirs;
570}
static QString getApplicationRoot()
static QString saveLocation(const QString &type, const QString &suffix=QString(), bool create=true)

References aliases(), d, dbgResources, getApplicationRoot(), and saveLocation().

◆ findExtraResourceDirs()

QStringList KoResourcePaths::findExtraResourceDirs ( ) const
private

Definition at line 755 of file KoResourcePaths.cpp.

756{
757 QStringList extraResourceDirs =
758 QString::fromUtf8(qgetenv("EXTRA_RESOURCE_DIRS"))
759 .split(';', Qt::SkipEmptyParts);
760
761 const KConfigGroup cfg(KSharedConfig::openConfig(), "");
762 const QString customPath =
764 if (!customPath.isEmpty()) {
765 extraResourceDirs << customPath;
766 }
767
768 if (getAppDataLocation() != QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)) {
769 extraResourceDirs << getAppDataLocation();
770 }
771
772 return extraResourceDirs;
773}
static const QString resourceLocationKey
static QString getAppDataLocation()

References getAppDataLocation(), and KisResourceLocator::resourceLocationKey.

◆ findResourceInternal()

QString KoResourcePaths::findResourceInternal ( const QString & type,
const QString & fileName )
private

Definition at line 441 of file KoResourcePaths.cpp.

442{
443 QStringList aliases = d->aliases(type);
444 dbgResources<< "aliases" << aliases << getApplicationRoot();
445 QString resource = QStandardPaths::locate(QStandardPaths::AppDataLocation, fileName, QStandardPaths::LocateFile);
446
447 if (resource.isEmpty()) {
448 Q_FOREACH (const QString &alias, aliases) {
449 resource = QStandardPaths::locate(d->mapTypeToQStandardPaths(type), alias + '/' + fileName, QStandardPaths::LocateFile);
450 if (QFile::exists(resource)) {
451 break;
452 }
453 }
454 }
455 if (resource.isEmpty() || !QFile::exists(resource)) {
456 QString approot = getApplicationRoot();
457 Q_FOREACH (const QString &alias, aliases) {
458 resource = approot + "/share/" + alias + '/' + fileName;
459 if (QFile::exists(resource)) {
460 break;
461 }
462 }
463 }
464 if (resource.isEmpty() || !QFile::exists(resource)) {
465 QString approot = getApplicationRoot();
466 Q_FOREACH (const QString &alias, aliases) {
467 resource = approot + "/share/krita/" + alias + '/' + fileName;
468 if (QFile::exists(resource)) {
469 break;
470 }
471 }
472 }
473
474 if (resource.isEmpty() || !QFile::exists(resource)) {
475 QStringList extraResourceDirs = findExtraResourceDirs();
476
477 if (!extraResourceDirs.isEmpty()) {
478 Q_FOREACH(const QString &extraResourceDir, extraResourceDirs) {
479 if (aliases.isEmpty()) {
480 resource = extraResourceDir + '/' + fileName;
481 dbgResources<< "\t4" << resource;
482 if (QFile::exists(resource)) {
483 break;
484 }
485 }
486 else {
487 Q_FOREACH (const QString &alias, aliases) {
488 resource = extraResourceDir + '/' + alias + '/' + fileName;
489 dbgResources<< "\t4" << resource;
490 if (QFile::exists(resource)) {
491 break;
492 }
493 }
494 }
495 }
496 }
497 }
498
499 dbgResources<< "findResource: type" << type << "filename" << fileName << "resource" << resource;
500 Q_ASSERT(!resource.isEmpty());
501 return resource;
502}

References aliases(), d, dbgResources, findExtraResourceDirs(), and getApplicationRoot().

◆ getAllUserResourceFoldersLocationsForWindowsStore()

void KoResourcePaths::getAllUserResourceFoldersLocationsForWindowsStore ( QString & standardLocation,
QString & privateLocation )
static

getAllAppDataLocationsForWindowsStore Use this to get both private and general appdata folders which also considers user's choice of custom resource folder Used in GeneralTab in kis_dlg_preferences, and KisViewManager::openResourceDirectory().

Parameters
standardLocation- location in standard AppData%
privateLocation- location in private app AppData% location, only relevant for Windows Store
Returns
either both appdata locations, or just the custom resource folder

Definition at line 289 of file KoResourcePaths.cpp.

290{
291 standardLocation = "";
292 privateLocation = "";
293 QString resourcePath = QDir(KisResourceLocator::instance()->resourceLocationBase()).absolutePath();
294#ifndef Q_OS_WIN
295 // not Windows, no problem
296 standardLocation = resourcePath;
297 return;
298#else
300 standardLocation = resourcePath; // Windows, but not Windows Store, so no problem
301 return;
302 }
303
304 // running inside Windows Store
305 const QDir resourceDir(resourcePath);
306 QDir appDataGeneralDir = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
307 appDataGeneralDir.cdUp();
308 const QString appDataGeneralDirPath = appDataGeneralDir.path();
309 if (resourceDir.absolutePath().contains(appDataGeneralDirPath, Qt::CaseInsensitive)) {
310 // resource folder location is inside appdata, so it can cause issues
311 // from inside of Krita, we can't determine whether it uses genuine %AppData% or the private Windows Store one
312 // so, half of the time, a custom folder inside %AppData% wouldn't work
313 // we can't fix that, we can only inform users about it or prevent them from choosing such folder
314 // in any case, here we need to return both folders: inside normal appdata and the private one
315 // (note that this case also handles the default resource folder called "krita" inside the appdata)
316
317
318 const QString folderName = QFileInfo(resourcePath).fileName();
319
320 const QString privateAppData = KisWindowsPackageUtils::getPackageRoamingAppDataLocation();
321 const QDir privateResourceDir(QDir::fromNativeSeparators(privateAppData) + '/' + folderName);
322
323 standardLocation = resourcePath;
324
325 if (privateResourceDir.exists()) {
326 privateLocation = privateResourceDir.absolutePath();
327 }
328
329 return;
330
331 } else {
332 standardLocation = resourcePath; // custom folder not inside AppData, so no problem (hopefully)
333 return;
334 }
335
336#endif
337}
static KisResourceLocator * instance()

References KisWindowsPackageUtils::getPackageRoamingAppDataLocation(), KisResourceLocator::instance(), and KisWindowsPackageUtils::isRunningInPackage().

◆ getAppDataLocation()

QString KoResourcePaths::getAppDataLocation ( )
static

Our code expects the resources location to be absolute. Otherwise functions like makeStorageLocationRelative() or makeStorageLocationAbsolute() do not work.

Definition at line 220 of file KoResourcePaths.cpp.

221{
222 if (!s_overrideAppDataLocation.isEmpty()) {
224 }
225
226 QString path;
227
228 KConfigGroup cfg(KSharedConfig::openConfig(), "");
229 path = cfg.readEntry(KisResourceLocator::resourceLocationKey, QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
230
231 QFileInfo fi(path);
232
233#if defined Q_OS_UNIX
234 // Check that path is not a Windows path (e.g., "C:/", "D:/", etc.),
235 // that can happen when a config file from Windows installation is
236 // moved to a Linux system.
237 QRegularExpression windowsPathPattern("^[A-Za-z]:/");
238 if (windowsPathPattern.match(path).hasMatch()) {
239 warnResources << "WARNING: KoResourcePaths::getAppDataLocation(): path appears to be a Windows path! Resetting to default..."
240 << path
241 << "->"
242 << QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
243 path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
244 fi.setFile(path);
245 cfg.writeEntry(KisResourceLocator::resourceLocationKey, path);
246 }
247#elif defined Q_OS_WIN
248 // Check that path is not a Linux path, that can happen when a config
249 // file from Linux installation is moved to a Windows system.
250 QRegularExpression windowsPathPattern("^/[^/]");
251 if (windowsPathPattern.match(path).hasMatch()) {
252 warnResources << "WARNING: KoResourcePaths::getAppDataLocation(): path appears to be a Unix path! Resetting to default..."
253 << path
254 << "->"
255 << QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
256 path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
257 fi.setFile(path);
258 cfg.writeEntry(KisResourceLocator::resourceLocationKey, path);
259 }
260#endif /* Q_OS_UNIX */
261
266 if (fi.isRelative()) {
267 warnResources << "WARNING: KoResourcePaths::getAppDataLocation(): resources location is not absolute! Fixing..." << path;
268 path = fi.absoluteFilePath();
269 fi.setFile(path);
270 cfg.writeEntry(KisResourceLocator::resourceLocationKey, path);
271 }
272
273 // Check whether an existing location is writable
274 if (fi.exists() && !fi.isWritable()) {
275 path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
276 }
277 else if (!fi.exists()) {
278 // Check whether a non-existing location can be created
279 if (!QDir().mkpath(path)) {
280 path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
281 }
282 QDir().rmpath(path);
283 }
284 return path;
285
286
287}
static QString s_overrideAppDataLocation
getAppDataLocation Use this instead of QStandardPaths::AppDataLocation! The user can configure the lo...
#define warnResources
Definition kis_debug.h:85

References KisResourceLocator::resourceLocationKey, s_overrideAppDataLocation, and warnResources.

◆ getApplicationRoot()

QString KoResourcePaths::getApplicationRoot ( )
static

Definition at line 215 of file KoResourcePaths.cpp.

216{
217 return getInstallationPrefix();
218}

◆ locate()

QString KoResourcePaths::locate ( const QString & type,
const QString & filename )
static

This function is just for convenience. It simply calls KoResourcePaths::findResource((type, filename).

Parameters
typeThe type of the wanted resource, see KStandardDirs
filenameA relative filename of the resource
Returns
A full path to the filename specified in the second argument, or QString() if not found

Definition at line 377 of file KoResourcePaths.cpp.

378{
379 return cleanup(s_instance->locateInternal(type, filename));
380}

◆ locateInternal()

QString KoResourcePaths::locateInternal ( const QString & type,
const QString & filename )
private

Definition at line 726 of file KoResourcePaths.cpp.

727{
728 QStringList aliases = d->aliases(type);
729
730 QStringList locations;
731 if (aliases.isEmpty()) {
732 locations << QStandardPaths::locate(d->mapTypeToQStandardPaths(type), filename, QStandardPaths::LocateFile);
733 }
734
735 Q_FOREACH (const QString &alias, aliases) {
736 locations << QStandardPaths::locate(d->mapTypeToQStandardPaths(type),
737 (alias.endsWith('/') ? alias : alias + '/') + filename, QStandardPaths::LocateFile);
738 }
739 dbgResources << "locate: type" << type << "filename" << filename << "locations" << locations;
740 if (locations.size() > 0) {
741 return locations.first();
742 }
743 else {
744 return "";
745 }
746}

References aliases(), d, and dbgResources.

◆ locateLocal()

QString KoResourcePaths::locateLocal ( const QString & type,
const QString & filename,
bool createDir = false )
static

This function is much like locate. However it returns a filename suitable for writing to. No check is made if the specified filename actually exists. Missing directories are created. If filename is only a directory, without a specific file, filename must have a trailing slash.

Parameters
typeThe type of the wanted resource, see KStandardDirs
filenameA relative filename of the resource
Returns
A full path to the filename specified in the second argument, or QString() if not found

Definition at line 382 of file KoResourcePaths.cpp.

383{
384 return cleanup(s_instance->locateLocalInternal(type, filename, createDir));
385}

◆ locateLocalInternal()

QString KoResourcePaths::locateLocalInternal ( const QString & type,
const QString & filename,
bool createDir = false )
private

Definition at line 748 of file KoResourcePaths.cpp.

749{
750 QString path = saveLocationInternal(type, "", createDir);
751 dbgResources << "locateLocal: type" << type << "filename" << filename << "CreateDir" << createDir << "path" << path;
752 return path + '/' + filename;
753}
QString saveLocationInternal(const QString &type, const QString &suffix=QString(), bool create=true)

References dbgResources, and saveLocationInternal().

◆ mapTypeToQStandardPaths()

QStandardPaths::StandardLocation KoResourcePaths::mapTypeToQStandardPaths ( const QString & type)
inline

Definition at line 183 of file KoResourcePaths.cpp.

184 {
185 if (type == "appdata") {
186 return QStandardPaths::AppDataLocation;
187 }
188 else if (type == "data") {
189 return QStandardPaths::AppDataLocation;
190 }
191 else if (type == "cache") {
192 return QStandardPaths::CacheLocation;
193 }
194 else if (type == "locale") {
195 return QStandardPaths::AppDataLocation;
196 }
197 else if (type == "genericdata") {
198 return QStandardPaths::GenericDataLocation;
199 }
200 else {
201 return QStandardPaths::AppDataLocation;
202 }
203 }

◆ resourceDirsInternal()

QStringList KoResourcePaths::resourceDirsInternal ( const QString & type)
private

Definition at line 659 of file KoResourcePaths.cpp.

660{
661 QStringList resourceDirs;
662 QStringList aliases = d->aliases(type);
663
664 Q_FOREACH (const QString &alias, aliases) {
665 QStringList aliasDirs;
666
667 aliasDirs << QStandardPaths::locateAll(d->mapTypeToQStandardPaths(type), alias, QStandardPaths::LocateDirectory);
668
669 aliasDirs << getInstallationPrefix() + "share/" + alias + "/"
670 << QStandardPaths::locateAll(d->mapTypeToQStandardPaths(type), alias, QStandardPaths::LocateDirectory);
671 aliasDirs << getInstallationPrefix() + "share/krita/" + alias + "/"
672 << QStandardPaths::locateAll(d->mapTypeToQStandardPaths(type), alias, QStandardPaths::LocateDirectory);
673
674 appendResources(&resourceDirs, aliasDirs, true);
675 }
676
677 dbgResources << "resourceDirs: type" << type << resourceDirs;
678
679 return resourceDirs;
680}

References aliases(), d, and dbgResources.

◆ saveLocation()

QString KoResourcePaths::saveLocation ( const QString & type,
const QString & suffix = QString(),
bool create = true )
static

Finds a location to save files into for the given type in the user's home directory.

Parameters
typeThe type of location to return.
suffixA subdirectory name. Makes it easier for you to create subdirectories. You can't pass filenames here, you have to pass directory names only and add possible filename in that directory yourself. A directory name always has a trailing slash ('/').
createIf set, saveLocation() will create the directories needed (including those given by suffix).
Returns
A path where resources of the specified type should be saved, or QString() if the resource type is unknown.

Definition at line 372 of file KoResourcePaths.cpp.

373{
374 return QDir::cleanPath(s_instance->saveLocationInternal(type, suffix, create)) + '/';
375}

◆ saveLocationInternal()

QString KoResourcePaths::saveLocationInternal ( const QString & type,
const QString & suffix = QString(),
bool create = true )
private

Definition at line 682 of file KoResourcePaths.cpp.

683{
684 QString path;
685
686 bool useStandardLocation = false;
687 const QStringList aliases = d->aliases(type);
688 const QStandardPaths::StandardLocation location = d->mapTypeToQStandardPaths(type);
689
690 if (location == QStandardPaths::AppDataLocation) {
691 KConfigGroup cfg(KSharedConfig::openConfig(), "");
692 path = cfg.readEntry(KisResourceLocator::resourceLocationKey, "");
693 }
694
695 if (path.isEmpty()) {
696 path = QStandardPaths::writableLocation(location);
697 useStandardLocation = true;
698 }
699
700#ifndef Q_OS_ANDROID
701 // on Android almost all config locations we save to are app specific,
702 // and don't end with "krita".
703 if (!path.endsWith("krita") && useStandardLocation) {
704 path += "/krita";
705 }
706#endif
707
708 if (!aliases.isEmpty()) {
709 path += '/' + aliases.first();
710 } else {
711
712 if (!suffix.isEmpty()) {
713 path += "/" + suffix;
714 }
715 }
716
717 QDir d(path);
718 if (!d.exists() && create) {
719 d.mkpath(path);
720 }
721 dbgResources << "saveLocation: type" << type << "suffix" << suffix << "create" << create << "path" << path;
722
723 return path;
724}
QPainterPath create(const char32_t codepoint, double height)
Creates a tofu missing glyph indicator representing the provided Unicode codepoint.

References aliases(), d, dbgResources, and KisResourceLocator::resourceLocationKey.

Member Data Documentation

◆ absolutes

QMap<QString, QStringList> KoResourcePaths::absolutes

Definition at line 159 of file KoResourcePaths.cpp.

◆ absolutesMutex

QMutex KoResourcePaths::absolutesMutex

Definition at line 163 of file KoResourcePaths.cpp.

◆ d

QScopedPointer<Private> KoResourcePaths::d
private

Definition at line 265 of file KoResourcePaths.h.

◆ relatives

QMap<QString, QStringList> KoResourcePaths::relatives

Definition at line 160 of file KoResourcePaths.cpp.

◆ relativesMutex

QMutex KoResourcePaths::relativesMutex

Definition at line 162 of file KoResourcePaths.cpp.

◆ s_overrideAppDataLocation

QString KoResourcePaths::s_overrideAppDataLocation
static

getAppDataLocation Use this instead of QStandardPaths::AppDataLocation! The user can configure the location where resources and other user writable items are stored now.

Returns
the configured location for the appdata folder

Definition at line 62 of file KoResourcePaths.h.


The documentation for this class was generated from the following files: