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