diff --git a/libs/resources/KisResourceCacheDb.cpp b/libs/resources/KisResourceCacheDb.cpp index 73174ac5d3..42bbf63b2d 100644 --- a/libs/resources/KisResourceCacheDb.cpp +++ b/libs/resources/KisResourceCacheDb.cpp @@ -1,1170 +1,1176 @@ /* * Copyright (C) 2018 Boudewijn Rempt * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "KisResourceCacheDb.h" #include #include #include #include #include #include #include #include #include #include #include #include #include "KisResourceLocator.h" #include "KisResourceCacheDb.h" #include "KisResourceLoaderRegistry.h" const QString dbDriver = "QSQLITE"; const QString KisResourceCacheDb::dbLocationKey { "ResourceCacheDbDirectory" }; const QString KisResourceCacheDb::resourceCacheDbFilename { "resourcecache.sqlite" }; const QString KisResourceCacheDb::databaseVersion { "0.0.2" }; QStringList KisResourceCacheDb::storageTypes { QStringList() }; QStringList KisResourceCacheDb::disabledBundles { QStringList() << "Krita_3_Default_Resources.bundle" }; bool KisResourceCacheDb::s_valid {false}; QString KisResourceCacheDb::s_lastError {QString()}; bool KisResourceCacheDb::isValid() { return s_valid; } QString KisResourceCacheDb::lastError() { return s_lastError; } QSqlError createDatabase(const QString &location) { // NOTE: if the id's of Unknown and Memory in the database // will change, and that will break the queries that // remove Unknown and Memory storages on start-up. KisResourceCacheDb::storageTypes << KisResourceStorage::storageTypeToString(KisResourceStorage::StorageType(1)) << KisResourceStorage::storageTypeToString(KisResourceStorage::StorageType(2)) << KisResourceStorage::storageTypeToString(KisResourceStorage::StorageType(3)) << KisResourceStorage::storageTypeToString(KisResourceStorage::StorageType(4)) << KisResourceStorage::storageTypeToString(KisResourceStorage::StorageType(5)) << KisResourceStorage::storageTypeToString(KisResourceStorage::StorageType(6)) ; if (!QSqlDatabase::connectionNames().isEmpty()) { infoResources << "Already connected to resource cache database"; return QSqlError(); } QDir dbLocation(location); if (!dbLocation.exists()) { dbLocation.mkpath(dbLocation.path()); } QSqlDatabase db = QSqlDatabase::addDatabase(dbDriver); db.setDatabaseName(location + "/" + KisResourceCacheDb::resourceCacheDbFilename); //qDebug() << "QuerySize supported" << db.driver()->hasFeature(QSqlDriver::QuerySize); if (!db.open()) { qWarning() << "Could not connect to resource cache database"; return db.lastError(); } QStringList tables = QStringList() << "version_information" << "storage_types" << "resource_types" << "storages" << "tags" << "resources" << "versioned_resources" << "resource_tags" << "metadata"; QStringList dbTables; // Verify whether we should recreate the database { bool allTablesPresent = true; dbTables = db.tables(); Q_FOREACH(const QString &table, tables) { if (!dbTables.contains(table)) { allTablesPresent = false; } } bool schemaIsOutDated = false; QString schemaVersion = "Unknown"; QString kritaVersion = "Unknown"; int creationDate = 0; if (dbTables.contains("version_information")) { // Verify the version number QFile f(":/get_version_information.sql"); if (f.open(QFile::ReadOnly)) { QSqlQuery q(f.readAll()); if (q.size() > 0) { q.first(); schemaVersion = q.value(0).toString(); kritaVersion = q.value(1).toString(); creationDate = q.value(2).toInt(); if (schemaVersion != KisResourceCacheDb::databaseVersion) { // XXX: Implement migration schemaIsOutDated = true; qFatal("Database schema is outdated, migration is needed. Database migration has NOT been implemented yet."); } } } else { return QSqlError("Error executing SQL", "Could not open get_version_information.sql", QSqlError::StatementError); } } if (allTablesPresent && !schemaIsOutDated) { KisUsageLogger::log(QString("Database is up to date. Version: %1, created by Krita %2, at %3") .arg(schemaVersion) .arg(kritaVersion) .arg(QDateTime::fromSecsSinceEpoch(creationDate).toString())); return QSqlError(); } } // Create tables Q_FOREACH(const QString &table, tables) { QFile f(":/create_" + table + ".sql"); if (f.open(QFile::ReadOnly)) { QSqlQuery q; if (!q.exec(f.readAll())) { qWarning() << "Could not create table" << table << q.lastError(); return db.lastError(); } infoResources << "Created table" << table; } else { return QSqlError("Error executing SQL", QString("Could not find SQL file %1").arg(table), QSqlError::StatementError); } } // Create indexes QStringList indexes = QStringList() << "storages" << "versioned_resources"; Q_FOREACH(const QString &index, indexes) { QFile f(":/create_index_" + index + ".sql"); if (f.open(QFile::ReadOnly)) { QSqlQuery q; if (!q.exec(f.readAll())) { qWarning() << "Could not create index" << index; return db.lastError(); } infoResources << "Created table" << index; } else { return QSqlError("Error executing SQL", QString("Could not find SQL file %1").arg(index), QSqlError::StatementError); } } // Fill lookup tables { if (dbTables.contains("storage_types")) { QSqlQuery q; if (!q.exec("DELETE * FROM storage_types;")) { qWarning() << "Could not clear table storage_types" << db.lastError(); } } QFile f(":/fill_storage_types.sql"); if (f.open(QFile::ReadOnly)) { QString sql = f.readAll(); Q_FOREACH(const QString &originType, KisResourceCacheDb::storageTypes) { QSqlQuery q(sql); q.addBindValue(originType); if (!q.exec()) { qWarning() << "Could not insert" << originType << db.lastError() << q.executedQuery(); return db.lastError(); } } infoResources << "Filled lookup table storage_types"; } else { return QSqlError("Error executing SQL", QString("Could not find SQL fill_storage_types.sql."), QSqlError::StatementError); } } { if (dbTables.contains("resource_types")) { QSqlQuery q; if (!q.exec("DELETE * FROM resource_types;")) { qWarning() << "Could not clear table resource_types" << db.lastError(); } } QFile f(":/fill_resource_types.sql"); if (f.open(QFile::ReadOnly)) { QString sql = f.readAll(); Q_FOREACH(const QString &resourceType, KisResourceLoaderRegistry::instance()->resourceTypes()) { QSqlQuery q(sql); q.addBindValue(resourceType); if (!q.exec()) { qWarning() << "Could not insert" << resourceType << db.lastError() << q.executedQuery(); return db.lastError(); } } infoResources << "Filled lookup table resource_types"; } else { return QSqlError("Error executing SQL", QString("Could not find SQL fill_resource_types.sql."), QSqlError::StatementError); } } { QFile f(":/fill_version_information.sql"); if (f.open(QFile::ReadOnly)) { QString sql = f.readAll(); QSqlQuery q; q.prepare(sql); q.addBindValue(KisResourceCacheDb::databaseVersion); q.addBindValue(KritaVersionWrapper::versionString()); q.addBindValue(QDateTime::currentDateTimeUtc().toSecsSinceEpoch()); if (!q.exec()) { qWarning() << "Could not insert the current version" << db.lastError() << q.executedQuery() << q.boundValues(); return db.lastError(); } infoResources << "Filled version table"; } else { return QSqlError("Error executing SQL", QString("Could not find SQL fill_version_information.sql."), QSqlError::StatementError); } } return QSqlError(); } bool KisResourceCacheDb::initialize(const QString &location) { QSqlError err = createDatabase(location); s_valid = !err.isValid(); switch (err.type()) { case QSqlError::NoError: s_lastError = QString(); break; case QSqlError::ConnectionError: s_lastError = QString("Could not initialize the resource cache database. Connection error: %1").arg(err.text()); break; case QSqlError::StatementError: s_lastError = QString("Could not initialize the resource cache database. Statement error: %1").arg(err.text()); break; case QSqlError::TransactionError: s_lastError = QString("Could not initialize the resource cache database. Transaction error: %1").arg(err.text()); break; case QSqlError::UnknownError: s_lastError = QString("Could not initialize the resource cache database. Unknown error: %1").arg(err.text()); break; } // Delete all storages that are no longer known to the resource locator (including the memory storages) deleteTemporaryResources(); return s_valid; } int KisResourceCacheDb::resourceIdForResource(const QString &resourceName, const QString &resourceType, const QString &storageLocation) { Q_ASSERT(QThread::currentThread() == qApp->thread()); QFile f(":/select_resource_id.sql"); f.open(QFile::ReadOnly); QSqlQuery q; if (!q.prepare(f.readAll())) { qWarning() << "Could not read and prepare resourceIdForResource" << q.lastError(); return -1; } q.bindValue(":name", resourceName); q.bindValue(":resource_type", resourceType); q.bindValue(":storage_location", storageLocation); if (!q.exec()) { qWarning() << "Could not query resourceIdForResource" << q.boundValues() << q.lastError(); return -1; } if (!q.first()) { return -1; } return q.value(0).toInt(); } bool KisResourceCacheDb::resourceNeedsUpdating(int resourceId, QDateTime timestamp) { QSqlQuery q; if (!q.prepare("SELECT timestamp\n" "FROM versioned_resources\n" "WHERE resource_id = :resource_id\n" "AND version = (SELECT MAX(version)\n" " FROM versioned_resources\n" " WHERE resource_id = :resource_id);")) { qWarning() << "Could not prepare resourceNeedsUpdating statement" << q.lastError(); return false; } q.bindValue(":resource_id", resourceId); if (!q.exec()) { qWarning() << "Could not query for the most recent timestamp" << q.boundValues() << q.lastError(); return false; } if (!q.first()) { qWarning() << "Inconsistent database: could not find a version for resource with Id" << resourceId; return false; } QVariant resourceTimeStamp = q.value(0); if (!resourceTimeStamp.isValid()) { qWarning() << "Could not retrieve timestamp from versioned_resources" << resourceId; return false; } return (timestamp.toSecsSinceEpoch() > resourceTimeStamp.toInt()); } bool KisResourceCacheDb::addResourceVersion(int resourceId, QDateTime timestamp, KisResourceStorageSP storage, KoResourceSP resource) { bool r = false; // Create the new version. The resource is expected to have an updated version number, or // this will fail on the unique index on resource_id, storage_id and version. { QSqlQuery q; r = q.prepare("INSERT INTO versioned_resources \n" "(resource_id, storage_id, version, location, timestamp, md5sum)\n" "VALUES\n" "( :resource_id\n" ", (SELECT id \n" " FROM storages \n" " WHERE location = :storage_location)\n" ", :version\n" ", :location\n" ", :timestamp\n" ", :md5sum\n" ");"); if (!r) { qWarning() << "Could not prepare addResourceVersion statement" << q.lastError(); return r; } q.bindValue(":resource_id", resourceId); q.bindValue(":storage_location", KisResourceLocator::instance()->makeStorageLocationRelative(storage->location())); q.bindValue(":version", resource->version() + 1); q.bindValue(":location", QFileInfo(resource->filename()).fileName()); q.bindValue(":timestamp", timestamp.toSecsSinceEpoch()); Q_ASSERT(!resource->md5().isEmpty()); q.bindValue(":md5sum", resource->md5().toHex()); r = q.exec(); if (!r) { qWarning() << "Could not execute addResourceVersion statement" << q.boundValues() << q.lastError(); return r; } } // Update the resource itself. The resource gets a new filename when it's updated { QSqlQuery q; r = q.prepare("UPDATE resources\n" "SET name = :name\n" ", filename = :filename\n" ", tooltip = :tooltip\n" ", thumbnail = :thumbnail\n" ", version = :version\n" "WHERE id = :id"); if (!r) { qWarning() << "Could not prepare updateResource statement" << q.lastError(); return r; } q.bindValue(":name", resource->name()); q.bindValue(":filename", QFileInfo(resource->filename()).fileName()); q.bindValue(":tooltip", i18n(resource->name().toUtf8())); q.bindValue(":version", resource->version()); QByteArray ba; QBuffer buf(&ba); buf.open(QBuffer::WriteOnly); resource->image().save(&buf, "PNG"); buf.close(); q.bindValue(":thumbnail", ba); q.bindValue(":id", resourceId); r = q.exec(); if (!r) { qWarning() << "Could not update resource" << q.boundValues() << q.lastError(); } } return r; } bool KisResourceCacheDb::addResource(KisResourceStorageSP storage, QDateTime timestamp, KoResourceSP resource, const QString &resourceType) { bool r = false; if (!s_valid) { qWarning() << "KisResourceCacheDb::addResource: The database is not valid"; return false; } if (!resource || !resource->valid()) { qWarning() << "KisResourceCacheDb::addResource: The resource is not valid"; // We don't care about invalid resources and will just ignore them. return true; } bool temporary = (storage->type() == KisResourceStorage::StorageType::Memory); // Check whether it already exists int resourceId = resourceIdForResource(resource->name(), resourceType, KisResourceLocator::instance()->makeStorageLocationRelative(storage->location())); if (resourceId > -1) { if (resourceNeedsUpdating(resourceId, timestamp)) { r = addResourceVersion(resourceId, timestamp, storage, resource); } return true; } QSqlQuery q; r = q.prepare("INSERT INTO resources \n" "(storage_id, resource_type_id, name, filename, tooltip, thumbnail, status, temporary) \n" "VALUES \n" "((SELECT id " " FROM storages " " WHERE location = :storage_location)\n" ", (SELECT id\n" " FROM resource_types\n" " WHERE name = :resource_type)\n" ", :name\n" ", :filename\n" ", :tooltip\n" ", :thumbnail\n" ", :status\n" ", :temporary);"); if (!r) { qWarning() << "Could not prepare addResource statement" << q.lastError(); return r; } q.bindValue(":storage_location", KisResourceLocator::instance()->makeStorageLocationRelative(storage->location())); q.bindValue(":resource_type", resourceType); q.bindValue(":name", resource->name()); q.bindValue(":filename", QFileInfo(resource->filename()).fileName()); q.bindValue(":tooltip", i18n(resource->name().toUtf8())); QByteArray ba; QBuffer buf(&ba); buf.open(QBuffer::WriteOnly); resource->image().save(&buf, "PNG"); buf.close(); q.bindValue(":thumbnail", ba); q.bindValue(":status", 1); q.bindValue(":temporary", (temporary ? 1 : 0)); r = q.exec(); if (!r) { qWarning() << "Could not execute addResource statement" << q.boundValues() << q.lastError(); return r; } resourceId = resourceIdForResource(resource->name(), resourceType, KisResourceLocator::instance()->makeStorageLocationRelative(storage->location())); // Then add a new version r = q.prepare("INSERT INTO versioned_resources\n" "(resource_id, storage_id, version, location, timestamp, md5sum)\n" "VALUES\n" "(:resource_id\n" ", (SELECT id FROM storages\n" " WHERE location = :storage_location)\n" ", :version\n" ", :location\n" ", :timestamp\n" ", :md5sum\n" ");"); if (!r) { qWarning() << "Could not prepare intitial addResourceVersion statement" << q.lastError(); return r; } q.bindValue(":resource_id", resourceId); q.bindValue(":storage_location", KisResourceLocator::instance()->makeStorageLocationRelative(storage->location())); q.bindValue(":version", resource->version()); q.bindValue(":location", QFileInfo(resource->filename()).fileName()); q.bindValue(":timestamp", timestamp.toSecsSinceEpoch()); //Q_ASSERT(!resource->md5().isEmpty()); if (resource->md5().isEmpty()) { qDebug() << "No md5 for resource" << resource->name() << resourceType << storage->location(); } q.bindValue(":md5sum", resource->md5().toHex()); r = q.exec(); if (!r) { qWarning() << "Could not execute initial addResourceVersion statement" << q.boundValues() << q.lastError(); } return r; } bool KisResourceCacheDb::addResources(KisResourceStorageSP storage, QString resourceType) { QSqlDatabase::database().transaction(); QSharedPointer iter = storage->resources(resourceType); while (iter->hasNext()) { iter->next(); KoResourceSP resource = iter->resource(); if (resource && resource->valid()) { if (!addResource(storage, iter->lastModified(), resource, iter->type())) { qWarning() << "Could not add resource" << QFileInfo(resource->filename()).fileName() << "to the database"; } } } QSqlDatabase::database().commit(); return true; } bool KisResourceCacheDb::removeResource(int resourceId) { if (resourceId < 0) { qWarning() << "Invalid resource id; cannot remove resource"; return false; } QSqlQuery q; bool r = q.prepare("UPDATE resources\n" "SET status = 0\n" "WHERE id = :resource_id"); if (!r) { qWarning() << "Could not prepare removeResource query" << q.lastError(); } q.bindValue(":resource_id", resourceId); if (!q.exec()) { qWarning() << "Could not update resource" << resourceId << "to inactive" << q.lastError(); return false; } return true; } bool KisResourceCacheDb::tagResource(KisResourceStorageSP storage, const QString resourceName, KisTagSP tag, const QString &resourceType) { // Get resource id int resourceId = resourceIdForResource(resourceName, resourceType, KisResourceLocator::instance()->makeStorageLocationRelative(storage->location())); if (resourceId < 0) { qWarning() << "Could not find resource to tag" << KisResourceLocator::instance()->makeStorageLocationRelative(storage->location()) << resourceName << resourceType; return false; } // Get tag id int tagId {-1}; { QFile f(":/select_tag.sql"); if (f.open(QFile::ReadOnly)) { QSqlQuery q; if (!q.prepare(f.readAll())) { qWarning() << "Could not read and prepare select_tag.sql" << q.lastError(); return false; } q.bindValue(":url", tag->url()); q.bindValue(":resource_type", resourceType); if (!q.exec()) { qWarning() << "Could not query tags" << q.boundValues() << q.lastError(); return false; } if (!q.first()) { qWarning() << "Could not find tag" << q.boundValues() << q.lastError(); return false; } tagId = q.value(0).toInt(); } } QSqlQuery q; if (!q.prepare("INSERT INTO resource_tags\n" "(resource_id, tag_id)\n" "VALUES\n" "(:resource_id, :tag_id);")) { qWarning() << "Could not prepare tagResource statement" << q.lastError(); return false; } q.bindValue(":resource_id", resourceId); q.bindValue(":tag_id", tagId); if (!q.exec()) { qWarning() << "Could not execute tagResource stagement" << q.boundValues() << q.lastError(); return false; } return true; } bool KisResourceCacheDb::hasTag(const QString &url, const QString &resourceType) { QFile f(":/select_tag.sql"); if (f.open(QFile::ReadOnly)) { QSqlQuery q; if (!q.prepare(f.readAll())) { qWarning() << "Could not read and prepare select_tag.sql" << q.lastError(); return false; } q.bindValue(":url", url); q.bindValue(":resource_type", resourceType); if (!q.exec()) { qWarning() << "Could not query tags" << q.boundValues() << q.lastError(); } return q.first(); } qWarning() << "Could not open select_tag.sql"; return false; } bool KisResourceCacheDb::addTag(const QString &resourceType, const QString url, const QString name, const QString comment) { if (hasTag(url, resourceType)) { return true; } QSqlQuery q; if (!q.prepare("INSERT INTO tags\n" "( url, name, comment, resource_type_id, active)\n" "VALUES\n" "( :url\n" ", :name\n" ", :comment\n" ", (SELECT id\n" " FROM resource_types\n" " WHERE name = :resource_type)\n" ", 1" ");")) { qWarning() << "Could not prepare add tag statement" << q.lastError(); return false; } q.bindValue(":url", url); q.bindValue(":name", name); q.bindValue(":comment", comment); q.bindValue(":resource_type", resourceType); if (!q.exec()) { qWarning() << "Could not insert tag" << q.boundValues() << q.lastError(); } return true; } bool KisResourceCacheDb::addTags(KisResourceStorageSP storage, QString resourceType) { QSqlDatabase::database().transaction(); QSharedPointer iter = storage->tags(resourceType); while(iter->hasNext()) { iter->next(); if (!addTag(resourceType, iter->url(), iter->name(), iter->comment())) { qWarning() << "Could not add tag" << iter->url() << "to the database"; } if (!iter->tag()->defaultResources().isEmpty()) { Q_FOREACH(const QString &resourceName, iter->tag()->defaultResources()) { if (!tagResource(storage, resourceName, iter->tag(), resourceType)) { qWarning() << "Could not tag resource" << resourceName << "with tag" << iter->url(); } } } } QSqlDatabase::database().commit(); return true; } bool KisResourceCacheDb::addStorage(KisResourceStorageSP storage, bool preinstalled) { bool r = true; if (!s_valid) { qWarning() << "The database is not valid"; return false; } { QSqlQuery q; r = q.prepare("SELECT * FROM storages WHERE location = :location"); q.bindValue(":location", KisResourceLocator::instance()->makeStorageLocationRelative(storage->location())); r = q.exec(); if (!r) { qWarning() << "Could not select from storages"; return r; } if (q.first()) { qDebug() << "Storage already exists" << storage; return true; } } // Insert the storage; { QSqlQuery q; r = q.prepare("INSERT INTO storages\n " - "(storage_type_id, location, timestamp, pre_installed, active)\n" + "(storage_type_id, location, timestamp, pre_installed, active, thumbnail)\n" "VALUES\n" - "(:storage_type_id, :location, :timestamp, :pre_installed, :active);"); + "(:storage_type_id, :location, :timestamp, :pre_installed, :active, :thumbnail);"); if (!r) { qWarning() << "Could not prepare query" << q.lastError(); return r; } q.bindValue(":storage_type_id", static_cast(storage->type())); q.bindValue(":location", KisResourceLocator::instance()->makeStorageLocationRelative(storage->location())); q.bindValue(":timestamp", storage->timestamp().toSecsSinceEpoch()); q.bindValue(":pre_installed", preinstalled ? 1 : 0); q.bindValue(":active", !disabledBundles.contains(storage->name())); + QByteArray ba; + QBuffer buf(&ba); + buf.open(QBuffer::WriteOnly); + storage->thumbnail().save(&buf, "PNG"); + buf.close(); + q.bindValue(":thumbnail", ba); r = q.exec(); if (!r) qWarning() << "Could not execute query" << q.lastError(); } // Insert the metadata { QStringList keys = storage->metaDataKeys(); if (keys.size() > 0) { QSqlQuery q; if (!q.prepare("SELECT MAX(id)\n" "FROM storages\n")) { qWarning() << "Could not create select storages query for metadata" << q.lastError(); } if (!q.exec()) { qWarning() << "Could not execute select storages query for metadata" << q.lastError(); } q.first(); int id = q.value(0).toInt(); QMap metadata; Q_FOREACH(const QString &key, storage->metaDataKeys()) { metadata[key] = storage->metaData(key); } addMetaDataForId(metadata, id, "storages"); } } Q_FOREACH(const QString &resourceType, KisResourceLoaderRegistry::instance()->resourceTypes()) { if (!KisResourceCacheDb::addResources(storage, resourceType)) { qWarning() << "Failed to add all resources for storage" << storage; r = false; } if (!KisResourceCacheDb::addTags(storage, resourceType)) { qWarning() << "Failed to add all tags for storage" << storage; } } return r; } bool KisResourceCacheDb::deleteStorage(KisResourceStorageSP storage) { { QSqlQuery q; if (!q.prepare("DELETE FROM resources\n" "WHERE id IN (SELECT versioned_resources.resource_id\n" " FROM versioned_resources\n" " WHERE versioned_resources.storage_id = (SELECT storages.id\n" " FROM storages\n" " WHERE storages.location = :location)\n" " );")) { qWarning() << "Could not prepare delete resources query in deleteStorage" << q.lastError(); return false; } q.bindValue(":location", KisResourceLocator::instance()->makeStorageLocationRelative(storage->location())); if (!q.exec()) { qWarning() << "Could not execute delete resources query in deleteStorage" << q.lastError(); return false; } } { QSqlQuery q; if (!q.prepare("DELETE FROM versioned_resources\n" "WHERE storage_id = (SELECT storages.id\n" " FROM storages\n" " WHERE storages.location = :location);")) { qWarning() << "Could not prepare delete versioned_resources query" << q.lastError(); return false; } q.bindValue(":location", KisResourceLocator::instance()->makeStorageLocationRelative(storage->location())); if (!q.exec()) { qWarning() << "Could not execute delete versioned_resources query" << q.lastError(); return false; } } { QSqlQuery q; if (!q.prepare("DELETE FROM storages\n" "WHERE location = :location;")) { qWarning() << "Could not prepare delete storages query" << q.lastError(); return false; } q.bindValue(":location", KisResourceLocator::instance()->makeStorageLocationRelative(storage->location())); if (!q.exec()) { qWarning() << "Could not execute delete storages query" << q.lastError(); return false; } } return true; } bool KisResourceCacheDb::synchronizeStorage(KisResourceStorageSP storage) { qDebug() << "Going to synchronize" << storage->location(); QTime t; t.start(); QSqlDatabase::database().transaction(); if (!s_valid) { qWarning() << "KisResourceCacheDb::addResource: The database is not valid"; return false; } bool success = true; // Find the storage in the database QSqlQuery q; if (!q.prepare("SELECT id\n" ", timestamp\n" ", pre_installed\n" "FROM storages\n" "WHERE location = :location\n")) { qWarning() << "Could not prepare storage timestamp statement" << q.lastError(); } q.bindValue(":location", KisResourceLocator::instance()->makeStorageLocationRelative(storage->location())); if (!q.exec()) { qWarning() << "Could not execute storage timestamp statement" << q.boundValues() << q.lastError(); } if (!q.first()) { // This is a new storage, the user must have dropped it in the path before restarting Krita, so add it. qDebug() << "Adding storage to the database:" << storage; if (!addStorage(storage, false)) { qWarning() << "Could not add new storage" << storage->name() << "to the database"; success = false; } return true; } // Only check the time stamp for container storages, not the contents if (storage->type() != KisResourceStorage::StorageType::Folder) { qDebug() << storage->location() << "is not a folder, going to check timestamps. Database:" << q.value(1).toInt() << ", File:" << storage->timestamp().toSecsSinceEpoch(); if (!q.value(0).isValid()) { qWarning() << "Could not retrieve timestamp for storage" << KisResourceLocator::instance()->makeStorageLocationRelative(storage->location()); success = false; } if (storage->timestamp().toSecsSinceEpoch() > q.value(1).toInt()) { qDebug() << "Deleting" << storage->location() << "because the one on disk is newer."; if (!deleteStorage(storage)) { qWarning() << "Could not delete storage" << KisResourceLocator::instance()->makeStorageLocationRelative(storage->location()); success = false; } qDebug() << "Inserting" << storage->location(); if (!addStorage(storage, q.value(2).toBool())) { qWarning() << "Could not add storage" << KisResourceLocator::instance()->makeStorageLocationRelative(storage->location()); success = false; } } } else { // This is a folder, we need to check what's on disk and what's in the database // Check whether everything in the storage is in the database QList resourcesToBeDeleted; Q_FOREACH(const QString &resourceType, KisResourceLoaderRegistry::instance()->resourceTypes()) { QStringList resourcesOnDisk; // Check the folder QSharedPointer iter = storage->resources(resourceType); while (iter->hasNext()) { iter->next(); qDebug() << "\tadding resources" << iter->url(); KoResourceSP resource = iter->resource(); resourcesOnDisk << QFileInfo(iter->url()).fileName(); if (resource) { if (!addResource(storage, iter->lastModified(), resource, iter->type())) { qWarning() << "Could not add/update resource" << QFileInfo(resource->filename()).fileName() << "to the database"; success = false; } } } qDebug() << "Checking for" << resourceType << ":" << resourcesOnDisk; QSqlQuery q; q.setForwardOnly(true); if (!q.prepare("SELECT resources.id, resources.filename\n" "FROM resources\n" ", resource_types\n" "WHERE resources.resource_type_id = resource_types.id\n" "AND resource_types.name = :resource_type")) { qWarning() << "Could not prepare resource by type query" << q.lastError(); success = false; continue; } q.bindValue(":resource_type", resourceType); if (!q.exec()) { qWarning() << "Could not exec resource by type query" << q.boundValues() << q.lastError(); success = false; continue; } while (q.next()) { if (!resourcesOnDisk.contains(q.value(1).toString())) { resourcesToBeDeleted << q.value(0).toInt(); } } } QSqlQuery deleteResources; if (!deleteResources.prepare("DELETE FROM resources WHERE id = :id")) { success = false; qWarning() << "Could not prepare delete Resources query"; } QSqlQuery deleteResourceVersions; if (!deleteResourceVersions.prepare("DELETE FROM versioned_resources WHERE resource_id = :id")) { success = false; qWarning() << "Could not prepare delete Resources query"; } Q_FOREACH(int id, resourcesToBeDeleted) { deleteResourceVersions.bindValue(":id", id); if (!deleteResourceVersions.exec()) { success = false; qWarning() << "Could not delete resource version" << deleteResourceVersions.boundValues() << deleteResourceVersions.lastError(); } deleteResources.bindValue(":id", id); if (!deleteResources.exec()) { success = false; qWarning() << "Could not delete resource" << deleteResources.boundValues() << deleteResources.lastError(); } } } QSqlDatabase::database().commit(); qDebug() << "Synchronizing the storages took" << t.msec() << "milliseconds for" << storage->location(); return success; } void KisResourceCacheDb::deleteTemporaryResources() { QSqlDatabase::database().transaction(); QSqlQuery q; if (!q.prepare("DELETE FROM versioned_resources\n" "WHERE storage_id in (SELECT id\n" " FROM storages\n" " WHERE storage_type_id == :storage_type)")) { qWarning() << "Could not prepare delete versioned resources from Unknown or Memory storages query." << q.lastError(); } q.bindValue(":storage_type", (int)KisResourceStorage::StorageType::Memory); if (!q.exec()) { qWarning() << "Could not execute delete versioned resources from Unknown or Memory storages query." << q.lastError(); } if (!q.prepare("DELETE FROM resources\n" "WHERE storage_id in (SELECT id\n" " FROM storages\n" " WHERE storage_type_id == :storage_type)")) { qWarning() << "Could not prepare delete resources from Unknown or Memory storages query." << q.lastError(); } q.bindValue(":storage_type", (int)KisResourceStorage::StorageType::Memory); if (!q.exec()) { qWarning() << "Could not execute delete resources from Unknown or Memory storages query." << q.lastError(); } if (!q.prepare("DELETE FROM versioned_resources\n" "WHERE resource_id IN (SELECT id FROM resources\n" " WHERE temporary = 1)")) { qWarning() << "Could not prepare delete temporary versioned resources query." << q.lastError(); } if (!q.exec()) { qWarning() << "Could not execute delete temporary versioned resources query." << q.lastError(); } if (!q.prepare("DELETE FROM resources\n" "WHERE temporary = 1")) { qWarning() << "Could not prepare delete temporary resources query." << q.lastError(); return; } if (!q.exec()) { qWarning() << "Could not execute delete temporary resources query." << q.lastError(); } if (!q.prepare("DELETE FROM storages\n" "WHERE storage_type_id == :storage_type\n")) { qWarning() << "Could not prepare delete Unknown or Memory storages query." << q.lastError(); } q.bindValue(":storage_type", (int)KisResourceStorage::StorageType::Memory); if (!q.exec()) { qWarning() << "Could not execute delete Unknown or Memory storages query." << q.lastError(); } QSqlDatabase::database().commit(); } QMap KisResourceCacheDb::metaDataForId(int id, const QString &tableName) { QMap map; QSqlQuery q; q.setForwardOnly(true); if (!q.prepare("SELECT key\n" ", value\n" "FROM metadata\n" "WHERE foreign_id = :id\n" "AND table_name = :table")) { qWarning() << "Could not prepare metadata query" << q.lastError(); return map; } q.bindValue(":id", id); q.bindValue(":table", tableName); if (!q.exec()) { qWarning() << "Could not execute metadata query" << q.lastError(); return map; } while (q.next()) { QString key = q.value(0).toString(); QByteArray ba = q.value(1).toByteArray(); QDataStream ds(QByteArray::fromBase64(ba)); QVariant value; ds >> value; map[key] = value; } return map; } bool KisResourceCacheDb::updateMetaDataForId(const QMap map, int id, const QString &tableName) { QSqlDatabase::database().transaction(); { QSqlQuery q; if (!q.prepare("DELETE FROM metadata\n" "WHERE foreign_id = :id\n" "AND table_name = :table\n")) { qWarning() << "Could not prepare delete metadata query" << q.lastError(); return false; } q.bindValue(":id", id); q.bindValue(":table", tableName); if (!q.exec()) { QSqlDatabase::database().rollback(); qWarning() << "Could not execute delete metadata query" << q.lastError(); return false; } } if (addMetaDataForId(map, id, tableName)) { QSqlDatabase::database().commit(); } else { QSqlDatabase::database().rollback(); } return true; } bool KisResourceCacheDb::addMetaDataForId(const QMap map, int id, const QString &tableName) { QSqlQuery q; if (!q.prepare("INSERT INTO metadata\n" "(foreign_id, table_name, key, value)\n" "VALUES\n" "(:id, :table, :key, :value)")) { QSqlDatabase::database().rollback(); qWarning() << "Could not create insert metadata query" << q.lastError(); return false; } QMap::const_iterator iter = map.cbegin(); while (iter != map.cend()) { q.bindValue(":id", id); q.bindValue(":table", tableName); q.bindValue(":key", iter.key()); QVariant v = iter.value(); QByteArray ba; QDataStream ds(&ba, QIODevice::WriteOnly); ds << v; ba = ba.toBase64(); q.bindValue(":value", QString::fromLatin1(ba)); if (!q.exec()) { qWarning() << "Could not insert metadata" << q.lastError(); return false; } ++iter; } return true; } diff --git a/libs/resources/KisResourceModel.cpp b/libs/resources/KisResourceModel.cpp index 15ad4f429e..3e7cdca133 100644 --- a/libs/resources/KisResourceModel.cpp +++ b/libs/resources/KisResourceModel.cpp @@ -1,423 +1,430 @@ /* * Copyright (C) 2018 Boudewijn Rempt * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "KisResourceModel.h" #include #include #include #include #include #include #include struct KisResourceModel::Private { QSqlQuery resourcesQuery; QSqlQuery tagQuery; QString resourceType; int columnCount {9}; int cachedRowCount {-1}; }; //static int s_i = 0; KisResourceModel::KisResourceModel(const QString &resourceType, QObject *parent) : QAbstractTableModel(parent) , d(new Private) { //qDebug() << "ResourceModel" << s_i << resourceType; s_i++; d->resourceType = resourceType; bool r = d->resourcesQuery.prepare("SELECT resources.id\n" ", resources.storage_id\n" ", resources.name\n" ", resources.filename\n" ", resources.tooltip\n" ", resources.thumbnail\n" ", resources.status\n" ", storages.location\n" ", resources.version\n" ", resource_types.name as resource_type\n" "FROM resources\n" ", resource_types\n" ", storages\n" "WHERE resources.resource_type_id = resource_types.id\n" "AND resources.storage_id = storages.id\n" "AND resource_types.name = :resource_type\n" "AND resources.status = 1\n" "AND storages.active = 1"); if (!r) { qWarning() << "Could not prepare KisResourceModel query" << d->resourcesQuery.lastError(); } d->resourcesQuery.bindValue(":resource_type", d->resourceType); resetQuery(); r = d->tagQuery.prepare("SELECT tags.id\n" ", tags.url\n" ", tags.name\n" ", tags.comment\n" "FROM tags\n" ", resource_tags\n" "WHERE tags.active > 0\n" // make sure the tag is active "AND tags.id = resource_tags.tag_id\n" // join tags + resource_tags by tag_id "AND resource_tags.resource_id = :resource_id\n"); // make sure we're looking for tags for a specific resource if (!r) { qWarning() << "Could not prepare TagsForResource query" << d->tagQuery.lastError(); } } KisResourceModel::~KisResourceModel() { delete d; } int KisResourceModel::columnCount(const QModelIndex &/*parent*/) const { return d->columnCount; } QVariant KisResourceModel::data(const QModelIndex &index, int role) const { QVariant v; if (!index.isValid()) return v; if (index.row() > rowCount()) return v; if (index.column() > d->columnCount) return v; bool pos = const_cast(this)->d->resourcesQuery.seek(index.row()); if (pos) { switch(role) { case Qt::DisplayRole: { switch(index.column()) { case Id: return d->resourcesQuery.value("id"); case StorageId: return d->resourcesQuery.value("storage_id"); case Name: return d->resourcesQuery.value("name"); case Filename: return d->resourcesQuery.value("filename"); case Tooltip: return d->resourcesQuery.value("tooltip"); case Image: - ; + { + QByteArray ba = d->resourcesQuery.value("thumbnail").toByteArray(); + QBuffer buf(&ba); + buf.open(QBuffer::ReadOnly); + QImage img; + img.load(&buf, "PNG"); + return QVariant::fromValue(img); + } case Status: return d->resourcesQuery.value("status"); case Location: return d->resourcesQuery.value("location"); case ResourceType: return d->resourcesQuery.value("resource_type"); default: ; }; } case Qt::DecorationRole: { if (index.column() == Image) { QByteArray ba = d->resourcesQuery.value("thumbnail").toByteArray(); QBuffer buf(&ba); buf.open(QBuffer::ReadOnly); QImage img; img.load(&buf, "PNG"); return QVariant::fromValue(img); } return QVariant(); } case Qt::ToolTipRole: /* Falls through. */ case Qt::StatusTipRole: /* Falls through. */ case Qt::WhatsThisRole: return d->resourcesQuery.value("tooltip"); case Qt::UserRole + Id: return d->resourcesQuery.value("id"); case Qt::UserRole + StorageId: return d->resourcesQuery.value("storage_id"); case Qt::UserRole + Name: return d->resourcesQuery.value("name"); case Qt::UserRole + Filename: return d->resourcesQuery.value("filename"); case Qt::UserRole + Tooltip: return d->resourcesQuery.value("tooltip"); case Qt::UserRole + Image: { QByteArray ba = d->resourcesQuery.value("thumbnail").toByteArray(); QBuffer buf(&ba); buf.open(QBuffer::ReadOnly); QImage img; img.load(&buf, "PNG"); return QVariant::fromValue(img); } case Qt::UserRole + Status: return d->resourcesQuery.value("status"); case Qt::UserRole + Location: return d->resourcesQuery.value("location"); case Qt::UserRole + ResourceType: return d->resourcesQuery.value("resource_type"); case Qt::UserRole + Tags: { QVector tags = tagsForResource(d->resourcesQuery.value("id").toInt()); QStringList tagNames; Q_FOREACH(const KisTagSP tag, tags) { tagNames << tag->name(); } return tagNames; } case Qt::UserRole + Dirty: { QString storageLocation = d->resourcesQuery.value("location").toString(); QString filename = d->resourcesQuery.value("filename").toString(); // An uncached resource has not been loaded, so it cannot be dirty if (!KisResourceLocator::instance()->resourceCached(storageLocation, d->resourceType, filename)) { return false; } else { // Now we have to check the resource, but that's cheap since it's been loaded in any case KoResourceSP resource = resourceForIndex(index); return resource->isDirty(); } } case Qt::UserRole + MetaData: { QMap r = KisResourceLocator::instance()->metaDataForResource(d->resourcesQuery.value("id").toInt()); return r; } default: ; } } return v; } //static int s_i2 {0}; KoResourceSP KisResourceModel::resourceForIndex(QModelIndex index) const { KoResourceSP resource = 0; if (!index.isValid()) return resource; if (index.row() > rowCount()) return resource; if (index.column() > d->columnCount) return resource; //qDebug() << "KisResourceModel::resourceForIndex" << s_i2 << d->resourceType; s_i2++; bool pos = const_cast(this)->d->resourcesQuery.seek(index.row()); if (pos) { QString storageLocation = d->resourcesQuery.value("location").toString(); QString filename = d->resourcesQuery.value("filename").toString(); resource = KisResourceLocator::instance()->resource(storageLocation, d->resourceType, filename); resource->setResourceId(d->resourcesQuery.value("id").toInt()); resource->setVersion(d->resourcesQuery.value("version").toInt()); resource->setFilename(filename); resource->setStorageLocation(storageLocation); } return resource; } KoResourceSP KisResourceModel::resourceForId(int id) const { return KisResourceLocator::instance()->resourceForId(id); } //static int s_i3 {0}; QModelIndex KisResourceModel::indexFromResource(KoResourceSP resource) const { if (!resource || !resource->valid()) return QModelIndex(); //qDebug() << "KisResourceModel::indexFromResource" << s_i3 << d->resourceType; s_i3++; // For now a linear seek to find the first resource with the right id d->resourcesQuery.first(); do { if (d->resourcesQuery.value("id").toInt() == resource->resourceId()) { return createIndex(d->resourcesQuery.at(), 0); } } while (d->resourcesQuery.next()); return QModelIndex(); } //static int s_i4 {0}; bool KisResourceModel::removeResource(const QModelIndex &index) { if (index.row() > rowCount()) return false; if (index.column() > d->columnCount) return false; //qDebug() << "KisResourceModel::removeResource" << s_i4 << d->resourceType; s_i4++; bool pos = d->resourcesQuery.seek(index.row()); if (!pos) return false; int resourceId = d->resourcesQuery.value("id").toInt(); if (!KisResourceLocator::instance()->removeResource(resourceId)) { qWarning() << "Failed to remove resource" << resourceId; return false; } return resetQuery(); } //static int s_i5 {0}; bool KisResourceModel::removeResource(KoResourceSP resource) { if (!resource || !resource->valid()) return false; //qDebug() << "KisResourceModel::remvoeResource 2" << s_i5 << d->resourceType; s_i5++; if (!KisResourceLocator::instance()->removeResource(resource->resourceId())) { qWarning() << "Failed to remove resource" << resource->resourceId(); return false; } return resetQuery(); } //static int s_i6 {0}; bool KisResourceModel::importResourceFile(const QString &filename) { //qDebug() << "KisResourceModel::importResource" << s_i6 << d->resourceType; s_i6++; if (!KisResourceLocator::instance()->importResourceFromFile(d->resourceType, filename)) { qWarning() << "Failed to import resource" << filename; return false; } return resetQuery(); } //static int s_i7 {0}; bool KisResourceModel::addResource(KoResourceSP resource, bool save) { if (!resource || !resource->valid()) { qWarning() << "Cannot add resource. Resource is null or not valid"; return false; } //qDebug() << "KisResourceModel::addResource" << s_i7 << d->resourceType; s_i7++; if (!KisResourceLocator::instance()->addResource(d->resourceType, resource, save ? "memory" : "")) { qWarning() << "Failed to add resource" << resource->name(); return false; } return resetQuery(); } //static int s_i8 {0}; bool KisResourceModel::updateResource(KoResourceSP resource) { if (!resource || !resource->valid()) { qWarning() << "Cannot update resource. Resource is null or not valid"; return false; } //qDebug() << "KisResourceModel::updateResource" << s_i8 << d->resourceType; s_i8++; if (!KisResourceLocator::instance()->updateResource(d->resourceType, resource)) { qWarning() << "Failed to update resource"; return false; } return resetQuery(); } //static int s_i9 {0}; bool KisResourceModel::setResourceMetaData(KoResourceSP resource, QMap metadata) { //qDebug() << "KisResourceModel::setResourceMetaData" << s_i9 << d->resourceType; s_i9++; Q_ASSERT(resource->resourceId() > -1); return KisResourceLocator::instance()->setMetaDataForResource(resource->resourceId(), metadata); } bool KisResourceModel::resetQuery() { QTime t; t.start(); beginResetModel(); bool r = d->resourcesQuery.exec(); if (!r) { qWarning() << "Could not select" << d->resourceType << "resources" << d->resourcesQuery.lastError() << d->resourcesQuery.boundValues(); } d->cachedRowCount = -1; endResetModel(); emit afterResourcesLayoutReset(); qDebug() << "KisResourceModel::resetQuery for" << d->resourceType << "took" << t.elapsed() << "ms"; return r; } QVector KisResourceModel::tagsForResource(int resourceId) const { d->tagQuery.bindValue(":resource_id", resourceId); bool r = d->tagQuery.exec(); if (!r) { qWarning() << "Could not select tags for" << resourceId << d->tagQuery.lastError() << d->tagQuery.boundValues(); } QVector tags; while (d->tagQuery.next()) { //qDebug() << d->tagQuery.value(0).toString() << d->tagQuery.value(1).toString() << d->tagQuery.value(2).toString(); KisTagSP tag(new KisTag()); tag->setId(d->tagQuery.value("id").toInt()); tag->setUrl(d->tagQuery.value("url").toString()); tag->setName(d->tagQuery.value("name").toString()); tag->setComment(d->tagQuery.value("comment").toString()); tag->setValid(true); tag->setActive(true); tags << tag; } return tags; } int KisResourceModel::rowCount(const QModelIndex &) const { if (d->cachedRowCount < 0) { QSqlQuery q; q.prepare("SELECT count(*)\n" "FROM resources\n" ", resource_types\n" ", storages\n" "WHERE resources.resource_type_id = resource_types.id\n" "AND resource_types.name = :resource_type\n" "AND resources.storage_id = storages.id\n" "AND resources.status = 1\n" "AND storages.active = 1"); q.bindValue(":resource_type", d->resourceType); q.exec(); q.first(); const_cast(this)->d->cachedRowCount = q.value(0).toInt(); } return d->cachedRowCount; } diff --git a/libs/resources/KisResourceStorage.cpp b/libs/resources/KisResourceStorage.cpp index 1e688608cd..b00f001f6a 100644 --- a/libs/resources/KisResourceStorage.cpp +++ b/libs/resources/KisResourceStorage.cpp @@ -1,201 +1,206 @@ /* * Copyright (C) 2018 Boudewijn Rempt * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "KisResourceStorage.h" #include #include #include #include "KisFolderStorage.h" #include "KisBundleStorage.h" #include "KisMemoryStorage.h" const QString KisResourceStorage::s_meta_generator("meta:generator"); const QString KisResourceStorage::s_meta_author("dc:author"); const QString KisResourceStorage::s_meta_title("dc:title"); const QString KisResourceStorage::s_meta_description("dc:description"); const QString KisResourceStorage::s_meta_initial_creator("meta:initial-creator"); const QString KisResourceStorage::s_meta_creator("cd:creator"); const QString KisResourceStorage::s_meta_creation_date("meta:creation-data"); const QString KisResourceStorage::s_meta_dc_date("meta:dc-date"); const QString KisResourceStorage::s_meta_user_defined("meta:meta-userdefined"); const QString KisResourceStorage::s_meta_name("meta:name"); const QString KisResourceStorage::s_meta_value("meta:value"); const QString KisResourceStorage::s_meta_version("meta:bundle-version"); Q_GLOBAL_STATIC(KisStoragePluginRegistry, s_instance); KisStoragePluginRegistry::KisStoragePluginRegistry() { m_storageFactoryMap[KisResourceStorage::StorageType::Folder] = new KisStoragePluginFactory(); m_storageFactoryMap[KisResourceStorage::StorageType::Memory] = new KisStoragePluginFactory(); m_storageFactoryMap[KisResourceStorage::StorageType::Bundle] = new KisStoragePluginFactory(); } void KisStoragePluginRegistry::addStoragePluginFactory(KisResourceStorage::StorageType storageType, KisStoragePluginFactoryBase *factory) { m_storageFactoryMap[storageType] = factory; } KisStoragePluginRegistry *KisStoragePluginRegistry::instance() { return s_instance; } class KisResourceStorage::Private { public: QString name; QString location; bool valid {false}; KisResourceStorage::StorageType storageType {KisResourceStorage::StorageType::Unknown}; QSharedPointer storagePlugin; }; KisResourceStorage::KisResourceStorage(const QString &location) : d(new Private()) { d->location = location; d->name = QFileInfo(d->location).fileName(); QFileInfo fi(d->location); if (fi.isDir()) { d->storagePlugin.reset(KisStoragePluginRegistry::instance()->m_storageFactoryMap[StorageType::Folder]->create(location)); d->storageType = StorageType::Folder; d->valid = fi.isWritable(); } else if (d->location.endsWith(".bundle")) { d->storagePlugin.reset(KisStoragePluginRegistry::instance()->m_storageFactoryMap[StorageType::Bundle]->create(location)); d->storageType = StorageType::Bundle; // XXX: should we also check whether there's a valid metadata entry? Or is this enough? d->valid = (fi.isReadable() && QuaZip(d->location).open(QuaZip::mdUnzip)); } else if (d->location.endsWith(".abr")) { d->storagePlugin.reset(KisStoragePluginRegistry::instance()->m_storageFactoryMap[StorageType::AdobeBrushLibrary]->create(location)); d->storageType = StorageType::AdobeBrushLibrary; d->valid = fi.isReadable(); } else if (d->location.endsWith(".asl")) { d->storagePlugin.reset(KisStoragePluginRegistry::instance()->m_storageFactoryMap[StorageType::AdobeStyleLibrary]->create(location)); d->storageType = StorageType::AdobeStyleLibrary; d->valid = fi.isReadable(); } else if (!d->location.isEmpty()) { d->storagePlugin.reset(KisStoragePluginRegistry::instance()->m_storageFactoryMap[StorageType::Memory]->create(location)); d->name = location; d->storageType = StorageType::Memory; d->valid = true; } } KisResourceStorage::~KisResourceStorage() { } KisResourceStorage::KisResourceStorage(const KisResourceStorage &rhs) : d(new Private) { *this = rhs; } KisResourceStorage &KisResourceStorage::operator=(const KisResourceStorage &rhs) { if (this != &rhs) { d->name = rhs.d->name; d->location = rhs.d->location; d->storageType = rhs.d->storageType; if (d->storageType == StorageType::Memory) { d->storagePlugin = QSharedPointer(new KisMemoryStorage(*dynamic_cast(rhs.d->storagePlugin.data()))); } else { d->storagePlugin = rhs.d->storagePlugin; } d->valid = false; } return *this; } KisResourceStorageSP KisResourceStorage::clone() const { return KisResourceStorageSP(new KisResourceStorage(*this)); } QString KisResourceStorage::name() const { return d->name; } QString KisResourceStorage::location() const { return d->location; } KisResourceStorage::StorageType KisResourceStorage::type() const { return d->storageType; } +QImage KisResourceStorage::thumbnail() const +{ + return d->storagePlugin->thumbnail(); +} + QDateTime KisResourceStorage::timestamp() const { return d->storagePlugin->timestamp(); } KisResourceStorage::ResourceItem KisResourceStorage::resourceItem(const QString &url) { return d->storagePlugin->resourceItem(url); } KoResourceSP KisResourceStorage::resource(const QString &url) { return d->storagePlugin->resource(url); } QSharedPointer KisResourceStorage::resources(const QString &resourceType) const { return d->storagePlugin->resources(resourceType); } QSharedPointer KisResourceStorage::tags(const QString &resourceType) const { return d->storagePlugin->tags(resourceType); } bool KisResourceStorage::addTag(const QString &resourceType, KisTagSP tag) { return d->storagePlugin->addTag(resourceType, tag); } bool KisResourceStorage::addResource(KoResourceSP resource) { return d->storagePlugin->addResource(resource->resourceType().first, resource); } bool KisResourceStorage::valid() const { return d->valid; } QStringList KisResourceStorage::metaDataKeys() const { return d->storagePlugin->metaDataKeys(); } QVariant KisResourceStorage::metaData(const QString &key) const { return d->storagePlugin->metaData(key); } diff --git a/libs/resources/KisResourceStorage.h b/libs/resources/KisResourceStorage.h index 85f3729f39..fd6dd73f12 100644 --- a/libs/resources/KisResourceStorage.h +++ b/libs/resources/KisResourceStorage.h @@ -1,234 +1,237 @@ /* * Copyright (C) 2018 Boudewijn Rempt * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef KISRESOURCESTORAGE_H #define KISRESOURCESTORAGE_H #include #include #include #include #include #include #include #include #include class KisStoragePlugin; class KRITARESOURCES_EXPORT KisStoragePluginFactoryBase { public: virtual ~KisStoragePluginFactoryBase(){} virtual KisStoragePlugin *create(const QString &/*location*/) { return 0; } }; template class KRITARESOURCES_EXPORT KisStoragePluginFactory : public KisStoragePluginFactoryBase { public: KisStoragePlugin *create(const QString &location) override { return new T(location); } }; class KisResourceStorage; typedef QSharedPointer KisResourceStorageSP; /** * The KisResourceStorage class is the base class for * places where resources can be stored. Examples are * folders, bundles or Adobe resource libraries like * ABR files. */ class KRITARESOURCES_EXPORT KisResourceStorage { public: /// A resource item is simply an entry in the storage, struct ResourceItem { virtual ~ResourceItem() {} QString url; QString folder; QString resourceType; QDateTime lastModified; }; class TagIterator { public: virtual ~TagIterator() {} virtual bool hasNext() const = 0; /// The iterator is only valid if next() has been called at least once. virtual void next() = 0; /// The untranslated name of the tag, to be used for making connections to resources virtual QString url() const = 0; /// The translated name of the tag, to be shown in the GUI virtual QString name() const = 0; /// An extra, optional comment for the tag virtual QString comment() const = 0; /// A tag object on which we can set properties and which we can save virtual KisTagSP tag() const = 0; }; class ResourceIterator { public: virtual ~ResourceIterator() {} virtual bool hasNext() const = 0; /// The iterator is only valid if next() has been called at least once. virtual void next() = 0; virtual QString url() const = 0; virtual QString type() const = 0; virtual QDateTime lastModified() const = 0; /// This only loads the resource when called virtual KoResourceSP resource() const = 0; }; enum class StorageType : int { Unknown = 1, Folder = 2, Bundle = 3, AdobeBrushLibrary = 4, AdobeStyleLibrary = 5, Memory = 6 }; static QString storageTypeToString(StorageType storageType) { switch (storageType) { case StorageType::Unknown: return i18n("Unknown"); case StorageType::Folder: return i18n("Folder"); case StorageType::Bundle: return i18n("Bundle"); case StorageType::AdobeBrushLibrary: return i18n("Adobe Brush Library"); case StorageType::AdobeStyleLibrary: return i18n("Adobe Style Library"); case StorageType::Memory: return i18n("Memory"); default: return i18n("Invalid"); } } KisResourceStorage(const QString &location); ~KisResourceStorage(); KisResourceStorage(const KisResourceStorage &rhs); KisResourceStorage &operator=(const KisResourceStorage &rhs); KisResourceStorageSP clone() const; /// The filename of the storage if it's a bundle or Adobe Library. This can /// also be empty (for the folder storage) or "memory" for the storage for /// temporary resources, a UUID for storages associated with documents. QString name() const; /// The absolute location of the storage QString location() const; /// true if the storage exists and can be used bool valid() const; /// The type of the storage StorageType type() const; + /// The icond for the storage + QImage thumbnail() const; + /// The time and date when the storage was last modified, or created /// for memory storages. QDateTime timestamp() const; /// And entry in the storage; this is not the loaded resource ResourceItem resourceItem(const QString &url); /// The loaded resource for an entry in the storage KoResourceSP resource(const QString &url); /// An iterator over all the resources in the storage QSharedPointer resources(const QString &resourceType) const; /// An iterator over all the tags in the resource QSharedPointer tags(const QString &resourceType) const; /// Adds a tag to the storage, however, it does not store the links between /// tags and resources. bool addTag(const QString &resourceType, KisTagSP tag); /// Adds the given resource to the storage. bool addResource(KoResourceSP resource); static const QString s_meta_generator; static const QString s_meta_author; static const QString s_meta_title; static const QString s_meta_description; static const QString s_meta_initial_creator; static const QString s_meta_creator; static const QString s_meta_creation_date; static const QString s_meta_dc_date; static const QString s_meta_user_defined; static const QString s_meta_name; static const QString s_meta_value; static const QString s_meta_version; QStringList metaDataKeys() const; QVariant metaData(const QString &key) const; private: class Private; QScopedPointer d; }; inline QDebug operator<<(QDebug dbg, const KisResourceStorageSP storage) { dbg.nospace() << "[RESOURCESTORAGE] Name: " << storage->name() << " Version: " << storage->location() << " Valid: " << storage->valid() << " Storage: " << KisResourceStorage::storageTypeToString(storage->type()) << " Timestamp: " << storage->timestamp(); return dbg.space(); } class KRITARESOURCES_EXPORT KisStoragePluginRegistry { public: KisStoragePluginRegistry(); void addStoragePluginFactory(KisResourceStorage::StorageType storageType, KisStoragePluginFactoryBase *factory); static KisStoragePluginRegistry *instance(); private: friend class KisResourceStorage; QMap m_storageFactoryMap; }; #endif // KISRESOURCESTORAGE_H diff --git a/libs/resources/KisStorageModel.cpp b/libs/resources/KisStorageModel.cpp index 0418646712..f4308c3143 100644 --- a/libs/resources/KisStorageModel.cpp +++ b/libs/resources/KisStorageModel.cpp @@ -1,168 +1,187 @@ /* * Copyright (c) 2019 boud * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "KisStorageModel.h" #include Q_GLOBAL_STATIC(KisStorageModel, s_instance) struct KisStorageModel::Private { int cachedRowCount {-1}; QSqlQuery query; }; KisStorageModel::KisStorageModel(QObject *parent) : QAbstractTableModel(parent) , d(new Private()) { prepareQuery(); } KisStorageModel *KisStorageModel::instance() { return s_instance; } KisStorageModel::~KisStorageModel() { } int KisStorageModel::rowCount(const QModelIndex & /*parent*/) const { if (d->cachedRowCount < 0) { QSqlQuery q; q.prepare("SELECT count(*)\n" "FROM storages\n"); q.exec(); q.first(); const_cast(this)->d->cachedRowCount = q.value(0).toInt(); } return d->cachedRowCount; } int KisStorageModel::columnCount(const QModelIndex &/*parent*/) const { return 6; } QVariant KisStorageModel::data(const QModelIndex &index, int role) const { QVariant v; if (!index.isValid()) return v; if (index.row() > rowCount()) return v; if (index.column() > (int)Active) return v; bool pos = d->query.seek(index.row()); if (pos) { switch(role) { case Qt::DisplayRole: { switch(index.column()) { case Id: return d->query.value("id"); case StorageType: return d->query.value("storage_type"); case Location: return d->query.value("location"); case TimeStamp: return d->query.value("timestamp"); case PreInstalled: return d->query.value("pre_installed"); case Active: return d->query.value("active"); + case Thumbnail: + { + QByteArray ba = d->query.value("thumbnail").toByteArray(); + QBuffer buf(&ba); + buf.open(QBuffer::ReadOnly); + QImage img; + img.load(&buf, "PNG"); + return QVariant::fromValue(img); + } default: return v; } } case Qt::UserRole + Id: return d->query.value("id"); case Qt::UserRole + StorageType: return d->query.value("storage_type"); case Qt::UserRole + Location: return d->query.value("location"); case Qt::UserRole + TimeStamp: return d->query.value("timestamp"); case Qt::UserRole + PreInstalled: return d->query.value("pre_installed"); case Qt::UserRole + Active: return d->query.value("active"); + case Qt::UserRole + Thumbnail: + { + QByteArray ba = d->query.value("thumbnail").toByteArray(); + QBuffer buf(&ba); + buf.open(QBuffer::ReadOnly); + QImage img; + img.load(&buf, "PNG"); + return QVariant::fromValue(img); + } default: ; } } return v; } bool KisStorageModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.isValid()) { if (role == Qt::CheckStateRole) { QSqlQuery q; bool r = q.prepare("UPDATE storages\n" "SET active = :active\n" "WHERE id = :id\n"); q.bindValue(":active", value); q.bindValue(":id", index.data(Qt::UserRole + Id)); if (!r) { qWarning() << "Could not prepare KisStorageModel update query" << d->query.lastError(); return false; } r = q.exec(); if (!r) { qWarning() << "Could not execute KisStorageModel update query" << d->query.lastError(); return false; } } } QAbstractTableModel::setData(index, value, role); return prepareQuery(); } Qt::ItemFlags KisStorageModel::flags(const QModelIndex &index) const { return QAbstractTableModel::flags(index) | Qt::ItemIsEditable; } bool KisStorageModel::prepareQuery() { beginResetModel(); bool r = d->query.prepare("SELECT storages.id as id\n" ", storage_types.name as storage_type\n" ", location\n" ", timestamp\n" ", pre_installed\n" ", active\n" + ", thumbnail\n" "FROM storages\n" ", storage_types\n" "WHERE storages.storage_type_id = storage_types.id\n"); if (!r) { qWarning() << "Could not prepare KisStorageModel query" << d->query.lastError(); } r = d->query.exec(); if (!r) { qWarning() << "Could not execute KisStorageModel query" << d->query.lastError(); } d->cachedRowCount = -1; endResetModel(); return r; } diff --git a/libs/resources/KisStorageModel.h b/libs/resources/KisStorageModel.h index 19e1f47ec0..338d3fc021 100644 --- a/libs/resources/KisStorageModel.h +++ b/libs/resources/KisStorageModel.h @@ -1,64 +1,65 @@ /* * Copyright (c) 2019 boud * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KISSTORAGEMODEL_H #define KISSTORAGEMODEL_H #include #include #include #include "kritaresources_export.h" class KRITARESOURCES_EXPORT KisStorageModel : public QAbstractTableModel { public: enum Columns { Id = 0, StorageType, Location, TimeStamp, PreInstalled, - Active + Active, + Thumbnail }; static KisStorageModel * instance(); KisStorageModel(QObject *parent = 0); ~KisStorageModel() override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role) const override; bool setData(const QModelIndex &index, const QVariant &value, int role) override; Qt::ItemFlags flags(const QModelIndex &index) const override; private: KisStorageModel(const KisStorageModel&); KisStorageModel operator=(const KisStorageModel&); bool prepareQuery(); struct Private; QScopedPointer d; }; #endif // KISSTORAGEMODEL_H diff --git a/libs/resources/sql/create_storages.sql b/libs/resources/sql/create_storages.sql index e1d9500e3c..54186a18b3 100644 --- a/libs/resources/sql/create_storages.sql +++ b/libs/resources/sql/create_storages.sql @@ -1,10 +1,11 @@ CREATE TABLE IF NOT EXISTS storages ( id INTEGER PRIMARY KEY AUTOINCREMENT , storage_type_id INTEGER , location TEXT , timestamp INTEGER , pre_installed INTEGER , active INTEGER +, thumbnail BLOB /* the image representing the storage visually*/ , FOREIGN KEY(storage_type_id) REFERENCES storage_types(id) , UNIQUE(location) );