KIMAP Library
idlejob.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 "idlejob.h" 00021 00022 #include <QtCore/QTimer> 00023 #include <KDE/KLocale> 00024 00025 #include "job_p.h" 00026 #include "message_p.h" 00027 #include "session_p.h" 00028 00029 namespace KIMAP 00030 { 00031 class IdleJobPrivate : public JobPrivate 00032 { 00033 public: 00034 IdleJobPrivate( IdleJob *job, Session *session, const QString& name ) 00035 : JobPrivate( session, name ), q(job), 00036 messageCount( -1 ), recentCount( -1 ), 00037 lastMessageCount( -1 ), lastRecentCount( -1 ), 00038 originalSocketTimeout( -1 ) { } 00039 ~IdleJobPrivate() { } 00040 00041 void emitStats() 00042 { 00043 emitStatsTimer.stop(); 00044 00045 emit q->mailBoxStats(q, m_session->selectedMailBox(), 00046 messageCount, recentCount); 00047 00048 lastMessageCount = messageCount; 00049 lastRecentCount = recentCount; 00050 00051 messageCount = -1; 00052 recentCount = -1; 00053 } 00054 00055 IdleJob * const q; 00056 00057 QTimer emitStatsTimer; 00058 00059 int messageCount; 00060 int recentCount; 00061 00062 int lastMessageCount; 00063 int lastRecentCount; 00064 00065 int originalSocketTimeout; 00066 }; 00067 } 00068 00069 using namespace KIMAP; 00070 00071 IdleJob::IdleJob( Session *session ) 00072 : Job( *new IdleJobPrivate(this, session, i18nc("name of the idle job", "Idle")) ) 00073 { 00074 Q_D(IdleJob); 00075 connect( &d->emitStatsTimer, SIGNAL(timeout()), 00076 this, SLOT(emitStats()) ); 00077 } 00078 00079 IdleJob::~IdleJob() 00080 { 00081 } 00082 00083 void KIMAP::IdleJob::stop() 00084 { 00085 Q_D(IdleJob); 00086 d->sessionInternal()->setSocketTimeout( d->originalSocketTimeout ); 00087 d->sessionInternal()->sendData( "DONE" ); 00088 } 00089 00090 void IdleJob::doStart() 00091 { 00092 Q_D(IdleJob); 00093 d->originalSocketTimeout = d->sessionInternal()->socketTimeout(); 00094 d->sessionInternal()->setSocketTimeout( -1 ); 00095 d->tags << d->sessionInternal()->sendCommand( "IDLE" ); 00096 } 00097 00098 void IdleJob::handleResponse( const Message &response ) 00099 { 00100 Q_D(IdleJob); 00101 00102 // We can predict it'll be handled by handleErrorReplies() so emit 00103 // pending signals now (if needed) so that result() will really be 00104 // the last emitted signal. 00105 if ( !response.content.isEmpty() 00106 && d->tags.size() == 1 00107 && d->tags.contains( response.content.first().toString() ) 00108 && ( d->messageCount>=0 || d->recentCount>=0 ) ) { 00109 d->emitStats(); 00110 } 00111 00112 00113 if (handleErrorReplies(response) == NotHandled ) { 00114 if ( response.content.size() > 0 && response.content[0].toString()=="+" ) { 00115 // Got the continuation all is fine 00116 return; 00117 00118 } else if ( response.content.size() > 2 ) { 00119 if ( response.content[2].toString()=="EXISTS" ) { 00120 if ( d->messageCount>=0 ) { 00121 d->emitStats(); 00122 } 00123 00124 d->messageCount = response.content[1].toString().toInt(); 00125 } else if ( response.content[2].toString()=="RECENT" ) { 00126 if ( d->recentCount>=0 ) { 00127 d->emitStats(); 00128 } 00129 00130 d->recentCount = response.content[1].toString().toInt(); 00131 } 00132 } 00133 00134 if ( d->messageCount>=0 && d->recentCount>=0 ) { 00135 d->emitStats(); 00136 } else if ( d->messageCount>=0 || d->recentCount>=0 ) { 00137 d->emitStatsTimer.start( 200 ); 00138 } 00139 } 00140 } 00141 00142 QString KIMAP::IdleJob::lastMailBox() const 00143 { 00144 Q_D(const IdleJob); 00145 return d->m_session->selectedMailBox(); 00146 } 00147 00148 int KIMAP::IdleJob::lastMessageCount() const 00149 { 00150 Q_D(const IdleJob); 00151 return d->lastMessageCount; 00152 } 00153 00154 int KIMAP::IdleJob::lastRecentCount() const 00155 { 00156 Q_D(const IdleJob); 00157 return d->lastRecentCount; 00158 } 00159 00160 #include "idlejob.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 04:39:10 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 04:39:10 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.