255d3ec42e86146356e7f58d13ff72db962f13b7
[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
48   Q_PROPERTY(QString networkName READ networkName WRITE setNetworkName STORED false)
49   Q_PROPERTY(QString currentServer READ currentServer WRITE setCurrentServer STORED false)
50   Q_PROPERTY(QString myNick READ myNick WRITE setMyNick STORED false)
51   Q_PROPERTY(QByteArray codecForEncoding READ codecForEncoding WRITE setCodecForEncoding STORED false)
52   Q_PROPERTY(QByteArray codecForDecoding READ codecForDecoding WRITE setCodecForDecoding STORED false)
53   Q_PROPERTY(IdentityId identityId READ identity WRITE setIdentity STORED false)
54   Q_PROPERTY(bool isConnected READ isConnected WRITE setConnected STORED false)
55
56 public:
57   Network(const NetworkId &networkid, QObject *parent = 0);
58   // ~Network();
59
60   NetworkId networkId() const;
61
62   SignalProxy *proxy() const;
63   void setProxy(SignalProxy *proxy);
64
65   bool isMyNick(const QString &nick) const;
66   bool isMe(IrcUser *ircuser) const;
67
68   bool isChannelName(const QString &channelname) const;
69
70   bool isConnected() const;
71
72   QString prefixToMode(const QString &prefix);
73   QString prefixToMode(const QCharRef &prefix);
74   QString modeToPrefix(const QString &mode);
75   QString modeToPrefix(const QCharRef &mode);
76
77   QString networkName() const;
78   QString currentServer() const;
79   QString myNick() const;
80   IdentityId identity() const;
81   QStringList nicks() const;
82   QStringList channels() const;
83   QList<QVariantMap> serverList() const;
84
85   NetworkInfo networkInfo() const;
86   void setNetworkInfo(const NetworkInfo &);
87
88   QString prefixes();
89   QString prefixModes();
90
91   bool supports(const QString &param) const;
92   QString support(const QString &param) const;
93
94   IrcUser *newIrcUser(const QString &hostmask);
95   IrcUser *newIrcUser(const QByteArray &hostmask);
96   IrcUser *ircUser(QString nickname) const;
97   IrcUser *ircUser(const QByteArray &nickname) const;
98   QList<IrcUser *> ircUsers() const;
99   quint32 ircUserCount() const;
100
101   IrcChannel *newIrcChannel(const QString &channelname);
102   IrcChannel *newIrcChannel(const QByteArray &channelname);
103   IrcChannel *ircChannel(QString channelname) const;
104   IrcChannel *ircChannel(const QByteArray &channelname) const;
105   QList<IrcChannel *> ircChannels() const;
106   quint32 ircChannelCount() const;
107
108   QByteArray codecForEncoding() const;
109   QByteArray codecForDecoding() const;
110   void setCodecForEncoding(QTextCodec *codec);
111   void setCodecForDecoding(QTextCodec *codec);
112
113   QString decodeString(const QByteArray &text) const;
114   QByteArray encodeString(const QString string) const;
115
116 public slots:
117   void setNetworkName(const QString &networkName);
118   void setCurrentServer(const QString &currentServer);
119   void setConnected(bool isConnected);
120   void setMyNick(const QString &mynick);
121   void setIdentity(IdentityId);
122
123   void setServerList(const QList<QVariantMap> &serverList);
124
125   void setCodecForEncoding(const QByteArray &codecName);
126   void setCodecForDecoding(const QByteArray &codecName);
127
128   void addSupport(const QString &param, const QString &value = QString());
129   void removeSupport(const QString &param);
130
131   inline void addIrcUser(const QString &hostmask) { newIrcUser(hostmask); }
132   void removeIrcUser(QString nick);
133   
134   //init geters
135   QVariantMap initSupports() const;
136   QVariantList initServerList() const;
137   QStringList initIrcUsers() const;
138   QStringList initIrcChannels() const;
139   
140   //init seters
141   void initSetSupports(const QVariantMap &supports);
142   void initSetServerList(const QVariantList &serverList);
143   void initSetIrcUsers(const QStringList &hostmasks);
144   void initSetChannels(const QStringList &channels);
145   
146   IrcUser *updateNickFromMask(const QString &mask);
147
148   // these slots are to keep the hashlists of all users and the
149   // channel lists up to date
150   void ircUserNickChanged(QString newnick);
151
152   void requestConnect();
153
154 private slots:
155   void ircUserDestroyed();
156   void channelDestroyed();
157   void removeIrcUser(IrcUser *ircuser);
158   void ircUserInitDone();
159   void ircChannelInitDone();
160
161 signals:
162   void networkNameSet(const QString &networkName);
163   void currentServerSet(const QString &currentServer);
164   void connectedSet(bool isConnected);
165   void myNickSet(const QString &mynick);
166   void identitySet(IdentityId);
167
168   void serverListSet(const QList<QVariantMap> &serverList);
169
170   void codecForEncodingSet(const QString &codecName);
171   void codecForDecodingSet(const QString &codecName);
172
173   void supportAdded(const QString &param, const QString &value);
174   void supportRemoved(const QString &param);
175
176   void ircUserAdded(const QString &hostmask);
177   void ircUserAdded(IrcUser *);
178   void ircChannelAdded(const QString &channelname);
179   void ircChannelAdded(IrcChannel *);
180
181   void ircUserRemoved(const QString &nick);
182
183   // needed for client sync progress
184   void ircUserRemoved(QObject *);
185   void ircChannelRemoved(QObject *);
186
187   void ircUserInitDone(IrcUser *);
188   void ircChannelInitDone(IrcChannel *);
189
190   void connectRequested(NetworkId = 0);
191
192 private:
193   NetworkId _networkId;
194   IdentityId _identity;
195
196   QString _myNick;
197   QString _networkName;
198   QString _currentServer;
199   bool _connected;
200
201   QString _prefixes;
202   QString _prefixModes;
203
204   QHash<QString, IrcUser *> _ircUsers;  // stores all known nicks for the server
205   QHash<QString, IrcChannel *> _ircChannels; // stores all known channels
206   QHash<QString, QString> _supports;  // stores results from RPL_ISUPPORT
207
208   QList<QVariantMap> _serverList;
209   //QVariantMap networkSettings;
210
211   QPointer<SignalProxy> _proxy;
212   void determinePrefixes();
213
214   QTextCodec *_codecForEncoding;
215   QTextCodec *_codecForDecoding;
216
217 };
218
219 //! Stores all editable information about a network (as opposed to runtime state).
220 struct NetworkInfo {
221   NetworkId networkId;
222   IdentityId identity;
223   QString networkName;
224   QByteArray codecForEncoding;
225   QByteArray codecForDecoding;
226   QList<QVariantMap> serverList;
227
228 };
229
230
231 #endif