Preparing for the core session state being stored in the database.
[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);
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   //! Decode a string using the server (network) decoding.
63   QString serverDecode(const QByteArray &string) const;
64
65   //! Decode a string using a channel-specific encoding if one is set (and use the standard encoding else).
66   QString channelDecode(const QString &channelName, const QByteArray &string) const;
67
68   //! Decode a string using an IrcUser-specific encoding, if one exists (using the standaed encoding else).
69   QString userDecode(const QString &userNick, const QByteArray &string) const;
70
71   //! Encode a string using the server (network) encoding.
72   QByteArray serverEncode(const QString &string) const;
73
74   //! Encode a string using the channel-specific encoding, if set, and use the standard encoding else.
75   QByteArray channelEncode(const QString &channelName, const QString &string) const;
76
77   //! Encode a string using the user-specific encoding, if set, and use the standard encoding else.
78   QByteArray userEncode(const QString &userNick, const QString &string) const;
79
80   inline QString channelKey(const QString &channel) const { return _channelKeys.value(channel.toLower(), QString()); }
81   inline QStringList persistentChannels() const { return _channelKeys.keys(); }
82
83 public slots:
84   // void setServerOptions();
85   void connectToIrc(bool reconnecting = false);
86   void disconnectFromIrc(bool requested = true);
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   void setChannelJoined(const QString &channel);
93   void setChannelParted(const QString &channel);
94   void addChannelKey(const QString &channel, const QString &key);
95   void removeChannelKey(const QString &channel);
96
97 private slots:
98   void sendPerform();
99   void autoReconnectSettingsChanged();
100   void doAutoReconnect();
101   void sendWho();
102   void nickChanged(const QString &newNick, const QString &oldNick); // this signal is inteded to rename query buffers in the storage backend
103
104 signals:
105   // #void networkState(QString net, QVariantMap data);
106   void recvRawServerMsg(QString);
107   void displayStatusMsg(QString);
108   //void displayMsg(Message msg);
109   void displayMsg(Message::Type, BufferInfo::Type, QString target, QString text, QString sender = "", quint8 flags = Message::None);
110   void connected(NetworkId networkId);   ///< Emitted after receipt of 001 to indicate that we can now send data to the IRC server
111   void disconnected(NetworkId networkId);
112   void connectionStateChanged(Network::ConnectionState);
113   void connectionInitialized(); ///< Emitted after receipt of 001 to indicate that we can now send data to the IRC server
114   void connectionError(const QString &errorMsg);
115
116   void quitRequested(NetworkId networkId);
117
118   //void queryRequested(QString network, QString nick);
119   void nickChanged(const NetworkId &networkId, const QString &newNick, const QString &oldNick); // this signal is inteded to rename query buffers in the storage backend
120   void channelJoined(NetworkId, const QString &channel, const QString &key = QString());
121   void channelParted(NetworkId, const QString &channel);
122
123 private slots:
124   void socketHasData();
125   void socketError(QAbstractSocket::SocketError);
126   void socketConnected();
127   void socketDisconnected();
128   void socketStateChanged(QAbstractSocket::SocketState);
129   void setConnectionState(Network::ConnectionState);
130   void networkInitialized(const QString &currentServer);
131
132 private:
133   QTcpSocket socket;
134   Network::ConnectionState _connectionState;
135
136   Network *_network;
137   CoreSession *_coreSession;
138   BufferInfo _statusBufferInfo;
139
140   IrcServerHandler *_ircServerHandler;
141   UserInputHandler *_userInputHandler;
142   CtcpHandler *_ctcpHandler;
143
144   QHash<QString, QString> _channelKeys;  // stores persistent channels and their passwords, if any
145
146   QTimer _autoReconnectTimer;
147   int _autoReconnectCount;
148
149   QTimer _whoTimer;
150
151   class ParseError : public Exception {
152   public:
153     ParseError(QString cmd, QString prefix, QStringList params);
154   };
155
156   class UnknownCmdError : public Exception {
157   public:
158     UnknownCmdError(QString cmd, QString prefix, QStringList params);
159   };
160 };
161
162 #endif