00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "contactsearchjob.h"
00023
00024 #include <akonadi/itemfetchscope.h>
00025
00026 using namespace Akonadi;
00027
00028 class ContactSearchJob::Private
00029 {
00030 public:
00031 int mLimit;
00032 };
00033
00034 ContactSearchJob::ContactSearchJob( QObject * parent )
00035 : ItemSearchJob( QString(), parent ), d( new Private() )
00036 {
00037 fetchScope().fetchFullPayload();
00038 d->mLimit = -1;
00039
00040
00041 ItemSearchJob::setQuery( QLatin1String( ""
00042 "prefix nco:<http://www.semanticdesktop.org/ontologies/2007/03/22/nco#>"
00043 "SELECT ?r WHERE { ?r a nco:PersonContact }" ) );
00044 }
00045
00046 ContactSearchJob::~ContactSearchJob()
00047 {
00048 delete d;
00049 }
00050
00051 void ContactSearchJob::setQuery( Criterion criterion, const QString &value )
00052 {
00053 setQuery( criterion, value, ExactMatch );
00054 }
00055
00056 void ContactSearchJob::setQuery( Criterion criterion, const QString &value, Match match )
00057 {
00058 if ( match == StartsWithMatch && value.size() < 4 )
00059 match = ExactMatch;
00060
00061 QString query = QString::fromLatin1(
00062 "prefix nco:<http://www.semanticdesktop.org/ontologies/2007/03/22/nco#>" );
00063
00064 if ( match == ExactMatch ) {
00065 if ( criterion == Name ) {
00066 query += QString::fromLatin1(
00067 "SELECT DISTINCT ?r "
00068 "WHERE { "
00069 " graph ?g { "
00070 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00071 " ?r a nco:PersonContact . "
00072 " ?r nco:fullname \"%1\"^^<http://www.w3.org/2001/XMLSchema#string>. "
00073 " } "
00074 "} " );
00075 } else if ( criterion == Email ) {
00076 query += QString::fromLatin1(
00077 "SELECT DISTINCT ?person "
00078 "WHERE { "
00079 " graph ?g { "
00080 " ?person <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00081 " ?person a nco:PersonContact ; "
00082 " nco:hasEmailAddress ?email . "
00083 " ?email nco:emailAddress \"%1\"^^<http://www.w3.org/2001/XMLSchema#string> . "
00084 " } "
00085 "}" );
00086 } else if ( criterion == NickName ) {
00087 query += QString::fromLatin1(
00088 "SELECT DISTINCT ?r "
00089 "WHERE { "
00090 " graph ?g { "
00091 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00092 " ?r a nco:PersonContact . "
00093 " ?r nco:nickname \"%1\"^^<http://www.w3.org/2001/XMLSchema#string> ."
00094 " } "
00095 "}" );
00096 } else if ( criterion == NameOrEmail ) {
00097 query += QString::fromLatin1(
00098 "SELECT DISTINCT ?r "
00099 "WHERE { "
00100 " graph ?g { "
00101 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00102 " ?r a nco:PersonContact . "
00103 " { ?r nco:fullname \"%1\"^^<http://www.w3.org/2001/XMLSchema#string>. } "
00104 " UNION "
00105 " { ?r nco:nameGiven \"%1\"^^<http://www.w3.org/2001/XMLSchema#string>. } "
00106 " UNION "
00107 " { ?r nco:nameFamily \"%1\"^^<http://www.w3.org/2001/XMLSchema#string>. } "
00108 " UNION "
00109 " { ?r nco:hasEmailAddress ?email . "
00110 " ?email nco:emailAddress \"%1\"^^<http://www.w3.org/2001/XMLSchema#string> . } "
00111 " } "
00112 "}" );
00113 } else if ( criterion == ContactUid ) {
00114 query += QString::fromLatin1(
00115 "SELECT DISTINCT ?r "
00116 "WHERE { "
00117 " graph ?g { "
00118 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00119 " ?r a nco:PersonContact . "
00120 " ?r nco:contactUID \"%1\"^^<http://www.w3.org/2001/XMLSchema#string> ."
00121 " } "
00122 "}" );
00123 }
00124 } else if ( match == StartsWithMatch ) {
00125 if ( criterion == Name ) {
00126 query += QString::fromLatin1(
00127 "SELECT DISTINCT ?r "
00128 "WHERE { "
00129 " graph ?g { "
00130 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00131 " ?r a nco:PersonContact . "
00132 " ?r nco:fullname ?v . "
00133 " ?v bif:contains \"'%1*'\" . "
00134 " } "
00135 "} " );
00136 } else if ( criterion == Email ) {
00137 query += QString::fromLatin1(
00138 "SELECT DISTINCT ?person "
00139 "WHERE { "
00140 " graph ?g { "
00141 " ?person <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00142 " ?person a nco:PersonContact ; "
00143 " nco:hasEmailAddress ?email . "
00144 " ?email nco:emailAddress ?v . "
00145 " ?v bif:contains \"'%1*'\" . "
00146 " } "
00147 "}" );
00148 } else if ( criterion == NickName ) {
00149 query += QString::fromLatin1(
00150 "SELECT DISTINCT ?r "
00151 "WHERE { "
00152 " graph ?g { "
00153 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00154 " ?r a nco:PersonContact . "
00155 " ?r nco:nickname ?v . "
00156 " ?v bif:contains \"'%1*'\" . "
00157 " } "
00158 "}" );
00159 } else if ( criterion == NameOrEmail ) {
00160 query += QString::fromLatin1(
00161 "SELECT DISTINCT ?r "
00162 "WHERE { "
00163 " graph ?g { "
00164 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00165 " ?r a nco:PersonContact . "
00166 " { ?r nco:fullname ?v . "
00167 " ?v bif:contains \"'%1*'\" . } "
00168 " UNION "
00169 " { ?r nco:nameGiven ?v . "
00170 " ?v bif:contains \"'%1*'\" . } "
00171 " UNION "
00172 " { ?r nco:nameFamily ?v . "
00173 " ?v bif:contains \"'%1*'\" . } "
00174 " UNION "
00175 " { ?r nco:hasEmailAddress ?email . "
00176 " ?email nco:emailAddress ?v . "
00177 " ?v bif:contains \"'%1*'\" . } "
00178 " } "
00179 "}" );
00180 } else if ( criterion == ContactUid ) {
00181 query += QString::fromLatin1(
00182 "SELECT DISTINCT ?r "
00183 "WHERE { "
00184 " graph ?g { "
00185 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00186 " ?r a nco:PersonContact . "
00187 " ?r nco:contactUID ?v . "
00188 " ?v bif:contains \"'%1*'\" . "
00189 " } "
00190 "}" );
00191 }
00192 } else if ( match == ContainsMatch ) {
00193 if ( criterion == Name ) {
00194 query += QString::fromLatin1(
00195 "SELECT DISTINCT ?r "
00196 "WHERE { "
00197 " graph ?g { "
00198 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00199 " ?r a nco:PersonContact . "
00200 " ?r nco:fullname ?v . "
00201 " ?v bif:contains \"'%1'\" . "
00202 " } "
00203 "} " );
00204 } else if ( criterion == Email ) {
00205 query += QString::fromLatin1(
00206 "SELECT DISTINCT ?person "
00207 "WHERE { "
00208 " graph ?g { "
00209 " ?person <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00210 " ?person a nco:PersonContact ; "
00211 " nco:hasEmailAddress ?email . "
00212 " ?email nco:emailAddress ?v . "
00213 " ?v bif:contains \"'%1'\" . "
00214 " } "
00215 "}" );
00216 } else if ( criterion == NickName ) {
00217 query += QString::fromLatin1(
00218 "SELECT DISTINCT ?r "
00219 "WHERE { "
00220 " graph ?g { "
00221 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00222 " ?r a nco:PersonContact . "
00223 " ?r nco:nickname ?v . "
00224 " ?v bif:contains \"'%1'\" . "
00225 " } "
00226 "}" );
00227 } else if ( criterion == NameOrEmail ) {
00228 query += QString::fromLatin1(
00229 "SELECT DISTINCT ?r "
00230 "WHERE { "
00231 " graph ?g { "
00232 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00233 " ?r a nco:PersonContact . "
00234 " { ?r nco:fullname ?v . "
00235 " ?v bif:contains \"'%1'\" . } "
00236 " UNION "
00237 " { ?r nco:nameGiven ?v . "
00238 " ?v bif:contains \"'%1'\" . } "
00239 " UNION "
00240 " { ?r nco:nameFamily ?v . "
00241 " ?v bif:contains \"'%1'\" . } "
00242 " UNION "
00243 " { ?r nco:hasEmailAddress ?email . "
00244 " ?email nco:emailAddress ?v . "
00245 " ?v bif:contains \"'%1'\" . } "
00246 " } "
00247 "}" );
00248 } else if ( criterion == ContactUid ) {
00249 query += QString::fromLatin1(
00250 "SELECT DISTINCT ?r "
00251 "WHERE { "
00252 " graph ?g { "
00253 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00254 " ?r a nco:PersonContact . "
00255 " ?r nco:contactUID ?v . "
00256 " ?v bif:contains \"'%1'\" . "
00257 " } "
00258 "}" );
00259 }
00260 }
00261
00262 if ( d->mLimit != -1 ) {
00263 query += QString::fromLatin1( " LIMIT %1" ).arg( d->mLimit );
00264 }
00265 query = query.arg( value );
00266
00267 ItemSearchJob::setQuery( query );
00268 }
00269
00270 void ContactSearchJob::setLimit( int limit )
00271 {
00272 d->mLimit = limit;
00273 }
00274
00275 KABC::Addressee::List ContactSearchJob::contacts() const
00276 {
00277 KABC::Addressee::List contacts;
00278
00279 foreach ( const Item &item, items() ) {
00280 if ( item.hasPayload<KABC::Addressee>() )
00281 contacts.append( item.payload<KABC::Addressee>() );
00282 }
00283
00284 return contacts;
00285 }
00286
00287 #include "contactsearchjob.moc"