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

KIMAP Library

listjob.cpp

00001 /*
00002     Copyright (c) 2009 Kevin Ottens <ervin@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 "listjob.h"
00021 
00022 #include <QtCore/QTimer>
00023 #include <KDE/KLocale>
00024 
00025 #include "job_p.h"
00026 #include "message_p.h"
00027 #include "rfccodecs.h"
00028 #include "session_p.h"
00029 
00030 namespace KIMAP
00031 {
00032   class ListJobPrivate : public JobPrivate
00033   {
00034     public:
00035       ListJobPrivate( ListJob *job, Session *session, const QString& name ) : JobPrivate(session, name), q(job), includeUnsubscribed(false) { }
00036       ~ListJobPrivate() { }
00037 
00038       void emitPendings()
00039       {
00040         if ( pendingDescriptors.isEmpty() ) {
00041           return;
00042         }
00043 
00044         emit q->mailBoxesReceived( pendingDescriptors, pendingFlags );
00045 
00046         pendingDescriptors.clear();
00047         pendingFlags.clear();
00048       }
00049 
00050       ListJob * const q;
00051 
00052       bool includeUnsubscribed;
00053       QList<MailBoxDescriptor> namespaces;
00054       QByteArray command;
00055 
00056       QList<MailBoxDescriptor> descriptors;
00057       QMap< MailBoxDescriptor, QList<QByteArray> > flags;
00058 
00059       QTimer emitPendingsTimer;
00060       QList<MailBoxDescriptor> pendingDescriptors;
00061       QList< QList<QByteArray> > pendingFlags;
00062   };
00063 }
00064 
00065 using namespace KIMAP;
00066 
00067 ListJob::ListJob( Session *session )
00068   : Job( *new ListJobPrivate(this, session, i18n("List")) )
00069 {
00070   Q_D(ListJob);
00071   connect( &d->emitPendingsTimer, SIGNAL( timeout() ),
00072            this, SLOT( emitPendings() ) );
00073 }
00074 
00075 ListJob::~ListJob()
00076 {
00077 }
00078 
00079 void ListJob::setIncludeUnsubscribed( bool include )
00080 {
00081   Q_D(ListJob);
00082   d->includeUnsubscribed = include;
00083 }
00084 
00085 bool ListJob::isIncludeUnsubscribed() const
00086 {
00087   Q_D(const ListJob);
00088   return d->includeUnsubscribed;
00089 }
00090 
00091 void ListJob::setQueriedNamespaces( const QList<MailBoxDescriptor> &namespaces )
00092 {
00093   Q_D(ListJob);
00094   d->namespaces = namespaces;
00095 }
00096 
00097 QList<MailBoxDescriptor> ListJob::queriedNamespaces() const
00098 {
00099   Q_D(const ListJob);
00100   return d->namespaces;
00101 }
00102 
00103 QList<MailBoxDescriptor> ListJob::mailBoxes() const
00104 {
00105   Q_D(const ListJob);
00106   return d->descriptors;
00107 }
00108 
00109 QMap< MailBoxDescriptor, QList<QByteArray> > ListJob::flags() const
00110 {
00111   Q_D(const ListJob);
00112   return d->flags;
00113 }
00114 
00115 void ListJob::doStart()
00116 {
00117   Q_D(ListJob);
00118 
00119   d->command = "LSUB";
00120   if (d->includeUnsubscribed) {
00121     d->command = "LIST";
00122   }
00123 
00124   d->emitPendingsTimer.start( 100 );
00125 
00126   if ( d->namespaces.isEmpty() ) {
00127     d->tags << d->sessionInternal()->sendCommand( d->command, "\"\" *" );
00128   } else {
00129     foreach ( const MailBoxDescriptor &descriptor, d->namespaces ) {
00130       QString parameters = "\"\" \"%1\"";
00131 
00132       if ( descriptor.name.endsWith( descriptor.separator ) ) {
00133         QString name = encodeImapFolderName( descriptor.name );
00134         name.chop( 1 );
00135         d->tags << d->sessionInternal()->sendCommand( d->command,
00136                                                       parameters.arg( name ).toUtf8() );
00137       }
00138 
00139       d->tags << d->sessionInternal()->sendCommand( d->command,
00140                                                     parameters.arg( descriptor.name+'*' ).toUtf8() );
00141     }
00142   }
00143 }
00144 
00145 void ListJob::handleResponse( const Message &response )
00146 {
00147   Q_D(ListJob);
00148 
00149   // We can predict it'll be handled by handleErrorReplies() so stop
00150   // the timer now so that result() will really be the last emitted signal.
00151   if ( !response.content.isEmpty()
00152        && d->tags.size() == 1
00153        && d->tags.contains( response.content.first().toString() ) ) {
00154     d->emitPendingsTimer.stop();
00155     d->emitPendings();
00156   }
00157 
00158   if (handleErrorReplies(response) == NotHandled) {
00159     if ( response.content.size() >= 5
00160            && response.content[1].toString()==d->command ) {
00161       QList<QByteArray> flags = response.content[2].toList();
00162       QByteArray separator = response.content[3].toString();
00163       Q_ASSERT(separator.size()==1);
00164       QByteArray fullName;
00165       for ( int i=4; i<response.content.size(); i++ ) {
00166         fullName+= response.content[i].toString()+' ';
00167       }
00168       fullName.chop( 1 );
00169 
00170       fullName = decodeImapFolderName( fullName );
00171 
00172       MailBoxDescriptor mailBoxDescriptor;
00173       mailBoxDescriptor.separator = QChar( separator[0] );
00174       mailBoxDescriptor.name = QString::fromUtf8( fullName );
00175 
00176       d->descriptors << mailBoxDescriptor;
00177       d->flags[mailBoxDescriptor] = flags;
00178 
00179       d->pendingDescriptors << mailBoxDescriptor;
00180       d->pendingFlags << flags;
00181     }
00182   }
00183 }
00184 
00185 #include "listjob.moc"

KIMAP Library

Skip menu "KIMAP Library"
  • Main Page
  • 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.7.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