~ubuntu-branches/ubuntu/saucy/kopete/saucy-proposed

« back to all changes in this revision

Viewing changes to protocols/irc/libkirc/kernel/kircsocket.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:22:39 UTC
  • Revision ID: package-import@ubuntu.com-20130621022239-63l3zc8p0nf26pt6
Tags: upstream-4.10.80
ImportĀ upstreamĀ versionĀ 4.10.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    kircsocket.cpp - IRC socket.
 
3
 
 
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>
 
7
 
 
8
    Kopete    (c) 2002-2007 by the Kopete developers <kopete-devel@kde.org>
 
9
 
 
10
    *************************************************************************
 
11
    *                                                                       *
 
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.                                   *
 
16
    *                                                                       *
 
17
    *************************************************************************
 
18
*/
 
19
 
 
20
#include "kircsocket.moc"
 
21
#include "kircsocket_p.moc"
 
22
 
 
23
#include "kirccontext.h"
 
24
#include "kircentity.h"
 
25
#include "kirchandler.h"
 
26
 
 
27
#include <kdebug.h>
 
28
 
 
29
#include <qtextcodec.h>
 
30
#include <qtimer.h>
 
31
 
 
32
 
 
33
using namespace KIrc;
 
34
 
 
35
SocketPrivate::SocketPrivate(KIrc::Socket *socket)
 
36
                : q_ptr(socket)
 
37
                , socket(0)
 
38
                , state(KIrc::Socket::Idle)
 
39
{
 
40
}
 
41
 
 
42
void SocketPrivate::socketGotError(QAbstractSocket::SocketError error)
 
43
{
 
44
        Q_Q(Socket);
 
45
//      QString errStr = d->socket->errorString();
 
46
        kDebug(14121) << "Socket error: " << error;
 
47
//      postErrorEvent(errStr);
 
48
 
 
49
        q->close();
 
50
}
 
51
 
 
52
void SocketPrivate::socketReadyRead()
 
53
{
 
54
        Q_Q(Socket);
 
55
 
 
56
        // The caller can also be self so lets check the socket still exist
 
57
        
 
58
        if (socket && socket->canReadLine())
 
59
        {
 
60
                bool ok = false;
 
61
                QByteArray raw = socket->readLine();
 
62
 
 
63
                Message msg = Message::fromLine(raw, &ok);
 
64
//              msg.setDirection(Message::InGoing);
 
65
 
 
66
                if (ok)
 
67
                {
 
68
                        emit q->receivedMessage(msg);
 
69
                        context->onMessage(context, msg, q);
 
70
                }
 
71
//              else
 
72
//                      postErrorEvent(i18n("Parse error while parsing: %1").arg(msg.rawLine()));
 
73
 
 
74
                // FIXME: post events instead of reschudeling ?
 
75
                QTimer::singleShot( 0, this, SLOT(socketReadyRead()) );
 
76
        }
 
77
 
 
78
//      if(d->socket->socketStatus() != KExtendedSocket::connected)
 
79
//              error();
 
80
}
 
81
 
 
82
Socket::Socket(Context *context, SocketPrivate *socketp)
 
83
        : QObject(context)
 
84
        , d_ptr(socketp)
 
85
{
 
86
        Q_D(Socket);
 
87
        d->context = context;
 
88
        d->owner = new Entity(context);
 
89
}
 
90
 
 
91
Socket::~Socket()
 
92
{
 
93
        delete d_ptr;
 
94
}
 
95
 
 
96
Socket::ConnectionState Socket::connectionState() const
 
97
{
 
98
        Q_D(const Socket);
 
99
        return d->state;
 
100
}
 
101
 
 
102
void Socket::setConnectionState(Socket::ConnectionState newstate)
 
103
{
 
104
        Q_D(Socket);
 
105
        if (d->state != newstate)
 
106
        {
 
107
                d->state = newstate;
 
108
                emit connectionStateChanged(newstate);
 
109
        }
 
110
}
 
111
 
 
112
QAbstractSocket *Socket::socket()
 
113
{
 
114
        Q_D(Socket);
 
115
        return d->socket;
 
116
}
 
117
 
 
118
void Socket::setSocket(QAbstractSocket *socket)
 
