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