00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "contactgroupsearchjob.h"
00023
00024 #include <akonadi/itemfetchscope.h>
00025
00026 using namespace Akonadi;
00027
00028 class ContactGroupSearchJob::Private
00029 {
00030 public:
00031 int mLimit;
00032 };
00033
00034 ContactGroupSearchJob::ContactGroupSearchJob( 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>ContactGroup</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:ContactGroup }"
00054 #endif
00055 ) );
00056 }
00057
00058 ContactGroupSearchJob::~ContactGroupSearchJob()
00059 {
00060 delete d;
00061 }
00062
00063 void ContactGroupSearchJob::setQuery( Criterion criterion, const QString &value )
00064 {
00065
00066
00067 setQuery( criterion, value, ExactMatch );
00068 }
00069
00070 void ContactGroupSearchJob::setQuery( Criterion criterion, const QString &value, Match match )
00071 {
00072 QString query;
00073
00074 #ifndef AKONADI_USE_STRIGI_SEARCH
00075 query = QString::fromLatin1( "prefix nco:<http://www.semanticdesktop.org/ontologies/2007/03/22/nco#>" );
00076 #endif
00077
00078 if ( match == ExactMatch ) {
00079 if ( criterion == Name ) {
00080 query += QString::fromLatin1(
00081 #ifdef AKONADI_USE_STRIGI_SEARCH
00082 "<request>"
00083 " <query>"
00084 " <and>"
00085 " <equals>"
00086 " <field name=\"type\"/>"
00087 " <string>ContactGroup</string>"
00088 " </equals>"
00089 " <equals>"
00090 " <field name=\"contactGroupName\"/>"
00091 " <string>%1</string>"
00092 " </equals>"
00093 " </and>"
00094 " </query>"
00095 "</request>"
00096 #else
00097 "SELECT DISTINCT ?group "
00098 "WHERE { "
00099 " graph ?g { "
00100 " ?group <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00101 " ?group nco:contactGroupName \"%1\"^^<http://www.w3.org/2001/XMLSchema#string>."
00102 " } "
00103 "}"
00104 #endif
00105 );
00106 }
00107 } else if ( match == ContainsMatch ) {
00108 if ( criterion == Name ) {
00109 query += QString::fromLatin1(
00110 #ifdef AKONADI_USE_STRIGI_SEARCH
00111 "<request>"
00112 " <query>"
00113 " <and>"
00114 " <equals>"
00115 " <field name=\"type\"/>"
00116 " <string>ContactGroup</string>"
00117 " </equals>"
00118 " <contains>"
00119 " <field name=\"contactGroupName\"/>"
00120 " <string>%1</string>"
00121 " </contains>"
00122 " </and>"
00123 " </query>"
00124 "</request>"
00125 #else
00126 "SELECT DISTINCT ?group "
00127 "WHERE { "
00128 " graph ?g { "
00129 " ?group <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00130 " ?group nco:contactGroupName ?v . "
00131 " ?v bif:contains \"'%1'\""
00132 " } "
00133 "}"
00134 #endif
00135 );
00136 }
00137 } else if ( match == StartsWithMatch ) {
00138 if ( criterion == Name ) {
00139 query += QString::fromLatin1(
00140 #ifdef AKONADI_USE_STRIGI_SEARCH
00141 "<request>"
00142 " <query>"
00143 " <and>"
00144 " <equals>"
00145 " <field name=\"type\"/>"
00146 " <string>ContactGroup</string>"
00147 " </equals>"
00148 " <startsWith>"
00149 " <field name=\"contactGroupName\"/>"
00150 " <string>%1</string>"
00151 " </startsWith>"
00152 " </and>"
00153 " </query>"
00154 "</request>"
00155 #else
00156 "SELECT DISTINCT ?group "
00157 "WHERE { "
00158 " graph ?g { "
00159 " ?group <" + akonadiItemIdUri().toEncoded() + "> ?itemId . "
00160 " ?group nco:contactGroupName ?v . "
00161 " ?v bif:contains \"'%1*'\""
00162 " } "
00163 "}"
00164 #endif
00165 );
00166 }
00167 }
00168
00169 if ( d->mLimit != -1 ) {
00170 #ifndef AKONADI_USE_STRIGI_SEARCH
00171 query += QString::fromLatin1( " LIMIT %1" ).arg( d->mLimit );
00172 #endif
00173 }
00174
00175 query = query.arg( value );
00176
00177 ItemSearchJob::setQuery( query );
00178 }
00179
00180 void ContactGroupSearchJob::setLimit( int limit )
00181 {
00182 d->mLimit = limit;
00183 }
00184
00185 KABC::ContactGroup::List ContactGroupSearchJob::contactGroups() const
00186 {
00187 KABC::ContactGroup::List contactGroups;
00188
00189 foreach ( const Item &item, items() ) {
00190 if ( item.hasPayload<KABC::ContactGroup>() )
00191 contactGroups.append( item.payload<KABC::ContactGroup>() );
00192 }
00193
00194 return contactGroups;
00195 }
00196
00197 #include "contactgroupsearchjob.moc"