File indexing completed on 2024-04-28 15:17:41

0001 /*
0002     This file is part of the KDE Baloo Project
0003     SPDX-FileCopyrightText: 2013 Vishesh Handa <me@vhanda.in>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #ifndef BALOO_TERM_H
0009 #define BALOO_TERM_H
0010 
0011 #include <QString>
0012 #include <QVariant>
0013 #include <QDebug>
0014 
0015 namespace Baloo {
0016 
0017 class Term
0018 {
0019 public:
0020     enum Comparator {
0021         Auto,
0022         Equal,
0023         Contains,
0024         Greater,
0025         GreaterEqual,
0026         Less,
0027         LessEqual,
0028     };
0029 
0030     enum Operation {
0031         None,
0032         And,
0033         Or,
0034     };
0035 
0036     Term();
0037     Term(const Term& t);
0038 
0039     /**
0040      * The Item must contain the property \p property
0041      */
0042     explicit Term(const QString& property);
0043 
0044     /**
0045      * The Item must contain the property \p property with
0046      * value \value.
0047      *
0048      * The default comparator is Auto which has the following behavior
0049      * For Strings - Contains
0050      * For DateTime - Contains
0051      * For any other type - Equals
0052      */
0053     Term(const QString& property, const QVariant& value, Comparator c = Auto);
0054 
0055     /**
0056      * This term is a combination of other terms
0057      */
0058     explicit Term(Operation op);
0059     Term(Operation op, const Term& t);
0060     Term(Operation op, const QList<Term>& t);
0061     Term(const Term& lhs, Operation op, const Term& rhs);
0062     ~Term();
0063 
0064     bool isValid() const;
0065 
0066     /**
0067      * Negate this term. Negation only applies for Equal or Contains
0068      * For other Comparators you must invert it yourself
0069      */
0070     void setNegation(bool isNegated);
0071 
0072     bool negated() const;
0073     bool isNegated() const;
0074 
0075     void addSubTerm(const Term& term);
0076     void setSubTerms(const QList<Term>& terms);
0077 
0078     /**
0079      * Returns the first subTerm in the list of subTerms
0080      */
0081     Term subTerm() const;
0082     QList<Term> subTerms() const;
0083 
0084     void setOperation(Operation op);
0085     Operation operation() const;
0086 
0087     bool isEmpty() const;
0088     bool empty() const;
0089 
0090     /**
0091      * Return the property this term is targeting
0092      */
0093     QString property() const;
0094     void setProperty(const QString& property);
0095 
0096     QVariant value() const;
0097     void setValue(const QVariant& value);
0098 
0099     Comparator comparator() const;
0100     void setComparator(Comparator c);
0101 
0102     void setUserData(const QString& name, const QVariant& value);
0103     QVariant userData(const QString& name) const;
0104 
0105     QVariantMap toVariantMap() const;
0106     static Term fromVariantMap(const QVariantMap& map);
0107 
0108     bool operator == (const Term& rhs) const;
0109 
0110     Term& operator=(const Term& rhs);
0111 
0112 private:
0113     class Private;
0114     Private* d;
0115 };
0116 
0117 inline Term operator &&(const Term& lhs, const Term& rhs)
0118 {
0119     if (lhs.isEmpty())
0120         return rhs;
0121     else if (rhs.isEmpty())
0122         return lhs;
0123 
0124     return {lhs, Term::And, rhs};
0125 }
0126 
0127 inline Term operator ||(const Term& lhs, const Term& rhs)
0128 {
0129     if (lhs.isEmpty())
0130         return rhs;
0131     else if (rhs.isEmpty())
0132         return lhs;
0133 
0134     return {lhs, Term::Or, rhs};
0135 }
0136 
0137 inline Term operator !(const Term& rhs)
0138 {
0139     Term t(rhs);
0140     t.setNegation(!rhs.isNegated());
0141     return t;
0142 }
0143 
0144 /**
0145  * Helper for QTest
0146  * \sa QTest::toString
0147  *
0148  * @since: 5.70
0149  */
0150 char *toString(const Term& term);
0151 
0152 }
0153 
0154 QDebug operator <<(QDebug d, const Baloo::Term& t);
0155 
0156 #endif