32 if (!url.contains(
'/'))
return std::nullopt;
34 QStringList parts = url.split(
'/', Qt::SkipEmptyParts);
36 if (parts.isEmpty())
return std::nullopt;
38 const QString resourceType = parts[0];
40 const QString resourceFilename = parts.join(
'/');
41 return std::make_pair(resourceType, resourceFilename);
63 return m_it.hasNext();
73 return m_it.peekPrevious();
77 QVectorIterator<KisTagSP>
m_it;
91 QHash<QString, QVector<KisTagSP>>
tags;
111 d->resourcesNew = rhs.
d->resourcesNew;
112 d->tags = rhs.
d->tags;
113 d->metadata = rhs.
d->metadata;
119 d->resourcesNew = rhs.
d->resourcesNew;
121 Q_FOREACH(
const QString &key, rhs.
d->tags.keys()) {
122 Q_FOREACH(
const KisTagSP tag, rhs.
d->tags[key]) {
123 if (!
d->tags.contains(key)) {
126 d->tags[key] << tag->clone();
135 QHash<QString, StoredResource> &typedResources =
136 d->resourcesNew[resourceType];
139 [&typedResources] (
const QString &filename) {
140 return typedResources.contains(filename);
143 const QString newFilename =
146 if (newFilename.isEmpty())
return false;
151 storedResource.
timestamp = QDateTime::currentDateTime();
152 storedResource.
data.reset(
new QByteArray());
153 QBuffer buffer(storedResource.
data.data());
154 buffer.open(QIODevice::WriteOnly);
155 bool result =
resource->saveToDevice(&buffer);
161 typedResources.insert(newFilename, storedResource);
177 const QString resourceType =
resource->resourceType().first;
178 const QString resourceFilename =
resource->filename();
182 if (
d->resourcesNew.contains(resourceType) &&
183 d->resourcesNew[resourceType].contains(resourceFilename)) {
186 d->resourcesNew[resourceType][resourceFilename];
188 if (storedResource.
data->size() > 0) {
189 QBuffer buffer(storedResource.
data.data());
190 buffer.open(QIODevice::ReadOnly);
194 qWarning() <<
"Cannot load resource from device in KisMemoryStorage::loadVersionedResource";
206 if (!parsedUrl)
return false;
207 auto [resourceType, resourceFilename] = *parsedUrl;
210 if (
d->resourcesNew.contains(resourceType) &&
211 d->resourcesNew[resourceType].contains(resourceFilename)) {
216 storedResource.
timestamp = QDateTime::currentDateTime();
217 storedResource.
data.reset(
new QByteArray(device->readAll()));
219 QHash<QString, StoredResource> &typedResources =
220 d->resourcesNew[resourceType];
221 typedResources.insert(resourceFilename, storedResource);
229 if (!parsedUrl)
return false;
230 auto [resourceType, resourceFilename] = *parsedUrl;
232 if (!
d->resourcesNew.contains(resourceType) ||
233 !
d->resourcesNew[resourceType].contains(resourceFilename)) {
238 d->resourcesNew[resourceType][resourceFilename];
240 if (!storedResource.
data) {
241 qWarning() <<
"Stored resource doesn't have a serialized representation!";
245 device->write(*storedResource.
data);
251 QHash<QString, StoredResource> &typedResources =
d->resourcesNew[resourceType];
253 if (typedResources.contains(
resource->filename())) {
261 storedResource.
timestamp = QDateTime::currentDateTime();
262 storedResource.
data.reset(
new QByteArray());
264 QBuffer buffer(storedResource.
data.data());
265 buffer.open(QIODevice::WriteOnly);
266 if (!
resource->saveToDevice(&buffer)) {
274 typedResources.insert(
resource->filename(), storedResource);
282 if (!parsedUrl)
return false;
283 auto [resourceType, resourceFilename] = *parsedUrl;
285 if (
d->resourcesNew.contains(resourceType)) {
286 return d->resourcesNew[resourceType].remove(resourceFilename) > 0;
299 if (existingIt != typedTags.end()) {
300 typedTags.erase(existingIt);
303 typedTags.append(tag);
313 if (existingIt != typedTags.end()) {
314 typedTags.erase(existingIt);
324 if (!parsedUrl)
return QString();
325 auto [resourceType, resourceFilename] = *parsedUrl;
329 if (
d->resourcesNew.contains(resourceType) &&
330 d->resourcesNew[resourceType].contains(resourceFilename)) {
333 d->resourcesNew[resourceType][resourceFilename];
335 if (storedResource.
data->size() > 0 || storedResource.
resource.isNull()) {
338 result = storedResource.
resource->md5Sum();
350 QHash<QString, StoredResource> &typedResources =
351 d->resourcesNew[resourceType];
353 for (
auto it = typedResources.begin(); it != typedResources.end(); ++it) {
359 entries.append(entry);
392 if (
d->metadata.contains(key)) {
393 r =
d->metadata[key];
float value(const T *src, size_t ch)
static KisResourcesInterfaceSP instance()
QHash< QString, QHash< QString, StoredResource > > resourcesNew
QMap< QString, QVariant > metadata
QHash< QString, QVector< KisTagSP > > tags
The KisMemoryStorage class stores the temporary resources that are not saved to disk or bundle....
virtual ~KisMemoryStorage()
QSharedPointer< KisResourceStorage::TagIterator > tags(const QString &resourceType) override
bool saveAsNewVersion(const QString &resourceType, KoResourceSP resource) override
bool exportResource(const QString &url, QIODevice *device) override
bool importResource(const QString &url, QIODevice *device) override
void setMetaData(const QString &key, const QVariant &value) override
KisMemoryStorage & operator=(const KisMemoryStorage &rhs)
This clones all contained resources and tags from rhs.
KisMemoryStorage(const QString &location=QString("memory"))
bool loadVersionedResource(KoResourceSP resource) override
bool testingRemoveResource(const QString &url)
KisResourceStorage::ResourceItem resourceItem(const QString &url) override
bool testingAddTag(const QString &resourceType, KisTagSP tag)
QSharedPointer< KisResourceStorage::ResourceIterator > resources(const QString &resourceType) override
bool addResource(const QString &resourceType, KoResourceSP resource) override
QStringList metaDataKeys() const override
QString resourceMd5(const QString &url) override
bool testingRemoveTag(const QString &resourceType, const QString &tagUrl)
QVariant metaData(const QString &key) const override
QScopedPointer< Private > d
static const QString s_meta_name
virtual KoResourceSP resource(const QString &url)
static void detectFileVersions(QVector< VersionedResourceEntry > &allFiles)
static QString chooseUniqueName(KoResourceSP resource, int minVersion, std::function< bool(QString)> checkExists)
QString url() const
The unique identifier for the tag. Since tag urls are compared COLLATE NOCASE, tag urls must be ASCII...
static QString generateHash(const QString &filename)
generateHash reads the given file and generates a hex-encoded md5sum for the file.
QVectorIterator< KisTagSP > m_it
bool hasNext() const override
void next() override
The iterator is only valid if next() has been called at least once.
KisTagSP tag() const override
A tag object on which we can set properties and which we can save.
MemoryTagIterator(const QVector< KisTagSP > &tags)
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
QSharedPointer< T > toQShared(T *ptr)
std::optional< std::pair< QString, QString > > splitResourceUrl(const QString &url)
auto mem_equal_to(MemTypeNoRef Class::*ptr, MemType &&value)
mem_equal_to is an unary functor that compares a member of the object to a given value
A resource item is simply an entry in the storage,.
QSharedPointer< QByteArray > data