File indexing completed on 2024-11-24 04:39:26

0001 /*
0002     This file is part of Akonadi Contact.
0003 
0004     SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #pragma once
0010 
0011 #include "akonadi-contact-core_export.h"
0012 
0013 #include <Akonadi/ItemSearchJob>
0014 #include <KContacts/Addressee>
0015 
0016 #include <memory>
0017 
0018 namespace Akonadi
0019 {
0020 class ContactSearchJobPrivate;
0021 
0022 /**
0023  * @short Job that searches for contacts in the Akonadi storage.
0024  *
0025  * This job searches for contacts that match given search criteria and returns
0026  * the list of contacts.
0027  *
0028  * Examples:
0029  *
0030  * @code
0031  *
0032  * // Search all contacts with email address tokoe@kde.org
0033  * Akonadi::ContactSearchJob *job = new Akonadi::ContactSearchJob();
0034  * job->setQuery( Akonadi::ContactSearchJob::Email, "tokoe@kde.org" );
0035  * connect( job, SIGNAL(result(KJob*)), this, SLOT(searchResult(KJob*)) );
0036  *
0037  * ...
0038  *
0039  * MyClass::searchResult( KJob *job )
0040  * {
0041  *   Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob*>( job );
0042  *   const KContacts::Addressee::List contacts = searchJob->contacts();
0043  *   // do something with the contacts
0044  * }
0045  *
0046  * @endcode
0047  *
0048  * @code
0049  *
0050  * // Search for all existing contacts
0051  * Akonadi::ContactSearchJob *job = new Akonadi::ContactSearchJob();
0052  * connect( job, SIGNAL(result(KJob*)), this, SLOT(searchResult(KJob*)) );
0053  *
0054  * ...
0055  *
0056  * MyClass::searchResult( KJob *job )
0057  * {
0058  *   Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob*>( job );
0059  *   const KContacts::Addressee::List contacts = searchJob->contacts();
0060  *   // do something with the contacts
0061  * }
0062  *
0063  * @endcode
0064  *
0065  * @author Tobias Koenig <tokoe@kde.org>
0066  * @since 4.4
0067  */
0068 class AKONADI_CONTACT_CORE_EXPORT ContactSearchJob : public ItemSearchJob
0069 {
0070     Q_OBJECT
0071 
0072 public:
0073     /**
0074      * Creates a new contact search job.
0075      *
0076      * @param parent The parent object.
0077      */
0078     explicit ContactSearchJob(QObject *parent = nullptr);
0079 
0080     /**
0081      * Destroys the contact search job.
0082      */
0083     ~ContactSearchJob() override;
0084 
0085     /**
0086      * Describes the criteria that can be searched for.
0087      */
0088     enum Criterion {
0089         Name, ///< The name of the contact.
0090         Email, ///< The email address of the contact.
0091         NickName, ///< The nickname of the contact.
0092         NameOrEmail, ///< The name or email address of the contact. @since 4.5
0093         ContactUid ///< The global unique identifier of the contact. @since 4.5
0094     };
0095 
0096     /**
0097      * Describes the type of pattern matching that shall be used.
0098      *
0099      * @since 4.5
0100      */
0101     enum Match {
0102         ExactMatch, ///< The result must match exactly the pattern (case sensitive).
0103         StartsWithMatch, ///< The result must start with the pattern (case insensitive).
0104         ContainsMatch, ///< The result must contain the pattern (case insensitive).
0105         ContainsWordBoundaryMatch ///< The result must contain a word starting with the pattern (case insensitive).
0106     };
0107 
0108     /**
0109      * Sets the @p criterion and @p value for the search with @p match.
0110      * @param criterion the query criterion to compare with
0111      * @param value the value to match against
0112      * @param match how to match the given value
0113      * @since 4.5
0114      */
0115     void setQuery(Criterion criterion, const QString &value, Match match = ExactMatch);
0116 
0117     /**
0118      * Sets a @p limit on how many results will be returned by this search job.
0119      *
0120      * This is useful in situation where for example only the first search result is needed anyway,
0121      * setting a limit of 1 here will greatly reduce the resource usage during the
0122      * search.
0123      * This needs to be called before calling setQuery() to have an effect.
0124      * By default, the number of results is unlimited.
0125      * @param limit the upper limit for number of search results
0126      */
0127     void setLimit(int limit);
0128 
0129     /**
0130      * Returns the contacts that matched the search criteria.
0131      */
0132     [[nodiscard]] KContacts::Addressee::List contacts() const;
0133 
0134 private:
0135     //@cond PRIVATE
0136     std::unique_ptr<ContactSearchJobPrivate> const d;
0137     //@endcond
0138 };
0139 }