Krita Source Code Documentation
Loading...
Searching...
No Matches
KoResourceServer.h
Go to the documentation of this file.
1/* This file is part of the KDE project
2
3 SPDX-FileCopyrightText: 1999 Matthias Elter <elter@kde.org>
4 SPDX-FileCopyrightText: 2003 Patrick Julien <freak@codepimps.org>
5 SPDX-FileCopyrightText: 2005 Sven Langkamp <sven.langkamp@gmail.com>
6 SPDX-FileCopyrightText: 2007 Jan Hambrecht <jaham@gmx.net>
7 SPDX-FileCopyrightText: 2011 Srikanth Tiyyagura <srikanth.tulasiram@gmail.com>
8 SPDX-FileCopyrightText: 2013 Sascha Suelzer <s.suelzer@gmail.com>
9 SPDX-FileCopyrightText: 2003-2019 Boudewijn Rempt <boud@valdyas.org>
10
11 SPDX-License-Identifier: LGPL-2.1-or-later
12 */
13
14#ifndef KORESOURCESERVER_H
15#define KORESOURCESERVER_H
16
17#include <QString>
18#include <QList>
19#include <QFileInfo>
20#include <QApplication>
21#include <QThread>
22#include <QDir>
23#include <QTemporaryFile>
24
25#include "KoResource.h"
26#include "KoResourcePaths.h"
27#include "ksharedconfig.h"
28
30#include <KisResourceLocator.h>
31#include <KisResourceModel.h>
32#include <KisTagModel.h>
33#include <kis_assert.h>
34#include <kis_debug.h>
35
36#include <ResourceDebug.h>
37
38class KoResource;
39
40template <class T>
42{
43public:
45
46 virtual void unsetResourceServer() = 0;
47
52 virtual void resourceAdded(QSharedPointer<T> resource) = 0;
53
58 virtual void removingResource(QSharedPointer<T> resource) = 0;
59
64 virtual void resourceChanged(QSharedPointer<T> resource) = 0;
65
66
67};
68
74template <class T>
76{
77public:
78
80
81 KoResourceServer(const QString& type)
83 , m_tagModel(new KisTagModel(type))
84 , m_type(type)
85 {
86 Q_ASSERT(!type.isEmpty());
87 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
88 if (QThread::currentThread() != qApp->thread()) {
89 qDebug().noquote() << kisBacktrace();
90 }
91
92 }
93
95 {
96 delete m_resourceModel;
97 delete m_tagModel;
98 Q_FOREACH (ObserverType* observer, m_observers) {
99 observer->unsetResourceServer();
100 }
101 }
102
105 {
106 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
107 if (QThread::currentThread() != qApp->thread()) {
108 qDebug().noquote() << kisBacktrace();
109 }
110
111 return m_resourceModel;
112 }
113
116 {
117
118 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
119 if (QThread::currentThread() != qApp->thread()) {
120 qDebug().noquote() << kisBacktrace();
121 }
122
123 return m_resourceModel->resourceForIndex(m_resourceModel->index(0, 0)).dynamicCast<T>();
124 }
125
126 int resourceCount() const {
127
128 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
129 return m_resourceModel->rowCount();
130 }
131
133 bool addResource(QSharedPointer<T> resource, bool save = true) {
134
135 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
136 if (QThread::currentThread() != qApp->thread()) {
137 qDebug().noquote() << kisBacktrace();
138 }
139
140 if (!resource || !resource->valid()) {
141 warnResource << "Tried to add an invalid resource!" << resource;
142 return false;
143 }
144
145 if (m_resourceModel->addResource(resource, save ? QString() : "memory")) {
147 return true;
148 }
149
150 return false;
151 }
152
155
156 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
157 if (QThread::currentThread() != qApp->thread()) {
158 qDebug().noquote() << kisBacktrace();
159 }
160
163 return true;
164 }
165 return false;
166 }
167
172
179 KoResourceSP importResourceFile(const QString &filename, const bool allowOverwrite)
180 {
181
182 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
183 if (QThread::currentThread() != qApp->thread()) {
184 qDebug().noquote() << kisBacktrace();
185 }
186
187 return m_resourceModel->importResourceFile(filename, allowOverwrite);
188 }
189
191 void removeResourceFile(const QString & filename)
192 {
193 QFileInfo fi(filename);
194
196 if (!resource) {
197 warnResource << "Resource file do not exist ";
198 return;
199 }
201 }
202
208 void addObserver(ObserverType* observer)
209 {
210 if (observer && !m_observers.contains(observer)) {
211 m_observers.append(observer);
212 }
213 }
214
220 {
221 int index = m_observers.indexOf(observer);
222 if (index < 0) {
223 return;
224 }
225 m_observers.removeAt( index );
226 }
227
228private:
229
230 QSharedPointer<T> resourceByFilename(const QString& filename) const
231 {
232 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
233 if (QThread::currentThread() != qApp->thread()) {
234 qDebug().noquote() << kisBacktrace();
235 }
236
237
238 if (filename.isEmpty() || filename.isNull()) {
239 return nullptr;
240 }
242
243 if (resources.size() > 0) {
244 return resources.first().dynamicCast<T>();
245 }
246
247 return nullptr;
248 }
249
250
251 QSharedPointer<T> resourceByName(const QString& name) const
252 {
253 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
254 if (QThread::currentThread() != qApp->thread()) {
255 qDebug().noquote() << kisBacktrace();
256 }
257
258 if (name.isEmpty() || name.isNull()) {
259 return nullptr;
260 }
261
263
264 if (resources.size() > 0) {
265 return resources.first().dynamicCast<T>();
266 }
267
268 return nullptr;
269
270 }
271
272 QSharedPointer<T> resourceByMD5(const QString& md5) const
273 {
274 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
275 if (QThread::currentThread() != qApp->thread()) {
276 qDebug().noquote() << kisBacktrace();
277 }
278 if (md5.isEmpty() || md5.isNull()) {
279 return nullptr;
280 }
282
283 if (resources.size() > 0) {
284 return resources.first().dynamicCast<T>();
285 }
286
287 return nullptr;
288 }
289
290public:
291
301 QSharedPointer<T> resource(const QString &md5, const QString &fileName, const QString &name)
302 {
303 return KisGlobalResourcesInterface::instance()->source<T>(m_type).bestMatch(md5, fileName, name);
304 }
305
311 {
312 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
313 if (QThread::currentThread() != qApp->thread()) {
314 qDebug().noquote() << kisBacktrace();
315 }
318 return result;
319 }
320
325 {
326 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
327 if (QThread::currentThread() != qApp->thread()) {
328 qDebug().noquote() << kisBacktrace();
329 }
332
333 return result;
334 }
335
337 {
338 if (resource.isNull()) {
339 return QVector<KisTagSP>();
340 }
341 return m_resourceModel->tagsForResource(resource->resourceId());
342 }
343
344protected:
345
347 {
348 Q_FOREACH (ObserverType* observer, m_observers) {
349 observer->resourceAdded(resource);
350 }
351 }
352
354 {
355 Q_FOREACH (ObserverType* observer, m_observers) {
356 observer->removingResource(resource);
357 }
358 }
359
361 {
362 Q_FOREACH (ObserverType* observer, m_observers) {
363 observer->resourceChanged(resource);
364 }
365 }
366
367private:
368
372 QString m_type;
373};
374
375#endif // KORESOURCESERVER_H
#define warnResource
bool setResourceInactive(const QModelIndex &index)
static KisResourcesInterfaceSP instance()
QString resourceLocationBase() const
resourceLocationBase is the place where all resource storages (folder, bundles etc....
static KisResourceLocator * instance()
The KisResourceModel class provides the main access to resources. It is possible to filter the resour...
KoResourceSP importResourceFile(const QString &filename, const bool allowOverwrite, const QString &storageId=QString("")) override
importResourceFile
bool reloadResource(KoResourceSP resource) override
reloadResource
QVector< KisTagSP > tagsForResource(int resourceId) const
QVector< KoResourceSP > resourcesForMD5(const QString md5sum) const
bool updateResource(KoResourceSP resource) override
updateResource creates a new version of the resource in the storage and in the database....
QVector< KoResourceSP > resourcesForFilename(QString fileName) const
QVector< KoResourceSP > resourcesForName(QString name) const
bool addResource(KoResourceSP resource, const QString &storageId=QString("")) override
addResource adds the given resource to the database and storage. If the resource already exists in th...
QModelIndex indexForResource(KoResourceSP resource) const override
indexFromResource
KoResourceSP resourceForIndex(QModelIndex index=QModelIndex()) const override
resourceForIndex returns a properly versioned and id'ed resource object
virtual void removingResource(QSharedPointer< T > resource)=0
virtual void resourceAdded(QSharedPointer< T > resource)=0
virtual void unsetResourceServer()=0
virtual void resourceChanged(QSharedPointer< T > resource)=0
void addObserver(ObserverType *observer)
void notifyResourceAdded(QSharedPointer< T > resource)
int resourceCount() const
virtual ~KoResourceServer()
QSharedPointer< T > resourceByMD5(const QString &md5) const
KisTagModel * m_tagModel
QSharedPointer< T > resource(const QString &md5, const QString &fileName, const QString &name)
resource retrieves a resource. If the md5sum is not empty, the resource will only be retrieved if a r...
KisResourceModel * m_resourceModel
bool reloadResource(QSharedPointer< T > resource)
void removeObserver(ObserverType *observer)
KisResourceModel * resourceModel() const
bool addResource(QSharedPointer< T > resource, bool save=true)
Adds an already loaded resource to the server.
bool updateResource(QSharedPointer< T > resource)
KoResourceServerObserver< T > ObserverType
bool removeResourceFromServer(QSharedPointer< T > resource)
Remove a resource from Resource Server but not from a file.
KoResourceSP importResourceFile(const QString &filename, const bool allowOverwrite)
QList< ObserverType * > m_observers
KoResourceServer(const QString &type)
QSharedPointer< T > firstResource() const
Return the first resource available.
QString saveLocation()
Returns path where to save user defined and imported resources to.
QSharedPointer< T > resourceByFilename(const QString &filename) const
void removeResourceFile(const QString &filename)
Removes the resource file from the resource server.
QSharedPointer< T > resourceByName(const QString &name) const
QVector< KisTagSP > assignedTagsList(KoResourceSP resource) const
void notifyResourceChanged(QSharedPointer< T > resource)
void notifyRemovingResource(QSharedPointer< T > resource)
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130
QString kisBacktrace()
Definition kis_debug.cpp:51