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

mailtransport

  • mailtransport
socket.cpp
1 /*
2  Copyright (C) 2006-2007 KovoKs <info@kovoks.nl>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 // Uncomment the next line for full comm debug
21 // #define comm_debug
22 
23 // Own
24 #include "socket.h"
25 
26 // Qt
27 #include <QRegExp>
28 #include <QByteArray>
29 #include <QSslSocket>
30 
31 // KDE
32 #include <KDebug>
33 #include <KLocalizedString>
34 #include <ksocketfactory.h>
35 
36 using namespace MailTransport;
37 
38 namespace MailTransport {
39 
40 class SocketPrivate
41 {
42  public:
43  SocketPrivate( Socket *s );
44  Socket *const q;
45  QSslSocket *socket;
46  QString server;
47  QString protocol;
48  int port;
49  bool secure;
50 
51  // slots
52  void slotConnected();
53  void slotStateChanged( QAbstractSocket::SocketState state );
54  void slotModeChanged( QSslSocket::SslMode state );
55  void slotSocketRead();
56  void slotSslErrors( const QList<QSslError> &errors );
57 
58  private:
59  QString m_msg;
60 };
61 
62 }
63 
64 SocketPrivate::SocketPrivate( Socket *s ) : q( s )
65 {
66 }
67 
68 void SocketPrivate::slotConnected()
69 {
70  kDebug();
71 
72  if ( !secure ) {
73  kDebug() << "normal connect";
74  emit q->connected();
75  } else {
76  kDebug() << "encrypted connect";
77  socket->startClientEncryption();
78  }
79 }
80 
81 void SocketPrivate::slotStateChanged( QAbstractSocket::SocketState state )
82 {
83 #ifdef comm_debug
84  kDebug() << "State is now:" << ( int ) state;
85 #endif
86  if ( state == QAbstractSocket::UnconnectedState ) {
87  emit q->failed();
88  }
89 }
90 
91 void SocketPrivate::slotModeChanged( QSslSocket::SslMode state )
92 {
93 #ifdef comm_debug
94  kDebug() << "Mode is now:" << state;
95 #endif
96  if ( state == QSslSocket::SslClientMode ) {
97  emit q->tlsDone();
98  }
99 }
100 
101 void SocketPrivate::slotSocketRead()
102 {
103  kDebug();
104 
105  if ( !socket ) {
106  return;
107  }
108 
109  m_msg += QLatin1String( socket->readAll() );
110 
111  if ( !m_msg.endsWith( QLatin1Char( '\n' ) ) ) {
112  return;
113  }
114 
115 #ifdef comm_debug
116  kDebug() << socket->isEncrypted() << m_msg.trimmed();
117 #endif
118 
119  emit q->data( m_msg );
120  m_msg.clear();
121 }
122 
123 void SocketPrivate::slotSslErrors( const QList<QSslError> & )
124 {
125  kDebug();
126  /* We can safely ignore the errors, we are only interested in the
127  capabilities. We're not sending auth info. */
128  socket->ignoreSslErrors();
129  emit q->connected();
130 }
131 
132 // ------------------ end private ---------------------------//
133 
134 Socket::Socket( QObject *parent )
135  : QObject( parent ), d( new SocketPrivate( this ) )
136 {
137  d->socket = 0;
138  d->port = 0;
139  d->secure = false;
140  kDebug();
141 }
142 
143 Socket::~Socket()
144 {
145  kDebug();
146  delete d;
147 }
148 
149 void Socket::reconnect()
150 {
151  kDebug() << "Connecting to:" << d->server << ":" << d->port;
152 
153 #ifdef comm_debug
154  // kDebug() << d->protocol;
155 #endif
156 
157  if ( d->socket ) {
158  return;
159  }
160 
161  d->socket =
162  static_cast<QSslSocket *>( KSocketFactory::connectToHost( d->protocol, d->server,
163  d->port, this ) );
164 
165  d->socket->setProtocol( QSsl::AnyProtocol );
166 
167  connect( d->socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
168  SLOT(slotStateChanged(QAbstractSocket::SocketState)) );
169  connect( d->socket, SIGNAL(modeChanged(QSslSocket::SslMode)),
170  SLOT(slotModeChanged(QSslSocket::SslMode)) );
171  connect( d->socket, SIGNAL(connected()), SLOT(slotConnected()) );
172  connect( d->socket, SIGNAL(readyRead()), SLOT(slotSocketRead()) );
173  connect( d->socket, SIGNAL(encrypted()), SIGNAL(connected()) );
174  connect( d->socket, SIGNAL(sslErrors(QList<QSslError>)),
175  SLOT(slotSslErrors(QList<QSslError>)) );
176 }
177 
178 void Socket::write( const QString &text )
179 {
180  // kDebug();
181  // Eat things in the queue when there is no connection. We need
182  // to get a connection first don't we...
183  if ( !d->socket || !available() ) {
184  return;
185  }
186 
187  QByteArray cs = ( text + QLatin1String( "\r\n" ) ).toLatin1();
188 
189 #ifdef comm_debug
190  kDebug() << "C :" << cs;
191 #endif
192 
193  d->socket->write( cs.data(), cs.size() );
194 }
195 
196 bool Socket::available()
197 {
198  // kDebug();
199  bool ok = d->socket && d->socket->state() == QAbstractSocket::ConnectedState;
200  return ok;
201 }
202 
203 void Socket::startTLS()
204 {
205  kDebug() << objectName();
206  d->socket->setProtocol( QSsl::TlsV1 );
207  d->socket->startClientEncryption();
208 }
209 
210 void Socket::setProtocol( const QString &proto )
211 {
212  d->protocol = proto;
213 }
214 
215 void Socket::setServer( const QString &server )
216 {
217  d->server = server;
218 }
219 
220 void Socket::setPort( int port )
221 {
222  d->port = port;
223 }
224 
225 void Socket::setSecure( bool what )
226 {
227  d->secure = what;
228 }
229 
230 #include "moc_socket.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:26:53 by doxygen 1.8.3.1 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.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • 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