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  {
62  socket.close();
63  }
64 }
65 
66 bool IRCClient::isConnected() const
67 {
68  return socket.state() == QTcpSocket::ConnectedState;
69 }
70 
71 void IRCClient::receiveSocketData()
72 {
73  while (socket.canReadLine())
74  {
75  QByteArray socketData = socket.readLine();
76  QString responseLine = QString::fromUtf8(socketData.constData(), socketData.size());
77  emit ircServerResponse(responseLine);
78  }
79 }
80 
81 void IRCClient::receiveSocketDataDelayed()
82 {
83  if (!recvTimer.isActive())
84  {
85  recvTimer.start();
86  }
87 }
88 
89 bool IRCClient::sendMessage(const QString& message)
90 {
91  if (!isConnected())
92  {
93  return false;
94  }
95 
96  QByteArray messageContent = message.toUtf8();
97  messageContent.append("\r\n");
98 
99  qint64 numBytesWritten = socket.write(messageContent);
100  socket.flush();
101 
102  return numBytesWritten == messageContent.size();
103 }
104 
105 void IRCClient::setFakeRecvLag(int lagMs)
106 {
107  recvTimer.setInterval(lagMs);
108 }
void infoMessage(const QString &message)
These are the messages that IRCClient class sends to inform the upper layers of progress.