• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.8.3 API Reference
  • KDE Home
  • 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 <boost/bind.hpp>
00023 #include <QtCore/QTimer>
00024 #include <KDE/KLocale>
00025 
00026 #include "job_p.h"
00027 #include "message_p.h"
00028 #include "rfccodecs.h"
00029 #include "session_p.h"
00030 
00031 namespace KIMAP
00032 {
00033   class ListJobPrivate : public JobPrivate
00034   {
00035     public:
00036       ListJobPrivate( ListJob *job, Session *session, const QString& name ) : JobPrivate(session, name), q(job), option(ListJob::NoOption) { }
00037       ~ListJobPrivate() { }
00038 
00039       void emitPendings()
00040       {
00041         if ( pendingDescriptors.isEmpty() ) {
00042           return;
00043         }
00044 
00045         emit q->mailBoxesReceived( pendingDescriptors, pendingFlags );
00046 
00047         pendingDescriptors.clear();
00048         pendingFlags.clear();
00049       }
00050 
00051       ListJob * const q;
00052 
00053       ListJob::Option option;
00054       QList<MailBoxDescriptor> namespaces;
00055       QByteArray command;
00056 
00057       QTimer emitPendingsTimer;
00058       QList<MailBoxDescriptor> pendingDescriptors;
00059       QList< QList<QByteArray> > pendingFlags;
00060   };
00061 }
00062 
00063 using namespace KIMAP;
00064 
00065 ListJob::ListJob( Session *session )
00066   : Job( *new ListJobPrivate(this, session, i18n("List")) )
00067 {
00068   Q_D(ListJob);
00069   connect( &d->emitPendingsTimer, SIGNAL(timeout()),
00070            this, SLOT(emitPendings()) );
00071 }
00072 
00073 ListJob::~ListJob()
00074 {
00075 }
00076 
00077 void ListJob::setIncludeUnsubscribed( bool include )
00078 {
00079   Q_D(ListJob);
00080   if (include) {
00081     d->option = ListJob::IncludeUnsubscribed;
00082   } else {
00083     d->option = ListJob::NoOption;
00084   }
00085 }
00086 
00087 bool ListJob::isIncludeUnsubscribed() const
00088 {
00089   Q_D(const ListJob);
00090   return ( d->option == ListJob::IncludeUnsubscribed );
00091 }
00092 
00093 void ListJob::setOption( Option option )
00094 {
00095   Q_D(ListJob);
00096   d->option = option;
00097 }
00098 
00099 ListJob::Option ListJob::option() const
00100 {
00101   Q_D(const ListJob);
00102   return d->option;
00103 }
00104 
00105 void ListJob::setQueriedNamespaces( const QList<MailBoxDescriptor> &namespaces )
00106 {
00107   Q_D(ListJob);
00108   d->namespaces = namespaces;
00109 }
00110 
00111 QList<MailBoxDescriptor> ListJob::queriedNamespaces() const
00112 {
00113   Q_D(const ListJob);
00114   return d->namespaces;
00115 }
00116 
00117 QList<MailBoxDescriptor> ListJob::mailBoxes() const
00118 {
00119   return QList<MailBoxDescriptor>();
00120 }
00121 
00122 QMap< MailBoxDescriptor, QList<QByteArray> > ListJob::flags() const
00123 {
00124   return QMap< MailBoxDescriptor, QList<QByteArray> >();
00125 }
00126 
00127 void ListJob::doStart()
00128 {
00129   Q_D(ListJob);
00130 
00131   switch (d->option) {
00132     break;
00133   case IncludeUnsubscribed:
00134     d->command = "LIST";
00135     break;
00136   case IncludeFolderRoleFlags:
00137     d->command = "XLIST";
00138     break;
00139   case NoOption:
00140   default:
00141     d->command = "LSUB";
00142   }
00143 
00144   d->emitPendingsTimer.start( 100 );
00145 
00146   if ( d->namespaces.isEmpty() ) {
00147     d->tags << d->sessionInternal()->sendCommand( d->command, "\"\" *" );
00148   } else {
00149     foreach ( const MailBoxDescriptor &descriptor, d->namespaces ) {
00150       QString parameters = "\"\" \"%1\"";
00151 
00152       if ( descriptor.name.endsWith( descriptor.separator ) ) {
00153         QString name = encodeImapFolderName( descriptor.name );
00154         name.chop( 1 );
00155         d->tags << d->sessionInternal()->sendCommand( d->command,
00156                                                       parameters.arg( name ).toUtf8() );
00157       }
00158 
00159       d->tags << d->sessionInternal()->sendCommand( d->command,
00160                                                     parameters.arg( descriptor.name+'*' ).toUtf8() );
00161     }
00162   }
00163 }
00164 
00165 void ListJob::handleResponse( const Message &response )
00166 {
00167   Q_D(ListJob);
00168 
00169   // We can predict it'll be handled by handleErrorReplies() so stop
00170   // the timer now so that result() will really be the last emitted signal.
00171   if ( !response.content.isEmpty()
00172        && d->tags.size() == 1
00173        && d->tags.contains( response.content.first().toString() ) ) {
00174     d->emitPendingsTimer.stop();
00175     d->emitPendings();
00176   }
00177 
00178   if ( handleErrorReplies( response ) == NotHandled ) {
00179     if ( response.content.size() >= 5 && response.content[1].toString() == d->command ) {
00180       QList<QByteArray> flags = response.content[2].toList();
00181       std::transform( flags.begin(), flags.end(), flags.begin(), boost::bind(&QByteArray::toLower, _1) );
00182       QByteArray separator = response.content[3].toString();
00183       if ( separator.isEmpty() ) {
00184         // Defaults to / for servers reporting an empty list
00185         // it's supposedly not a problem as servers doing that
00186         // only do it for mailboxes with no child.
00187         separator = "/"; //krazy:exclude=doublequote_chars since a QByteArray
00188       }
00189       Q_ASSERT(separator.size()==1);
00190       QByteArray fullName;
00191       for ( int i=4; i<response.content.size(); i++ ) {
00192         fullName += response.content[i].toString() + ' ';
00193       }
00194       fullName.chop( 1 );
00195 
00196       fullName = decodeImapFolderName( fullName );
00197 
00198       MailBoxDescriptor mailBoxDescriptor;
00199       mailBoxDescriptor.separator = QChar( separator[0] );
00200       mailBoxDescriptor.name = QString::fromUtf8( fullName );
00201       convertInboxName( mailBoxDescriptor );
00202 
00203       d->pendingDescriptors << mailBoxDescriptor;
00204       d->pendingFlags << flags;
00205     }
00206   }
00207 }
00208 
00209 void ListJob::convertInboxName(KIMAP::MailBoxDescriptor& descriptor)
00210 {
00211     //Inbox must be case sensitive, according to the RFC, so make it always uppercase
00212     QStringList pathParts = descriptor.name.split(descriptor.separator);
00213     if ( !pathParts.isEmpty() && pathParts[0].compare( QLatin1String("INBOX"), Qt::CaseInsensitive ) == 0 ) {
00214        pathParts.removeAt(0);
00215        descriptor.name = QLatin1String("INBOX");
00216        if ( !pathParts.isEmpty() )
00217         descriptor.name += descriptor.separator + pathParts.join( descriptor.separator );
00218     }
00219 }
00220 #include "listjob.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 7 2012 23:55:08 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIMAP Library

Skip menu "KIMAP Library"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.8.3 API Reference

Skip menu "kdepimlibs-4.8.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal