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 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;
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
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"