when renaming a buffer to a matching known irc user, the on/offline state is now...
[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   virtual 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
145   virtual inline bool isActive() const { return (bool)_ircUser; }
146   virtual QString toolTip(int column) const;
147
148   virtual void setBufferName(const QString &name);
149
150 public slots:
151   void setIrcUser(IrcUser *ircUser);
152   void removeIrcUser();
153
154 private:
155   IrcUser *_ircUser;
156 };
157
158 /*****************************************
159 *  ChannelBufferItem
160 *****************************************/
161 class UserCategoryItem;
162
163 class ChannelBufferItem : public BufferItem {
164   Q_OBJECT
165
166 public:
167   ChannelBufferItem(const BufferInfo &bufferInfo, AbstractTreeItem *parent);
168
169   virtual QVariant data(int column, int role) const;
170   virtual inline bool isActive() const { return (bool)_ircChannel; }
171   virtual QString toolTip(int column) const;
172
173   virtual inline QString topic() const { return (bool)_ircChannel ? _ircChannel->topic() : QString(); }
174   virtual inline int nickCount() const { return (bool)_ircChannel ? _ircChannel->ircUsers().count() : 0; }
175
176   void attachIrcChannel(IrcChannel *ircChannel);
177
178 public slots:
179   void join(const QList<IrcUser *> &ircUsers);
180   void part(IrcUser *ircUser);
181
182   UserCategoryItem *findCategoryItem(int categoryId);
183   void addUserToCategory(IrcUser *ircUser);
184   void addUsersToCategory(const QList<IrcUser *> &ircUser);
185   void removeUserFromCategory(IrcUser *ircUser);
186   void userModeChanged(IrcUser *ircUser);
187
188 private slots:
189   void ircChannelParted();
190
191 private:
192   IrcChannel *_ircChannel;
193 };
194
195 /*****************************************
196 *  User Category Items (like @vh etc.)
197 *****************************************/
198 class IrcUserItem;
199 class UserCategoryItem : public PropertyMapItem {
200   Q_OBJECT
201   Q_PROPERTY(QString categoryName READ categoryName)
202
203 public:
204   UserCategoryItem(int category, AbstractTreeItem *parent);
205
206   QString categoryName() const;
207   inline int categoryId() const { return _category; }
208   virtual QVariant data(int column, int role) const;
209
210   IrcUserItem *findIrcUser(IrcUser *ircUser);
211   void addUsers(const QList<IrcUser *> &ircUser);
212   bool removeUser(IrcUser *ircUser);
213
214   static int categoryFromModes(const QString &modes);
215
216 private:
217   int _category;
218
219   static const QList<QChar> categories;
220 };
221
222 /*****************************************
223 *  Irc User Items
224 *****************************************/
225 class IrcUserItem : public PropertyMapItem {
226   Q_OBJECT
227   Q_PROPERTY(QString nickName READ nickName)
228
229 public:
230   IrcUserItem(IrcUser *ircUser, AbstractTreeItem *parent);
231
232   inline QString nickName() const { return _ircUser ? _ircUser->nick() : QString(); }
233   inline bool isActive() const { return _ircUser ? !_ircUser->isAway() : false; }
234
235   inline IrcUser *ircUser() { return _ircUser; }
236   virtual QVariant data(int column, int role) const;
237   virtual QString toolTip(int column) const;
238
239 private slots:
240   inline void ircUserQuited() { parent()->removeChild(this); }
241
242 private:
243   QPointer<IrcUser> _ircUser;
244 };
245
246
247 /*****************************************
248  * NetworkModel
249  *****************************************/
250 class NetworkModel : public TreeModel {
251   Q_OBJECT
252
253 public:
254   enum Role {
255     BufferTypeRole = TreeModel::UserRole,
256     ItemActiveRole,
257     BufferActivityRole,
258     BufferIdRole,
259     NetworkIdRole,
260     BufferInfoRole,
261     ItemTypeRole,
262     UserAwayRole,
263     IrcUserRole,
264     IrcChannelRole
265   };
266
267   enum ItemType {
268     NetworkItemType = 0x01,
269     BufferItemType = 0x02,
270     UserCategoryItemType = 0x04,
271     IrcUserItemType = 0x08
272   };
273   Q_DECLARE_FLAGS(ItemTypes, ItemType)
274
275   NetworkModel(QObject *parent = 0);
276   static QList<QVariant> defaultHeader();
277
278   static bool mimeContainsBufferList(const QMimeData *mimeData);
279   static QList< QPair<NetworkId, BufferId> > mimeDataToBufferList(const QMimeData *mimeData);
280
281   virtual QStringList mimeTypes() const;
282   virtual QMimeData *mimeData(const QModelIndexList &) const;
283
284   void attachNetwork(Network *network);
285
286   bool isBufferIndex(const QModelIndex &) const;
287   //Buffer *getBufferByIndex(const QModelIndex &) const;
288   QModelIndex networkIndex(NetworkId networkId);
289   QModelIndex bufferIndex(BufferId bufferId);
290
291   const Network *networkByIndex(const QModelIndex &index) const;
292
293   BufferInfo::ActivityLevel bufferActivity(const BufferInfo &buffer) const;
294
295   //! Finds a buffer with a given name in a given network
296   /** This performs a linear search through all BufferItems, hence it is expensive.
297    *  @param networkId  The network which we search in
298    *  @param bufferName The bufferName we look for
299    *  @return The id of the buffer if found, an invalid one else
300    */
301   BufferId bufferId(NetworkId networkId, const QString &bufferName, Qt::CaseSensitivity cs = Qt::CaseInsensitive) const;
302
303   QString bufferName(BufferId bufferId) const;
304   BufferInfo::Type bufferType(BufferId bufferId) const;
305   BufferInfo bufferInfo(BufferId bufferId) const;
306   MsgId lastSeenMsgId(BufferId bufferId) const;
307   MsgId lastSeenMarkerMsgId(BufferId bufferId) const;
308   NetworkId networkId(BufferId bufferId) const;
309   QString networkName(BufferId bufferId) const;
310
311   inline QList<BufferId> allBufferIds() const { return _bufferItemCache.keys(); }
312   QList<BufferId> allBufferIdsSorted() const;
313   void sortBufferIds(QList<BufferId> &bufferIds) const;
314
315 public slots:
316   void bufferUpdated(BufferInfo bufferInfo);
317   void removeBuffer(BufferId bufferId);
318   void setLastSeenMsgId(const BufferId &bufferId, const MsgId &msgId);
319   void setBufferActivity(const BufferId &bufferId, BufferInfo::ActivityLevel activity);
320   void clearBufferActivity(const BufferId &bufferId);
321   void updateBufferActivity(const Message &msg);
322   void networkRemoved(const NetworkId &networkId);
323
324 signals:
325   void setLastSeenMsg(BufferId bufferId, MsgId msgId);
326
327 private slots:
328   void checkForRemovedBuffers(const QModelIndex &parent, int start, int end);
329   void checkForNewBuffers(const QModelIndex &parent, int start, int end);
330
331 private:
332   int networkRow(NetworkId networkId) const;
333   NetworkItem *findNetworkItem(NetworkId networkId) const;
334   NetworkItem *networkItem(NetworkId networkId);
335   inline BufferItem *findBufferItem(const BufferInfo &bufferInfo) const { return findBufferItem(bufferInfo.bufferId()); }
336   BufferItem *findBufferItem(BufferId bufferId) const;
337   BufferItem *bufferItem(const BufferInfo &bufferInfo);
338
339   static bool bufferItemLessThan(const BufferItem *left, const BufferItem *right);
340
341   QHash<BufferId, BufferItem *> _bufferItemCache;
342 };
343 Q_DECLARE_OPERATORS_FOR_FLAGS(NetworkModel::ItemTypes)
344
345 #endif // NETWORKMODEL_H