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
17
22
27
32
34{
35 Q_D(const KisResourcesInterface);
36
37 // use double-locking for fetching the source
38
40
41 {
42 QReadLocker l(&d->lock);
43 source = d->findExistingSource(type);
44 if (source) return *source;
45 }
46
47 {
48 QWriteLocker l(&d->lock);
49 source = d->findExistingSource(type);
50 if (source) return *source;
51
53
54 std::unique_ptr<ResourceSourceAdapter> sourcePtr(source);
55 d->sourceAdapters.emplace(type, std::move(sourcePtr));
56 }
57
59 return *source;
60}
61
63 : m_type(type)
64{
65}
66
70
71KoResourceSP KisResourcesInterface::ResourceSourceAdapter::bestMatch(const QString md5, const QString filename, const QString name)
72{
74
75 if (!md5.isEmpty()) {
76 Q_FOREACH (KoResourceSP res, resourcesForMD5(md5)) {
77 int penalty = 0;
78
79 if (!res->active()) {
80 penalty += 100;
81 }
82
83 if (!filename.isEmpty() && filename != res->filename()) {
86 penalty += 2;
87 }
88
89 if (!name.isEmpty() && name != res->name()) {
90 penalty++;
91 }
92
93 foundResources.append(qMakePair(res, penalty));
94 }
95 }
96
97#ifdef SANITY_CHECKS
104 const bool warnAboutIncorrectMd5Fetch =
105 foundResources.isEmpty() && !md5.isEmpty();
106#endif
107
108 if (foundResources.isEmpty()) {
109 if (!filename.isEmpty()) {
110 Q_FOREACH (KoResourceSP res, resourcesForFilename(filename)) {
111 int penalty = 0;
112
113 if (!res->active()) {
114 penalty += 100;
115 }
116
117 if (!name.isEmpty() && name != res->name()) {
118 penalty++;
119 }
120
121 foundResources.append(qMakePair(res, penalty));
122 }
123 } else if (!name.isEmpty()) {
124 Q_FOREACH (KoResourceSP res, resourcesForName(name)) {
125 int penalty = 0;
126
127 if (!res->active()) {
128 penalty += 100;
129 }
130
131 foundResources.append(qMakePair(res, penalty));
132 }
133 }
134 }
135
136 auto it = std::min_element(foundResources.begin(), foundResources.end(),
137 [] (const QPair<KoResourceSP, int> &lhs,
138 const QPair<KoResourceSP, int> &rhs) {return lhs.second < rhs.second;});
139
140 KoResourceSP result = it != foundResources.end() ? it->first : KoResourceSP();
141
142#ifdef SANITY_CHECKS
143 if (warnAboutIncorrectMd5Fetch && result) {
144 qWarning() << "KisResourcesInterface::ResourceSourceAdapter::bestMatch: failed to fetch a resource using md5; falling back for filename...";
145 qWarning() << " requested:" << ppVar(md5) << ppVar(filename) << ppVar(name);
146 qWarning() << " found:" << result;
147 }
148#endif
149
150 return result;
151}
152
153KoResourceLoadResult KisResourcesInterface::ResourceSourceAdapter::bestMatchLoadResult(const QString md5, const QString filename, const QString name)
154{
155 KoResourceSP resource = bestMatch(md5, filename, name);
156 return
157 resource ?
158 KoResourceLoadResult(resource) :
159 KoResourceLoadResult(KoResourceSignature(m_type, md5, filename, name));
160}
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)
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