• Skip to content
  • Skip to link menu
KDE 4.7 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • KDE Home
  • Contact Us
 

KIMAP Library

sessionthread.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 "sessionthread_p.h"
00021 
00022 #include <QtCore/QDebug>
00023 #include <QtCore/QTimer>
00024 
00025 #include <KDE/KDebug>
00026 
00027 #include "imapstreamparser.h"
00028 #include "message_p.h"
00029 #include "session.h"
00030 
00031 using namespace KIMAP;
00032 
00033 Q_DECLARE_METATYPE(KTcpSocket::Error)
00034 Q_DECLARE_METATYPE(KSslErrorUiData)
00035 static const int _kimap_socketErrorTypeId = qRegisterMetaType<KTcpSocket::Error>();
00036 static const int _kimap_sslErrorUiData = qRegisterMetaType<KSslErrorUiData>();
00037 
00038 SessionThread::SessionThread( const QString &hostName, quint16 port, Session *parent )
00039   : QThread(), m_hostName(hostName), m_port(port),
00040     m_session(parent), m_socket(0), m_stream(0), m_encryptedMode(false)
00041 {
00042   // Yeah, sounds weird, but QThread object is linked to the parent
00043   // thread not to itself, and I'm too lazy to introduce yet another
00044   // internal QObject
00045   moveToThread(this);
00046 }
00047 
00048 SessionThread::~SessionThread()
00049 {
00050   // don't call quit() directly, this will deadlock in wait() if exec() hasn't run yet
00051   QMetaObject::invokeMethod( this, "quit" );
00052   if ( !wait( 10 * 1000 ) ) {
00053     kWarning() << "Session thread refuses to die, killing harder...";
00054     terminate();
00055   }
00056 }
00057 
00058 void SessionThread::sendData( const QByteArray &payload )
00059 {
00060   QMutexLocker locker(&m_mutex);
00061 
00062   m_dataQueue.enqueue( payload );
00063   QTimer::singleShot( 0, this, SLOT( writeDataQueue() ) );
00064 }
00065 
00066 void SessionThread::writeDataQueue()
00067 {
00068   QMutexLocker locker(&m_mutex);
00069 
00070   while ( !m_dataQueue.isEmpty() ) {
00071     m_socket->write( m_dataQueue.dequeue() );
00072   }
00073 }
00074 
00075 void SessionThread::readMessage()
00076 {
00077   QMutexLocker locker(&m_mutex);
00078 
00079   if ( m_stream->availableDataSize()==0 ) {
00080     return;
00081   }
00082 
00083   Message message;
00084   QList<Message::Part> *payload = &message.content;
00085 
00086   try {
00087     while ( !m_stream->atCommandEnd() ) {
00088       if ( m_stream->hasString() ) {
00089         QByteArray string = m_stream->readString();
00090         if ( string == "NIL" ) {
00091           *payload << Message::Part( QList<QByteArray>() );
00092         } else {
00093           *payload << Message::Part(string);
00094         }
00095       } else if ( m_stream->hasList() ) {
00096         *payload << Message::Part(m_stream->readParenthesizedList());
00097       } else if ( m_stream->hasResponseCode() ) {
00098         payload = &message.responseCode;
00099       } else if ( m_stream->atResponseCodeEnd() ) {
00100         payload = &message.content;
00101       } else if ( m_stream->hasLiteral() ) {
00102         QByteArray literal;
00103         while ( !m_stream->atLiteralEnd() ) {
00104           literal+= m_stream->readLiteralPart();
00105         }
00106         *payload << Message::Part(literal);
00107       } else {
00108         // Oops! Something really bad happened
00109         throw ImapParserException( "Inconsistent state, probably due to some packet loss" );
00110       }
00111     }
00112 
00113     emit responseReceived(message);
00114 
00115   } catch (KIMAP::ImapParserException e) {
00116     qWarning() << "The stream parser raised an exception:" << e.what();
00117   }
00118 
00119   if ( m_stream->availableDataSize()>1 ) {
00120     QTimer::singleShot( 0, this, SLOT( readMessage() ) );
00121   }
00122 
00123 }
00124 
00125 void SessionThread::closeSocket()
00126 {
00127   QTimer::singleShot( 0, this, SLOT( doCloseSocket() ) );
00128 }
00129 
00130 void SessionThread::doCloseSocket()
00131 {
00132   m_encryptedMode = false;
00133   m_socket->close();
00134 }
00135 
00136 void SessionThread::reconnect()
00137 {
00138   QMutexLocker locker(&m_mutex);
00139 
00140   if ( m_socket->state() != SessionSocket::ConnectedState &&
00141        m_socket->state() != SessionSocket::ConnectingState ) {
00142     if (m_encryptedMode) {
00143       m_socket->connectToHostEncrypted(m_hostName, m_port);
00144     } else {
00145       m_socket->connectToHost(m_hostName, m_port);
00146     }
00147   }
00148 }
00149 
00150 void SessionThread::run()
00151 {
00152   m_socket = new SessionSocket;
00153   m_stream = new ImapStreamParser( m_socket );
00154   connect( m_socket, SIGNAL(readyRead()),
00155            this, SLOT(readMessage()), Qt::QueuedConnection );
00156 
00157   connect( m_socket, SIGNAL(disconnected()),
00158            m_session, SLOT(socketDisconnected()) );
00159   connect( m_socket, SIGNAL(connected()),
00160            m_session, SLOT(socketConnected()) );
00161   connect( m_socket, SIGNAL(error(KTcpSocket::Error)),
00162            m_session, SLOT(socketError()) );
00163 
00164 
00165   connect( this, SIGNAL(responseReceived(KIMAP::Message)),
00166            m_session, SLOT(responseReceived(KIMAP::Message)) );
00167 
00168   QTimer::singleShot( 0, this, SLOT( reconnect() ) );
00169   exec();
00170 
00171   delete m_stream;
00172   delete m_socket;
00173 }
00174 
00175 void SessionThread::startSsl(const KTcpSocket::SslVersion &version)
00176 {
00177   QMutexLocker locker(&m_mutex);
00178 
00179   m_socket->setAdvertisedSslVersion(version);
00180   m_socket->ignoreSslErrors();
00181   connect(m_socket, SIGNAL(encrypted()), this, SLOT(sslConnected()));
00182   m_socket->startClientEncryption();
00183 }
00184 
00185 void SessionThread::sslConnected()
00186 {
00187   QMutexLocker locker(&m_mutex);
00188   KSslCipher cipher = m_socket->sessionCipher();
00189 
00190   if ( m_socket->sslErrors().count() > 0 || m_socket->encryptionMode() != KTcpSocket::SslClientMode
00191       || cipher.isNull() || cipher.usedBits() == 0) {
00192       kDebug() << "Initial SSL handshake failed. cipher.isNull() is" << cipher.isNull()
00193                     << ", cipher.usedBits() is" << cipher.usedBits()
00194                     << ", the socket says:" <<  m_socket->errorString()
00195                     << "and the list of SSL errors contains"
00196                     << m_socket->sslErrors().count() << "items.";
00197      KSslErrorUiData errorData(m_socket);
00198      emit sslError(errorData);
00199   } else {
00200     kDebug() << "TLS negotiation done.";
00201     m_encryptedMode = true;
00202     emit encryptionNegotiationResult(true, m_socket->negotiatedSslVersion());
00203   }
00204 }
00205 
00206 void SessionThread::sslErrorHandlerResponse(bool response)
00207 {
00208   QMutexLocker locker(&m_mutex);
00209   if (response) {
00210     m_encryptedMode = true;
00211     emit encryptionNegotiationResult(true, m_socket->negotiatedSslVersion());
00212   } else {
00213      m_encryptedMode = false;
00214      //reconnect in unencrypted mode, so new commands can be issued
00215      m_socket->disconnectFromHost();
00216      m_socket->waitForDisconnected();
00217      m_socket->connectToHost(m_hostName, m_port);
00218      emit encryptionNegotiationResult(false, KTcpSocket::UnknownSslVersion);
00219   }
00220 }
00221 
00222 #include "sessionthread_p.moc"
00223 

KIMAP Library

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • 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
Generated for KDE-PIM Libraries by doxygen 1.7.5
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