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