File indexing completed on 2024-05-12 15:55:21

0001 /* SPDX-FileCopyrightText: 2003-2010 Jesper K. Pedersen <blackie@kde.org>
0002 
0003    SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 #include "ExactCategoryMatcher.h"
0006 
0007 #include <DB/ImageInfo.h>
0008 #include <kpabase/Logging.h>
0009 
0010 DB::ExactCategoryMatcher::ExactCategoryMatcher(const QString category)
0011     : m_category(category)
0012     , m_matcher(nullptr)
0013 {
0014 }
0015 
0016 DB::ExactCategoryMatcher::~ExactCategoryMatcher()
0017 {
0018     if (m_matcher) {
0019         delete m_matcher;
0020         m_matcher = nullptr;
0021     }
0022 }
0023 
0024 void DB::ExactCategoryMatcher::setMatcher(CategoryMatcher *subMatcher)
0025 {
0026     m_matcher = subMatcher;
0027     if (m_matcher)
0028         // always collect matched tags of _matcher:
0029         m_matcher->setShouldCreateMatchedSet(true);
0030 }
0031 
0032 bool DB::ExactCategoryMatcher::eval(ImageInfoPtr info, QMap<QString, StringSet> &alreadyMatched)
0033 {
0034     // it makes no sense to put one ExactCategoryMatcher into another, so we ignore alreadyMatched.
0035     Q_UNUSED(alreadyMatched);
0036 
0037     if (!m_matcher)
0038         return false;
0039 
0040     QMap<QString, StringSet> matchedTags;
0041 
0042     // first, do a regular match and collect all matched Tags.
0043     if (!m_matcher->eval(info, matchedTags))
0044         return false;
0045 
0046     // if the match succeeded, check if it is exact:
0047     for (const QString &item : info->itemsOfCategory(m_category))
0048         if (!matchedTags[m_category].contains(item))
0049             return false; // tag was not contained in matcher
0050     return true;
0051 }
0052 
0053 void DB::ExactCategoryMatcher::debug(int level) const
0054 {
0055     qCDebug(DBCategoryMatcherLog, "%sEXACT:", qPrintable(spaces(level)));
0056     m_matcher->debug(level + 1);
0057 }
0058 
0059 void DB::ExactCategoryMatcher::setShouldCreateMatchedSet(bool)
0060 {
0061     // no-op:
0062     // shouldCreateMatchedSet is already set to true for _matcher;
0063     // setting this to false would disable the ExactCategoryMatcher, so it is ignored.
0064 
0065     // only ExactCategoryMatcher ever calls setShouldCreateMatchedSet.
0066     // ExactCategoryMatcher are never stacked, so this can't be called.
0067     Q_ASSERT(false);
0068 }
0069 
0070 // vi:expandtab:tabstop=4 shiftwidth=4: