• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

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         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; // items pending for emitting itemsReceived()
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       // split fetch response into key/value pairs
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 QUrl ItemSearchJob::akonadiItemIdUri()
00142 {
00143   return QUrl( QLatin1String( "http://akonadi-project.org/ontologies/aneo#akonadiItemId" ) );
00144 }
00145 
00146 #include "itemsearchjob.moc"

akonadi

Skip menu "akonadi"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal