File indexing completed on 2024-05-12 05:11:18

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