4c04ceb064d813c28b9d477cafead530072c51e9
[quassel.git] / src / common / network.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 _NETWORK_H_
22 #define _NETWORK_H_
23
24 #include <QDebug>
25 #include <QString>
26 #include <QStringList>
27 #include <QList>
28 #include <QHash>
29 #include <QVariantMap>
30 #include <QPointer>
31 #include <QMutex>
32
33 #include "types.h"
34 #include "syncableobject.h"
35
36 class SignalProxy;
37 class IrcUser;
38 class IrcChannel;
39
40 // defined below!
41 struct NetworkInfo;
42
43 // TODO: ConnectionInfo to propagate and sync the current state of NetworkConnection, encodings etcpp
44
45 class Network : public SyncableObject {
46   Q_OBJECT
47   Q_ENUMS(ConnectionState Network::ConnectionState)
48
49   Q_PROPERTY(QString networkName READ networkName WRITE setNetworkName STORED false)
50   Q_PROPERTY(QString currentServer READ currentServer WRITE setCurrentServer STORED false)
51   Q_PROPERTY(QString myNick READ myNick WRITE setMyNick STORED false)
52   Q_PROPERTY(QByteArray codecForEncoding READ codecForEncoding WRITE setCodecForEncoding STORED false)
53   Q_PROPERTY(QByteArray codecForDecoding READ codecForDecoding WRITE setCodecForDecoding STORED false)
54   Q_PROPERTY(IdentityId identityId READ identity WRITE setIdentity STORED false)
55   Q_PROPERTY(bool isConnected READ isConnected WRITE setConnected STORED false)
56   Q_PROPERTY(Network::ConnectionState connectionState READ connectionState WRITE setConnectionState STORED false)
57
58 public:
59   enum ConnectionState { Disconnected, Connecting, Initializing, Initialized, Disconnecting };
60
61   Network(const NetworkId &networkid, QObject *parent = 0);
62   ~Network();
63
64   NetworkId networkId() const;
65
66   SignalProxy *proxy() const;
67   void setProxy(SignalProxy *proxy);
68
69   bool isMyNick(const QString &nick) const;
70   bool isMe(IrcUser *ircuser) const;
71
72   bool isChannelName(const QString &channelname) const;
73
74   bool isConnected() const;
75   Network::ConnectionState connectionState() const;
76
77   QString prefixToMode(const QString &prefix);
78   QString prefixToMode(const QCharRef &prefix);
79   QString modeToPrefix(const QString &mode);
80   QString modeToPrefix(const QCharRef &mode);
81
82   QString networkName() const;
83   QString currentServer() const;
84   QString myNick() const;
85   IdentityId identity() const;
86   QStringList nicks() const;
87   QStringList channels() const;
88   QVariantList serverList() const;
89
90   NetworkInfo networkInfo() const;
91   void setNetworkInfo(const NetworkInfo &);
92
93   QString prefixes();
94   QString prefixModes();
95
96   bool supports(const QString &param) const;
97   QString support(const QString &param) const;
98
99   IrcUser *newIrcUser(const QString &hostmask);
100   IrcUser *newIrcUser(const QByteArray &hostmask);
101   IrcUser *ircUser(QString nickname) const;
102   IrcUser *ircUser(const QByteArray &nickname) const;
103   QList<IrcUser *> ircUsers() const;
104   quint32 ircUserCount() const;
105
106   IrcChannel *newIrcChannel(const QString &channelname);
107   IrcChannel *newIrcChannel(const QByteArray &channelname);
108   IrcChannel *ircChannel(QString channelname) const;
109   IrcChannel *ircChannel(const QByteArray &channelname) const;
110   QList<IrcChannel *> ircChannels() const;
111   quint32 ircChannelCount() const;
112
113   QByteArray codecForEncoding() const;
114   QByteArray codecForDecoding() const;
115   void setCodecForEncoding(QTextCodec *codec);
116   void setCodecForDecoding(QTextCodec *codec);
117
118   QString decodeString(const QByteArray &text) const;
119   QByteArray encodeString(const QString string) const;
120
121 public slots:
122   void setNetworkName(const QString &networkName);
123   void setCurrentServer(const QString &currentServer);
124   void setConnected(bool isConnected);
125   void setConnectionState(Network::ConnectionState state);
126   void setMyNick(const QString &mynick);
127   void setIdentity(IdentityId);
128
129   void setServerList(const QVariantList &serverList);
130
131   void setCodecForEncoding(const QByteArray &codecName);
132   void setCodecForDecoding(const QByteArray &codecName);
133
134   void addSupport(const QString &param, const QString &value = QString());
135   void removeSupport(const QString &param);
136
137   inline void addIrcUser(const QString &hostmask) { newIrcUser(hostmask); }
138   void removeIrcUser(QString nick);
139   
140   //init geters
141   QVariantMap initSupports() const;
142   QVariantList initServerList() const;
143   QStringList initIrcUsers() const;
144   QStringList initIrcChannels() const;
145   
146   //init seters
147   void initSetSupports(const QVariantMap &supports);
148   void initSetServerList(const QVariantList &serverList);
149   void initSetIrcUsers(const QStringList &hostmasks);
150   void initSetChannels(const QStringList &channels);
151   
152   IrcUser *updateNickFromMask(const QString &mask);
153
154   // these slots are to keep the hashlists of all users and the
155   // channel lists up to date
156   void ircUserNickChanged(QString newnick);
157
158   void requestConnect() const;
159   void requestDisconnect() const;
160
161   void emitConnectionError(const QString &);
162
163 private slots:
164   void ircUserDestroyed();
165   void channelDestroyed();
166   void removeIrcUser(IrcUser *ircuser);
167   void ircUserInitDone();
168   void ircChannelInitDone();
169
170 signals:
171   void networkNameSet(const QString &networkName);
172   void currentServerSet(const QString &currentServer);
173   void connectedSet(bool isConnected);
174   void connectionStateSet(Network::ConnectionState);
175   void connectionError(const QString &errorMsg);
176   void myNickSet(const QString &mynick);
177   void identitySet(IdentityId);
178
179   void serverListSet(QVariantList serverList);
180
181   void codecForEncodingSet(const QString &codecName);
182   void codecForDecodingSet(const QString &codecName);
183
184   void supportAdded(const QString &param, const QString &value);
185   void supportRemoved(const QString &param);
186
187   void ircUserAdded(const QString &hostmask);
188   void ircUserAdded(IrcUser *);
189   void ircChannelAdded(const QString &channelname);
190   void ircChannelAdded(IrcChannel *);
191
192   void ircUserRemoved(const QString &nick);
193
194   // needed for client sync progress
195   void ircUserRemoved(QObject *);
196   void ircChannelRemoved(QObject *);
197
198   void ircUserInitDone(IrcUser *);
199   void ircChannelInitDone(IrcChannel *);
200
201   void connectRequested(NetworkId id = 0) const;
202   void disconnectRequested(NetworkId id = 0) const;
203
204 private:
205   NetworkId _networkId;
206   IdentityId _identity;
207
208   QString _myNick;
209   QString _networkName;
210   QString _currentServer;
211   bool _connected;
212   ConnectionState _connectionState;
213
214   QString _prefixes;
215   QString _prefixModes;
216
217   QHash<QString, IrcUser *> _ircUsers;  // stores all known nicks for the server
218   QHash<QString, IrcChannel *> _ircChannels; // stores all known channels
219   QHash<QString, QString> _supports;  // stores results from RPL_ISUPPORT
220
221   QVariantList _serverList;
222   QStringList _perform;
223   //QVariantMap networkSettings;
224
225   QPointer<SignalProxy> _proxy;
226   void determinePrefixes();
227
228   QTextCodec *_codecForEncoding;
229   QTextCodec *_codecForDecoding;
230
231 };
232
233 //! Stores all editable information about a network (as opposed to runtime state).
234 struct NetworkInfo {
235   NetworkId networkId;
236   QString networkName;
237   IdentityId identity;
238   QByteArray codecForEncoding;
239   QByteArray codecForDecoding;
240   QVariantList serverList;
241
242   bool operator==(const NetworkInfo &other) const;
243   bool operator!=(const NetworkInfo &other) const;
244 };
245
246 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info);
247 QDataStream &operator>>(QDataStream &in, NetworkInfo &info);
248
249 Q_DECLARE_METATYPE(NetworkInfo);
250
251 #endif