diff --git a/libs/resources/KisMemoryStorage.cpp b/libs/resources/KisMemoryStorage.cpp index e7eab0d391..9f627c8d0d 100644 --- a/libs/resources/KisMemoryStorage.cpp +++ b/libs/resources/KisMemoryStorage.cpp @@ -1,198 +1,229 @@ /* * 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 "KisMemoryStorage.h" #include #include #include #include #include #include #include -class KisMemoryStorage::Private { -public: - QHash > resources; - QHash > tags; -}; class MemoryTagIterator : public KisResourceStorage::TagIterator { public: MemoryTagIterator(QVector tags, const QString &resourceType) : m_tags(tags) , m_resourceType(resourceType) { } bool hasNext() const override { return m_currentPosition < m_tags.size(); } void next() override { const_cast(this)->m_currentPosition += 1; } QString url() const override { return tag() ? tag()->url() : QString(); } QString name() const override { return tag() ? tag()->name() : QString(); } QString comment() const override {return tag() ? tag()->comment() : QString(); } KisTagSP tag() const override { if (m_currentPosition >= m_tags.size()) return 0; return m_tags.at(m_currentPosition); } private: int m_currentPosition {0}; QVector m_tags; QString m_resourceType; }; class MemoryItem : public KisResourceStorage::ResourceItem { public: ~MemoryItem() override {} }; class MemoryIterator : public KisResourceStorage::ResourceIterator { public: MemoryIterator(QVector resources, const QString &resourceType) : m_resources(resources) , m_resourceType(resourceType) { } ~MemoryIterator() override {} bool hasNext() const override { return m_currentPosition < m_resources.size(); } void next() override { const_cast(this)->m_currentPosition++; } QString url() const override { if (resource()) { return resource()->filename(); } return QString(); } QString type() const override { return m_resourceType; } QDateTime lastModified() const override { return QDateTime::fromMSecsSinceEpoch(0); } KoResourceSP resource() const override { if (m_currentPosition > m_resources.size()) return 0; return m_resources.at(m_currentPosition - 1); } private: int m_currentPosition {0}; QVector m_resources; QString m_resourceType; }; +class KisMemoryStorage::Private { +public: + QHash > resources; + QHash > tags; +}; + -KisMemoryStorage::KisMemoryStorage(const QString &name) - : KisStoragePlugin(name) - , d(new Private()) +KisMemoryStorage::KisMemoryStorage(const QString &location) + : KisStoragePlugin(location) + , d(new Private) { } KisMemoryStorage::~KisMemoryStorage() { } +KisMemoryStorage::KisMemoryStorage(const KisMemoryStorage &rhs) + : KisStoragePlugin(rhs.location()) + , d(new Private) +{ + *this = rhs; +} + +KisMemoryStorage &KisMemoryStorage::operator=(const KisMemoryStorage &rhs) +{ + if (this != &rhs) { + Q_FOREACH(const QString &key, rhs.d->resources.keys()) { + Q_FOREACH(const KoResourceSP resource, rhs.d->resources[key]) { + if (!d->resources.contains(key)) { + d->resources[key] = QVector(); + } + d->resources[key] << resource->clone(); + } + } + Q_FOREACH(const QString &key, rhs.d->tags.keys()) { + Q_FOREACH(const KisTagSP tag, rhs.d->tags[key]) { + if (!d->tags.contains(key)) { + d->tags[key] = QVector(); + } + d->tags[key] << tag->clone(); + } + } + } + return *this; +} + bool KisMemoryStorage::addTag(const QString &resourceType, KisTagSP tag) { if (!d->tags.contains(resourceType)) { d->tags[resourceType] = QVector(); } if (!d->tags[resourceType].contains(tag)) { d->tags[resourceType].append(tag); } return true; } bool KisMemoryStorage::addResource(const QString &resourceType, KoResourceSP resource) { if (!d->resources.contains(resourceType)) { d->resources[resourceType] = QVector(); } if (!d->resources[resourceType].contains(resource)) { d->resources[resourceType].append(resource); } return true; } KisResourceStorage::ResourceItem KisMemoryStorage::resourceItem(const QString &url) { MemoryItem item; item.url = url; item.folder = QString(); item.lastModified = QDateTime::fromMSecsSinceEpoch(0); return item; } KoResourceSP KisMemoryStorage::resource(const QString &url) { KoResourceSP resource; QFileInfo fi(location() + '/' + url); const QString resourceType = fi.path().split("/").last(); Q_FOREACH(resource, d->resources[resourceType]) { if (resource->filename() == url) { break; } } return resource; } QSharedPointer KisMemoryStorage::resources(const QString &resourceType) { return QSharedPointer(new MemoryIterator(d->resources[resourceType], resourceType)); } QSharedPointer KisMemoryStorage::tags(const QString &resourceType) { return QSharedPointer(new MemoryTagIterator(d->tags[resourceType], resourceType)); } diff --git a/libs/resources/KisMemoryStorage.h b/libs/resources/KisMemoryStorage.h index fe5226e221..6488c14a7e 100644 --- a/libs/resources/KisMemoryStorage.h +++ b/libs/resources/KisMemoryStorage.h @@ -1,52 +1,55 @@ /* * 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 KISMEMORYSTORAGE_H #define KISMEMORYSTORAGE_H #include #include /** * @brief The KisMemoryStorage class stores the temporary resources * that are not saved to disk or bundle. */ class KRITARESOURCES_EXPORT KisMemoryStorage : public KisStoragePlugin { public: - KisMemoryStorage(const QString &name = QString("memory")); + KisMemoryStorage(const QString &location = QString("memory")); virtual ~KisMemoryStorage(); + KisMemoryStorage(const KisMemoryStorage &rhs); + KisMemoryStorage &operator=(const KisMemoryStorage &rhs); + bool addTag(const QString &resourceType, KisTagSP tag); bool addResource(const QString &resourceType, KoResourceSP resource); KisResourceStorage::ResourceItem resourceItem(const QString &url) override; KoResourceSP resource(const QString &url) override; QSharedPointer resources(const QString &resourceType) override; QSharedPointer tags(const QString &resourceType) override; private: class Private; QScopedPointer d; }; #endif // KISMEMORYSTORAGE_H diff --git a/libs/resources/KisTag.cpp b/libs/resources/KisTag.cpp index bc0348a0b4..8108fd3e0e 100644 --- a/libs/resources/KisTag.cpp +++ b/libs/resources/KisTag.cpp @@ -1,151 +1,175 @@ /* * 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 "KisTag.h" #include #include #include #include #include #include #include "kconfigini_p.h" #include "kconfigbackend_p.h" #include "kconfigdata.h" #include const QByteArray KisTag::s_group {"Desktop Entry"}; const QByteArray KisTag::s_type {"Type"}; const QByteArray KisTag::s_tag {"Tag"}; const QByteArray KisTag::s_name {"Name"}; const QByteArray KisTag::s_url {"URL"}; const QByteArray KisTag::s_comment {"Comment"}; const QByteArray KisTag::s_defaultResources {"Default Resources"}; class KisTag::Private { public: bool valid {false}; QString url; // This is the actual tag QString name; // The translated tag name QString comment; // The translated tag comment QStringList defaultResources; // The list of resources as defined in the tag file KEntryMap map; }; KisTag::KisTag() : d(new Private) { } KisTag::~KisTag() { } +KisTag::KisTag(const KisTag &rhs) + : d(new Private) +{ + *this = rhs; +} + +KisTag &KisTag::operator=(const KisTag &rhs) +{ + if (this != &rhs) { + d->valid = rhs.d->valid; + d->url = rhs.d->url; + d->name = rhs.d->name; + d->comment = rhs.d->comment; + d->defaultResources = rhs.d->defaultResources; + d->map = rhs.d->map; + } + return *this; +} + +KisTagSP KisTag::clone() const +{ + return KisTagSP(new KisTag(*this)); +} + bool KisTag::valid() const { return d->valid; } QString KisTag::name() const { return d->name; } void KisTag::setName(const QString &name) { d->map.setEntry(s_group, s_name, name, KEntryMap::EntryDirty); d->name = name; } QString KisTag::url() const { return d->url; } void KisTag::setUrl(const QString &url) { d->map.setEntry(s_group, s_url, url, KEntryMap::EntryDirty); d->url = url; } QString KisTag::comment() const { return d->comment; } void KisTag::setComment(const QString &comment) { d->map.setEntry(s_group, s_comment, comment, KEntryMap::EntryDirty); d->comment = comment; } QStringList KisTag::defaultResources() const { return d->defaultResources; } void KisTag::setDefaultResources(const QStringList &defaultResources) { d->defaultResources = defaultResources; } bool KisTag::load(QIODevice &io) { if (!io.isOpen()) { io.open(QIODevice::ReadOnly); } KIS_ASSERT(io.isOpen()); KConfigIniBackend ini; KConfigBackend::ParseInfo r = ini.parseConfigIO(io, QLocale().name().toUtf8(), d->map, KConfigBackend::ParseOption::ParseGlobal, false); if (r != KConfigBackend::ParseInfo::ParseOk) { qWarning() << "Could not load this tag file" << r; return false; } QString t = d->map.getEntry(s_group, s_type); if (t != s_tag) { qWarning() << "Not a tag desktop file" << t; return false; } d->url = d->map.getEntry(s_group, s_url); d->name = d->map.getEntry(s_group, s_name, QString(), KEntryMap::SearchLocalized); d->comment = d->map.getEntry(s_group, s_comment, QString(), KEntryMap::SearchLocalized); d->defaultResources = d->map.getEntry(s_group, s_defaultResources, QString()).split(',', QString::SkipEmptyParts); d->valid = true; return true; } bool KisTag::save(QIODevice &io) { KConfigIniBackend ini; d->map.setEntry(s_group, s_url, d->url, KEntryMap::EntryDirty); d->map.setEntry(s_group, s_name, d->name, KEntryMap::EntryDirty); d->map.setEntry(s_group, s_comment, d->comment, KEntryMap::EntryDirty); d->map.setEntry(s_group, s_defaultResources, d->defaultResources.join(','), KEntryMap::EntryDirty); ini.writeEntries(QLocale().name().toUtf8(), io, d->map); return true; } diff --git a/libs/resources/KisTag.h b/libs/resources/KisTag.h index 92b7c5a759..0d59503698 100644 --- a/libs/resources/KisTag.h +++ b/libs/resources/KisTag.h @@ -1,89 +1,95 @@ /* * 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 KISTAGLOADER_H #define KISTAGLOADER_H #include #include #include #include class QIODevice; #include "kritaresources_export.h" +class KisTag; +typedef QSharedPointer KisTagSP; + + /** * @brief The KisTag loads a tag from a .tag file. * A .tag file is a .desktop file. The following fields * are important: * * name: the name of the tag, which can be translated * comment: a tooltip for the tag, which can be translagted * url: the untranslated name of the tag * */ class KRITARESOURCES_EXPORT KisTag { public: KisTag(); virtual ~KisTag(); + KisTag(const KisTag &rhs); + KisTag &operator=(const KisTag &rhs); + KisTagSP clone() const; bool valid() const; QString name() const; void setName(const QString &name); QString url() const; void setUrl(const QString &url); QString comment() const; void setComment(const QString &comment); QStringList defaultResources() const; void setDefaultResources(const QStringList &defaultResources); bool load(QIODevice &io); bool save(QIODevice &io); private: static const QByteArray s_group; static const QByteArray s_type; static const QByteArray s_tag; static const QByteArray s_name; static const QByteArray s_url; static const QByteArray s_comment; static const QByteArray s_defaultResources; class Private; QScopedPointer d; }; -typedef QSharedPointer KisTagSP; inline QDebug operator<<(QDebug dbg, const KisTagSP tag) { dbg.space() << "[TAG] Name" << tag->name() << "Url" << tag->url() << "Comment" << tag->comment() << "Default resources" << tag->defaultResources().join(", "); return dbg.space(); } #endif // KISTAGLOADER_H