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 #ifdef AKONADI_USE_STRIGI_SEARCH
00043 "<request>"
00044 " <query>"
00045 " <equals>"
00046 " <field name=\"type\"/>"
00047 " <string>PersonContact</string>"
00048 " </equals>"
00049 " </query>"
00050 "</request>"
00051 #else
00052 "prefix nco:<http://www.semanticdesktop.org/ontologies/2007/03/22/nco#>"
00053 "SELECT ?r WHERE { ?r a nco:PersonContact }"
00054 #endif
00055 ) );
00056 }
00057
00058 ContactSearchJob::~ContactSearchJob()
00059 {
00060 delete d;
00061 }
00062
00063 void ContactSearchJob::setQuery( Criterion criterion, const QString &value )
00064 {
00065 setQuery( criterion, value, ExactMatch );
00066 }
00067
00068 void ContactSearchJob::setQuery( Criterion criterion, const QString &value, Match match )
00069 {
00070 if ( match == StartsWithMatch && value.size() < 4 )
00071 match = ExactMatch;
00072
00073 QString query;
00074
00075 #ifndef AKONADI_USE_STRIGI_SEARCH
00076 query = QString::fromLatin1( "prefix nco:<http://www.semanticdesktop.org/ontologies/2007/03/22/nco#>" );
00077 #endif
00078
00079 if ( match == ExactMatch ) {
00080 if ( criterion == Name ) {
00081 query += QString::fromLatin1(
00082 #ifdef AKONADI_USE_STRIGI_SEARCH
00083 "<request>"
00084 " <query>"
00085 " <and>"
00086 " <equals>"
00087 " <field name=\"type\"/>"
00088 " <string>PersonContact</string>"
00089 " </equals>"
00090 " <equals>"
00091 " <field name=\"fullname\"/>"
00092 " <string>%1</string>"
00093 " </equals>"
00094 " </and>"
00095 " </query>"
00096 "</request>"
00097 #else
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 " } "
00105 "} "
00106 #endif
00107 );
00108 } else if ( criterion == Email ) {
00109 query += QString::fromLatin1(
00110 #ifdef AKONADI_USE_STRIGI_SEARCH
00111 "<request>"
00112 " <query>"
00113 " <and>"
00114 " <equals>"
00115 " <field name=\"type\"/>"
00116 " <string>PersonContact</string>"
00117 " </equals>"
00118 " <equals>"
00119 " <field name=\"emailAddress\"/>"
00120 " <string>%1</string>"
00121 " </equals>"
00122 " </and>"
00123 " </query>"
00124 "</request>"
00125 #else
00126 "SELECT DISTINCT ?person "
00127 "WHERE { "
00128 " graph ?g { "
00129 " ?person <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00130 " ?person a nco:PersonContact ; "
00131 " nco:hasEmailAddress ?email . "
00132 " ?email nco:emailAddress \"%1\"^^<http://www.w3.org/2001/XMLSchema#string> . "
00133 " } "
00134 "}"
00135 #endif
00136 );
00137 } else if ( criterion == NickName ) {
00138 query += QString::fromLatin1(
00139 #ifdef AKONADI_USE_STRIGI_SEARCH
00140 "<request>"
00141 " <query>"
00142 " <and>"
00143 " <equals>"
00144 " <field name=\"type\"/>"
00145 " <string>PersonContact</string>"
00146 " </equals>"
00147 " <equals>"
00148 " <field name=\"nickname\"/>"
00149 " <string>%1</string>"
00150 " </equals>"
00151 " </and>"
00152 " </query>"
00153 "</request>"
00154 #else
00155 "SELECT DISTINCT ?r "
00156 "WHERE { "
00157 " graph ?g { "
00158 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00159 " ?r a nco:PersonContact . "
00160 " ?r nco:nickname \"%1\"^^<http://www.w3.org/2001/XMLSchema#string> ."
00161 " } "
00162 "}"
00163 #endif
00164 );
00165 } else if ( criterion == NameOrEmail ) {
00166 query += QString::fromLatin1(
00167 #ifdef AKONADI_USE_STRIGI_SEARCH
00168 "<request>"
00169 " <query>"
00170 " <and>"
00171 " <equals>"
00172 " <field name=\"type\"/>"
00173 " <string>PersonContact</string>"
00174 " </equals>"
00175 " <or>"
00176 " <equals>"
00177 " <field name=\"fullname\"/>"
00178 " <string>%1</string>"
00179 " </equals>"
00180 " <equals>"
00181 " <field name=\"nameGiven\"/>"
00182 " <string>%1</string>"
00183 " </equals>"
00184 " <equals>"
00185 " <field name=\"nameFamily\"/>"
00186 " <string>%1</string>"
00187 " </equals>"
00188 " <equals>"
00189 " <field name=\"emailAddress\"/>"
00190 " <string>%1</string>"
00191 " </equals>"
00192 " </or>"
00193 " </and>"
00194 " </query>"
00195 "</request>"
00196 #else
00197 "SELECT DISTINCT ?r "
00198 "WHERE { "
00199 " graph ?g { "
00200 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00201 " ?r a nco:PersonContact . "
00202 " { ?r nco:fullname \"%1\"^^<http://www.w3.org/2001/XMLSchema#string>. } "
00203 " UNION "
00204 " { ?r nco:nameGiven \"%1\"^^<http://www.w3.org/2001/XMLSchema#string>. } "
00205 " UNION "
00206 " { ?r nco:nameFamily \"%1\"^^<http://www.w3.org/2001/XMLSchema#string>. } "
00207 " UNION "
00208 " { ?r nco:hasEmailAddress ?email . "
00209 " ?email nco:emailAddress \"%1\"^^<http://www.w3.org/2001/XMLSchema#string> . } "
00210 " } "
00211 "}"
00212 #endif
00213 );
00214 } else if ( criterion == ContactUid ) {
00215 query += QString::fromLatin1(
00216 #ifdef AKONADI_USE_STRIGI_SEARCH
00217 "<request>"
00218 " <query>"
00219 " <and>"
00220 " <equals>"
00221 " <field name=\"type\"/>"
00222 " <string>PersonContact</string>"
00223 " </equals>"
00224 " <equals>"
00225 " <field name=\"contactUID\"/>"
00226 " <string>%1</string>"
00227 " </equals>"
00228 " </and>"
00229 " </query>"
00230 "</request>"
00231 #else
00232 "SELECT DISTINCT ?r "
00233 "WHERE { "
00234 " graph ?g { "
00235 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00236 " ?r a nco:PersonContact . "
00237 " ?r nco:contactUID \"%1\"^^<http://www.w3.org/2001/XMLSchema#string> ."
00238 " } "
00239 "}"
00240 #endif
00241 );
00242 }
00243 } else if ( match == StartsWithMatch ) {
00244 if ( criterion == Name ) {
00245 query += QString::fromLatin1(
00246 #ifdef AKONADI_USE_STRIGI_SEARCH
00247 "<request>"
00248 " <query>"
00249 " <and>"
00250 " <equals>"
00251 " <field name=\"type\"/>"
00252 " <string>PersonContact</string>"
00253 " </equals>"
00254 " <startsWith>"
00255 " <field name=\"fullname\"/>"
00256 " <string>%1</string>"
00257 " </startsWith>"
00258 " </and>"
00259 " </query>"
00260 "</request>"
00261 #else
00262 "SELECT DISTINCT ?r "
00263 "WHERE { "
00264 " graph ?g { "
00265 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00266 " ?r a nco:PersonContact . "
00267 " ?r nco:fullname ?v . "
00268 " ?v bif:contains \"'%1*'\" . "
00269 " } "
00270 "} "
00271 #endif
00272 );
00273 } else if ( criterion == Email ) {
00274 query += QString::fromLatin1(
00275 #ifdef AKONADI_USE_STRIGI_SEARCH
00276 "<request>"
00277 " <query>"
00278 " <and>"
00279 " <equals>"
00280 " <field name=\"type\"/>"
00281 " <string>PersonContact</string>"
00282 " </equals>"
00283 " <startsWith>"
00284 " <field name=\"emailAddress\"/>"
00285 " <string>%1</string>"
00286 " </startsWith>"
00287 " </and>"
00288 " </query>"
00289 "</request>"
00290 #else
00291 "SELECT DISTINCT ?person "
00292 "WHERE { "
00293 " graph ?g { "
00294 " ?person <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00295 " ?person a nco:PersonContact ; "
00296 " nco:hasEmailAddress ?email . "
00297 " ?email nco:emailAddress ?v . "
00298 " ?v bif:contains \"'%1*'\" . "
00299 " } "
00300 "}"
00301 #endif
00302 );
00303 } else if ( criterion == NickName ) {
00304 query += QString::fromLatin1(
00305 #ifdef AKONADI_USE_STRIGI_SEARCH
00306 "<request>"
00307 " <query>"
00308 " <and>"
00309 " <equals>"
00310 " <field name=\"type\"/>"
00311 " <string>PersonContact</string>"
00312 " </equals>"
00313 " <startsWith>"
00314 " <field name=\"nickname\"/>"
00315 " <string>%1</string>"
00316 " </startsWith>"
00317 " </and>"
00318 " </query>"
00319 "</request>"
00320 #else
00321 "SELECT DISTINCT ?r "
00322 "WHERE { "
00323 " graph ?g { "
00324 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00325 " ?r a nco:PersonContact . "
00326 " ?r nco:nickname ?v . "
00327 " ?v bif:contains \"'%1*'\" . "
00328 " } "
00329 "}"
00330 #endif
00331 );
00332 } else if ( criterion == NameOrEmail ) {
00333 query += QString::fromLatin1(
00334 #ifdef AKONADI_USE_STRIGI_SEARCH
00335 "<request>"
00336 " <query>"
00337 " <and>"
00338 " <equals>"
00339 " <field name=\"type\"/>"
00340 " <string>PersonContact</string>"
00341 " </equals>"
00342 " <or>"
00343 " <startsWith>"
00344 " <field name=\"fullname\"/>"
00345 " <string>%1</string>"
00346 " </startsWith>"
00347 " <startsWith>"
00348 " <field name=\"nameGiven\"/>"
00349 " <string>%1</string>"
00350 " </startsWith>"
00351 " <startsWith>"
00352 " <field name=\"nameFamily\"/>"
00353 " <string>%1</string>"
00354 " </startsWith>"
00355 " <startsWith>"
00356 " <field name=\"emailAddress\"/>"
00357 " <string>%1</string>"
00358 " </startsWith>"
00359 " </or>"
00360 " </and>"
00361 " </query>"
00362 "</request>"
00363 #else
00364 "SELECT DISTINCT ?r "
00365 "WHERE { "
00366 " graph ?g { "
00367 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00368 " ?r a nco:PersonContact . "
00369 " { ?r nco:fullname ?v . "
00370 " ?v bif:contains \"'%1*'\" . } "
00371 " UNION "
00372 " { ?r nco:nameGiven ?v . "
00373 " ?v bif:contains \"'%1*'\" . } "
00374 " UNION "
00375 " { ?r nco:nameFamily ?v . "
00376 " ?v bif:contains \"'%1*'\" . } "
00377 " UNION "
00378 " { ?r nco:hasEmailAddress ?email . "
00379 " ?email nco:emailAddress ?v . "
00380 " ?v bif:contains \"'%1*'\" . } "
00381 " } "
00382 "}"
00383 #endif
00384 );
00385 } else if ( criterion == ContactUid ) {
00386 query += QString::fromLatin1(
00387 #ifdef AKONADI_USE_STRIGI_SEARCH
00388 "<request>"
00389 " <query>"
00390 " <and>"
00391 " <equals>"
00392 " <field name=\"type\"/>"
00393 " <string>PersonContact</string>"
00394 " </equals>"
00395 " <startsWith>"
00396 " <field name=\"contactUID\"/>"
00397 " <string>%1</string>"
00398 " </startsWith>"
00399 " </and>"
00400 " </query>"
00401 "</request>"
00402 #else
00403 "SELECT DISTINCT ?r "
00404 "WHERE { "
00405 " graph ?g { "
00406 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00407 " ?r a nco:PersonContact . "
00408 " ?r nco:contactUID ?v . "
00409 " ?v bif:contains \"'%1*'\" . "
00410 " } "
00411 "}"
00412 #endif
00413 );
00414 }
00415 } else if ( match == ContainsMatch ) {
00416 if ( criterion == Name ) {
00417 query += QString::fromLatin1(
00418 #ifdef AKONADI_USE_STRIGI_SEARCH
00419 "<request>"
00420 " <query>"
00421 " <and>"
00422 " <equals>"
00423 " <field name=\"type\"/>"
00424 " <string>PersonContact</string>"
00425 " </equals>"
00426 " <contains>"
00427 " <field name=\"fullname\"/>"
00428 " <string>%1</string>"
00429 " </contains>"
00430 " </and>"
00431 " </query>"
00432 "</request>"
00433 #else
00434 "SELECT DISTINCT ?r "
00435 "WHERE { "
00436 " graph ?g { "
00437 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00438 " ?r a nco:PersonContact . "
00439 " ?r nco:fullname ?v . "
00440 " ?v bif:contains \"'%1'\" . "
00441 " } "
00442 "} "
00443 #endif
00444 );
00445 } else if ( criterion == Email ) {
00446 query += QString::fromLatin1(
00447 #ifdef AKONADI_USE_STRIGI_SEARCH
00448 "<request>"
00449 " <query>"
00450 " <and>"
00451 " <equals>"
00452 " <field name=\"type\"/>"
00453 " <string>PersonContact</string>"
00454 " </equals>"
00455 " <contains>"
00456 " <field name=\"emailAddress\"/>"
00457 " <string>%1</string>"
00458 " </contains>"
00459 " </and>"
00460 " </query>"
00461 "</request>"
00462 #else
00463 "SELECT DISTINCT ?person "
00464 "WHERE { "
00465 " graph ?g { "
00466 " ?person <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00467 " ?person a nco:PersonContact ; "
00468 " nco:hasEmailAddress ?email . "
00469 " ?email nco:emailAddress ?v . "
00470 " ?v bif:contains \"'%1'\" . "
00471 " } "
00472 "}"
00473 #endif
00474 );
00475 } else if ( criterion == NickName ) {
00476 query += QString::fromLatin1(
00477 #ifdef AKONADI_USE_STRIGI_SEARCH
00478 "<request>"
00479 " <query>"
00480 " <and>"
00481 " <equals>"
00482 " <field name=\"type\"/>"
00483 " <string>PersonContact</string>"
00484 " </equals>"
00485 " <contains>"
00486 " <field name=\"nickname\"/>"
00487 " <string>%1</string>"
00488 " </contains>"
00489 " </and>"
00490 " </query>"
00491 "</request>"
00492 #else
00493 "SELECT DISTINCT ?r "
00494 "WHERE { "
00495 " graph ?g { "
00496 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00497 " ?r a nco:PersonContact . "
00498 " ?r nco:nickname ?v . "
00499 " ?v bif:contains \"'%1'\" . "
00500 " } "
00501 "}"
00502 #endif
00503 );
00504 } else if ( criterion == NameOrEmail ) {
00505 query += QString::fromLatin1(
00506 #ifdef AKONADI_USE_STRIGI_SEARCH
00507 "<request>"
00508 " <query>"
00509 " <and>"
00510 " <equals>"
00511 " <field name=\"type\"/>"
00512 " <string>PersonContact</string>"
00513 " </equals>"
00514 " <or>"
00515 " <contains>"
00516 " <field name=\"fullname\"/>"
00517 " <string>%1</string>"
00518 " </contains>"
00519 " <contains>"
00520 " <field name=\"nameGiven\"/>"
00521 " <string>%1</string>"
00522 " </contains>"
00523 " <contains>"
00524 " <field name=\"nameFamily\"/>"
00525 " <string>%1</string>"
00526 " </contains>"
00527 " <contains>"
00528 " <field name=\"emailAddress\"/>"
00529 " <string>%1</string>"
00530 " </contains>"
00531 " </or>"
00532 " </and>"
00533 " </query>"
00534 "</request>"
00535 #else
00536 "SELECT DISTINCT ?r "
00537 "WHERE { "
00538 " graph ?g { "
00539 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00540 " ?r a nco:PersonContact . "
00541 " { ?r nco:fullname ?v . "
00542 " ?v bif:contains \"'%1'\" . } "
00543 " UNION "
00544 " { ?r nco:nameGiven ?v . "
00545 " ?v bif:contains \"'%1'\" . } "
00546 " UNION "
00547 " { ?r nco:nameFamily ?v . "
00548 " ?v bif:contains \"'%1'\" . } "
00549 " UNION "
00550 " { ?r nco:hasEmailAddress ?email . "
00551 " ?email nco:emailAddress ?v . "
00552 " ?v bif:contains \"'%1'\" . } "
00553 " } "
00554 "}"
00555 #endif
00556 );
00557 } else if ( criterion == ContactUid ) {
00558 query += QString::fromLatin1(
00559 #ifdef AKONADI_USE_STRIGI_SEARCH
00560 "<request>"
00561 " <query>"
00562 " <and>"
00563 " <equals>"
00564 " <field name=\"type\"/>"
00565 " <string>PersonContact</string>"
00566 " </equals>"
00567 " <contains>"
00568 " <field name=\"contactUID\"/>"
00569 " <string>%1</string>"
00570 " </contains>"
00571 " </and>"
00572 " </query>"
00573 "</request>"
00574 #else
00575 "SELECT DISTINCT ?r "
00576 "WHERE { "
00577 " graph ?g { "
00578 " ?r <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00579 " ?r a nco:PersonContact . "
00580 " ?r nco:contactUID ?v . "
00581 " ?v bif:contains \"'%1'\" . "
00582 " } "
00583 "}"
00584 #endif
00585 );
00586 }
00587 }
00588
00589 if ( d->mLimit != -1 ) {
00590 #ifndef AKONADI_USE_STRIGI_SEARCH
00591 query += QString::fromLatin1( " LIMIT %1" ).arg( d->mLimit );
00592 #endif
00593 }
00594 query = query.arg( value );
00595
00596 ItemSearchJob::setQuery( query );
00597 }
00598
00599 void ContactSearchJob::setLimit( int limit )
00600 {
00601 d->mLimit = limit;
00602 }
00603
00604 KABC::Addressee::List ContactSearchJob::contacts() const
00605 {
00606 KABC::Addressee::List contacts;
00607
00608 foreach ( const Item &item, items() ) {
00609 if ( item.hasPayload<KABC::Addressee>() )
00610 contacts.append( item.payload<KABC::Addressee>() );
00611 }
00612
00613 return contacts;
00614 }
00615
00616 #include "contactsearchjob.moc"