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