Work in progress
[quassel.git] / network / server.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005/06 by The Quassel Team                             *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program 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         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "server.h"
22
23 Server::Server() {
24   socket = new QTcpSocket();
25
26 }
27
28 Server::~Server() {
29   delete socket;
30 }
31
32 void Server::init() {
33   Message::init(&dispatchServerMsg, &dispatchUserMsg);
34 }
35
36 void Server::run() {
37   connect(socket, SIGNAL(connected()), this, SLOT(socketConnected()));
38   connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
39   connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
40   connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
41   connect(socket, SIGNAL(readyRead()), this, SLOT(socketHasData()));
42
43   stream.setDevice(socket);
44   //connectToIrc("irc.quakenet.org", 6667);
45   exec();
46 }
47
48 /*
49 QAbstractSocket::SocketState TcpConnection::state( ) const {
50   return socket.state();
51 }
52 */
53
54 void Server::connectToIrc( const QString & host, quint16 port ) {
55   qDebug() << "Connecting...";
56   socket->connectToHost(host, port);
57 }
58
59 void Server::disconnectFromIrc( ) {
60   socket->disconnectFromHost();
61 }
62
63 void Server::putRawLine( const QString &s ) {
64   qDebug() << "Raw line: " << s;
65   stream << s << "\r\n" << flush;
66   //Message::createFromServerString(this, s);
67 }
68
69 void Server::socketHasData( ) {
70   while(socket->canReadLine()) {
71     QString s = stream.readLine();
72     qDebug() << "Read: " << s;
73     emit recvRawServerMsg(s);
74     Message *msg = Message::createFromServerString(this, s);
75     if(msg) handleServerMsg(msg);
76     delete msg;
77   }
78 }
79
80 void Server::socketError( QAbstractSocket::SocketError err ) {
81   qDebug() << "Socket Error!";
82   //emit error(err);
83 }
84
85 void Server::socketConnected( ) {
86   qDebug() << "Socket connected!";
87   //emit connected();
88 }
89
90 void Server::socketDisconnected( ) {
91   qDebug() << "Socket disconnected!";
92   //emit disconnected();
93 }
94
95 void Server::socketStateChanged(QAbstractSocket::SocketState state) {
96   qDebug() << "Socket state changed: " << state;
97 }
98
99 void Server::handleServerMsg(Message *msg) {
100   
101
102 }
103
104 void Server::handleUserMsg(Message *msg) {
105
106
107 }
108