diff --git a/akonadi/contact/contactsearchjob.cpp b/akonadi/contact/contactsearchjob.cpp index 3297f19b0..56808759c 100644 --- a/akonadi/contact/contactsearchjob.cpp +++ b/akonadi/contact/contactsearchjob.cpp @@ -1,255 +1,287 @@ /* This file is part of Akonadi Contact. Copyright (c) 2009 Tobias Koenig 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 using namespace Akonadi; class ContactSearchJob::Private { public: int mLimit; }; ContactSearchJob::ContactSearchJob( QObject * parent ) : ItemSearchJob( QString(), parent ), d( new Private() ) { fetchScope().fetchFullPayload(); d->mLimit = -1; // by default search for all contacts ItemSearchJob::setQuery( QLatin1String( "" "prefix nco:" "SELECT ?r WHERE { ?r a nco:PersonContact }" ) ); } ContactSearchJob::~ContactSearchJob() { delete d; } void ContactSearchJob::setQuery( Criterion criterion, const QString &value ) { setQuery( criterion, value, ContainsMatch ); } void ContactSearchJob::setQuery( Criterion criterion, const QString &value, Match match ) { if ( match == StartsWithMatch && value.size() < 4 ) match = ExactMatch; QString query = QString::fromLatin1( "prefix nco:" ); if ( match == ExactMatch ) { if ( criterion == Name ) { query += QString::fromLatin1( "SELECT DISTINCT ?r " "WHERE { " " graph ?g { " " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . " " ?r a nco:PersonContact . " " ?r nco:fullname \"%1\"^^. " " } " "} " ); } else if ( criterion == Email ) { query += QString::fromLatin1( "SELECT DISTINCT ?person " "WHERE { " " graph ?g { " " ?person <" + akonadiItemIdUri().toEncoded() + "> ?itemId . " " ?person a nco:PersonContact ; " " nco:hasEmailAddress ?email . " " ?email nco:emailAddress \"%1\"^^ . " " } " "}" ); } else if ( criterion == NickName ) { query += QString::fromLatin1( "SELECT DISTINCT ?r " "WHERE { " " graph ?g { " " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . " " ?r a nco:PersonContact . " " ?r nco:nickname \"%1\"^^ ." " } " "}" ); } else if ( criterion == NameOrEmail ) { query += QString::fromLatin1( "SELECT DISTINCT ?r " "WHERE { " " graph ?g { " " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . " " ?r a nco:PersonContact . " " { ?r nco:fullname \"%1\"^^. } " " UNION " " { ?r nco:nameGiven \"%1\"^^. } " " UNION " " { ?r nco:nameFamily \"%1\"^^. } " " UNION " " { ?r nco:hasEmailAddress ?email . " " ?email nco:emailAddress \"%1\"^^ . } " " } " "}" ); + } else if ( criterion == ContactUid ) { + query += QString::fromLatin1( + "SELECT DISTINCT ?r " + "WHERE { " + " graph ?g { " + " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . " + " ?r a nco:PersonContact . " + " ?r nco:contactUID \"%1\"^^ ." + " } " + "}" ); } } else if ( match == StartsWithMatch ) { if ( criterion == Name ) { query += QString::fromLatin1( "SELECT DISTINCT ?r " "WHERE { " " graph ?g { " " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . " " ?r a nco:PersonContact . " " ?r nco:fullname ?v . " " ?v bif:contains \"'%1*'\" . " " } " "} " ); } else if ( criterion == Email ) { query += QString::fromLatin1( "SELECT DISTINCT ?person " "WHERE { " " graph ?g { " " ?person <" + akonadiItemIdUri().toEncoded() + "> ?itemId . " " ?person a nco:PersonContact ; " " nco:hasEmailAddress ?email . " " ?email nco:emailAddress ?v . " " ?v bif:contains \"'%1*'\" . " " } " "}" ); } else if ( criterion == NickName ) { query += QString::fromLatin1( "SELECT DISTINCT ?r " "WHERE { " " graph ?g { " " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . " " ?r a nco:PersonContact . " " ?r nco:nickname ?v . " " ?v bif:contains \"'%1*'\" . " " } " "}" ); } else if ( criterion == NameOrEmail ) { query += QString::fromLatin1( "SELECT DISTINCT ?r " "WHERE { " " graph ?g { " " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . " " ?r a nco:PersonContact . " " { ?r nco:fullname ?v . " " ?v bif:contains \"'%1*'\" . } " " UNION " " { ?r nco:nameGiven ?v . " " ?v bif:contains \"'%1*'\" . } " " UNION " " { ?r nco:nameFamily ?v . " " ?v bif:contains \"'%1*'\" . } " " UNION " " { ?r nco:hasEmailAddress ?email . " " ?email nco:emailAddress ?v . " " ?v bif:contains \"'%1*'\" . } " " } " "}" ); - } + } else if ( criterion == ContactUid ) { + query += QString::fromLatin1( + "SELECT DISTINCT ?r " + "WHERE { " + " graph ?g { " + " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . " + " ?r a nco:PersonContact . " + " ?r nco:contactUID ?v . " + " ?v bif:contains \"'%1*'\" . " + " } " + "}" ); + } } else if ( match == ContainsMatch ) { if ( criterion == Name ) { query += QString::fromLatin1( "SELECT DISTINCT ?r " "WHERE { " " graph ?g { " " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . " " ?r a nco:PersonContact . " " ?r nco:fullname ?v . " " ?v bif:contains \"'%1'\" . " " } " "} " ); } else if ( criterion == Email ) { query += QString::fromLatin1( "SELECT DISTINCT ?person " "WHERE { " " graph ?g { " " ?person <" + akonadiItemIdUri().toEncoded() + "> ?itemId . " " ?person a nco:PersonContact ; " " nco:hasEmailAddress ?email . " " ?email nco:emailAddress ?v . " " ?v bif:contains \"'%1'\" . " " } " "}" ); } else if ( criterion == NickName ) { query += QString::fromLatin1( "SELECT DISTINCT ?r " "WHERE { " " graph ?g { " " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . " " ?r a nco:PersonContact . " " ?r nco:nickname ?v . " " ?v bif:contains \"'%1'\" . " " } " "}" ); } else if ( criterion == NameOrEmail ) { query += QString::fromLatin1( "SELECT DISTINCT ?r " "WHERE { " " graph ?g { " " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . " " ?r a nco:PersonContact . " " { ?r nco:fullname ?v . " " ?v bif:contains \"'%1'\" . } " " UNION " " { ?r nco:nameGiven ?v . " " ?v bif:contains \"'%1'\" . } " " UNION " " { ?r nco:nameFamily ?v . " " ?v bif:contains \"'%1'\" . } " " UNION " " { ?r nco:hasEmailAddress ?email . " " ?email nco:emailAddress ?v . " " ?v bif:contains \"'%1'\" . } " " } " "}" ); + } else if ( criterion == ContactUid ) { + query += QString::fromLatin1( + "SELECT DISTINCT ?r " + "WHERE { " + " graph ?g { " + " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . " + " ?r a nco:PersonContact . " + " ?r nco:contactUID ?v . " + " ?v bif:contains \"'%1'\" . " + " } " + "}" ); } } if ( d->mLimit != -1 ) { query += QString::fromLatin1( " LIMIT %1" ).arg( d->mLimit ); } query = query.arg( value ); ItemSearchJob::setQuery( query ); } void ContactSearchJob::setLimit( int limit ) { d->mLimit = limit; } KABC::Addressee::List ContactSearchJob::contacts() const { KABC::Addressee::List contacts; foreach ( const Item &item, items() ) { if ( item.hasPayload() ) contacts.append( item.payload() ); } return contacts; } #include "contactsearchjob.moc" diff --git a/akonadi/contact/contactsearchjob.h b/akonadi/contact/contactsearchjob.h index 8059f44f8..be29fd698 100644 --- a/akonadi/contact/contactsearchjob.h +++ b/akonadi/contact/contactsearchjob.h @@ -1,159 +1,160 @@ /* This file is part of Akonadi Contact. Copyright (c) 2009 Tobias Koenig 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 #include #include 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. * * Examples: * * @code * * // Search all contacts with email address tokoe@kde.org * Akonadi::ContactSearchJob *job = new Akonadi::ContactSearchJob(); * job->setQuery( Akonadi::ContactSearchJob::Email, "tokoe@kde.org" ); * connect( job, SIGNAL( result( KJob* ) ), this, SLOT( searchResult( KJob* ) ) ); * * ... * * MyClass::searchResult( KJob *job ) * { * Akonadi::ContactSearchJob *searchJob = qobject_cast( job ); * const KABC::Addressee::List contacts = searchJob->contacts(); * // do something with the contacts * } * * @endcode * * @code * * // Search for all existing contacts * Akonadi::ContactSearchJob *job = new Akonadi::ContactSearchJob(); * connect( job, SIGNAL( result( KJob* ) ), this, SLOT( searchResult( KJob* ) ) ); * * ... * * MyClass::searchResult( KJob *job ) * { * Akonadi::ContactSearchJob *searchJob = qobject_cast( job ); * const KABC::Addressee::List contacts = searchJob->contacts(); * // do something with the contacts * } * * @endcode * * @author Tobias Koenig * @since 4.4 */ class AKONADI_CONTACT_EXPORT ContactSearchJob : public ItemSearchJob { 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. NickName, ///< The nickname of the contact. - NameOrEmail ///< The name or email address of the contact. @since 4.5 + NameOrEmail, ///< The name or email address of the contact. @since 4.5 + ContactUid ///< The global unique identifier of the contact. @since 4.5 }; /** * Describes the type of pattern matching that shall be used. * * @since 4.5 */ enum Match { 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). }; /** * Sets the @p criterion and @p value for the search. * * @note ExactMatch is used for the matching. * @todo Merge with the method below in KDE5 */ void setQuery( Criterion criterion, const QString &value ); /** * Sets the @p criterion and @p value for the search with @p match. * * @since 4.5 */ void setQuery( Criterion criterion, const QString &value, Match match ); /** * Sets a @p limit on how many results will be returned by this search job. * This is useful in situation where for example only the first search result is needed anyway, * setting a limit of 1 here will greatly reduce the resource usage of Nepomuk during the * search. * * This needs to be called before calling setQuery() to have an effect. * By default, the number of results is unlimited. */ void setLimit( int limit ); /** * Returns the contacts that matched the search criteria. */ KABC::Addressee::List contacts() const; private: //@cond PRIVATE class Private; Private* const d; //@endcond }; } #endif