adding tons of ifdefs so quassel will build again without ssl support
[quassel.git] / src / client / networkmodel.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 NETWORKMODEL_H
22 #define NETWORKMODEL_H
23
24 #include "bufferinfo.h"
25 #include "clientsettings.h"
26 #include "message.h"
27 #include "network.h"
28 #include "treemodel.h"
29
30 class BufferItem;
31
32 /*****************************************
33  *  Network Items
34  *****************************************/
35 class NetworkItem : public PropertyMapItem {
36   Q_OBJECT
37   Q_PROPERTY(QString networkName READ networkName)
38   Q_PROPERTY(QString currentServer READ currentServer)
39   Q_PROPERTY(int nickCount READ nickCount)
40
41 public:
42   NetworkItem(const NetworkId &netid, AbstractTreeItem *parent = 0);
43
44   virtual QVariant data(int column, int row) const;
45
46   inline bool isActive() const { return (bool)_network ? _network->isConnected() : false; }
47
48   inline const NetworkId &networkId() const { return _networkId; }
49   inline QString networkName() const { return (bool)_network ? _network->networkName() : QString(); }
50   inline QString currentServer() const { return (bool)_network ? _network->currentServer() : QString(); }
51   inline int nickCount() const { return (bool)_network ? _network->ircUsers().count() : 0; }
52
53   virtual QString toolTip(int column) const;
54
55   BufferItem *findBufferItem(BufferId bufferId);
56   inline BufferItem *findBufferItem(const BufferInfo &bufferInfo) { return findBufferItem(bufferInfo.bufferId()); }
57   BufferItem *bufferItem(const BufferInfo &bufferInfo);
58
59 public slots:
60   void setNetworkName(const QString &networkName);
61   void setCurrentServer(const QString &serverName);
62
63   void attachNetwork(Network *network);
64   void attachIrcChannel(IrcChannel *channel);
65   void attachIrcUser(IrcUser *ircUser);
66
67 private:
68   NetworkId _networkId;
69
70   QPointer<Network> _network;
71 };
72
73 /*****************************************
74  *  Fancy Buffer Items
75  *****************************************/
76 class BufferItem : public PropertyMapItem {
77   Q_OBJECT
78   Q_PROPERTY(QString bufferName READ bufferName WRITE setBufferName)
79   Q_PROPERTY(QString topic READ topic)
80   Q_PROPERTY(int nickCount READ nickCount)
81
82 public:
83   BufferItem(const BufferInfo &bufferInfo, AbstractTreeItem *parent = 0);
84
85   inline const BufferInfo &bufferInfo() const { return _bufferInfo; }
86   virtual QVariant data(int column, int role) const;
87   virtual bool setData(int column, const QVariant &value, int role);
88
89   inline BufferId bufferId() const { return _bufferInfo.bufferId(); }
90   inline BufferInfo::Type bufferType() const { return _bufferInfo.type(); }
91
92   void setBufferName(const QString &name);
93   virtual inline QString bufferName() const { return _bufferInfo.bufferName(); }
94   virtual inline QString topic() const { return QString(); }
95   virtual inline int nickCount() const { return 0; }
96
97   virtual inline bool isActive() const { return qobject_cast<NetworkItem *>(parent())->isActive(); }
98
99   inline const MsgId &lastSeenMsgId() const { return _lastSeenMsgId; }
100   inline const MsgId &lastSeenMarkerMsgId() const { return _lastSeenMarkerMsgId; }
101   void setLastSeenMsgId(const MsgId &msgId);
102   inline BufferInfo::ActivityLevel activityLevel() const { return _activity; }
103   void setActivityLevel(BufferInfo::ActivityLevel level);
104   void clearActivityLevel();
105   void updateActivityLevel(const Message &msg);
106
107   bool isCurrentBuffer() const;
108   virtual QString toolTip(int column) const;
109
110 public slots:
111   virtual inline void setTopic(const QString &) { emit dataChanged(1); }
112
113 private:
114   BufferInfo _bufferInfo;
115   BufferInfo::ActivityLevel _activity;
116   MsgId _lastSeenMsgId;
117   MsgId _lastSeenMarkerMsgId;
118 };
119
120 /*****************************************
121 *  StatusBufferItem
122 *****************************************/
123 class StatusBufferItem : public BufferItem {
124   Q_OBJECT
125
126 public:
127   StatusBufferItem(const BufferInfo &bufferInfo, NetworkItem *parent);
128
129   virtual QString toolTip(int column) const;
130   virtual inline QString bufferName() const { return tr("Status Buffer"); }
131 };
132
133 /*****************************************
134 *  QueryBufferItem
135 *****************************************/
136 class QueryBufferItem : public BufferItem {
137   Q_OBJECT
138
139 public:
140   QueryBufferItem(const BufferInfo &bufferInfo, NetworkItem *parent);
141
142   virtual QVariant data(int column, int role) const;
143   virtual inline bool isActive() const { return (bool)_ircUser; }
144   virtual QString toolTip(int column) const;
145
146 public slots:
147   void attachIrcUser(IrcUser *ircUser);
148   void ircUserQuited();
149
150 private:
151   IrcUser *_ircUser;
152 };
153
154 /*****************************************
155 *  ChannelBufferItem
156 *****************************************/
157 class UserCategoryItem;
158
159 class ChannelBufferItem : public BufferItem {
160   Q_OBJECT
161
162 public:
163   ChannelBufferItem(const BufferInfo &bufferInfo, AbstractTreeItem *parent);
164
165   virtual QVariant data(int column, int role) const;
166   virtual inline bool isActive() const { return (bool)_ircChannel; }
167   virtual QString toolTip(int column) const;
168
169   virtual inline QString topic() const { return (bool)_ircChannel ? _ircChannel->topic() : QString(); }
170   virtual inline int nickCount() const { return (bool)_ircChannel ? _ircChannel->ircUsers().count() : 0; }
171
172   void attachIrcChannel(IrcChannel *ircChannel);
173
174 public slots:
175   void join(const QList<IrcUser *> &ircUsers);
176   void part(IrcUser *ircUser);
177
178   UserCategoryItem *findCategoryItem(int categoryId);
179   void addUserToCategory(IrcUser *ircUser);
180   void addUsersToCategory(const QList<IrcUser *> &ircUser);
181   void removeUserFromCategory(IrcUser *ircUser);
182   void userModeChanged(IrcUser *ircUser);
183
184 private slots:
185   void ircChannelParted();
186
187 private:
188   IrcChannel *_ircChannel;
189 };
190
191 /*****************************************
192 *  User Category Items (like @vh etc.)
193 *****************************************/
194 class IrcUserItem;
195 class UserCategoryItem : public PropertyMapItem {
196   Q_OBJECT
197   Q_PROPERTY(QString categoryName READ categoryName)
198
199 public:
200   UserCategoryItem(int category, AbstractTreeItem *parent);
201
202   QString categoryName() const;
203   inline int categoryId() const { return _category; }
204   virtual QVariant data(int column, int role) const;
205
206   IrcUserItem *findIrcUser(IrcUser *ircUser);
207   void addUsers(const QList<IrcUser *> &ircUser);
208   bool removeUser(IrcUser *ircUser);
209
210   static int categoryFromModes(const QString &modes);
211
212 private:
213   int _category;
214
215   static const QList<QChar> categories;
216 };
217
218 /*****************************************
219 *  Irc User Items
220 *****************************************/
221 class IrcUserItem : public PropertyMapItem {
222   Q_OBJECT
223   Q_PROPERTY(QString nickName READ nickName)
224
225 public:
226   IrcUserItem(IrcUser *ircUser, AbstractTreeItem *parent);
227
228   inline QString nickName() const { return _ircUser ? _ircUser->nick() : QString(); }
229   inline bool isActive() const { return _ircUser ? !_ircUser->isAway() : false; }
230
231   inline IrcUser *ircUser() { return _ircUser; }
232   virtual QVariant data(int column, int role) const;
233   virtual QString toolTip(int column) const;
234
235 private slots:
236   inline void ircUserQuited() { parent()->removeChild(this); }
237
238 private:
239   QPointer<IrcUser> _ircUser;
240 };
241
242
243 /*****************************************
244  * NetworkModel
245  *****************************************/
246 class NetworkModel : public TreeModel {
247   Q_OBJECT
248
249 public:
250   enum Role {
251     BufferTypeRole = TreeModel::UserRole,
252     ItemActiveRole,
253     BufferActivityRole,
254     BufferIdRole,
255     NetworkIdRole,
256     BufferInfoRole,
257     ItemTypeRole,
258     UserAwayRole,
259     IrcUserRole,
260     IrcChannelRole
261   };
262
263   enum ItemType {
264     NetworkItemType = 0x01,
265     BufferItemType = 0x02,
266     UserCategoryItemType = 0x04,
267     IrcUserItemType = 0x08
268   };
269   Q_DECLARE_FLAGS(ItemTypes, ItemType)
270
271   NetworkModel(QObject *parent = 0);
272   static QList<QVariant> defaultHeader();
273
274   static bool mimeContainsBufferList(const QMimeData *mimeData);
275   static QList< QPair<NetworkId, BufferId> > mimeDataToBufferList(const QMimeData *mimeData);
276
277   virtual QStringList mimeTypes() const;
278   virtual QMimeData *mimeData(const QModelIndexList &) const;
279   virtual bool dropMimeData(const QMimeData *, Qt::DropAction, int, int, const QModelIndex &);
280
281   void attachNetwork(Network *network);
282
283   bool isBufferIndex(const QModelIndex &) const;
284   //Buffer *getBufferByIndex(const QModelIndex &) const;
285   QModelIndex networkIndex(NetworkId networkId);
286   QModelIndex bufferIndex(BufferId bufferId);
287
288   const Network *networkByIndex(const QModelIndex &index) const;
289
290   BufferInfo::ActivityLevel bufferActivity(const BufferInfo &buffer) const;
291
292   //! Finds a buffer with a given name in a given network
293   /** This performs a linear search through all BufferItems, hence it is expensive.
294    *  @param networkId  The network which we search in
295    *  @param bufferName The bufferName we look for
296    *  @return The id of the buffer if found, an invalid one else
297    */
298   BufferId bufferId(NetworkId networkId, const QString &bufferName, Qt::CaseSensitivity cs = Qt::CaseInsensitive) const;
299
300   QString bufferName(BufferId bufferId) const;
301   BufferInfo::Type bufferType(BufferId bufferId) const;
302   BufferInfo bufferInfo(BufferId bufferId) const;
303   MsgId lastSeenMsgId(BufferId bufferId) const;
304   MsgId lastSeenMarkerMsgId(BufferId bufferId) const;
305   NetworkId networkId(BufferId bufferId) const;
306   QString networkName(BufferId bufferId) const;
307
308   inline QList<BufferId> allBufferIds() const { return _bufferItemCache.keys(); }
309
310 public slots:
311   void bufferUpdated(BufferInfo bufferInfo);
312   void removeBuffer(BufferId bufferId);
313   void setLastSeenMsgId(const BufferId &bufferId, const MsgId &msgId);
314   void setBufferActivity(const BufferId &bufferId, BufferInfo::ActivityLevel activity);
315   void clearBufferActivity(const BufferId &bufferId);
316   void updateBufferActivity(const Message &msg);
317   void networkRemoved(const NetworkId &networkId);
318
319 signals:
320   void setLastSeenMsg(BufferId bufferId, MsgId msgId);
321
322 private slots:
323   void checkForRemovedBuffers(const QModelIndex &parent, int start, int end);
324   void checkForNewBuffers(const QModelIndex &parent, int start, int end);
325
326 private:
327   int networkRow(NetworkId networkId) const;
328   NetworkItem *findNetworkItem(NetworkId networkId) const;
329   NetworkItem *networkItem(NetworkId networkId);
330   inline BufferItem *findBufferItem(const BufferInfo &bufferInfo) const { return findBufferItem(bufferInfo.bufferId()); }
331   BufferItem *findBufferItem(BufferId bufferId) const;
332   BufferItem *bufferItem(const BufferInfo &bufferInfo);
333
334   QHash<BufferId, BufferItem *> _bufferItemCache;
335 };
336 Q_DECLARE_OPERATORS_FOR_FLAGS(NetworkModel::ItemTypes)
337
338 #endif // NETWORKMODEL_H