Krita Source Code Documentation
Loading...
Searching...
No Matches
KisResourcesInterface.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2020 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
7
8
9#include <QString>
10#include "kis_assert.h"
12
13#include "kis_debug.h"
14
15//#define SANITY_CHECKS
16#define CRASH_ON_SANITY_CHECK_FAILURE
17
18
23
28
33
35{
36 Q_D(const KisResourcesInterface);
37
38 // use double-locking for fetching the source
39
41
42 {
43 QReadLocker l(&d->lock);
44 source = d->findExistingSource(type);
45 if (source) return *source;
46 }
47
48 {
49 QWriteLocker l(&d->lock);
50 source = d->findExistingSource(type);
51 if (source) return *source;
52
54
55 std::unique_ptr<ResourceSourceAdapter> sourcePtr(source);
56 d->sourceAdapters.emplace(type, std::move(sourcePtr));
57 }
58
60 return *source;
61}
62
64 : m_type(type)
65{
66}
67
71
72KoResourceSP KisResourcesInterface::ResourceSourceAdapter::bestMatch(const QString md5, const QString filename, const QString name)
73{
74 return findResource(md5, filename, name, false);
75}
76
77KoResourceSP KisResourcesInterface::ResourceSourceAdapter::exactMatch(const QString md5, const QString filename, const QString name)
78{
79 return findResource(md5, filename, name, true);
80}
81
82KoResourceSP KisResourcesInterface::ResourceSourceAdapter::findResource(const QString md5, const QString filename, const QString name, bool exactMatch)
83{
85
86 if (!md5.isEmpty()) {
87 Q_FOREACH (KoResourceSP res, resourcesForMD5(md5)) {
88 int penalty = 0;
89
90 if (!res->active()) {
91 penalty += 100;
92 }
93
94 if (!filename.isEmpty() && filename != res->filename()) {
97 penalty += 2;
98 }
99
100 if (!name.isEmpty() && name != res->name()) {
101 penalty++;
102 }
103
104 foundResources.append(qMakePair(res, penalty));
105 }
106
109 if (exactMatch && foundResources.isEmpty()) {
110 return {};
111 }
112 }
113
114#ifdef SANITY_CHECKS
121 const bool warnAboutIncorrectMd5Fetch =
122 foundResources.isEmpty() && !md5.isEmpty();
123#endif
124
125 if (foundResources.isEmpty()) {
126 if (!filename.isEmpty()) {
127 Q_FOREACH (KoResourceSP res, resourcesForFilename(filename)) {
128 int penalty = 0;
129
130 if (!res->active()) {
131 penalty += 100;
132 }
133
134 if (!name.isEmpty() && name != res->name()) {
135 penalty++;
136 }
137
138 foundResources.append(qMakePair(res, penalty));
139 }
140 } else if (!name.isEmpty()) {
141 Q_FOREACH (KoResourceSP res, resourcesForName(name)) {
142 int penalty = 0;
143
144 if (!res->active()) {
145 penalty += 100;
146 }
147
148 foundResources.append(qMakePair(res, penalty));
149 }
150 }
151 }
152
153 auto it = std::min_element(foundResources.begin(), foundResources.end(),
154 [] (const QPair<KoResourceSP, int> &lhs,
155 const QPair<KoResourceSP, int> &rhs) {return lhs.second < rhs.second;});
156
157 KoResourceSP result = it != foundResources.end() ? it->first : KoResourceSP();
158
159#ifdef SANITY_CHECKS
160 if (warnAboutIncorrectMd5Fetch && result) {
161 qWarning() << "KisResourcesInterface::ResourceSourceAdapter::bestMatch: failed to fetch a resource using md5; falling back for filename...";
162 qWarning() << " requested:" << ppVar(md5) << ppVar(filename) << ppVar(name);
163 qWarning() << " found:" << result;
164 qWarning() << " candidates:" << foundResources;
165
166#ifdef CRASH_ON_SANITY_CHECK_FAILURE
167 qFatal("Exiting...");
168#endif /* CRASH_ON_SANITY_CHECK_FAILURE */
169 }
170#endif /* SANITY_CHECKS */
171
172 return result;
173}
174
175KoResourceLoadResult KisResourcesInterface::ResourceSourceAdapter::bestMatchLoadResult(const QString md5, const QString filename, const QString name)
176{
177 KoResourceSP resource = bestMatch(md5, filename, name);
178 return
179 resource ?
180 KoResourceLoadResult(resource) :
181 KoResourceLoadResult(KoResourceSignature(m_type, md5, filename, name));
182}
KoResourceSP bestMatch(const QString md5, const QString filename, const QString name)
bestMatch retrieves a resource, preferably by md5, but with filename and name as fallback for older f...
KoResourceLoadResult bestMatchLoadResult(const QString md5, const QString filename, const QString name)
KoResourceSP findResource(const QString md5, const QString filename, const QString name, bool exactMatch)
KoResourceSP exactMatch(const QString md5, const QString filename, const QString name)
exactMatch retrieves a resource, preferably by md5, but with filename and name as fallback for older ...
a provider-like interface class for accessing resource sources in Krita.
virtual ResourceSourceAdapter * createSourceImpl(const QString &type) const =0
ResourceSourceAdapter & source(const QString &type) const
A simple wrapper object for the main information about the resource.
#define KIS_ASSERT(cond)
Definition kis_assert.h:33
#define ppVar(var)
Definition kis_debug.h:155
QSharedPointer< KoResource > KoResourceSP