This is it, the long-awaited huge commit with the new Network handling.
[quassel.git] / src / core / networkconnection.h
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
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) version 3.                                           *
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 #ifndef _NETWORKCONNECTION_H_
22 #define _NETWORKCONNECTION_H_
23
24 #include <QAbstractSocket>
25 #include <QString>
26 #include <QStringList>
27 #include <QTcpSocket>
28 #include <QTimer>
29
30 #include "message.h"
31 #include "network.h"
32 #include "signalproxy.h"
33
34 class CoreSession;
35 class Network;
36
37 class IrcServerHandler;
38 class UserInputHandler;
39 class CtcpHandler;
40
41 class NetworkConnection : public QObject {
42   Q_OBJECT
43
44 public:
45   NetworkConnection(Network *network, CoreSession *session, const QVariant &previousState = QVariant());
46   ~NetworkConnection();
47
48   NetworkId networkId() const;
49   QString networkName() const;
50   Network *network() const;
51   CoreSession *coreSession() const;
52
53   bool isConnected() const;
54   Network::ConnectionState connectionState() const;
55
56   IrcServerHandler *ircServerHandler() const;
57   UserInputHandler *userInputHandler() const;
58   CtcpHandler *ctcpHandler() const;
59
60   //! Return data necessary to restore the connection state upon core restart
61   QVariant state() const;
62
63   //! Decode a string using the server (network) decoding.
64   QString serverDecode(const QByteArray &string) const;
65
66   //! Decode a string using a buffer-specific encoding if one is set (and use the server encoding else).
67   QString bufferDecode(const QString &bufferName, const QByteArray &string) const;
68
69   //! Decode a string using a IrcUser specific encoding, if one exists (using the server encoding else).
70   QString userDecode(const QString &userNick, const QByteArray &string) const;
71
72   //! Encode a string using the server (network) encoding.
73   QByteArray serverEncode(const QString &string) const;
74
75   //! Encode a string using the buffer-specific encoding, if set, and use the server encoding else.
76   QByteArray bufferEncode(const QString &bufferName, const QString &string) const;
77
78   //! Encode a string using the user-specific encoding, if set, and use the server encoding else.
79   QByteArray userEncode(const QString &userNick, const QString &string) const;
80
81 public slots:
82   // void setServerOptions();
83   void connectToIrc();
84   void disconnectFromIrc();
85   void userInput(QString buffer, QString msg);
86
87   void putRawLine(QString input);
88   void putCmd(QString cmd, QStringList params, QString prefix = 0);
89
90
91 private slots:
92   void sendPerform();
93
94 signals:
95   // #void networkState(QString net, QVariantMap data);
96   void recvRawServerMsg(QString);
97   void displayStatusMsg(QString);
98   //void displayMsg(Message msg);
99   void displayMsg(Message::Type, QString target, QString text, QString sender = "", quint8 flags = Message::None);
100   void connected(NetworkId networkId);   ///< Emitted after receipt of 001 to indicate that we can now send data to the IRC server
101   void disconnected(NetworkId networkId);
102   void connectionStateChanged(Network::ConnectionState);
103   void connectionInitialized(); ///< Emitted after receipt of 001 to indicate that we can now send data to the IRC server
104   void connectionError(const QString &errorMsg);
105
106   //void queryRequested(QString network, QString nick);
107
108
109 private slots:
110   void socketHasData();
111   void socketError(QAbstractSocket::SocketError);
112   void socketConnected();
113   void socketDisconnected();
114   void socketStateChanged(QAbstractSocket::SocketState);
115   void setConnectionState(Network::ConnectionState);
116   void networkInitialized();
117
118 private:
119   QTcpSocket socket;
120   Network::ConnectionState _connectionState;
121
122   Network *_network;
123   CoreSession *_coreSession;
124
125   IrcServerHandler *_ircServerHandler;
126   UserInputHandler *_userInputHandler;
127   CtcpHandler *_ctcpHandler;
128
129   QVariantMap networkSettings;
130   QVariantMap identity;
131
132   QVariant _previousState;
133
134   class ParseError : public Exception {
135   public:
136     ParseError(QString cmd, QString prefix, QStringList params);
137   };
138
139   class UnknownCmdError : public Exception {
140   public:
141     UnknownCmdError(QString cmd, QString prefix, QStringList params);
142   };
143     
144 };
145
146 #endif