Commit 07a45e59 authored by Boudewijn Rempt's avatar Boudewijn Rempt

Refactor KoResourceLoader to be template based

parent 56dc6e9b
......@@ -42,13 +42,13 @@ const QStringList KisResourceCacheDb::resourceTypes = QStringList() << "BRUSH_TI
<< "SESSION"
<< "UNKNOWN";
const QStringList KisResourceCacheDb::originTypes = QStringList() << "INSTALLATION" // Installed by Krita
<< "BUNDLE" // Bundle installed by the user
<< "ADOBE_LIBRARY" // ABR or ASL or similar Adobe resource library
<< "USER"; // Installed or created by the user
const QStringList KisResourceCacheDb::storageTypes = QStringList() << "FOLDER"
<< "BUNDLE"
<< "ADOBE_BRUSH_LIBRARY"
<< "ADOBE_STYLE_LIBRARY"; // Installed or created by the user
const QString KisResourceCacheDb::dbLocationKey {"ResourceCacheDbDirectory"};
const QString KisResourceCacheDb::ResourceCacheDbFilename {"resourcecache.sqlite"};
const QString KisResourceCacheDb::resourceCacheDbFilename {"resourcecache.sqlite"};
const QString KisResourceCacheDb::databaseVersion {"0.0.1"};
......@@ -100,7 +100,7 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
}
QSqlDatabase db = QSqlDatabase::addDatabase(dbDriver);
db.setDatabaseName(location + "/" + ResourceCacheDbFilename);
db.setDatabaseName(location + "/" + resourceCacheDbFilename);
if (!db.open()) {
infoResources << "Could not connect to resource cache database";
......@@ -189,7 +189,7 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
QFile f(":/fill_origin_types.sql");
if (f.open(QFile::ReadOnly)) {
QString sql = f.readAll();
Q_FOREACH(const QString &originType, originTypes) {
Q_FOREACH(const QString &originType, storageTypes) {
QSqlQuery query(sql);
query.addBindValue(originType);
if (!query.exec()) {
......
......@@ -36,9 +36,11 @@ class KRITARESOURCES_EXPORT KisResourceCacheDb
{
public:
static const QString dbLocationKey;
static const QString ResourceCacheDbFilename;
static const QString databaseVersion;
static const QString dbLocationKey; ///< configuration key for the location of the database
static const QString resourceCacheDbFilename; ///< filename of the database
static const QString databaseVersion; ///< current schema version
static const QStringList resourceTypes; ///< all types of resources
static const QStringList storageTypes; ///< kinds of places where resources can be stored
/**
* @brief KisResourceCacheDb create a resource cache database.
......@@ -61,9 +63,6 @@ public:
private:
friend class TestResourceCacheDb;
static const QStringList resourceTypes;
static const QStringList originTypes;
class Private;
QScopedPointer<Private> d;
......
......@@ -19,62 +19,3 @@
#include "KisResourceLoader.h"
class KisResourceLoader::Private
{
public:
QString folder;
QString type;
QString name;
QImage thumbnail {QString("UNKNOWN")};
QStringList mimetypes;
KoResourceSP resource {0};
};
KisResourceLoader::KisResourceLoader(const QString &folder, const QStringList &mimetypes)
: d(new Private())
{
d->folder = folder;
d->mimetypes = mimetypes;
}
KisResourceLoader::~KisResourceLoader()
{
}
QStringList KisResourceLoader::mimetypes() const
{
return d->mimetypes;
}
QString KisResourceLoader::folder() const
{
return d->folder;
}
KoResourceSP KisResourceLoader::resource() const
{
return d->resource;
}
void KisResourceLoader::setType(const QString &type)
{
d->type = type;
}
void KisResourceLoader::setThumbnail(const QImage &thumbnail)
{
d->thumbnail = thumbnail;
}
void KisResourceLoader::setName(const QString &name)
{
d->name = name;
}
void KisResourceLoader::setResource(KoResourceSP resource)
{
d->resource = resource;
}
......@@ -36,62 +36,80 @@
* class that must be implemented by actual resource classes and
* registered with the KisResourceLoaderRegistry.
*/
class KRITARESOURCES_EXPORT KisResourceLoader
class KRITARESOURCES_EXPORT KisResourceLoaderBase
{
public:
KisResourceLoader(const QString &folder, const QStringList &mimetypes);
virtual ~KisResourceLoader();
KisResourceLoaderBase(const QString &id, const QString &folder, const QStringList &mimetypes)
{
m_id = id;
m_folder = folder;
m_mimetypes = mimetypes;
}
virtual ~KisResourceLoaderBase()
{
}
/**
* @return the mimetypes this resource can load
*/
QStringList mimetypes() const;
QStringList mimetypes() const
{
return m_mimetypes;
}
/**
* @return the folder in the resource storage where resources
* of this type are located
*/
QString folder() const;
QString folder() const
{
return m_folder;
}
QString resourceType() const
{
return id();
}
/// For registration in KisResourceLoaderRegistry
virtual QString id() const = 0;
QString id() const
{
return m_id;
}
/**
* Load this resource.
* @return true if loading the resource succeeded.
*/
virtual bool load(QIODevice &dev) = 0;
/**
* Save this resource.
*@return true if saving the resource succeeded.
*/
virtual bool save(QIODevice &dev) const = 0;
QImage thumbnail() const;
QString name() const;
KoResourceSP resource() const;
protected:
void setType(const QString &type);
void setThumbnail(const QImage &thumbnail);
void setName(const QString &name);
void setResource(KoResourceSP resource);
virtual KoResourceSP load(const QString &name, QIODevice &dev) { Q_UNUSED(name); Q_UNUSED(dev); return 0; };
private:
QString m_id;
QString m_folder;
QStringList m_mimetypes;
class Private;
QScopedPointer<Private> d;
};
typedef QSharedPointer<KisResourceLoader> KisResourceLoaderSP;
template<typename T>
class KRITARESOURCES_EXPORT KisResourceLoader : public KisResourceLoaderBase {
public:
KisResourceLoader(const QString &id, const QString &folder, const QStringList &mimetypes)
: KisResourceLoaderBase(id, folder, mimetypes)
{
}
KoResourceSP load(const QString &name, QIODevice &dev) override
{
QSharedPointer<T> resource = QSharedPointer<T>::create(name);
if (resource->loadFromDevice(&dev)) {
return resource;
}
return 0;
}
};
#endif // KISRESOURCELOADER_H
......@@ -28,6 +28,7 @@ KisResourceLoaderRegistry::KisResourceLoaderRegistry(QObject *parent)
KisResourceLoaderRegistry::~KisResourceLoaderRegistry()
{
qDeleteAll(values());
}
KisResourceLoaderRegistry* KisResourceLoaderRegistry::instance()
......
......@@ -26,7 +26,7 @@
#include <kritaresources_export.h>
class KRITARESOURCES_EXPORT KisResourceLoaderRegistry : public QObject, public KoGenericRegistry<KisResourceLoaderSP>
class KRITARESOURCES_EXPORT KisResourceLoaderRegistry : public QObject, public KoGenericRegistry<KisResourceLoaderBase*>
{
Q_OBJECT
public:
......
......@@ -38,6 +38,8 @@
#include "KisResourceCacheDb.h"
const QString KisResourceLocator::resourceLocationKey {"ResourceDirectory"};
const QStringList KisResourceLocator::resourceTypeFolders = QStringList()
<< "tags"
<< "asl"
......
......@@ -59,7 +59,6 @@ KisResourceStorage::KisResourceStorage(const QString &location)
KisResourceStorage::~KisResourceStorage()
{
}
QString KisResourceStorage::location() const
......@@ -72,6 +71,16 @@ KisResourceStorage::StorageType KisResourceStorage::type() const
return d->storageType;
}
KoResourceSP KisResourceStorage::resource(const QString &url)
{
return 0;
}
QVector<KoResourceSP> KisResourceStorage::resources(const QString &resourceType)
{
return QVector<KoResourceSP>();
}
bool KisResourceStorage::valid() const
{
......
......@@ -23,6 +23,10 @@
#include <QSharedPointer>
#include <QScopedPointer>
#include <QString>
#include <QDateTime>
#include <KoResource.h>
#include <kritaresources_export.h>
......@@ -36,6 +40,11 @@ class KRITARESOURCES_EXPORT KisResourceStorage
{
public:
struct ResourceItem {
QString url;
QDateTime lastModified;
};
enum class StorageType {
Unknown,
Folder,
......@@ -51,6 +60,9 @@ public:
bool valid() const;
StorageType type() const;
KoResourceSP resource(const QString &url);
QVector<KoResourceSP> resources(const QString &resourceType);
private:
class Private;
QScopedPointer<Private> d;
......
......@@ -3,5 +3,9 @@ CREATE TABLE IF NOT EXISTS storages (
, origin_type_id INTEGER
, name TEXT
, location TEXT
, datestamp TEXT
, checksum TEXT
, pre_installed INTEGER
, active INTEGER
, FOREIGN KEY(origin_type_id) REFERENCES origin_types(id)
);
......@@ -29,7 +29,7 @@ void TestResourceCacheDb::initTestCase()
{
QDir dbLocation(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
if (dbLocation.exists()) {
QFile(dbLocation.path() + "/" + KisResourceCacheDb::ResourceCacheDbFilename).remove();
QFile(dbLocation.path() + "/" + KisResourceCacheDb::resourceCacheDbFilename).remove();
dbLocation.rmpath(dbLocation.path());
}
}
......@@ -70,7 +70,7 @@ void TestResourceCacheDb::testLookupTables()
QVERIFY(r);
QVERIFY(query.lastError() == QSqlError());
query.first();
QVERIFY(query.value(0).toInt() == KisResourceCacheDb::originTypes.count());
QVERIFY(query.value(0).toInt() == KisResourceCacheDb::storageTypes.count());
r = query.exec("SELECT COUNT(*) FROM resource_types");
QVERIFY(r);
......@@ -82,7 +82,7 @@ void TestResourceCacheDb::testLookupTables()
void TestResourceCacheDb::cleanupTestCase()
{
QDir dbLocation(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
bool res = QFile(dbLocation.path() + "/" + KisResourceCacheDb::ResourceCacheDbFilename).remove();
bool res = QFile(dbLocation.path() + "/" + KisResourceCacheDb::resourceCacheDbFilename).remove();
Q_ASSERT(res);
res = dbLocation.rmpath(dbLocation.path());
Q_ASSERT(res);
......
......@@ -25,39 +25,29 @@
#include <KisResourceLoaderRegistry.h>
#include <KisResourceLoader.h>
#include <KoResource.h>
class DummyResourceLoader : public KisResourceLoader {
class Dummy : public KoResource {
public:
DummyResourceLoader()
: KisResourceLoader("dummy", QStringList() << "x-application/dummy")
{
setType("UNKNOWN");
}
QString id() const override
{
return "dummy";
}
bool load(QIODevice&) override
{
return true;
}
bool save(QIODevice&) const override
{
return true;
}
Dummy(const QString &f) : KoResource(f) {}
bool load() override { return true; }
bool loadFromDevice(QIODevice *) override { return true; }
bool save() override { return true; }
};
void TestResourceLoaderRegistry::testRegistry()
{
KisResourceLoaderRegistry *reg = KisResourceLoaderRegistry::instance();
QSharedPointer<KisResourceLoader> loader(new DummyResourceLoader);
KisResourceLoader<Dummy> *loader = new KisResourceLoader<Dummy>("dummy", "dummy", QStringList() << "x-dummy");
reg->add(loader);
QVERIFY(reg->count() == 1);
KisResourceLoaderBase *l2 = reg->get("dummy");
QFile f;
KoResourceSP res = l2->load("test", f);
QVERIFY(res.data());
QVERIFY(dynamic_cast<Dummy*>(res.data()));
}
QTEST_MAIN(TestResourceLoaderRegistry)
......
......@@ -40,37 +40,38 @@
void TestResourceLocator::initTestCase()
{
srcLocation = QString(FILES_DATA_DIR);
QVERIFY2(QDir(srcLocation).exists(), srcLocation.toUtf8());
dstLocation = QString(FILES_DEST_DIR);
m_srcLocation = QString(FILES_DATA_DIR);
QVERIFY2(QDir(m_srcLocation).exists(), m_srcLocation.toUtf8());
m_dstLocation = QString(FILES_DEST_DIR);
cleanDstLocation();
KConfigGroup cfg(KSharedConfig::openConfig(), "");
cfg.writeEntry(KisResourceLocator::resourceLocationKey, dstLocation);
cfg.writeEntry(KisResourceLocator::resourceLocationKey, m_dstLocation);
}
void TestResourceLocator::testLocator()
void TestResourceLocator::testLocatorInitalization()
{
KisResourceLocator locator;
KisResourceLocator::LocatorError r = locator.initialize(srcLocation);
if (!locator.errorMessages().isEmpty()) qDebug() << locator.errorMessages();
KisResourceLocator::LocatorError r = m_locator.initialize(m_srcLocation);
if (!m_locator.errorMessages().isEmpty()) qDebug() << m_locator.errorMessages();
QVERIFY(r == KisResourceLocator::LocatorError::Ok);
QVERIFY(QDir(dstLocation).exists());
QVERIFY(QDir(m_dstLocation).exists());
Q_FOREACH(const QString &folder, KisResourceLocator::resourceTypeFolders) {
QDir dstDir(dstLocation + '/' + folder + '/');
QDir srcDir(srcLocation + '/' + folder + '/');
QDir dstDir(m_dstLocation + '/' + folder + '/');
QDir srcDir(m_srcLocation + '/' + folder + '/');
QVERIFY(dstDir.exists());
QVERIFY(dstDir.entryList(QDir::Files | QDir::NoDotAndDotDot) == srcDir.entryList(QDir::Files | QDir::NoDotAndDotDot));
}
QFile f(dstLocation + '/' + "KRITA_RESOURCE_VERSION");
QFile f(m_dstLocation + '/' + "KRITA_RESOURCE_VERSION");
QVERIFY(f.exists());
f.open(QFile::ReadOnly);
QVersionNumber version = QVersionNumber::fromString(QString::fromUtf8(f.readAll()));
QVERIFY(version == QVersionNumber::fromString(KritaVersionWrapper::versionString()));
}
locator.synchronizeDb();
void TestResourceLocator::testLocatorSynchronization()
{
QVERIFY(m_locator.synchronizeDb());
}
void TestResourceLocator::cleanupTestCase()
......@@ -80,26 +81,26 @@ void TestResourceLocator::cleanupTestCase()
bool TestResourceLocator::cleanDstLocation()
{
if (QDir(dstLocation).exists()) {
if (QDir(m_dstLocation).exists()) {
{
QDirIterator iter(dstLocation, QStringList() << "*", QDir::Files, QDirIterator::Subdirectories);
QDirIterator iter(m_dstLocation, QStringList() << "*", QDir::Files, QDirIterator::Subdirectories);
while (iter.hasNext()) {
iter.next();
QFile f(iter.filePath());
bool r = f.remove();
f.remove();
//qDebug() << (r ? "Removed" : "Failed to remove") << iter.filePath();
}
}
{
QDirIterator iter(dstLocation, QStringList() << "*", QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
QDirIterator iter(m_dstLocation, QStringList() << "*", QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
while (iter.hasNext()) {
iter.next();
bool r = QDir(iter.filePath()).rmpath(iter.filePath());
QDir(iter.filePath()).rmpath(iter.filePath());
//qDebug() << (r ? "Removed" : "Failed to remove") << iter.filePath();
}
}
return QDir().rmpath(dstLocation);
return QDir().rmpath(m_dstLocation);
}
return true;
}
......
......@@ -22,19 +22,25 @@
#include <QObject>
#include <KisResourceLocator.h>
class TestResourceLocator : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void testLocator();
void testLocatorInitalization();
void testLocatorSynchronization();
void cleanupTestCase();
private:
bool cleanDstLocation();
QString srcLocation;
QString dstLocation;
QString m_srcLocation;
QString m_dstLocation;
KisResourceLocator m_locator;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment