• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.8.3 API Reference
  • KDE Home
  • Contact Us
 

mailtransport

socket.cpp
00001 /*
00002     Copyright (C) 2006-2007 KovoKs <info@kovoks.nl>
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 // Uncomment the next line for full comm debug
00021 // #define comm_debug
00022 
00023 // Own
00024 #include "socket.h"
00025 
00026 // Qt
00027 #include <QRegExp>
00028 #include <QByteArray>
00029 #include <QSslSocket>
00030 
00031 // KDE
00032 #include <KDebug>
00033 #include <KLocalizedString>
00034 #include <ksocketfactory.h>
00035 
00036 using namespace MailTransport;
00037 
00038 namespace MailTransport
00039 {
00040   class SocketPrivate
00041   {
00042     public:
00043       SocketPrivate( Socket *s );
00044       Socket             *const q;
00045       QSslSocket         *socket;
00046       QString             server;
00047       QString             protocol;
00048       int                 port;
00049       bool                secure;
00050 
00051       // slots
00052       void slotConnected();
00053       void slotStateChanged( QAbstractSocket::SocketState state );
00054       void slotModeChanged( QSslSocket::SslMode  state );
00055       void slotSocketRead();
00056       void slotSslErrors( const QList<QSslError> &errors );
00057     private:
00058       QString m_msg;
00059   };
00060 }
00061 
00062 SocketPrivate::SocketPrivate( Socket *s ) : q( s )
00063 {
00064 }
00065 
00066 void SocketPrivate::slotConnected()
00067 {
00068   kDebug() ;
00069 
00070   if ( !secure ) {
00071     kDebug() << "normal connect";
00072     emit q->connected();
00073   } else {
00074     kDebug() << "encrypted connect";
00075     socket->startClientEncryption();
00076   }
00077 }
00078 
00079 void SocketPrivate::slotStateChanged( QAbstractSocket::SocketState state )
00080 {
00081 #ifdef comm_debug
00082   kDebug() << "State is now:" << ( int ) state;
00083 #endif
00084   if ( state == QAbstractSocket::UnconnectedState ) {
00085     emit q->failed();
00086   }
00087 }
00088 
00089 void SocketPrivate::slotModeChanged( QSslSocket::SslMode  state )
00090 {
00091 #ifdef comm_debug
00092   kDebug() << "Mode is now:" << state;
00093 #endif
00094   if ( state == QSslSocket::SslClientMode ) {
00095     emit q->tlsDone();
00096   }
00097 }
00098 
00099 void SocketPrivate::slotSocketRead()
00100 {
00101   kDebug();
00102 
00103   if ( !socket ) {
00104     return;
00105   }
00106 
00107   m_msg += QLatin1String( socket->readAll() );
00108 
00109   if ( !m_msg.endsWith( QLatin1Char( '\n' ) ) ) {
00110     return;
00111   }
00112 
00113 #ifdef comm_debug
00114   kDebug() << socket->isEncrypted() << m_msg.trimmed();
00115 #endif
00116 
00117   emit q->data( m_msg );
00118   m_msg.clear();
00119 }
00120 
00121 void SocketPrivate::slotSslErrors( const QList<QSslError> & )
00122 {
00123   kDebug();
00124   /* We can safely ignore the errors, we are only interested in the
00125   capabilities. We're not sending auth info. */
00126   socket->ignoreSslErrors();
00127   emit q->connected();
00128 }
00129 
00130 // ------------------ end private ---------------------------//
00131 
00132 Socket::Socket( QObject *parent )
00133     : QObject( parent ), d( new SocketPrivate( this ) )
00134 {
00135   d->socket = 0;
00136   d->port = 0;
00137   d->secure = false;
00138   kDebug();
00139 }
00140 
00141 Socket::~Socket()
00142 {
00143   kDebug();
00144   delete d;
00145 }
00146 
00147 void Socket::reconnect()
00148 {
00149   kDebug() << "Connecting to:" << d->server <<  ":" <<  d->port;
00150 
00151 #ifdef comm_debug
00152   // kDebug() << d->protocol;
00153 #endif
00154 
00155   if ( d->socket ) {
00156     return;
00157   }
00158 
00159   d->socket =
00160     static_cast<QSslSocket *>( KSocketFactory::connectToHost( d->protocol, d->server,
00161                                                               d->port, this ) );
00162 
00163   d->socket->setProtocol( QSsl::AnyProtocol );
00164 
00165   connect( d->socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
00166            SLOT(slotStateChanged(QAbstractSocket::SocketState)) );
00167   connect( d->socket, SIGNAL(modeChanged(QSslSocket::SslMode)),
00168            SLOT(slotModeChanged(QSslSocket::SslMode)) );
00169   connect( d->socket, SIGNAL(connected()), SLOT(slotConnected()) );
00170   connect( d->socket, SIGNAL(readyRead()), SLOT(slotSocketRead()) );
00171   connect( d->socket, SIGNAL(encrypted()), SIGNAL(connected()) );
00172   connect( d->socket, SIGNAL(sslErrors(QList<QSslError>)),
00173            SLOT(slotSslErrors(QList<QSslError>)) );
00174 }
00175 
00176 void Socket::write( const QString &text )
00177 {
00178   // kDebug();
00179   // Eat things in the queue when there is no connection. We need
00180   // to get a connection first don't we...
00181   if ( !d->socket || !available() ) {
00182     return;
00183   }
00184 
00185   QByteArray cs = ( text + QLatin1String( "\r\n" ) ).toLatin1();
00186 
00187 #ifdef comm_debug
00188   kDebug() << "C   :" << cs;
00189 #endif
00190 
00191   d->socket->write( cs.data(), cs.size() );
00192 }
00193 
00194 bool Socket::available()
00195 {
00196   // kDebug();
00197   bool ok = d->socket && d->socket->state() == QAbstractSocket::ConnectedState;
00198   return ok;
00199 }
00200 
00201 void Socket::startTLS()
00202 {
00203   kDebug() << objectName();
00204   d->socket->setProtocol( QSsl::TlsV1 );
00205   d->socket->startClientEncryption();
00206 }
00207 
00208 void Socket::setProtocol( const QString &proto )
00209 {
00210   d->protocol = proto;
00211 }
00212 
00213 void Socket::setServer( const QString &server )
00214 {
00215   d->server = server;
00216 }
00217 
00218 void Socket::setPort( int port )
00219 {
00220   d->port = port;
00221 }
00222 
00223 void Socket::setSecure( bool what )
00224 {
00225   d->secure = what;
00226 }
00227 
00228 #include "socket.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Apr 30 2012 21:49:03 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

mailtransport

Skip menu "mailtransport"
  • Main Page
  • 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