The new 'All Buffers' view is no properly presorted.
[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 bool setData(int column, const QVariant &value, int role);
144   virtual inline bool isActive() const { return (bool)_ircUser; }
145   virtual QString toolTip(int column) const;
146
147 public slots:
148   void attachIrcUser(IrcUser *ircUser);
149   void ircUserQuited();
150
151 private:
152   IrcUser *_ircUser;
153 };
154
155 /*****************************************
156 *  ChannelBufferItem
157 *****************************************/
158 class UserCategoryItem;
159
160 class ChannelBufferItem : public BufferItem {
161   Q_OBJECT
162
163 public:
164   ChannelBufferItem(const BufferInfo &bufferInfo, AbstractTreeItem *parent);
165
166   virtual QVariant data(int column, int role) const;
167   virtual inline bool isActive() const { return (bool)_ircChannel; }
168   virtual QString toolTip(int column) const;
169
170   virtual inline QString topic() const { return (bool)_ircChannel ? _ircChannel->topic() : QString(); }
171   virtual inline int nickCount() const { return (bool)_ircChannel ? _ircChannel->ircUsers().count() : 0; }
172
173   void attachIrcChannel(IrcChannel *ircChannel);
174
175 public slots:
176   void join(const QList<IrcUser *> &ircUsers);
177   void part(IrcUser *ircUser);
178
179   UserCategoryItem *findCategoryItem(int categoryId);
180   void addUserToCategory(IrcUser *ircUser);
181   void addUsersToCategory(const QList<IrcUser *> &ircUser);
182   void removeUserFromCategory(IrcUser *ircUser);
183   void userModeChanged(IrcUser *ircUser);
184
185 private slots:
186   void ircChannelParted();
187
188 private:
189   IrcChannel *_ircChannel;
190 };
191
192 /*****************************************
193 *  User Category Items (like @vh etc.)
194 *****************************************/
195 class IrcUserItem;
196 class UserCategoryItem : public PropertyMapItem {
197   Q_OBJECT
198   Q_PROPERTY(QString categoryName READ categoryName)
199
200 public:
201   UserCategoryItem(int category, AbstractTreeItem *parent);
202
203   QString categoryName() const;
204   inline int categoryId() const { return _category; }
205   virtual QVariant data(int column, int role) const;
206
207   IrcUserItem *findIrcUser(IrcUser *ircUser);
208   void addUsers(const QList<IrcUser *> &ircUser);
209   bool removeUser(IrcUser *ircUser);
210
211   static int categoryFromModes(const QString &modes);
212
213 private:
214   int _category;
215
216   static const QList<QChar> categories;
217 };
218
219 /*****************************************
220 *  Irc User Items
221 *****************************************/
222 class IrcUserItem : public PropertyMapItem {
223   Q_OBJECT
224   Q_PROPERTY(QString nickName READ nickName)
225
226 public:
227   IrcUserItem(IrcUser *ircUser, AbstractTreeItem *parent);
228
229   inline QString nickName() const { return _ircUser ? _ircUser->nick() : QString(); }
230   inline bool isActive() const { return _ircUser ? !_ircUser->isAway() : false; }
231
232   inline IrcUser *ircUser() { return _ircUser; }
233   virtual QVariant data(int column, int role) const;
234   virtual QString toolTip(int column) const;
235
236 private slots:
237   inline void ircUserQuited() { parent()->removeChild(this); }
238
239 private:
240   QPointer<IrcUser> _ircUser;
241 };
242
243
244 /*****************************************
245  * NetworkModel
246  *****************************************/
247 class NetworkModel : public TreeModel {
248   Q_OBJECT
249
250 public:
251   enum Role {
252     BufferTypeRole = TreeModel::UserRole,
253     ItemActiveRole,
254     BufferActivityRole,
255     BufferIdRole,
256     NetworkIdRole,
257     BufferInfoRole,
258     ItemTypeRole,
259     UserAwayRole,
260     IrcUserRole,
261     IrcChannelRole
262   };
263
264   enum ItemType {
265     NetworkItemType = 0x01,
266     BufferItemType = 0x02,
267     UserCategoryItemType = 0x04,
268     IrcUserItemType = 0x08
269   };
270   Q_DECLARE_FLAGS(ItemTypes, ItemType)
271
272   NetworkModel(QObject *parent = 0);
273   static QList<QVariant> defaultHeader();
274
275   static bool mimeContainsBufferList(const QMimeData *mimeData);
276   static QList< QPair<NetworkId, BufferId> > mimeDataToBufferList(const QMimeData *mimeData);
277
278   virtual QStringList mimeTypes() const;
279   virtual QMimeData *mimeData(const QModelIndexList &) const;
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   QList<BufferId> allBufferIdsSorted() const;
310   void sortBufferIds(QList<BufferId> &bufferIds) const;
311
312 public slots:
313   void bufferUpdated(BufferInfo bufferInfo);
314   void removeBuffer(BufferId bufferId);
315   void setLastSeenMsgId(const BufferId &bufferId, const MsgId &msgId);
316   void setBufferActivity(const BufferId &bufferId, BufferInfo::ActivityLevel activity);
317   void clearBufferActivity(const BufferId &bufferId);
318   void updateBufferActivity(const Message &msg);
319   void networkRemoved(const NetworkId &networkId);
320
321 signals:
322   void setLastSeenMsg(BufferId bufferId, MsgId msgId);
323
324 private slots:
325   void checkForRemovedBuffers(const QModelIndex &parent, int start, int end);
326   void checkForNewBuffers(const QModelIndex &parent, int start, int end);
327
328 private:
329   int networkRow(NetworkId networkId) const;
330   NetworkItem *findNetworkItem(NetworkId networkId) const;
331   NetworkItem *networkItem(NetworkId networkId);
332   inline BufferItem *findBufferItem(const BufferInfo &bufferInfo) const { return findBufferItem(bufferInfo.bufferId()); }
333   BufferItem *findBufferItem(BufferId bufferId) const;
334   BufferItem *bufferItem(const BufferInfo &bufferInfo);
335
336   static bool bufferItemLessThan(const BufferItem *left, const BufferItem *right);
337
338   QHash<BufferId, BufferItem *> _bufferItemCache;
339 };
340 Q_DECLARE_OPERATORS_FOR_FLAGS(NetworkModel::ItemTypes)
341
342 #endif // NETWORKMODEL_H