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

0001 /*
0002  * SPDX-FileCopyrightText: 2018 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #ifndef KISRESOURCECACHEDB_H
0008 #define KISRESOURCECACHEDB_H
0009 
0010 #include <QObject>
0011 
0012 #include <kritaresources_export.h>
0013 
0014 #include <KisResourceStorage.h>
0015 
0016 /**
0017  * @brief The KisResourceCacheDb class encapsulates the database that
0018  * caches information about the resources available to the user.
0019  *
0020  * KisApplication creates and initializes the database. All other methods
0021  * are static and can be used from anywhere.
0022  */
0023 class KRITARESOURCES_EXPORT KisResourceCacheDb
0024 {
0025 public:
0026 
0027     static const QString resourceCacheDbFilename; ///< filename of the database
0028     static const QString databaseVersion; ///< current schema version
0029     static QStringList storageTypes; ///< kinds of places where resources can be stored
0030     static QStringList disabledBundles; ///< the list of compatibility bundles that need to inactive by default
0031 
0032     /**
0033      * @brief isValid
0034      * @return true if the database has been correctly created, false if the database cannot be used
0035      */
0036     static bool isValid();
0037 
0038     /**
0039      * @brief lastError returns the last SQL error.
0040      */
0041     static QString lastError();
0042 
0043     /**
0044      * @brief initializes the database and updates the scheme if necessary. Does not actually
0045      * fill the database with pointers to resources.
0046      *
0047      * @param location the location of the database
0048      * @return true if the database has been initialized correctly
0049      */
0050     static bool initialize(const QString &location);
0051 
0052     /// Delete all storages that are Unknown or Memory and all resources that are marked temporary or belong to Unknown or Memory storages
0053     static void deleteTemporaryResources();
0054 
0055 private:
0056 
0057     friend class KisResourceLocator;
0058     friend class TestResourceLocator;
0059     friend class TestResourceCacheDb;
0060     friend class KisAllTagsModel;
0061     friend class KisResourceLoaderRegistry;
0062     friend class KisResourceUserOperations;
0063     friend class KisDocument;
0064     friend class KisAllResourcesModel;
0065 
0066     explicit KisResourceCacheDb(); // Deleted
0067     ~KisResourceCacheDb(); // Deleted
0068     KisResourceCacheDb operator=(const KisResourceCacheDb&); // Deleted
0069     /**
0070      * @brief registerResourceType registers this resource type in the database
0071      * @param resourceType the string that represents the type
0072      * @return true if the type was registered or had already been registered
0073      */
0074     static bool registerResourceType(const QString &resourceType);
0075 
0076     static QVector<int> resourcesForStorage(const QString &resourceType, const QString &storageLocation);
0077     static int resourceIdForResource(const QString &resourceFileName, const QString &resourceType, const QString &storageLocation);
0078     static bool resourceNeedsUpdating(int resourceId, QDateTime timestamp);
0079 
0080     /**
0081      * @brief addResourceVersion adds a new version of the resource to the database.
0082      * The resource itself already should be updated with the updated filename and version.
0083      * @param resourceId unique identifier for the resource
0084      * @param timestamp
0085      * @param storage
0086      * @param resource
0087      * @return true if the database was successfully updated
0088      */
0089     static bool addResourceVersion(int resourceId, QDateTime timestamp, KisResourceStorageSP storage, KoResourceSP resource);
0090 
0091     static bool addResourceVersionImpl(int resourceId, QDateTime timestamp, KisResourceStorageSP storage, KoResourceSP resource);
0092     static bool removeResourceVersionImpl(int resourceId, int version, KisResourceStorageSP storage);
0093 
0094     static bool updateResourceTableForResourceIfNeeded(int resourceId, const QString &resourceType, KisResourceStorageSP storage);
0095     static bool makeResourceTheCurrentVersion(int resourceId, KoResourceSP resource);
0096     static bool removeResourceCompletely(int resourceId);
0097 
0098     /// The function will find the resource only if it is the latest version
0099     static bool getResourceIdFromFilename(QString filename, QString resourceType, QString storageLocation, int &outResourceId);
0100     /// Note that here you can put even the original filename - any filename from the versioned_resources - and it will still find it
0101     static bool getResourceIdFromVersionedFilename(QString filename, QString resourceType, QString storageLocation, int& outResourceId);
0102     static bool getAllVersionsLocations(int resourceId, QStringList &outVersionsLocationsList);
0103 
0104 
0105     static bool addResource(KisResourceStorageSP storage, QDateTime timestamp, KoResourceSP resource, const QString &resourceType);
0106     static bool addResources(KisResourceStorageSP storage, QString resourceType);
0107 
0108     /// Make this resource active or inactive; this does not remove the resource from disk or from the database
0109     static bool setResourceActive(int resourceId, bool active = false);
0110 
0111     static bool tagResource(const QString &resourceFileName, KisTagSP tag, const QString &resourceType);
0112     static bool hasTag(const QString &url, const QString &resourceType);
0113     static bool linkTagToStorage(const QString &url, const QString &resourceType, const QString &storageLocation);
0114     static bool addTag(const QString &resourceType, const QString storageLocation, KisTagSP tag);
0115     static bool addTags(KisResourceStorageSP storage, QString resourceType);
0116 
0117     static bool addStorage(KisResourceStorageSP storage, bool preinstalled);
0118     static bool addStorageTags(KisResourceStorageSP storage);
0119 
0120     /// Actually delete the storage and all its resources from the database (i.e., nothing is set to inactive, it's deleted)
0121     static bool deleteStorage(KisResourceStorageSP storage);
0122     /// Actually delete the storage and all its resources from the database (i.e., nothing is set to inactive, it's deleted)
0123     ///  location - relative
0124     static bool deleteStorage(QString location);
0125     static bool synchronizeStorage(KisResourceStorageSP storage);
0126 
0127     /**
0128      * @brief metaDataForId
0129      * @param id
0130      * @param tableName
0131      * @return
0132      */
0133     static QMap<QString, QVariant> metaDataForId(int id, const QString &tableName);
0134 
0135     /**
0136      * @brief setMetaDataForId removes all metadata for the given id and table name,
0137      *  and inserts the metadata in the metadata table.
0138      * @param id
0139      * @param tableName
0140      * @return true if successful, false if not
0141      */
0142     static bool updateMetaDataForId(const QMap<QString, QVariant> map, int id, const QString &tableName);
0143     static bool addMetaDataForId(const QMap<QString, QVariant> map, int id, const QString &tableName);
0144 
0145     static bool s_valid;
0146     static QString s_lastError;
0147 };
0148 
0149 #endif // KISRESOURCECACHEDB_H