Krita Source Code Documentation
Loading...
Searching...
No Matches
KisResourcesInterface::ResourceSourceAdapter Class Referenceabstract

#include <KisResourcesInterface.h>

Public Member Functions

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 files that do not store the md5sum. If the resource is not found by md5 and the md5 isn't empty, then it will try to fallback to searching by filename, but will show a warning in case sanity checks are enabled.
 
KoResourceLoadResult bestMatchLoadResult (const QString md5, const QString filename, const QString name)
 
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 files that do not store the md5sum. If the resource is not found by md5 and the md5 isn't empty, then nullptr is returned (that is the difference to bestMatch).
 
virtual KoResourceSP fallbackResource () const =0
 
virtual QVector< KoResourceSPresourcesForFilename (const QString &filename) const =0
 
virtual QVector< KoResourceSPresourcesForMD5 (const QString &md5) const =0
 
virtual QVector< KoResourceSPresourcesForName (const QString &name) const =0
 
 ResourceSourceAdapter (const QString &type)
 
virtual ~ResourceSourceAdapter ()
 

Private Member Functions

KoResourceSP findResource (const QString md5, const QString filename, const QString name, bool exactMatch)
 
 Q_DISABLE_COPY (ResourceSourceAdapter)
 

Private Attributes

const QString m_type
 

Friends

class KisResourcesInterface
 

Detailed Description

Definition at line 43 of file KisResourcesInterface.h.

Constructor & Destructor Documentation

◆ ResourceSourceAdapter()

KisResourcesInterface::ResourceSourceAdapter::ResourceSourceAdapter ( const QString & type)

Definition at line 63 of file KisResourcesInterface.cpp.

◆ ~ResourceSourceAdapter()

KisResourcesInterface::ResourceSourceAdapter::~ResourceSourceAdapter ( )
virtual

Definition at line 68 of file KisResourcesInterface.cpp.

69{
70}

Member Function Documentation

◆ bestMatch()

KoResourceSP KisResourcesInterface::ResourceSourceAdapter::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 files that do not store the md5sum. If the resource is not found by md5 and the md5 isn't empty, then it will try to fallback to searching by filename, but will show a warning in case sanity checks are enabled.

If multiple resources with the same md5 exist, then it prefers the one with the same filename and name.

Returns
a resource, or 0 of the resource doesn't exist.

Definition at line 72 of file KisResourcesInterface.cpp.

73{
74 return findResource(md5, filename, name, false);
75}
KoResourceSP findResource(const QString md5, const QString filename, const QString name, bool exactMatch)

◆ bestMatchLoadResult()

KoResourceLoadResult KisResourcesInterface::ResourceSourceAdapter::bestMatchLoadResult ( const QString md5,
const QString filename,
const QString name )

Same as bestMatch(), but returns KoResourceLoadResult. In case the resource is not found in the backend storage, the load-result will be set in FailedLink state

Definition at line 175 of file KisResourcesInterface.cpp.

176{
177 KoResourceSP resource = bestMatch(md5, filename, name);
178 return
179 resource ?
180 KoResourceLoadResult(resource) :
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...
A simple wrapper object for the main information about the resource.
const char * name(StandardAction id)

◆ exactMatch()

KoResourceSP KisResourcesInterface::ResourceSourceAdapter::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 files that do not store the md5sum. If the resource is not found by md5 and the md5 isn't empty, then nullptr is returned (that is the difference to bestMatch).

If multiple resources with the same md5 exist, then it prefers the one with the same filename and name.

Returns
a resource, or 0 of the resource doesn't exist.

Definition at line 77 of file KisResourcesInterface.cpp.

78{
79 return findResource(md5, filename, name, true);
80}

◆ fallbackResource()

virtual KoResourceSP KisResourcesInterface::ResourceSourceAdapter::fallbackResource ( ) const
pure virtual

◆ findResource()

KoResourceSP KisResourcesInterface::ResourceSourceAdapter::findResource ( const QString md5,
const QString filename,
const QString name,
bool exactMatch )
private

filename is more important than name, so it gives higher penalty

In case of exact match we must find the resource via its md5 if provided

Definition at line 82 of file KisResourcesInterface.cpp.

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}
virtual QVector< KoResourceSP > resourcesForFilename(const QString &filename) const =0
virtual QVector< KoResourceSP > resourcesForMD5(const QString &md5) const =0
virtual QVector< KoResourceSP > resourcesForName(const QString &name) const =0
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 ...
#define ppVar(var)
Definition kis_debug.h:155
QSharedPointer< KoResource > KoResourceSP

References ppVar.

◆ Q_DISABLE_COPY()

KisResourcesInterface::ResourceSourceAdapter::Q_DISABLE_COPY ( ResourceSourceAdapter )
private

◆ resourcesForFilename()

virtual QVector< KoResourceSP > KisResourcesInterface::ResourceSourceAdapter::resourcesForFilename ( const QString & filename) const
pure virtual

◆ resourcesForMD5()

virtual QVector< KoResourceSP > KisResourcesInterface::ResourceSourceAdapter::resourcesForMD5 ( const QString & md5) const
pure virtual

◆ resourcesForName()

virtual QVector< KoResourceSP > KisResourcesInterface::ResourceSourceAdapter::resourcesForName ( const QString & name) const
pure virtual

Friends And Related Symbol Documentation

◆ KisResourcesInterface

friend class KisResourcesInterface
friend

Definition at line 49 of file KisResourcesInterface.h.

Member Data Documentation

◆ m_type

const QString KisResourcesInterface::ResourceSourceAdapter::m_type
private

Definition at line 95 of file KisResourcesInterface.h.


The documentation for this class was generated from the following files: