contactgroupsearchjob.cpp
00001 /* 00002 This file is part of Akonadi Contact. 00003 00004 Copyright (c) 2009 Tobias Koenig <tokoe@kde.org> 00005 00006 This library is free software; you can redistribute it and/or modify it 00007 under the terms of the GNU Library General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or (at your 00009 option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, but WITHOUT 00012 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00013 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00014 License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to the 00018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00019 02110-1301, USA. 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 // by default search for all contact groups 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 // Exact match was the default in 4.4, so we have to keep it and ContactSearchJob has something 00066 // else as default 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"