File indexing completed on 2024-04-14 03:49:49

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