Commit 03f8cead authored by Tobias Koenig's avatar Tobias Koenig

Add a job for searching contacts by email or name

svn path=/trunk/KDE/kdepim/akonadi/contact/; revision=1010884
parent d9e3c969
......@@ -36,6 +36,7 @@ set(akonadicontact_LIB_SRC
contactgroupviewerdialog.cpp
contactmetadata.cpp
contactmetadataattribute.cpp
contactsearchjob.cpp
contactviewer.cpp
contactviewerdialog.cpp
waitingoverlay.cpp
......
/*
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"
#include <akonadi/itemfetchjob.h>
#include <akonadi/itemfetchscope.h>
#include <akonadi/itemsearchjob.h>
using namespace Akonadi;
class ContactSearchJob::Private
{
public:
Private( ContactSearchJob *parent )
: mParent( parent ), mCriterion( ContactSearchJob::Name )
{
}
void searchResult( KJob *job )
{
ItemSearchJob *searchJob = qobject_cast<ItemSearchJob*>( job );
const Item::List items = searchJob->items();
mSearchResults = items.count();
if ( mSearchResults == 0 ) {
mParent->slotResult( mParent );
return;
}
foreach ( const Item &item, items ) {
ItemFetchJob *job = new ItemFetchJob( item );
job->fetchScope().fetchFullPayload();
mParent->connect( job, SIGNAL( result( KJob* ) ), mParent, SLOT( fetchResult( KJob* ) ) );
}
}
void fetchResult( KJob *job )
{
ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
const Item::List items = fetchJob->items();
if ( items.count() == 1 ) {
mContacts.append( items.first().payload<KABC::Addressee>() );
}
mSearchResults--;
if ( mSearchResults == 0 )
mParent->slotResult( mParent );
}
ContactSearchJob *mParent;
ContactSearchJob::Criterion mCriterion;
QString mValue;
KABC::Addressee::List mContacts;
int mSearchResults;
};
ContactSearchJob::ContactSearchJob( QObject * parent )
: Job( parent ), d( new Private( this ) )
{
}
ContactSearchJob::~ContactSearchJob()
{
}
void ContactSearchJob::setQuery( Criterion criterion, const QString &value )
{
d->mCriterion = criterion;
d->mValue = value;
}
void ContactSearchJob::doStart()
{
QString query;
if ( d->mCriterion == Name ) {
query = QString::fromLatin1( ""
"prefix nco:<http://www.semanticdesktop.org/ontologies/2007/03/22/nco#>"
"SELECT ?r WHERE {"
" ?r nco:fullname \"%1\"^^<http://www.w3.org/2001/XMLSchema#string>."
"}" );
} else if ( d->mCriterion == Email ) {
query = QString::fromLatin1( ""
"prefix nco:<http://www.semanticdesktop.org/ontologies/2007/03/22/nco>"
"SELECT ?person WHERE {"
" ?person nco:hasEmailAddress ?email ."
" ?email nco:emailAddress \"%1\"^^<http://www.w3.org/2001/XMLSchema#string> ."
" }" );
}
query = query.arg( d->mValue );
ItemSearchJob *job = new ItemSearchJob( query, this );
connect( job, SIGNAL( result( KJob* ) ), this, SLOT( searchResult( KJob* ) ) );
job->start();
}
KABC::Addressee::List ContactSearchJob::contacts() const
{
return d->mContacts;
}
#include "contactsearchjob.moc"
/*
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"
#include <akonadi/job.h>
#include <kabc/addressee.h>
namespace Akonadi {
/**
* @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.
*
* @code
*
* Akonadi::ContactSearchJob *job = new Akonadi::ContactSearchJob();
* job->
* connect( job, SIGNAL( result( KJob* ) ), this, SLOT( searchResult( KJob* ) ) );
*
* ...
*
* MyClass::searchResult( KJob *job )
* {
* Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob*>( job );
* const KABC::Addressee::List contacts = searchJob->contact();
* // do something with the contacts
* }
*
* @endcode
*
* @author Tobias Koenig <tokoe@kde.org>
*/
class AKONADI_CONTACT_EXPORT ContactSearchJob : public Job
{
Q_OBJECT
public:
/**
* Creates a new contact search job.
*
* @param parent The parent object.
*/
explicit ContactSearchJob( QObject *parent = 0 );
/**
* Destroys the contact search job.
*/
~ContactSearchJob();
/**
* Describes the criteria that can be searched for.
*/
enum Criterion
{
Name, ///< The name of the contact.
Email ///< The email address of the contact.
};
/**
* Sets the @p criterion and @p value for the search.
*/
void setQuery( Criterion criterion, const QString &value );
/**
* Returns the contacts that matched the search criteria.
*
* @note The items only contain the uid but no payload.
*/
KABC::Addressee::List contacts() const;
protected:
void doStart();
private:
//@cond PRIVATE
class Private;
Private* const d;
Q_PRIVATE_SLOT( d, void searchResult( KJob* ) )
Q_PRIVATE_SLOT( d, void fetchResult( KJob* ) )
//@endcond
};
}
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment