KIMAP Library
listjob.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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 QByteArray command;
00054 QList<MailBoxDescriptor> descriptors;
00055 QMap< MailBoxDescriptor, QList<QByteArray> > flags;
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 d->includeUnsubscribed = include;
00081 }
00082
00083 bool ListJob::isIncludeUnsubscribed() const
00084 {
00085 Q_D(const ListJob);
00086 return d->includeUnsubscribed;
00087 }
00088
00089 QList<MailBoxDescriptor> ListJob::mailBoxes() const
00090 {
00091 Q_D(const ListJob);
00092 return d->descriptors;
00093 }
00094
00095 QMap< MailBoxDescriptor, QList<QByteArray> > ListJob::flags() const
00096 {
00097 Q_D(const ListJob);
00098 return d->flags;
00099 }
00100
00101 void ListJob::doStart()
00102 {
00103 Q_D(ListJob);
00104
00105 d->command = "LSUB";
00106 if (d->includeUnsubscribed) {
00107 d->command = "LIST";
00108 }
00109
00110 d->emitPendingsTimer.start( 100 );
00111 d->tag = d->sessionInternal()->sendCommand( d->command, "\"\" *" );
00112 }
00113
00114 void ListJob::handleResponse( const Message &response )
00115 {
00116 Q_D(ListJob);
00117
00118 if (handleErrorReplies(response) == NotHandled) {
00119 if ( response.content.size() >= 5
00120 && response.content[1].toString()==d->command ) {
00121 QList<QByteArray> flags = response.content[2].toList();
00122 QByteArray separator = response.content[3].toString();
00123 Q_ASSERT(separator.size()==1);
00124 QByteArray fullName;
00125 for ( int i=4; i<response.content.size(); i++ ) {
00126 fullName+= response.content[i].toString()+' ';
00127 }
00128 fullName.chop( 1 );
00129
00130 fullName = decodeImapFolderName( fullName );
00131
00132 MailBoxDescriptor mailBoxDescriptor;
00133 mailBoxDescriptor.separator = QChar( separator[0] );
00134 mailBoxDescriptor.name = QString::fromUtf8( fullName );
00135
00136 d->descriptors << mailBoxDescriptor;
00137 d->flags[mailBoxDescriptor] = flags;
00138
00139 d->pendingDescriptors << mailBoxDescriptor;
00140 d->pendingFlags << flags;
00141 }
00142 } else {
00143 d->emitPendingsTimer.stop();
00144 d->emitPendings();
00145 }
00146 }
00147
00148 #include "listjob.moc"