Finally! The new identities plus a nice shiny settingspage for editing them are done!
[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 <QThread>
29 #include <QTimer>
30
31 #include "message.h"
32 #include "signalproxy.h"
33
34 class Network;
35
36 class IrcServerHandler;
37 class UserInputHandler;
38 class CtcpHandler;
39 class CoreSession;
40
41 /*!
42  * This is a server object, managing a single connection to an IRC server, handling the associated channels and so on.
43  * We have this running in its own thread mainly to not block other server objects or the core if something goes wrong,
44  * e.g. if some scripts starts running wild...
45  */
46
47 class NetworkConnection : public QThread {
48   Q_OBJECT
49
50 public:
51   NetworkConnection(UserId uid, NetworkId networkId, QString network, const QVariant &previousState = QVariant());
52   ~NetworkConnection();
53
54   UserId userId() const { return _userId; } 
55
56   // networkState state();
57   bool isConnected() const { return socket.state() == QAbstractSocket::ConnectedState; }
58
59   NetworkId networkId() const;
60   QString networkName() const;  // hasbeen getNetwork()
61
62   Network *network() const { return _network; }
63   IrcServerHandler *ircServerHandler() const { return _ircServerHandler; }
64   UserInputHandler *userInputHandler() const { return _userInputHandler; }
65   CtcpHandler *ctcpHandler() const { return _ctcpHandler; }
66
67   QVariant state(); ///< Return data necessary to restore the server's state upon core restart
68
69   //! Decode a string using the server (network) decoding.
70   QString serverDecode(const QByteArray &string) const;
71
72   //! Decode a string using a buffer-specific encoding if one is set (and use the server encoding else).
73   QString bufferDecode(const QString &bufferName, const QByteArray &string) const;
74
75   //! Decode a string using a IrcUser specific encoding, if one exists (using the server encoding else).
76   QString userDecode(const QString &userNick, const QByteArray &string) const;
77
78   //! Encode a string using the server (network) encoding.
79   QByteArray serverEncode(const QString &string) const;
80
81   //! Encode a string using the buffer-specific encoding, if set, and use the server encoding else.
82   QByteArray bufferEncode(const QString &bufferName, const QString &string) const;
83
84   //! Encode a string using the user-specific encoding, if set, and use the server encoding else.
85   QByteArray userEncode(const QString &userNick, const QString &string) const;
86
87 public slots:
88   // void setServerOptions();
89   void connectToIrc(QString net);
90   void disconnectFromIrc(QString net);
91   void userInput(uint netid, QString buffer, QString msg);
92
93   void putRawLine(QString input);
94   void putCmd(QString cmd, QStringList params, QString prefix = 0);
95
96
97 private slots:
98   void threadFinished();
99   void sendPerform();
100
101 signals:
102   void networkState(QString net, QVariantMap data);
103   void recvRawServerMsg(QString);
104   void displayStatusMsg(QString);
105   //void displayMsg(Message msg);
106   void displayMsg(Message::Type, QString target, QString text, QString sender = "", quint8 flags = Message::None);
107   void connected(uint networkId);
108   void disconnected(uint networkId);
109
110   void connectionInitialized(); ///< Emitted after receipt of 001 to indicate that we can now send data to the IRC server
111
112   void synchronizeClients();
113   
114   void queryRequested(QString network, QString nick);
115
116
117 private slots:
118   void run();
119   void socketHasData();
120   void socketError(QAbstractSocket::SocketError);
121   void socketConnected();
122   void socketStateChanged(QAbstractSocket::SocketState);
123
124 private:
125   UserId _userId;
126   NetworkId _networkId;
127
128   QTcpSocket socket;
129
130   IrcServerHandler *_ircServerHandler;
131   UserInputHandler *_userInputHandler;
132   CtcpHandler *_ctcpHandler;
133
134   Network *_network;
135
136   QVariantMap networkSettings;
137   QVariantMap identity;
138
139   QVariant _previousState;
140
141   CoreSession *coreSession() const;
142   
143   class ParseError : public Exception {
144   public:
145     ParseError(QString cmd, QString prefix, QStringList params);
146   };
147
148   class UnknownCmdError : public Exception {
149   public:
150     UnknownCmdError(QString cmd, QString prefix, QStringList params);
151   };
152     
153 };
154
155 #endif