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.
72{
74
75 if (!md5.isEmpty()) {
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()) {
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()) {
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
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}
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
QSharedPointer< KoResource > KoResourceSP
const char * name(StandardAction id)