2
kircsocket.cpp - IRC socket.
4
Copyright (c) 2002 by Nick Betcher <nbetcher@kde.org>
5
Copyright (c) 2003-2007 by Michel Hermier <michel.hermier@gmail.com>
6
Copyright (c) 2006 by Tommi Rantala <tommi.rantala@cs.helsinki.fi>
8
Kopete (c) 2002-2007 by the Kopete developers <kopete-devel@kde.org>
10
*************************************************************************
12
* This program is free software; you can redistribute it and/or modify *
13
* it under the terms of the GNU General Public License as published by *
14
* the Free Software Foundation; either version 2 of the License, or *
15
* (at your option) any later version. *
17
*************************************************************************
20
#include "kircsocket.moc"
21
#include "kircsocket_p.moc"
23
#include "kirccontext.h"
24
#include "kircentity.h"
25
#include "kirchandler.h"
29
#include <qtextcodec.h>
35
SocketPrivate::SocketPrivate(KIrc::Socket *socket)
38
, state(KIrc::Socket::Idle)
42
void SocketPrivate::socketGotError(QAbstractSocket::SocketError error)
45
// QString errStr = d->socket->errorString();
46
kDebug(14121) << "Socket error: " << error;
47
// postErrorEvent(errStr);
52
void SocketPrivate::socketReadyRead()
56
// The caller can also be self so lets check the socket still exist
58
if (socket && socket->canReadLine())
61
QByteArray raw = socket->readLine();
63
Message msg = Message::fromLine(raw, &ok);
64
// msg.setDirection(Message::InGoing);
68
emit q->receivedMessage(msg);
69
context->onMessage(context, msg, q);
72
// postErrorEvent(i18n("Parse error while parsing: %1").arg(msg.rawLine()));
74
// FIXME: post events instead of reschudeling ?
75
QTimer::singleShot( 0, this, SLOT(socketReadyRead()) );
78
// if(d->socket->socketStatus() != KExtendedSocket::connected)
82
Socket::Socket(Context *context, SocketPrivate *socketp)
88
d->owner = new Entity(context);
96
Socket::ConnectionState Socket::connectionState() const
102
void Socket::setConnectionState(Socket::ConnectionState newstate)
105
if (d->state != newstate)
108
emit connectionStateChanged(newstate);
112
QAbstractSocket *Socket::socket()
118
void Socket::setSocket(QAbstractSocket *socket)
123
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
124
d, SLOT(socketGotError(QAbstractSocket::SocketError)));
125
connect(socket, SIGNAL(readyRead()),
126
d, SLOT(socketReadyRead()));
128
connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
129
SLOT(socketStateChanged(QAbstractSocket::SocketState)));
132
EntityPtr Socket::owner() const
138
void Socket::writeMessage(const Message &msg)
143
#warning Check message validity before sending it
146
if (!d->socket || d->socket->state() != QAbstractSocket::ConnectedState)
148
kDebug(14121)<<"Error sending message "<<msg.toLine();
149
// postErrorEvent(i18n("Attempting to send while not connected: %1", msg.data()));
153
qint64 wrote = d->socket->write(msg.toLine());
156
kDebug(14121) << "Socket write failed!";
158
//kDebug(14121) << QString::fromLatin1("(%1 bytes) >> %2").arg(wrote).arg(QString(msg.toLine()));
169
void Socket::socketStateChanged(QAbstractSocket::SocketState newstate)
175
case QAbstractSocket::UnconnectedState:
176
setConnectionState(Socket::Idle);
178
case QAbstractSocket::HostLookupState:
179
setConnectionState(Socket::HostLookup);
181
case QAbstractSocket::ConnectingState:
182
setConnectionState(Socket::Connecting);
184
// MUST BE HANDLED BY CHILDREN
185
// case QAbstractSocket::ConnectedState:
186
// setConnectionState(Socket::Open);
188
case QAbstractSocket::ClosingState:
189
setConnectionState(Socket::Closing);
192
// postErrorEvent(i18n("Unknown SocketState value:%1", newstate));
198
void Socket::showInfoDialog()
202
static_cast<KSSLSocket*>( d->socket )->showInfoDialog();
206
QByteArray Socket::encode(const QString &str, bool *success, QTextCodec *codec) const
211
if (!codec || !codec->canEncode(str))
213
if (!d->defaultCodec || !d->defaultCodec->canEncode(str))
215
if (!UTF8 || !UTF8->canEncode(str))
217
// postErrorEvent(i18n("Codec Error: .").arg(codec->name()));
222
// postErrorEvent(i18n("Codec Warning: Using codec %1 for:\n%2.")
223
// .arg(UTF8->name(), str));
228
codec = d->defaultCodec;
232
return codec->fromUnicode(str);