File indexing completed on 2024-04-28 05:11:00

0001 /*
0002  * articlematcher.h
0003  *
0004  * SPDX-FileCopyrightText: 2004, 2005 Frerich Raabe <raabe@kde.org>
0005  * SPDX-FileCopyrightText: 2005 Frank Osterfeld <osterfeld@kde.org>
0006  *
0007  * SPDX-License-Identifier: BSD-2-Clause
0008  */
0009 #pragma once
0010 
0011 #include "akregatorpart_export.h"
0012 #include <QList>
0013 #include <QString>
0014 #include <QVariant>
0015 
0016 class KConfigGroup;
0017 
0018 namespace Akregator
0019 {
0020 class Article;
0021 
0022 namespace Filters
0023 {
0024 class AbstractMatcher;
0025 class Criterion;
0026 
0027 /** Abstract base class for matchers, a matcher just takes an article and checks whether the article matches some criterion or not.
0028  *  @author Frank Osterfeld
0029  */
0030 class AKREGATORPART_EXPORT AbstractMatcher
0031 {
0032 public:
0033     virtual ~AbstractMatcher();
0034 
0035     virtual bool matches(const Article &article) const = 0;
0036 
0037     virtual void writeConfig(KConfigGroup *config) const = 0;
0038     virtual void readConfig(KConfigGroup *config) = 0;
0039 
0040     virtual bool operator==(const AbstractMatcher &) const = 0;
0041     virtual bool operator!=(const AbstractMatcher &other) const = 0;
0042 
0043 protected:
0044     AbstractMatcher();
0045 
0046 private:
0047     Q_DISABLE_COPY(AbstractMatcher)
0048 };
0049 
0050 /** a powerful matcher supporting multiple criterions, which can be combined      via logical OR or AND
0051  *  @author Frerich Raabe
0052  */
0053 class AKREGATORPART_EXPORT ArticleMatcher : public AbstractMatcher
0054 {
0055 public:
0056     enum Association { None, LogicalAnd, LogicalOr };
0057 
0058     ArticleMatcher();
0059     ArticleMatcher(const QList<Criterion> &criteria, Association assoc);
0060 
0061     ~ArticleMatcher() override;
0062 
0063     bool matches(const Article &article) const override;
0064     bool operator==(const AbstractMatcher &other) const override;
0065     bool operator!=(const AbstractMatcher &other) const override;
0066 
0067     void writeConfig(KConfigGroup *config) const override;
0068     void readConfig(KConfigGroup *config) override;
0069 
0070 private:
0071     static Association stringToAssociation(const QString &assocStr);
0072     static QString associationToString(Association association);
0073 
0074     bool anyCriterionMatches(const Article &a) const;
0075     bool allCriteriaMatch(const Article &a) const;
0076 
0077     QList<Criterion> m_criteria;
0078     Association m_association;
0079 };
0080 
0081 /** Criterion for ArticleMatcher
0082  *  @author Frerich Raabe
0083  */
0084 class AKREGATORPART_EXPORT Criterion
0085 {
0086 public:
0087     enum Subject { Title, Description, Link, Status, KeepFlag, Author };
0088 
0089     static QString subjectToString(Subject subj);
0090     static Subject stringToSubject(const QString &subjStr);
0091 
0092     enum Predicate { Contains = 0x01, Equals = 0x02, Matches = 0x03, Negation = 0x80 };
0093 
0094     static QString predicateToString(Predicate pred);
0095     static Predicate stringToPredicate(const QString &predStr);
0096 
0097     Criterion();
0098     Criterion(Subject subject, Predicate predicate, const QVariant &object);
0099     virtual ~Criterion() = default;
0100 
0101     bool satisfiedBy(const Article &article) const;
0102 
0103     virtual void writeConfig(KConfigGroup *config) const;
0104     virtual void readConfig(KConfigGroup *config);
0105 
0106     Subject subject() const;
0107     Predicate predicate() const;
0108     QVariant object() const;
0109     bool operator==(const Criterion &other) const
0110     {
0111         return m_subject == other.m_subject && m_predicate == other.m_predicate && m_object == other.m_object;
0112     }
0113 
0114 private:
0115     Subject m_subject;
0116     Predicate m_predicate;
0117     QVariant m_object;
0118 };
0119 } // namespace Filters
0120 } // namespace Akregator