ircclient.cpp
1 //------------------------------------------------------------------------------
2 // ircclient.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "ircclient.h"
24 #include "log.h"
25 
26 IRCClient::IRCClient()
27 {
28  recvTimer.setSingleShot(true);
29  setFakeRecvLag(0);
30 
31  QObject::connect(&socket, SIGNAL(readyRead()), this, SLOT(receiveSocketDataDelayed()));
32  QObject::connect(&recvTimer, SIGNAL(timeout()), this, SLOT(receiveSocketData()));
33 }
34 
35 IRCClient::~IRCClient()
36 {
37  disconnect();
38 }
39 
40 void IRCClient::connect(const QString &address, unsigned short port)
41 {
42  this->port = port;
43  this->hostName = address;
44 
45  emit infoMessage(tr("IRC: Connecting: %1:%2").arg(this->hostName).arg(this->port));
46  socket.connectToHost(address, port);
47 }
48 
49 void IRCClient::connectSocketSignals(SocketSignalsAdapter *pAdapter)
50 {
51  pAdapter->pSocket = &socket;
52  pAdapter->connect(&socket, SIGNAL(connected()), SLOT(connected()));
53  pAdapter->connect(&socket, SIGNAL(disconnected()), SLOT(disconnected()));
54  pAdapter->connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(errorReceived(QAbstractSocket::SocketError)));
55  pAdapter->connect(this, SIGNAL(infoMessage(const QString&)), SLOT(infoMessage(const QString&)));
56 }
57 
58 void IRCClient::disconnect()
59 {
60  if (isConnected())
61  socket.close();
62 }
63 
64 bool IRCClient::isConnected() const
65 {
66  return socket.state() == QTcpSocket::ConnectedState;
67 }
68 
69 void IRCClient::receiveSocketData()
70 {
71  while (socket.canReadLine())
72  {
73  QByteArray socketData = socket.readLine();
74  QString responseLine = QString::fromUtf8(socketData.constData(), socketData.size());
75  emit ircServerResponse(responseLine);
76  }
77 }
78 
79 void IRCClient::receiveSocketDataDelayed()
80 {
81  if (!recvTimer.isActive())
82  recvTimer.start();
83 }
84 
85 bool IRCClient::sendMessage(const QString &message)
86 {
87  if (!isConnected())
88  return false;
89 
90  QByteArray messageContent = message.toUtf8();
91  messageContent.append("\r\n");
92 
93  qint64 numBytesWritten = socket.write(messageContent);
94  socket.flush();
95 
96  return numBytesWritten == messageContent.size();
97 }
98 
99 void IRCClient::setFakeRecvLag(int lagMs)
100 {
101  recvTimer.setInterval(lagMs);
102 }
void infoMessage(const QString &message)
These are the messages that IRCClient class sends to inform the upper layers of progress.