119
{
 
120
        Q_D(Socket);
 
121
        d->socket = socket;
 
122
 
 
123
        connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
 
124
                d,      SLOT(socketGotError(QAbstractSocket::SocketError)));
 
125
        connect(socket, SIGNAL(readyRead()),
 
126
                d,      SLOT(socketReadyRead()));
 
127
 
 
128
        connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
 
129
                        SLOT(socketStateChanged(QAbstractSocket::SocketState)));
 
130
}
 
131
 
 
132
EntityPtr Socket::owner() const
 
133
{
 
134
        Q_D(const Socket);
 
135
        return d->owner;
 
136
}
 
137
 
 
138
void Socket::writeMessage(const Message &msg)
 
139
{
 
140
        Q_D(Socket);
 
141
 
 
142
#ifdef __GNUC__
 
143
        #warning Check message validity before sending it
 
144
#endif
 
145
 
 
146
        if (!d->socket || d->socket->state() != QAbstractSocket::ConnectedState)
 
147
        {
 
148
                kDebug(14121)<<"Error sending message "<<msg.toLine();
 
149
//              postErrorEvent(i18n("Attempting to send while not connected: %1", msg.data()));
 
150
                return;
 
151
        }
 
152
 
 
153
        qint64 wrote = d->socket->write(msg.toLine());
 
154
 
 
155
        if (wrote == -1)
 
156
                kDebug(14121) << "Socket write failed!";
 
157
 
 
158
        //kDebug(14121) << QString::fromLatin1("(%1 bytes) >> %2").arg(wrote).arg(QString(msg.toLine()));
 
159
}
 
160
 
 
161
void Socket::close()
 
162
{
 
163
        Q_D(Socket);
 
164
 
 
165
        delete d->socket;
 
166
        d->socket = 0;
 
167
}
 
168
 
 
169
void Socket::socketStateChanged(QAbstractSocket::SocketState newstate)
 
170
{
 
171
        Q_D(Socket);
 
172
 
 
173
        switch (newstate)
 
174
        {
 
175
        case QAbstractSocket::UnconnectedState:
 
176
                setConnectionState(Socket::Idle);
 
177
                break;
 
178
        case QAbstractSocket::HostLookupState:
 
179
                setConnectionState(Socket::HostLookup);
 
180
                break;
 
181
        case QAbstractSocket::ConnectingState:
 
182
                setConnectionState(Socket::Connecting);
 
183
                break;
 
184
//      MUST BE HANDLED BY CHILDREN
 
185
//      case QAbstractSocket::ConnectedState:
 
186
//              setConnectionState(Socket::Open);
 
187
//              break;
 
188
        case QAbstractSocket::ClosingState:
 
189
                setConnectionState(Socket::Closing);
 
190
                break;
 
191
        default:
 
192
//              postErrorEvent(i18n("Unknown SocketState value:%1", newstate));
 
193
                close();
 
194
        }
 
195
}
 
196
 
 
197
#if 0
 
198
void Socket::showInfoDialog()
 
199
{
 
200
        if( d->useSSL )
 
201
        {
 
202
                static_cast<KSSLSocket*>( d->socket )->showInfoDialog();
 
203
        }
 
204
}
 
205
 
 
206
QByteArray Socket::encode(const QString &str, bool *success, QTextCodec *codec) const
 
207
{
 
208
        kDebug(14121) ;
 
209
        *success = false;
 
210
 
 
211
        if (!codec || !codec->canEncode(str))
 
212
        {
 
213
                if (!d->defaultCodec || !d->defaultCodec->canEncode(str))
 
214
                {
 
215
                        if (!UTF8 || !UTF8->canEncode(str))
 
216
                        {
 
217
//                              postErrorEvent(i18n("Codec Error: .").arg(codec->name()));
 
218
                                return QByteArray();
 
219
                        }
 
220
                        else
 
221
                        {
 
222
//                              postErrorEvent(i18n("Codec Warning: Using codec %1 for:\n%2.")
 
223
//                                      .arg(UTF8->name(), str));
 
224
                                codec = UTF8;
 
225
                        }
 
226
                }
 
227
                else
 
228
                        codec = d->defaultCodec;
 
229
        }
 
230
 
 
231
        *success = true;
 
232
        return codec->fromUnicode(str);
 
233
}
 
234
#endif