2 /***************************************************************************
3 gwbytestream.cpp - Byte Stream using KNetwork sockets
6 copyright : (C) 2004 by Till Gerken <till@tantalo.net>
7 Copyright : (c) 2006 Novell, Inc http://www.opensuse.org
9 Kopete (C) 2004-2007 Kopete developers <kopete-devel@kde.org>
10 ***************************************************************************/
12 /***************************************************************************
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 ***************************************************************************/
21 #include "gwbytestream.h"
22 #include <ksocketfactory.h>
28 KNetworkByteStream::KNetworkByteStream ( QObject
*parent
)
29 : ByteStream ( parent
)
31 kDebug () << "Instantiating new KNetwork byte stream.";
33 // reset close tracking flag
40 bool KNetworkByteStream::connect ( QString host
, QString service
)
42 kDebug () << "Connecting to " << host
<< ", service " << service
;
43 mSocket
= KSocketFactory::connectToHost( "gwims", host
, service
.toUInt(), this );
45 QObject::connect( mSocket
, SIGNAL(error(QAbstractSocket::SocketError
)),
46 this, SLOT(slotError(QAbstractSocket::SocketError
)) );
47 QObject::connect( mSocket
, SIGNAL(connected()), this, SLOT(slotConnected()) );
48 QObject::connect( mSocket
, SIGNAL(disconnected()), this, SLOT(slotConnectionClosed()) );
49 QObject::connect( mSocket
, SIGNAL(readyRead()), this, SLOT(slotReadyRead()) );
50 QObject::connect( mSocket
, SIGNAL(bytesWritten(qint64
)), this, SLOT(slotBytesWritten(qint64
)) );
54 bool KNetworkByteStream::isOpen () const
57 // determine if socket is open
60 return socket()->isOpen ();
69 void KNetworkByteStream::close ()
71 kDebug () << "Closing stream.";
73 // close the socket and set flag that we are closing it ourselves
80 int KNetworkByteStream::tryWrite ()
83 // send all data from the buffers to the socket
84 QByteArray writeData
= takeWrite();
85 socket()->write ( writeData
.data (), writeData
.size () );
87 return writeData
.size ();
91 QTcpSocket
*KNetworkByteStream::socket () const
98 KNetworkByteStream::~KNetworkByteStream ()
102 void KNetworkByteStream::slotConnected ()
109 void KNetworkByteStream::slotConnectionClosed ()
111 kDebug () << "Socket has been closed.";
113 // depending on who closed the socket, emit different signals
116 kDebug () << "..by ourselves!";
117 kDebug() << "socket error is \"" << socket()->errorString() << "\"";
118 emit
connectionClosed ();
122 kDebug () << "..by the other end";
123 emit
delayedCloseFinished ();
128 void KNetworkByteStream::slotReadyRead ()
130 appendRead ( socket()->readAll() );
136 void KNetworkByteStream::slotBytesWritten ( qint64 bytes
)
139 emit
bytesWritten ( bytes
);
143 void KNetworkByteStream::slotError ( QAbstractSocket::SocketError code
)
145 kDebug () << "Socket error " << mSocket
->errorString() << "' - Code : " << code
;
149 #include "gwbytestream.moc"