akonadi
itemsearchjob.cpp
00001 /* 00002 Copyright (c) 2009 Tobias Koenig <tokoe@kde.org> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 00018 */ 00019 00020 #include "itemsearchjob.h" 00021 00022 #include "imapparser_p.h" 00023 #include "itemfetchscope.h" 00024 #include "job_p.h" 00025 #include "protocolhelper_p.h" 00026 00027 #include <QtCore/QTimer> 00028 00029 using namespace Akonadi; 00030 00031 class Akonadi::ItemSearchJobPrivate : public JobPrivate 00032 { 00033 public: 00034 ItemSearchJobPrivate( ItemSearchJob *parent, const QString &query ) 00035 : JobPrivate( parent ), mQuery( query ) 00036 { 00037 } 00038 00039 void timeout() 00040 { 00041 Q_Q( Akonadi::ItemSearchJob ); 00042 00043 mEmitTimer->stop(); // in case we are called by result() 00044 if ( !mPendingItems.isEmpty() ) { 00045 if ( !q->error() ) 00046 emit q->itemsReceived( mPendingItems ); 00047 mPendingItems.clear(); 00048 } 00049 } 00050 00051 Q_DECLARE_PUBLIC( ItemSearchJob ) 00052 00053 QString mQuery; 00054 Item::List mItems; 00055 ItemFetchScope mFetchScope; 00056 Item::List mPendingItems; // items pending for emitting itemsReceived() 00057 QTimer* mEmitTimer; 00058 }; 00059 00060 ItemSearchJob::ItemSearchJob( const QString & query, QObject * parent ) 00061 : Job( new ItemSearchJobPrivate( this, query ), parent ) 00062 { 00063 Q_D( ItemSearchJob ); 00064 00065 d->mEmitTimer = new QTimer( this ); 00066 d->mEmitTimer->setSingleShot( true ); 00067 d->mEmitTimer->setInterval( 100 ); 00068 connect( d->mEmitTimer, SIGNAL(timeout()), this, SLOT(timeout()) ); 00069 connect( this, SIGNAL(result(KJob*)), this, SLOT(timeout()) ); 00070 } 00071 00072 ItemSearchJob::~ItemSearchJob() 00073 { 00074 } 00075 00076 void ItemSearchJob::setQuery( const QString &query ) 00077 { 00078 Q_D( ItemSearchJob ); 00079 00080 d->mQuery = query; 00081 } 00082 00083 void ItemSearchJob::setFetchScope( const ItemFetchScope &fetchScope ) 00084 { 00085 Q_D( ItemSearchJob ); 00086 00087 d->mFetchScope = fetchScope; 00088 } 00089 00090 ItemFetchScope &ItemSearchJob::fetchScope() 00091 { 00092 Q_D( ItemSearchJob ); 00093 00094 return d->mFetchScope; 00095 } 00096 00097 void ItemSearchJob::doStart() 00098 { 00099 Q_D( ItemSearchJob ); 00100 00101 QByteArray command = d->newTag() + " SEARCH "; 00102 command += ImapParser::quote( d->mQuery.toUtf8() ); 00103 command += ' ' + ProtocolHelper::itemFetchScopeToByteArray( d->mFetchScope ); 00104 command += '\n'; 00105 d->writeData( command ); 00106 } 00107 00108 void ItemSearchJob::doHandleResponse( const QByteArray & tag, const QByteArray & data ) 00109 { 00110 Q_D( ItemSearchJob ); 00111 00112 if ( tag == "*" ) { 00113 int begin = data.indexOf( "SEARCH" ); 00114 if ( begin >= 0 ) { 00115 00116 // split fetch response into key/value pairs 00117 QList<QByteArray> fetchResponse; 00118 ImapParser::parseParenthesizedList( data, fetchResponse, begin + 7 ); 00119 00120 Item item; 00121 ProtocolHelper::parseItemFetchResult( fetchResponse, item ); 00122 if ( !item.isValid() ) 00123 return; 00124 00125 d->mItems.append( item ); 00126 d->mPendingItems.append( item ); 00127 if ( !d->mEmitTimer->isActive() ) 00128 d->mEmitTimer->start(); 00129 return; 00130 } 00131 } 00132 kDebug() << "Unhandled response: " << tag << data; 00133 } 00134 00135 Item::List ItemSearchJob::items() const 00136 { 00137 Q_D( const ItemSearchJob ); 00138 00139 return d->mItems; 00140 } 00141 00142 QUrl ItemSearchJob::akonadiItemIdUri() 00143 { 00144 return QUrl( QLatin1String( "http://akonadi-project.org/ontologies/aneo#akonadiItemId" ) ); 00145 } 00146 00147 #include "itemsearchjob.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 04:52:57 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 04:52:57 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.