diff --git a/libs/resources/KisBundleStorage.cpp b/libs/resources/KisBundleStorage.cpp index 600937bd7e..d66d1c963c 100644 --- a/libs/resources/KisBundleStorage.cpp +++ b/libs/resources/KisBundleStorage.cpp @@ -1,159 +1,184 @@ /* * 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 "KisBundleStorage.h" #include #include +#include #include "KisResourceStorage.h" #include "KoResourceBundle.h" #include "KoResourceBundleManifest.h" class BundleTagIterator : public KisResourceStorage::TagIterator { public: BundleTagIterator(KoResourceBundle *bundle, const QString &resourceType) : m_bundle(bundle) , m_resourceType(resourceType) - {} + { + QList resources = m_bundle->manifest().files(resourceType); + Q_FOREACH(KoResourceBundleManifest::ResourceReference resource, resources) { + Q_FOREACH(const QString &tagname, resource.tagList) { + if (!m_tags.contains(tagname)){ + KisTagSP tag = QSharedPointer(new KisTag()); + tag->setName(tagname); + tag->setComment(tagname); + tag->setUrl(bundle->filename() + ':' + tagname); + m_tags[tagname] = tag; + } + m_tags[tagname]->setDefaultResources(m_tags[tagname]->defaultResources() << resource.resourcePath); + } + } + m_tagIterator.reset(new QListIterator(m_tags.values())); + } - bool hasNext() const override {return false; } - void next() const override {} + bool hasNext() const override + { + return m_tagIterator->hasNext(); + } + void next() const override + { + const_cast(this)->m_tag = m_tagIterator->next(); + } - QString url() const override { return QString(); } - QString name() const override { return QString(); } - QString comment() const override {return QString(); } - KisTagSP tag() const override { return 0; } + QString url() const override { return m_tag ? m_tag->url() : QString(); } + QString name() const override { return m_tag ? m_tag->name() : QString(); } + QString comment() const override {return m_tag ? m_tag->comment() : QString(); } + KisTagSP tag() const override { return m_tag; } private: + QHash m_tags; KoResourceBundle *m_bundle {0}; QString m_resourceType; + QScopedPointer > m_tagIterator; + KisTagSP m_tag; }; class BundleIterator : public KisResourceStorage::ResourceIterator { public: BundleIterator(KoResourceBundle *bundle, const QString &resourceType) : m_bundle(bundle) , m_resourceType(resourceType) { m_entriesIterator.reset(new QListIterator(m_bundle->manifest().files(resourceType))); } bool hasNext() const override { return m_entriesIterator->hasNext(); } void next() const override { KoResourceBundleManifest::ResourceReference ref = m_entriesIterator->next(); const_cast(this)->m_resourceReference = ref; } QString url() const override { return m_resourceReference.resourcePath; } QString type() const override { return m_resourceType; } QDateTime lastModified() const override { return QFileInfo(m_bundle->filename()).lastModified(); } /// This only loads the resource when called QByteArray md5sum() const override { return m_resourceReference.md5sum; } /// This only loads the resource when called KoResourceSP resource() const override { return m_bundle->resource(m_resourceType, m_resourceReference.resourcePath); } private: KoResourceBundle *m_bundle {0}; QString m_resourceType; QScopedPointer > m_entriesIterator; KoResourceBundleManifest::ResourceReference m_resourceReference; }; class KisBundleStorage::Private { public: QScopedPointer bundle; }; KisBundleStorage::KisBundleStorage(const QString &location) : KisStoragePlugin(location) , d(new Private()) { d->bundle.reset(new KoResourceBundle(location)); if (!d->bundle->load()) { qWarning() << "Could not load bundle" << location; } } KisBundleStorage::~KisBundleStorage() { } KisResourceStorage::ResourceItem KisBundleStorage::resourceItem(const QString &url) { return KisResourceStorage::ResourceItem(); } KoResourceSP KisBundleStorage::resource(const QString &url) { return 0; } QSharedPointer KisBundleStorage::resources(const QString &resourceType) { return QSharedPointer(new BundleIterator(d->bundle.data(), resourceType)); } QSharedPointer KisBundleStorage::tags(const QString &resourceType) { return QSharedPointer(new BundleTagIterator(d->bundle.data(), resourceType)); } QImage KisBundleStorage::thumbnail() const { return d->bundle->image(); } QString KisBundleStorage::metaData(const QString &key) const { return d->bundle->metaData(key); } diff --git a/libs/resources/tests/TestBundleStorage.cpp b/libs/resources/tests/TestBundleStorage.cpp index fb24357386..f2d30d2267 100644 --- a/libs/resources/tests/TestBundleStorage.cpp +++ b/libs/resources/tests/TestBundleStorage.cpp @@ -1,91 +1,90 @@ /* * Copyright (c) 2018 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 "TestBundleStorage.h" #include #include #include #include #include #include #include #include "DummyResource.h" void TestBundleStorage::initTestCase() { KisResourceLoaderRegistry *reg = KisResourceLoaderRegistry::instance(); reg->add(new KisResourceLoader("gbr_brushes", "brushes", QStringList() << "image/x-gimp-brush")); reg->add(new KisResourceLoader("gih_brushes", "brushes", QStringList() << "image/x-gimp-brush-animated")); reg->add(new KisResourceLoader("svg_brushes", "brushes", QStringList() << "image/svg+xml")); reg->add(new KisResourceLoader("png_brushes", "brushes", QStringList() << "image/png")); reg->add("brushes", new KisResourceLoader("paintoppresets", "paintoppresets", QStringList() << "application/x-krita-paintoppreset")); QList src = QImageReader::supportedMimeTypes(); QStringList allImageMimes; Q_FOREACH(const QByteArray ba, src) { allImageMimes << QString::fromUtf8(ba); } reg->add(new KisResourceLoader("patterns", "patterns", allImageMimes)); } void TestBundleStorage::testMetaData() { KisBundleStorage storage(KRITA_SOURCE_DIR + QString("/krita/data/bundles/Krita_4_Default_Resources.bundle")); QVERIFY(storage.location() == KRITA_SOURCE_DIR + QString("/krita/data/bundles/Krita_4_Default_Resources.bundle")); qDebug() << storage.metaData(KisStoragePlugin::s_meta_generator); QVERIFY(!storage.metaData(KisStoragePlugin::s_meta_generator).isEmpty()); QVERIFY(!storage.metaData(KisStoragePlugin::s_meta_author).isEmpty()); QVERIFY(!storage.metaData(KisStoragePlugin::s_meta_description).isEmpty()); QVERIFY(!storage.metaData(KisStoragePlugin::s_meta_initial_creator).isEmpty()); QVERIFY(!storage.metaData(KisStoragePlugin::s_meta_dc_date).isEmpty()); QVERIFY(!storage.metaData(KisStoragePlugin::s_meta_version).isEmpty()); } void TestBundleStorage::testResourceIterator() { KisBundleStorage storage(KRITA_SOURCE_DIR + QString("/krita/data/bundles/Krita_4_Default_Resources.bundle")); QSharedPointer iter = storage.resources("brushes"); QVERIFY(iter->hasNext()); int count = 0; while (iter->hasNext()) { iter->next(); KoResourceSP res = iter->resource(); QVERIFY(res); count++; } QVERIFY(count > 0); } void TestBundleStorage::testTagIterator() { KisBundleStorage storage(KRITA_SOURCE_DIR + QString("/krita/data/bundles/Krita_4_Default_Resources.bundle")); QSharedPointer iter = storage.tags("paintoppresets"); QVERIFY(iter->hasNext()); int count = 0; while (iter->hasNext()) { iter->next(); - qDebug() << iter->url() << iter->name() << iter->tag(); count++; } - QVERIFY(count == 1); + QVERIFY(count > 0); } QTEST_MAIN(TestBundleStorage)