Commit 3a9bfd4b authored by Boudewijn Rempt's avatar Boudewijn Rempt

Implement the FolderStorage component

parent c6fd5ebd
......@@ -33,6 +33,7 @@ target_link_libraries(kritaresources
Qt5::Sql
kritaversion
kritaglobal
kritaplugin
KF5::ConfigCore
KF5::CoreAddons
KF5::I18n
......
......@@ -35,10 +35,6 @@ KisResourceStorage::ResourceItem KisAbrStorage::resourceItem(const QString &url)
return KisResourceStorage::ResourceItem();
}
KisResourceStorage::ResourceItemIterator KisAbrStorage::resourceItems(const QString &resourceType)
{
return KisResourceStorage::ResourceItemIterator();
}
KoResourceSP KisAbrStorage::resource(const QString &url)
{
......
......@@ -29,7 +29,6 @@ public:
virtual ~KisAbrStorage();
KisResourceStorage::ResourceItem resourceItem(const QString &url) override;
KisResourceStorage::ResourceItemIterator resourceItems(const QString &resourceType) override;
KoResourceSP resource(const QString &url) override;
KisResourceStorage::ResourceIterator resources(const QString &resourceType) override;
......
......@@ -35,11 +35,6 @@ KisResourceStorage::ResourceItem KisAslStorage::resourceItem(const QString &url)
return KisResourceStorage::ResourceItem();
}
KisResourceStorage::ResourceItemIterator KisAslStorage::resourceItems(const QString &resourceType)
{
return KisResourceStorage::ResourceItemIterator();
}
KoResourceSP KisAslStorage::resource(const QString &url)
{
return 0;
......
......@@ -29,9 +29,7 @@ public:
virtual ~KisAslStorage();
KisResourceStorage::ResourceItem resourceItem(const QString &url) override;
KisResourceStorage::ResourceItemIterator resourceItems(const QString &resourceType) override;
KoResourceSP resource(const QString &url) override;
KoResourceSP resource(const QString &url) override;
KisResourceStorage::ResourceIterator resources(const QString &resourceType) override;
};
......
......@@ -33,11 +33,6 @@ KisResourceStorage::ResourceItem KisBundleStorage::resourceItem(const QString &u
return KisResourceStorage::ResourceItem();
}
KisResourceStorage::ResourceItemIterator KisBundleStorage::resourceItems(const QString &resourceType)
{
return KisResourceStorage::ResourceItemIterator();
}
KoResourceSP KisBundleStorage::resource(const QString &url)
{
return 0;
......
......@@ -29,8 +29,6 @@ public:
virtual ~KisBundleStorage();
KisResourceStorage::ResourceItem resourceItem(const QString &url) override;
KisResourceStorage::ResourceItemIterator resourceItems(const QString &resourceType) override;
KoResourceSP resource(const QString &url) override;
KisResourceStorage::ResourceIterator resources(const QString &resourceType) override;
};
......
......@@ -19,10 +19,116 @@
#include "KisFolderStorage.h"
#include <QDirIterator>
#include <kis_debug.h>
#include <KisResourceLoaderRegistry.h>
class FolderItem : public KisResourceStorage::ResourceItem
{
public:
~FolderItem() override {}
QByteArray md5sum() const override
{
KisResourceLoaderBase *loader = KisResourceLoaderRegistry::instance()->get(resourceType);
if (loader) {
QFile f(url);
f.open(QFile::ReadOnly);
KoResourceSP res = loader->load(url, f);
f.close();
if (res) {
return res->md5();
}
}
return QByteArray();
}
};
class FolderIterator : public KisResourceStorage::ResourceIterator
{
public:
FolderIterator(const QString &location, const QString &resourceType)
: m_location(location)
, m_resourceType(resourceType)
{
m_loader = KisResourceLoaderRegistry::instance()->get(resourceType);
KIS_ASSERT(m_loader);
m_dirIterator.reset(new QDirIterator(location + '/' + resourceType,
m_loader->filters(),
QDir::Files | QDir::Readable,
QDirIterator::Subdirectories));
}
~FolderIterator() override {}
bool hasNext() const override
{
return m_dirIterator->hasNext();
}
void next() const override
{
m_dirIterator->next();
}
QString url() const override
{
return m_dirIterator->filePath();
}
QString type() const override
{
return m_resourceType;
}
QDateTime lastModified() const override
{
return m_dirIterator->fileInfo().lastModified();
}
QByteArray md5sum() const override
{
if (!loadResourceInternal()) {
qWarning() << "Could not load resource" << m_dirIterator->filePath();
return QByteArray();
}
return m_resource->md5();
}
KoResourceSP resource() const override
{
if (!loadResourceInternal()) {
qWarning() << "Could not load resource" << m_dirIterator->filePath();
}
return m_resource;
}
protected:
bool loadResourceInternal() const {
if (!m_resource && m_resource->filename() != m_dirIterator->filePath()) {
QFile f(m_dirIterator->filePath());
f.open(QFile::ReadOnly);
const_cast<FolderIterator*>(this)->m_resource = m_loader->load(m_dirIterator->filePath(), f);
f.close();
}
return !m_resource.isNull();
}
KoResourceSP m_resource;
KisResourceLoaderBase *m_loader {0};
QScopedPointer<QDirIterator> m_dirIterator;
const QString m_location;
const QString m_resourceType;
};
KisFolderStorage::KisFolderStorage(const QString &location)
: KisStoragePlugin(location)
{
}
KisFolderStorage::~KisFolderStorage()
......@@ -31,20 +137,28 @@ KisFolderStorage::~KisFolderStorage()
KisResourceStorage::ResourceItem KisFolderStorage::resourceItem(const QString &url)
{
return KisResourceStorage::ResourceItem();
}
QFileInfo fi(url);
FolderItem item;
item.url = url;
item.resourceType = fi.path().split("/").last();
item.lastModified = fi.lastModified();
KisResourceStorage::ResourceItemIterator KisFolderStorage::resourceItems(const QString &resourceType)
{
return KisResourceStorage::ResourceItemIterator();
return item;
}
KoResourceSP KisFolderStorage::resource(const QString &url)
{
return 0;
QFileInfo fi(url);
const QString resourceType = fi.path().split("/").last();
KisResourceLoaderBase *loader = KisResourceLoaderRegistry::instance()->get(resourceType);
QFile f(url);
f.open(QFile::ReadOnly);
KoResourceSP res = loader->load(url, f);
f.close();
return res;
}
KisResourceStorage::ResourceIterator KisFolderStorage::resources(const QString &resourceType)
{
return KisResourceStorage::ResourceIterator();
return FolderIterator(location(), resourceType);
}
......@@ -29,8 +29,6 @@ public:
virtual ~KisFolderStorage();
KisResourceStorage::ResourceItem resourceItem(const QString &url) override;
KisResourceStorage::ResourceItemIterator resourceItems(const QString &resourceType) override;
KoResourceSP resource(const QString &url) override;
KisResourceStorage::ResourceIterator resources(const QString &resourceType) override;
......
......@@ -258,8 +258,21 @@ bool KisResourceCacheDb::initialize(const QString &location)
bool KisResourceCacheDb::addResource(KoResourceSP resource)
{
// XXX
return false;
bool r = true;
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";
return false;
}
return r;
}
bool KisResourceCacheDb::addResources(KisResourceStorageSP storage, QString folder)
......@@ -279,7 +292,10 @@ bool KisResourceCacheDb::addStorage(KisResourceStorageSP storage, bool preinstal
{
bool r = true;
if (!s_valid) return false;
if (!s_valid) {
qWarning() << "The database is not valid";
return false;
}
{
QSqlQuery q;
......
......@@ -16,6 +16,25 @@
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <KisResourceLoader.h>
#include <KisMimeDatabase.h>
/**
* @return a set of filters ("*.bla,*.foo") that is suitable for filtering
* the contents of a directory.
*/
QStringList KisResourceLoaderBase::filters() const
{
QStringList filters;
Q_FOREACH(const QString &mimeType, mimetypes()) {
QStringList suffixes = KisMimeDatabase::suffixesForMimeType(mimeType);
Q_FOREACH(const QString &suffix, suffixes) {
filters << "*." + suffix;
}
}
return filters;
}
#include "KisResourceLoader.h"
......@@ -51,6 +51,12 @@ public:
{
}
/**
* @return a set of filters ("*.bla,*.foo") that is suitable for filtering
* the contents of a directory.
*/
QStringList filters() const;
/**
* @return the mimetypes this resource can load
*/
......
......@@ -173,6 +173,7 @@ bool KisResourceLocator::initializeDb()
QDirIterator iter(d->resourceLocation, filters, QDir::Files, QDirIterator::Subdirectories);
while (iter.hasNext()) {
iter.next();
qDebug() << "Considering storage" << iter.filePath();
KisResourceStorageSP storage = QSharedPointer<KisResourceStorage>::create(iter.filePath());
if (!KisResourceCacheDb::addStorage(storage, true)) {
d->errorMessages.append(i18n("Could not add storage %1 to the cache database").arg(iter.filePath()));
......
......@@ -99,11 +99,6 @@ KisResourceStorage::ResourceItem KisResourceStorage::resourceItem(const QString
return d->storagePlugin->resourceItem(url);
}
KisResourceStorage::ResourceItemIterator KisResourceStorage::resourceItems(const QString &resourceType)
{
return d->storagePlugin->resourceItems(resourceType);
}
KoResourceSP KisResourceStorage::resource(const QString &url)
{
return d->storagePlugin->resource(url);
......
......@@ -47,35 +47,28 @@ public:
struct ResourceItem {
virtual ~ResourceItem() {}
QString url;
QString type;
QString resourceType;
QDateTime lastModified;
virtual QByteArray md5sum() const {return QByteArray();}
};
class ResourceItemIterator
class ResourceIterator
{
public:
virtual ~ResourceItemIterator() {}
virtual ~ResourceIterator() {}
virtual bool hasNext() const {return false;};
virtual void next() const{};
virtual QString url() const {return QString();}
virtual QString type() const {return QString();}
virtual QDateTime lastModified() const {return QDateTime();}
/// This only loads the resource when called
virtual QByteArray md5sum() const {return QByteArray();}
};
class ResourceIterator : public ResourceItemIterator
{
public:
virtual ~ResourceIterator() {}
/// This only loads the resource when called
virtual KoResourceSP resource() const {return 0;}
};
......@@ -96,10 +89,7 @@ public:
StorageType type() const;
QDateTime timestamp() const;
ResourceItem resourceItem(const QString &url);
ResourceItemIterator resourceItems(const QString &resourceType);
KoResourceSP resource(const QString &url);
ResourceIterator resources(const QString &resourceType);
......
......@@ -39,8 +39,6 @@ public:
virtual ~KisStoragePlugin();
virtual KisResourceStorage::ResourceItem resourceItem(const QString &url) = 0;
virtual KisResourceStorage::ResourceItemIterator resourceItems(const QString &resourceType) = 0;
virtual KoResourceSP resource(const QString &url) = 0;
virtual KisResourceStorage::ResourceIterator resources(const QString &resourceType) = 0;
......
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