File indexing completed on 2024-05-26 05:14:16

0001 /*
0002     SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QSharedPointer>
0010 
0011 #include "akonadicore_export.h"
0012 
0013 namespace Akonadi
0014 {
0015 class SearchTermPrivate;
0016 
0017 /**
0018  * Search term represents the actual condition within query.
0019  *
0020  * SearchTerm can either have multiple subterms, or can be so-called endterm, when
0021  * there are no more subterms, but instead the actual condition is specified, that
0022  * is have key, value and relation between them.
0023  *
0024  * @since 4.13
0025  */
0026 class AKONADICORE_EXPORT SearchTerm
0027 {
0028 public:
0029     enum Relation {
0030         RelAnd,
0031         RelOr,
0032     };
0033 
0034     enum Condition {
0035         CondEqual,
0036         CondGreaterThan,
0037         CondGreaterOrEqual,
0038         CondLessThan,
0039         CondLessOrEqual,
0040         CondContains,
0041     };
0042 
0043     /**
0044      * Constructs a term where all subterms will be in given relation
0045      */
0046     explicit SearchTerm(SearchTerm::Relation relation = SearchTerm::RelAnd);
0047 
0048     /**
0049      * Constructs an end term
0050      */
0051     SearchTerm(const QString &key, const QVariant &value, SearchTerm::Condition condition = SearchTerm::CondEqual);
0052 
0053     SearchTerm(const SearchTerm &other);
0054     ~SearchTerm();
0055 
0056     SearchTerm &operator=(const SearchTerm &other);
0057     [[nodiscard]] bool operator==(const SearchTerm &other) const;
0058 
0059     [[nodiscard]] bool isNull() const;
0060 
0061     /**
0062      * Returns key of this end term.
0063      */
0064     [[nodiscard]] QString key() const;
0065 
0066     /**
0067      * Returns value of this end term.
0068      */
0069     [[nodiscard]] QVariant value() const;
0070 
0071     /**
0072      * Returns relation between key and value.
0073      */
0074     [[nodiscard]] SearchTerm::Condition condition() const;
0075 
0076     /**
0077      * Adds a new subterm to this term.
0078      *
0079      * Subterms will be in relation as specified in SearchTerm constructor.
0080      *
0081      * If there are subterms in a term, key, value and condition are ignored.
0082      */
0083     void addSubTerm(const SearchTerm &term);
0084 
0085     /**
0086      * Returns all subterms, or an empty list if this is an end term.
0087      */
0088     [[nodiscard]] QList<SearchTerm> subTerms() const;
0089 
0090     /**
0091      * Returns relation in which all subterms are.
0092      */
0093     [[nodiscard]] SearchTerm::Relation relation() const;
0094 
0095     /**
0096      * Sets whether the entire term is negated.
0097      */
0098     void setIsNegated(bool negated);
0099 
0100     /**
0101      * Returns whether the entire term is negated.
0102      */
0103     [[nodiscard]] bool isNegated() const;
0104 
0105 private:
0106     QSharedDataPointer<SearchTermPrivate> d;
0107 };
0108 
0109 class SearchQueryPrivate;
0110 
0111 /**
0112  * @brief A query that can be passed to ItemSearchJob or others.
0113  *
0114  * @since 4.13
0115  */
0116 class AKONADICORE_EXPORT SearchQuery
0117 {
0118 public:
0119     /**
0120      * Constructs query where all added terms will be in given relation
0121      */
0122     explicit SearchQuery(SearchTerm::Relation rel = SearchTerm::RelAnd);
0123 
0124     ~SearchQuery();
0125     SearchQuery(const SearchQuery &other);
0126     SearchQuery &operator=(const SearchQuery &other);
0127     bool operator==(const SearchQuery &other) const;
0128 
0129     bool isNull() const;
0130 
0131     /**
0132      * Adds a new term.
0133      */
0134     void addTerm(const QString &key, const QVariant &value, SearchTerm::Condition condition = SearchTerm::CondEqual);
0135 
0136     /**
0137      * Adds a new term with subterms
0138      */
0139     void addTerm(const SearchTerm &term);
0140 
0141     /**
0142      * Sets the root term
0143      */
0144     void setTerm(const SearchTerm &term);
0145 
0146     /**
0147      * Returns the root term.
0148      */
0149     SearchTerm term() const;
0150 
0151     /**
0152      * Sets the maximum number of results.
0153      *
0154      * Note that this limit is only evaluated per search backend,
0155      * so the total number of results retrieved may be larger.
0156      */
0157     void setLimit(int limit);
0158 
0159     /**
0160      * Returns the maximum number of results.
0161      *
0162      * The default value is -1, indicating no limit.
0163      */
0164     int limit() const;
0165 
0166     QByteArray toJSON() const;
0167     static SearchQuery fromJSON(const QByteArray &json);
0168 
0169 private:
0170     QSharedDataPointer<SearchQueryPrivate> d;
0171 };
0172 
0173 /**
0174  * A search term for an email field.
0175  *
0176  * This class can be used to create queries that akonadi email search backends understand.
0177  *
0178  * @since 4.13
0179  */
0180 class AKONADICORE_EXPORT EmailSearchTerm : public SearchTerm
0181 {
0182 public:
0183     /**
0184      * All fields expect a search string unless noted otherwise.
0185      */
0186     enum EmailSearchField {
0187         Unknown,
0188         Subject,
0189         Body,
0190         Message, // Complete message including headers, body and attachment
0191         Headers, // All headers
0192         HeaderFrom,
0193         HeaderTo,
0194         HeaderCC,
0195         HeaderBCC,
0196         HeaderReplyTo,
0197         HeaderOrganization,
0198         HeaderListId,
0199         HeaderResentFrom,
0200         HeaderXLoop,
0201         HeaderXMailingList,
0202         HeaderXSpamFlag,
0203         HeaderDate, // Expects QDateTime
0204         HeaderOnlyDate, // Expects QDate
0205         MessageStatus, // Expects message flag from Akonadi::MessageFlags. Boolean filter.
0206         ByteSize, // Expects int
0207         Attachment, // Textsearch on attachment
0208         MessageTag
0209     };
0210 
0211     /**
0212      * Constructs an email end term
0213      */
0214     EmailSearchTerm(EmailSearchField field, const QVariant &value, SearchTerm::Condition condition = SearchTerm::CondEqual);
0215 
0216     /**
0217      * Translates field to key
0218      */
0219     static QString toKey(EmailSearchField);
0220 
0221     /**
0222      * Translates key to field
0223      */
0224     static EmailSearchField fromKey(const QString &key);
0225 };
0226 
0227 /**
0228  * A search term for a contact field.
0229  *
0230  * This class can be used to create queries that akonadi contact search backends understand.
0231  *
0232  * @since 4.13
0233  */
0234 class AKONADICORE_EXPORT ContactSearchTerm : public SearchTerm
0235 {
0236 public:
0237     enum ContactSearchField {
0238         Unknown,
0239         Name,
0240         Email,
0241         Nickname,
0242         Uid,
0243         All // Special field: matches all contacts.
0244     };
0245 
0246     ContactSearchTerm(ContactSearchField field, const QVariant &value, SearchTerm::Condition condition = SearchTerm::CondEqual);
0247 
0248     /**
0249      * Translates field to key
0250      */
0251     static QString toKey(ContactSearchField);
0252 
0253     /**
0254      * Translates key to field
0255      */
0256     static ContactSearchField fromKey(const QString &key);
0257 };
0258 
0259 /**
0260  * A search term for a incidence field.
0261  *
0262  * This class can be used to create queries that akonadi incidence search backends understand.
0263  *
0264  * @since 5.0
0265  */
0266 class AKONADICORE_EXPORT IncidenceSearchTerm : public SearchTerm
0267 {
0268 public:
0269     enum IncidenceSearchField {
0270         Unknown,
0271         All,
0272         PartStatus, // Own PartStatus
0273         Organizer,
0274         Summary,
0275         Location
0276     };
0277 
0278     IncidenceSearchTerm(IncidenceSearchField field, const QVariant &value, SearchTerm::Condition condition = SearchTerm::CondEqual);
0279 
0280     /**
0281      * Translates field to key
0282      */
0283     static QString toKey(IncidenceSearchField);
0284 
0285     /**
0286      * Translates key to field
0287      */
0288     static IncidenceSearchField fromKey(const QString &key);
0289 };
0290 
0291 }