akonadi
itemsearchjob.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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();
00044 if ( !mPendingItems.isEmpty() ) {
00045 emit q->itemsReceived( mPendingItems );
00046 mPendingItems.clear();
00047 }
00048 }
00049
00050 Q_DECLARE_PUBLIC( ItemSearchJob )
00051
00052 QString mQuery;
00053 Item::List mItems;
00054 ItemFetchScope mFetchScope;
00055 Item::List mPendingItems;
00056 QTimer* mEmitTimer;
00057 };
00058
00059 ItemSearchJob::ItemSearchJob( const QString & query, QObject * parent )
00060 : Job( new ItemSearchJobPrivate( this, query ), parent )
00061 {
00062 Q_D( ItemSearchJob );
00063
00064 d->mEmitTimer = new QTimer( this );
00065 d->mEmitTimer->setSingleShot( true );
00066 d->mEmitTimer->setInterval( 100 );
00067 connect( d->mEmitTimer, SIGNAL( timeout() ), this, SLOT( timeout() ) );
00068 connect( this, SIGNAL( result( KJob* ) ), this, SLOT( timeout() ) );
00069 }
00070
00071 ItemSearchJob::~ItemSearchJob()
00072 {
00073 }
00074
00075 void ItemSearchJob::setQuery( const QString &query )
00076 {
00077 Q_D( ItemSearchJob );
00078
00079 d->mQuery = query;
00080 }
00081
00082 void ItemSearchJob::setFetchScope( const ItemFetchScope &fetchScope )
00083 {
00084 Q_D( ItemSearchJob );
00085
00086 d->mFetchScope = fetchScope;
00087 }
00088
00089 ItemFetchScope &ItemSearchJob::fetchScope()
00090 {
00091 Q_D( ItemSearchJob );
00092
00093 return d->mFetchScope;
00094 }
00095
00096 void ItemSearchJob::doStart()
00097 {
00098 Q_D( ItemSearchJob );
00099
00100 QByteArray command = d->newTag() + " SEARCH ";
00101 command += ImapParser::quote( d->mQuery.toUtf8() );
00102 command += ' ' + ProtocolHelper::itemFetchScopeToByteArray( d->mFetchScope );
00103 command += '\n';
00104 d->writeData( command );
00105 }
00106
00107 void ItemSearchJob::doHandleResponse( const QByteArray & tag, const QByteArray & data )
00108 {
00109 Q_D( ItemSearchJob );
00110
00111 if ( tag == "*" ) {
00112 int begin = data.indexOf( "SEARCH" );
00113 if ( begin >= 0 ) {
00114
00115
00116 QList<QByteArray> fetchResponse;
00117 ImapParser::parseParenthesizedList( data, fetchResponse, begin + 7 );
00118
00119 Item item;
00120 ProtocolHelper::parseItemFetchResult( fetchResponse, item );
00121 if ( !item.isValid() )
00122 return;
00123
00124 d->mItems.append( item );
00125 d->mPendingItems.append( item );
00126 if ( !d->mEmitTimer->isActive() )
00127 d->mEmitTimer->start();
00128 return;
00129 }
00130 }
00131 kDebug() << "Unhandled response: " << tag << data;
00132 }
00133
00134 Item::List ItemSearchJob::items() const
00135 {
00136 Q_D( const ItemSearchJob );
00137
00138 return d->mItems;
00139 }
00140
00141 #include "itemsearchjob.moc"