| 1 | /* |
| 2 | This file is part of the KDE Baloo Project |
| 3 | SPDX-FileCopyrightText: 2013 Vishesh Handa <me@vhanda.in> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 6 | */ |
| 7 | |
| 8 | #ifndef BALOO_TERM_H |
| 9 | #define BALOO_TERM_H |
| 10 | |
| 11 | #include <QString> |
| 12 | #include <QVariant> |
| 13 | #include <QDebug> |
| 14 | |
| 15 | #include <memory> |
| 16 | |
| 17 | namespace Baloo { |
| 18 | |
| 19 | class Term |
| 20 | { |
| 21 | public: |
| 22 | enum Comparator { |
| 23 | Auto, |
| 24 | Equal, |
| 25 | Contains, |
| 26 | Greater, |
| 27 | GreaterEqual, |
| 28 | Less, |
| 29 | LessEqual, |
| 30 | }; |
| 31 | |
| 32 | enum Operation { |
| 33 | None, |
| 34 | And, |
| 35 | Or, |
| 36 | }; |
| 37 | |
| 38 | Term(); |
| 39 | Term(const Term& t); |
| 40 | |
| 41 | /* |
| 42 | * The Item must contain the property property |
| 43 | */ |
| 44 | explicit Term(const QString& property); |
| 45 | |
| 46 | /* |
| 47 | * The Item must contain the property property with |
| 48 | * value value. |
| 49 | * |
| 50 | * The default comparator is Auto which has the following behavior |
| 51 | * For Strings - Contains |
| 52 | * For DateTime - Contains |
| 53 | * For any other type - Equals |
| 54 | */ |
| 55 | Term(const QString& property, const QVariant& value, Comparator c = Auto); |
| 56 | |
| 57 | /* |
| 58 | * This term is a combination of other terms |
| 59 | */ |
| 60 | explicit Term(Operation op); |
| 61 | Term(Operation op, const Term& t); |
| 62 | Term(Operation op, const QList<Term>& t); |
| 63 | Term(const Term& lhs, Operation op, const Term& rhs); |
| 64 | ~Term(); |
| 65 | |
| 66 | bool isValid() const; |
| 67 | |
| 68 | /* |
| 69 | * Negate this term. Negation only applies for Equal or Contains |
| 70 | * For other Comparators you must invert it yourself |
| 71 | */ |
| 72 | void setNegation(bool isNegated); |
| 73 | |
| 74 | bool negated() const; |
| 75 | bool isNegated() const; |
| 76 | |
| 77 | void addSubTerm(const Term& term); |
| 78 | void setSubTerms(const QList<Term>& terms); |
| 79 | |
| 80 | /* |
| 81 | * Returns the first subTerm in the list of subTerms |
| 82 | */ |
| 83 | Term subTerm() const; |
| 84 | QList<Term> subTerms() const; |
| 85 | |
| 86 | void setOperation(Operation op); |
| 87 | Operation operation() const; |
| 88 | |
| 89 | bool isEmpty() const; |
| 90 | bool empty() const; |
| 91 | |
| 92 | /* |
| 93 | * Return the property this term is targeting |
| 94 | */ |
| 95 | QString property() const; |
| 96 | void setProperty(const QString& property); |
| 97 | |
| 98 | QVariant value() const; |
| 99 | void setValue(const QVariant& value); |
| 100 | |
| 101 | Comparator comparator() const; |
| 102 | void setComparator(Comparator c); |
| 103 | |
| 104 | void setUserData(const QString& name, const QVariant& value); |
| 105 | QVariant userData(const QString& name) const; |
| 106 | |
| 107 | QVariantMap toVariantMap() const; |
| 108 | static Term fromVariantMap(const QVariantMap& map); |
| 109 | |
| 110 | bool operator == (const Term& rhs) const; |
| 111 | |
| 112 | Term& operator=(const Term& rhs); |
| 113 | |
| 114 | private: |
| 115 | class Private; |
| 116 | std::unique_ptr<Private> const d; |
| 117 | }; |
| 118 | |
| 119 | inline Term operator &&(const Term& lhs, const Term& rhs) |
| 120 | { |
| 121 | if (lhs.isEmpty()) |
| 122 | return rhs; |
| 123 | else if (rhs.isEmpty()) |
| 124 | return lhs; |
| 125 | |
| 126 | return {lhs, Term::And, rhs}; |
| 127 | } |
| 128 | |
| 129 | inline Term operator ||(const Term& lhs, const Term& rhs) |
| 130 | { |
| 131 | if (lhs.isEmpty()) |
| 132 | return rhs; |
| 133 | else if (rhs.isEmpty()) |
| 134 | return lhs; |
| 135 | |
| 136 | return {lhs, Term::Or, rhs}; |
| 137 | } |
| 138 | |
| 139 | inline Term operator !(const Term& rhs) |
| 140 | { |
| 141 | Term t(rhs); |
| 142 | t.setNegation(!rhs.isNegated()); |
| 143 | return t; |
| 144 | } |
| 145 | |
| 146 | char *toString(const Term& term); |
| 147 | |
| 148 | } |
| 149 | |
| 150 | QDebug operator <<(QDebug d, const Baloo::Term& t); |
| 151 | |
| 152 | Q_DECLARE_TYPEINFO(Baloo::Term, Q_RELOCATABLE_TYPE); |
| 153 | |
| 154 | #endif |
| 155 | |