contactsearchjob.h 4.65 KB
Newer Older
1
/*
Tobias Koenig's avatar
Tobias Koenig committed
2 3
    This file is part of Akonadi Contact.

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
    Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#ifndef AKONADI_CONTACTSEARCHJOB_H
#define AKONADI_CONTACTSEARCHJOB_H

#include "akonadi-contact_export.h"

27
#include <itemsearchjob.h>
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
28
#include <kcontacts/addressee.h>
29

Laurent Montel's avatar
Laurent Montel committed
30 31
namespace Akonadi
{
32 33 34 35 36 37 38

/**
 * @short Job that searches for contacts in the Akonadi storage.
 *
 * This job searches for contacts that match given search criteria and returns
 * the list of contacts.
 *
39 40
 * Examples:
 *
41 42
 * @code
 *
43
 * // Search all contacts with email address tokoe@kde.org
44
 * Akonadi::ContactSearchJob *job = new Akonadi::ContactSearchJob();
Tobias Koenig's avatar
Tobias Koenig committed
45
 * job->setQuery( Akonadi::ContactSearchJob::Email, "tokoe@kde.org" );
Guy Maurel's avatar
Guy Maurel committed
46
 * connect( job, SIGNAL(result(KJob*)), this, SLOT(searchResult(KJob*)) );
47 48 49 50 51 52
 *
 * ...
 *
 * MyClass::searchResult( KJob *job )
 * {
 *   Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob*>( job );
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
53
 *   const KContacts::Addressee::List contacts = searchJob->contacts();
54 55 56 57 58 59 60 61 62
 *   // do something with the contacts
 * }
 *
 * @endcode
 *
 * @code
 *
 * // Search for all existing contacts
 * Akonadi::ContactSearchJob *job = new Akonadi::ContactSearchJob();
Guy Maurel's avatar
Guy Maurel committed
63
 * connect( job, SIGNAL(result(KJob*)), this, SLOT(searchResult(KJob*)) );
64 65 66 67 68 69
 *
 * ...
 *
 * MyClass::searchResult( KJob *job )
 * {
 *   Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob*>( job );
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
70
 *   const KContacts::Addressee::List contacts = searchJob->contacts();
71 72 73 74 75 76
 *   // do something with the contacts
 * }
 *
 * @endcode
 *
 * @author Tobias Koenig <tokoe@kde.org>
Tobias Koenig's avatar
Tobias Koenig committed
77
 * @since 4.4
78
 */
79
class AKONADI_CONTACT_EXPORT ContactSearchJob : public ItemSearchJob
80
{
Guy Maurel's avatar
Guy Maurel committed
81
    Q_OBJECT
82

Guy Maurel's avatar
Guy Maurel committed
83
public:
84 85 86 87 88
    /**
     * Creates a new contact search job.
     *
     * @param parent The parent object.
     */
Laurent Montel's avatar
Laurent Montel committed
89
    explicit ContactSearchJob(QObject *parent = nullptr);
90 91 92 93 94 95 96 97 98

    /**
     * Destroys the contact search job.
     */
    ~ContactSearchJob();

    /**
     * Describes the criteria that can be searched for.
     */
99
    enum Criterion {
Guy Maurel's avatar
Guy Maurel committed
100 101 102 103 104
        Name,       ///< The name of the contact.
        Email,      ///< The email address of the contact.
        NickName,   ///< The nickname of the contact.
        NameOrEmail, ///< The name or email address of the contact. @since 4.5
        ContactUid   ///< The global unique identifier of the contact. @since 4.5
105 106 107 108 109 110 111
    };

    /**
     * Describes the type of pattern matching that shall be used.
     *
     * @since 4.5
     */
112
    enum Match {
Guy Maurel's avatar
Guy Maurel committed
113 114 115 116
        ExactMatch,      ///< The result must match exactly the pattern (case sensitive).
        StartsWithMatch, ///< The result must start with the pattern (case insensitive).
        ContainsMatch,    ///< The result must contain the pattern (case insensitive).
        ContainsWordBoundaryMatch ///< The result must contain a word starting with the pattern (case insensitive).
117 118
    };

119 120
    /**
     * Sets the @p criterion and @p value for the search with @p match.
Kevin Krammer's avatar
Kevin Krammer committed
121 122 123
     * @param criterion the query criterion to compare with
     * @param value the value to match against
     * @param match how to match the given value
124 125
     * @since 4.5
     */
Guy Maurel's avatar
Guy Maurel committed
126
    void setQuery(Criterion criterion, const QString &value, Match match = ExactMatch);
127

128 129
    /**
     * Sets a @p limit on how many results will be returned by this search job.
Kevin Krammer's avatar
Kevin Krammer committed
130
     *
131
     * This is useful in situation where for example only the first search result is needed anyway,
132
     * setting a limit of 1 here will greatly reduce the resource usage during the
133 134 135
     * search.
     * This needs to be called before calling setQuery() to have an effect.
     * By default, the number of results is unlimited.
Kevin Krammer's avatar
Kevin Krammer committed
136
     * @param limit the upper limit for number of search results
137
     */
Guy Maurel's avatar
Guy Maurel committed
138
    void setLimit(int limit);
139

140 141
    /**
     * Returns the contacts that matched the search criteria.
142
     */
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
143
    KContacts::Addressee::List contacts() const;
144

Guy Maurel's avatar
Guy Maurel committed
145
private:
146 147
    //@cond PRIVATE
    class Private;
Guy Maurel's avatar
Guy Maurel committed
148
    Private *const d;
149 150 151 152 153 154
    //@endcond
};

}

#endif