• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • 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.h>
00033 #include <ksocketfactory.h>
00034 #include <klocale.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   };
00058 }
00059 
00060 SocketPrivate::SocketPrivate( Socket* s) : q(s)
00061 {
00062 }
00063 
00064 void SocketPrivate::slotConnected()
00065 {
00066   kDebug( 5324 ) ;
00067 
00068   if ( !secure ) {
00069     kDebug( 5324 ) << "normal connect";
00070     emit q->connected();
00071   } else {
00072     kDebug( 5324 ) << "encrypted connect";
00073     socket->startClientEncryption();
00074   }
00075 }
00076 
00077 void SocketPrivate::slotStateChanged( QAbstractSocket::SocketState state )
00078 {
00079 #ifdef comm_debug
00080   kDebug( 5324 ) << q->objectName() << "State is now:" << ( int ) state;
00081 #endif
00082   if ( state == QAbstractSocket::UnconnectedState )
00083     emit q->failed();
00084 }
00085 
00086 void SocketPrivate::slotModeChanged( QSslSocket::SslMode  state )
00087 {
00088 #ifdef comm_debug
00089   kDebug( 5324 ) << q->objectName() << "Mode is now:" << state;
00090 #endif
00091 }
00092 
00093 void SocketPrivate::slotSocketRead()
00094 {
00095   kDebug(5324) << q->objectName();
00096 
00097   if ( !socket )
00098     return;
00099 
00100   static QString msg;
00101   msg += QLatin1String( socket->readAll() );
00102 
00103   if ( !msg.endsWith( QLatin1Char( '\n' ) ) )
00104     return;
00105 
00106 #ifdef comm_debug
00107   kDebug( 5324 ) << q->objectName() << socket->isEncrypted() << msg.trimmed();
00108 #endif
00109 
00110   emit q->data( msg );
00111   msg.clear();
00112 }
00113 
00114 void SocketPrivate::slotSslErrors( const QList<QSslError> & )
00115 {
00116   kDebug( 5324 ) ;
00117   /* We can safely ignore the errors, we are only interested in the
00118   capabilities. We're not sending auth info. */
00119   socket->ignoreSslErrors();
00120   emit q->connected();
00121 }
00122 
00123 
00124 // ------------------ end private ---------------------------//
00125 
00126 Socket::Socket( QObject* parent )
00127     : QObject( parent ), d( new SocketPrivate( this ) )
00128 {
00129   d->socket = 0;
00130   d->port = 0;
00131   d->secure = false;
00132   kDebug( 5324 ) ;
00133 }
00134 
00135 Socket::~Socket()
00136 {
00137   kDebug( 5324 ) << objectName() ;
00138   delete d;
00139 }
00140 
00141 void Socket::reconnect()
00142 {
00143   kDebug( 5324 ) << objectName() << "Connecting to:" << d->server
00144           <<  ":" <<  d->port;
00145 
00146 #ifdef comm_debug
00147   // kDebug(5324) << objectName() << d->protocol;
00148 #endif
00149 
00150   if ( d->socket )
00151     return;
00152 
00153   d->socket = static_cast<QSslSocket*>
00154              ( KSocketFactory::connectToHost( d->protocol, d->server, d->port, this
00155                                             ) );
00156 
00157   d->socket->setProtocol( QSsl::AnyProtocol );
00158 
00159   connect( d->socket, SIGNAL( stateChanged( QAbstractSocket::SocketState ) ),
00160            SLOT( slotStateChanged( QAbstractSocket::SocketState ) ) );
00161   connect( d->socket, SIGNAL( modeChanged( QSslSocket::SslMode ) ),
00162            SLOT( slotModeChanged( QSslSocket::SslMode ) ) );
00163   connect( d->socket, SIGNAL( connected() ), SLOT( slotConnected() ) );
00164   connect( d->socket, SIGNAL( readyRead() ), SLOT( slotSocketRead() ) );
00165   connect( d->socket, SIGNAL( encrypted() ), SIGNAL( connected() ) );
00166   connect( d->socket, SIGNAL( sslErrors( const QList<QSslError> & ) ),
00167            SLOT( slotSslErrors( const QList<QSslError>& ) ) );
00168 }
00169 
00170 void Socket::write( const QString& text )
00171 {
00172   // kDebug(5324) << objectName() ;
00173   // Eat things in the queue when there is no connection. We need
00174   // to get a connection first don't we...
00175   if ( !d->socket || !available() )
00176     return;
00177 
00178   QByteArray cs = ( text + QLatin1String( "\r\n" ) ).toLatin1();
00179 
00180 #ifdef comm_debug
00181   kDebug( 5324 ) << objectName() << "C   :" << cs;
00182 #endif
00183 
00184   d->socket->write( cs.data(), cs.size() );
00185 }
00186 
00187 bool Socket::available()
00188 {
00189   // kDebug(5324) << objectName() ;
00190   bool ok = d->socket && d->socket->state() == QAbstractSocket::ConnectedState;
00191   return ok;
00192 }
00193 
00194 void Socket::setProtocol( const QString& proto )
00195 {
00196   d->protocol = proto;
00197 }
00198 
00199 void Socket::setServer( const QString& server )
00200 {
00201   d->server = server;
00202 }
00203       
00204 void Socket::setPort( int port )
00205 {
00206   d->port = port;
00207 }
00208 
00209 void Socket::setSecure( bool what )
00210 {
00211   d->secure = what;
00212 }
00213 
00214 #include "socket.moc"

mailtransport

Skip menu "mailtransport"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.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