File indexing completed on 2024-05-12 15:59:56

0001 /*
0002  * SPDX-FileCopyrightText: 2015 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 #ifndef KORESOURCEPATHS_H
0007 #define KORESOURCEPATHS_H
0008 
0009 #include <QScopedPointer>
0010 #include <QString>
0011 #include <QStringList>
0012 
0013 #include <kritaresources_export.h>
0014 
0015 
0016 /**
0017  * The usual place to look for assets is Qt's AppDataLocation.
0018  * This corresponds to XDG_DATA_DIRS on Linux. To ensure your installation and
0019  * path are configured correctly, ensure your files are located in the directories
0020  * contained in this variable:
0021  *
0022  * QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
0023  *
0024  * This can be overridden in Krita's configuration.
0025  *
0026  * Unfortunately, we are mixing up two things in the appdatalocation:
0027  *
0028  *  * resources: brushes, presets and so on
0029  *  * assets: color themes, icc profiles and other weird stuff
0030  *
0031  * There are many debug lines that can be uncommented for more specific installation
0032  * checks. In the future these should be converted to qloggingcategory to enable
0033  * convenient enable/disable functionality.
0034  *
0035  * Note: DO NOT USE THIS CLASS WHEN LOCATING RESOURCES LIKE BRUSHES OR GRADIENTS. Use
0036  * KisResourceLocator instead.
0037  */
0038 class KRITARESOURCES_EXPORT KoResourcePaths
0039 {
0040 public:
0041 
0042     KoResourcePaths();
0043     virtual ~KoResourcePaths();
0044 
0045     enum SearchOption { NoSearchOptions = 0,
0046                         Recursive = 1,
0047                         IgnoreExecBit = 4
0048                       };
0049     Q_DECLARE_FLAGS(SearchOptions, SearchOption)
0050 
0051 
0052 
0053     static QString getApplicationRoot();
0054 
0055     /**
0056      * @brief getAppDataLocation Use this instead of QStandardPaths::AppDataLocation! The
0057      * user can configure the location where resources and other user writable items are stored
0058      * now.
0059      *
0060      * @return the configured location for the appdata folder
0061      */
0062     static QString s_overrideAppDataLocation; // This is set from KisApplicationArguments
0063     static QString getAppDataLocation();
0064 
0065     /**
0066      * @brief getAllAppDataLocationsForWindowsStore Use this to get both private and general appdata folders
0067      * which also considers user's choice of custom resource folder
0068      * Used in GeneralTab in kis_dlg_preferences, and KisViewManager::openResourceDirectory().
0069      * @param standardLocation - location in standard %AppData%
0070      * @param privateLocation - location in private app %AppData% location, only relevant for Windows Store
0071      * @return either both appdata locations, or just the custom resource folder
0072      */
0073     static void getAllUserResourceFoldersLocationsForWindowsStore(QString& standardLocation, QString& privateLocation);
0074 
0075     /**
0076      * Adds suffixes for asset types.
0077      *
0078      * You may add as many as you need, but it is advised that there
0079      * is exactly one to make writing definite.
0080      *
0081      * The later a suffix is added, the higher its priority. Note, that the
0082      * suffix should end with / but doesn't have to start with one (as prefixes
0083      * should end with one). So adding a suffix for app_pics would look
0084      * like KoStandardPaths::addResourceType("app_pics", "data", "app/pics");
0085      *
0086      * @param type Specifies a short descriptive string to access
0087      * files of this type.
0088      * @param basetype Specifies an already known type, or 0 if none
0089      * @param relativename Specifies a directory relative to the basetype
0090      * @param priority if true, the directory is added before any other,
0091      * otherwise after
0092      */
0093     static void addAssetType(const QString &type, const char *basetype,
0094                                 const QString &relativeName, bool priority = true);
0095 
0096 
0097     /**
0098      * Adds absolute path at the beginning of the search path for
0099      * particular types (for example in case of icons where
0100      * the user specifies extra paths).
0101      *
0102      * You shouldn't need this function in 99% of all cases besides
0103      * adding user-given paths.
0104      *
0105      * @param type Specifies a short descriptive string to access files
0106      * of this type.
0107      * @param absdir Points to directory where to look for this specific
0108      * type. Non-existent directories may be saved but pruned.
0109      * @param priority if true, the directory is added before any other,
0110      * otherwise after
0111      */
0112     static void addAssetDir(const QString &type, const QString &dir, bool priority = true);
0113 
0114     /**
0115      * Tries to find a resource in the following order:
0116      * @li All PREFIX/\<relativename> paths (most recent first).
0117      * @li All absolute paths (most recent first).
0118      *
0119      * The filename should be a filename relative to the base dir
0120      * for resources. So it's a way to get the path to libkdecore.la
0121      * to findResource("lib", "libkdecore.la"). KStandardDirs will
0122      * then look into the subdir lib of all elements of all prefixes
0123      * ($KDEDIRS) for a file libkdecore.la and return the path to
0124      * the first one it finds (e.g. /opt/kde/lib/libkdecore.la).
0125      *
0126      * Example:
0127      * @code
0128      * QString iconfilename = KStandardPaths::findResource("icon",QString("oxygen/22x22/apps/ktip.png"));
0129      * @endcode
0130      *
0131      * @param type The type of the wanted resource
0132      * @param filename A relative filename of the resource.
0133      *
0134      * @return A full path to the filename specified in the second
0135      *         argument, or QString() if not found.
0136      */
0137 
0138     static QString findAsset(const QString &type, const QString &fileName);
0139 
0140     /**
0141      * Tries to find all directories whose names consist of the
0142      * specified type and a relative path. So
0143      * findDirs("xdgdata-apps", "Settings") would return
0144      * @li /home/joe/.local/share/applications/Settings/
0145      * @li /usr/share/applications/Settings/
0146      *
0147      * (from the most local to the most global)
0148      *
0149      * Note that it appends @c / to the end of the directories,
0150      * so you can use this right away as directory names.
0151      *
0152      * @param type The type of the base directory.
0153      * @param reldir Relative directory.
0154      *
0155      * @return A list of matching directories, or an empty
0156      *         list if the resource specified is not found.
0157      */
0158     static QStringList findDirs(const QString &type);
0159 
0160     /**
0161      * Tries to find all resources with the specified type.
0162      *
0163      * The function will look into all specified directories
0164      * and return all filenames in these directories.
0165      *
0166      * The "most local" files are returned before the "more global" files.
0167      *
0168      * @param type The type of resource to locate directories for.
0169      * @param filter Only accept filenames that fit to filter. The filter
0170      *        may consist of an optional directory and a QRegExp
0171      *        wildcard expression. E.g. <tt>"images\*.jpg"</tt>.
0172      *        Use QString() if you do not want a filter.
0173      * @param options if the flags passed include Recursive, subdirectories
0174      *        will also be search.
0175      *
0176      * @return List of all the files whose filename matches the
0177      *         specified filter.
0178      */
0179     static QStringList findAllAssets(const QString &type,
0180                                         const QString &filter = QString(),
0181                                         SearchOptions options = NoSearchOptions);
0182 
0183     /**
0184      * @param type The type of resource
0185      * @return The list of possible directories for the specified @p type.
0186      * The function updates the cache if possible.  If the resource
0187      * type specified is unknown, it will return an empty list.
0188      * Note, that the directories are assured to exist beside the save
0189      * location, which may not exist, but is returned anyway.
0190      */
0191     static QStringList assetDirs(const QString &type);
0192 
0193     /**
0194      * Finds a location to save files into for the given type
0195      * in the user's home directory.
0196      *
0197      * @param type The type of location to return.
0198      * @param suffix A subdirectory name.
0199      *             Makes it easier for you to create subdirectories.
0200      *   You can't pass filenames here, you _have_ to pass
0201      *       directory names only and add possible filename in
0202      *       that directory yourself. A directory name always has a
0203      *       trailing slash ('/').
0204      * @param create If set, saveLocation() will create the directories
0205      *        needed (including those given by @p suffix).
0206      *
0207      * @return A path where resources of the specified type should be
0208      *         saved, or QString() if the resource type is unknown.
0209      */
0210     static QString saveLocation(const QString &type, const QString &suffix = QString(), bool create = true);
0211 
0212     /**
0213      * This function is just for convenience. It simply calls
0214      * KoResourcePaths::findResource((type, filename).
0215      *
0216      * @param type   The type of the wanted resource, see KStandardDirs
0217      * @param filename   A relative filename of the resource
0218      *
0219      * @return A full path to the filename specified in the second
0220      *         argument, or QString() if not found
0221      **/
0222     static QString locate(const QString &type, const QString &filename);
0223 
0224     /**
0225      * This function is much like locate. However it returns a
0226      * filename suitable for writing to. No check is made if the
0227      * specified @p filename actually exists. Missing directories
0228      * are created. If @p filename is only a directory, without a
0229      * specific file, @p filename must have a trailing slash.
0230      *
0231      * @param type   The type of the wanted resource, see KStandardDirs
0232      * @param filename   A relative filename of the resource
0233      *
0234      * @return A full path to the filename specified in the second
0235      *         argument, or QString() if not found
0236      **/
0237     static QString locateLocal(const QString &type, const QString &filename, bool createDir = false);
0238 
0239 private:
0240 
0241     void addResourceTypeInternal(const QString &type, const QString &basetype,
0242                                  const QString &relativeName, bool priority);
0243 
0244     void addResourceDirInternal(const QString &type, const QString &absdir, bool priority);
0245 
0246     QString findResourceInternal(const QString &type, const QString &fileName);
0247 
0248     QStringList findDirsInternal(const QString &type);
0249 
0250     QStringList findAllResourcesInternal(const QString &type,
0251                                          const QString &filter = QString(),
0252                                          SearchOptions options = NoSearchOptions) const;
0253 
0254     QStringList resourceDirsInternal(const QString &type);
0255 
0256     QString saveLocationInternal(const QString &type, const QString &suffix = QString(), bool create = true);
0257 
0258     QString locateInternal(const QString &type, const QString &filename);
0259 
0260     QString locateLocalInternal(const QString &type, const QString &filename, bool createDir = false);
0261 
0262     QStringList findExtraResourceDirs() const;
0263 
0264     class Private;
0265     QScopedPointer<Private> d;
0266 };
0267 
0268 Q_DECLARE_OPERATORS_FOR_FLAGS(KoResourcePaths::SearchOptions)
0269 
0270 #endif // KORESOURCEPATHS_H