contactsearchjob.cpp 3.32 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
    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.
*/

#include "contactsearchjob.h"
23
#include <searchquery.h>
24

25
#include <itemfetchscope.h>
Kevin Krammer's avatar
Kevin Krammer committed
26

27 28
using namespace Akonadi;

29
class Q_DECL_HIDDEN ContactSearchJob::Private
30
{
Guy Maurel's avatar
Guy Maurel committed
31
public:
32 33 34
    int mLimit;
};

Guy Maurel's avatar
Guy Maurel committed
35
ContactSearchJob::ContactSearchJob(QObject *parent)
36
    : ItemSearchJob(parent)
Guy Maurel's avatar
Guy Maurel committed
37
    , d(new Private())
38
{
Guy Maurel's avatar
Guy Maurel committed
39 40
    fetchScope().fetchFullPayload();
    d->mLimit = -1;
41

Guy Maurel's avatar
Guy Maurel committed
42
    setMimeTypes(QStringList() << KContacts::Addressee::mimeType());
43

Guy Maurel's avatar
Guy Maurel committed
44 45 46 47
    // by default search for all contacts
    Akonadi::SearchQuery query;
    query.addTerm(ContactSearchTerm(ContactSearchTerm::All, QVariant(), SearchTerm::CondEqual));
    ItemSearchJob::setQuery(query);
48 49 50 51
}

ContactSearchJob::~ContactSearchJob()
{
Guy Maurel's avatar
Guy Maurel committed
52
    delete d;
53 54
}

Guy Maurel's avatar
Guy Maurel committed
55
static Akonadi::SearchTerm::Condition matchType(ContactSearchJob::Match match)
Till Adam's avatar
Till Adam committed
56
{
Guy Maurel's avatar
Guy Maurel committed
57
    switch (match) {
58
    case ContactSearchJob::ExactMatch:
Guy Maurel's avatar
Guy Maurel committed
59
        return Akonadi::SearchTerm::CondEqual;
60 61 62
    case ContactSearchJob::StartsWithMatch:
    case ContactSearchJob::ContainsWordBoundaryMatch:
    case ContactSearchJob::ContainsMatch:
Guy Maurel's avatar
Guy Maurel committed
63 64 65
        return Akonadi::SearchTerm::CondContains;
    }
    return Akonadi::SearchTerm::CondEqual;
Till Adam's avatar
Till Adam committed
66 67
}

Guy Maurel's avatar
Guy Maurel committed
68
void ContactSearchJob::setQuery(Criterion criterion, const QString &value, Match match)
69
{
Laurent Montel's avatar
Laurent Montel committed
70
    Akonadi::SearchQuery query(SearchTerm::RelOr);
Guy Maurel's avatar
Guy Maurel committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

    if (criterion == Name) {
        query.addTerm(ContactSearchTerm(ContactSearchTerm::Name, value, matchType(match)));
    } else if (criterion == Email) {
        query.addTerm(ContactSearchTerm(ContactSearchTerm::Email, value, matchType(match)));
    } else if (criterion == NickName) {
        query.addTerm(ContactSearchTerm(ContactSearchTerm::Nickname, value, matchType(match)));
    } else if (criterion == NameOrEmail) {
        query.addTerm(ContactSearchTerm(ContactSearchTerm::Name, value, matchType(match)));
        query.addTerm(ContactSearchTerm(ContactSearchTerm::Email, value, matchType(match)));
    } else if (criterion == ContactUid) {
        query.addTerm(ContactSearchTerm(ContactSearchTerm::Uid, value, matchType(match)));
    }

    query.setLimit(d->mLimit);

    ItemSearchJob::setQuery(query);
88 89
}

Guy Maurel's avatar
Guy Maurel committed
90
void ContactSearchJob::setLimit(int limit)
Thomas McGuire's avatar
Thomas McGuire committed
91
{
Guy Maurel's avatar
Guy Maurel committed
92
    d->mLimit = limit;
Thomas McGuire's avatar
Thomas McGuire committed
93 94
}

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
95
KContacts::Addressee::List ContactSearchJob::contacts() const
96
{
Guy Maurel's avatar
Guy Maurel committed
97
    KContacts::Addressee::List contacts;
98

Laurent Montel's avatar
Laurent Montel committed
99 100
    const Akonadi::Item::List lstItems = items();
    for (const Item &item : lstItems) {
Guy Maurel's avatar
Guy Maurel committed
101 102 103
        if (item.hasPayload<KContacts::Addressee>()) {
            contacts.append(item.payload<KContacts::Addressee>());
        }
104
    }
105

Guy Maurel's avatar
Guy Maurel committed
106
    return contacts;
107
}