1 /***************************************************************************
2 * Copyright (C) 2005-2014 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #ifndef NETWORKMODEL_H
22 #define NETWORKMODEL_H
24 #include "bufferinfo.h"
25 #include "clientsettings.h"
28 #include "treemodel.h"
31 class StatusBufferItem;
33 /*****************************************
35 *****************************************/
36 class NetworkItem : public PropertyMapItem
39 Q_PROPERTY(QString networkName READ networkName)
40 Q_PROPERTY(QString currentServer READ currentServer)
41 Q_PROPERTY(int nickCount READ nickCount)
44 NetworkItem(const NetworkId &netid, AbstractTreeItem *parent = 0);
46 virtual QVariant data(int column, int row) const;
48 inline bool isActive() const { return (bool)_network ? _network->isConnected() : false; }
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; }
55 virtual QString toolTip(int column) const;
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; }
63 void setNetworkName(const QString &networkName);
64 void setCurrentServer(const QString &serverName);
66 void attachNetwork(Network *network);
67 void attachIrcChannel(IrcChannel *channel);
68 void attachIrcUser(IrcUser *ircUser);
71 void networkDataChanged(int column = -1);
74 void onBeginRemoveChilds(int start, int end);
75 void onNetworkDestroyed();
79 StatusBufferItem *_statusBufferItem;
81 QPointer<Network> _network;
85 /*****************************************
87 *****************************************/
88 class BufferItem : public PropertyMapItem
91 Q_PROPERTY(QString bufferName READ bufferName WRITE setBufferName)
92 Q_PROPERTY(QString topic READ topic)
93 Q_PROPERTY(int nickCount READ nickCount)
96 BufferItem(const BufferInfo &bufferInfo, AbstractTreeItem *parent = 0);
98 inline const BufferInfo &bufferInfo() const { return _bufferInfo; }
99 virtual QVariant data(int column, int role) const;
100 virtual bool setData(int column, const QVariant &value, int role);
102 inline BufferId bufferId() const { return _bufferInfo.bufferId(); }
103 inline BufferInfo::Type bufferType() const { return _bufferInfo.type(); }
105 virtual void setBufferName(const QString &name);
106 virtual inline QString bufferName() const { return _bufferInfo.bufferName(); }
107 virtual inline QString topic() const { return QString(); }
108 virtual inline int nickCount() const { return 0; }
110 virtual inline bool isActive() const { return qobject_cast<NetworkItem *>(parent())->isActive(); }
112 inline MsgId lastSeenMsgId() const { return _lastSeenMsgId; }
113 inline MsgId markerLineMsgId() const { return _markerLineMsgId; }
114 void setLastSeenMsgId(MsgId msgId);
115 void setMarkerLineMsgId(MsgId msgId);
117 inline BufferInfo::ActivityLevel activityLevel() const { return _activity; }
118 void setActivityLevel(BufferInfo::ActivityLevel level);
119 void clearActivityLevel();
120 void updateActivityLevel(const Message &msg);
122 inline const MsgId &firstUnreadMsgId() const { return _firstUnreadMsgId; }
124 bool isCurrentBuffer() const;
125 virtual QString toolTip(int column) const;
128 virtual inline void setTopic(const QString &) { emit dataChanged(1); }
129 virtual inline void setEncrypted(bool) { emit dataChanged(); }
132 BufferInfo _bufferInfo;
133 BufferInfo::ActivityLevel _activity;
134 MsgId _lastSeenMsgId;
135 MsgId _markerLineMsgId;
136 MsgId _firstUnreadMsgId;
140 /*****************************************
142 *****************************************/
143 class StatusBufferItem : public BufferItem
148 StatusBufferItem(const BufferInfo &bufferInfo, NetworkItem *parent);
150 virtual QString toolTip(int column) const;
151 virtual inline QString bufferName() const { return tr("Status Buffer"); }
155 /*****************************************
157 *****************************************/
158 class QueryBufferItem : public BufferItem
163 QueryBufferItem(const BufferInfo &bufferInfo, NetworkItem *parent);
165 virtual QVariant data(int column, int role) const;
166 virtual bool setData(int column, const QVariant &value, int role);
168 virtual inline bool isActive() const { return (bool)_ircUser; }
169 virtual QString toolTip(int column) const;
171 virtual void setBufferName(const QString &name);
174 void setIrcUser(IrcUser *ircUser);
175 void removeIrcUser();
182 /*****************************************
184 *****************************************/
185 class UserCategoryItem;
187 class ChannelBufferItem : public BufferItem
192 ChannelBufferItem(const BufferInfo &bufferInfo, AbstractTreeItem *parent);
194 virtual QVariant data(int column, int role) const;
195 virtual inline bool isActive() const { return (bool)_ircChannel; }
196 virtual QString toolTip(int column) const;
198 virtual inline QString topic() const { return (bool)_ircChannel ? _ircChannel->topic() : QString(); }
199 virtual inline int nickCount() const { return (bool)_ircChannel ? _ircChannel->ircUsers().count() : 0; }
201 void attachIrcChannel(IrcChannel *ircChannel);
204 void join(const QList<IrcUser *> &ircUsers);
205 void part(IrcUser *ircUser);
207 UserCategoryItem *findCategoryItem(int categoryId);
208 void addUserToCategory(IrcUser *ircUser);
209 void addUsersToCategory(const QList<IrcUser *> &ircUser);
210 void removeUserFromCategory(IrcUser *ircUser);
211 void userModeChanged(IrcUser *ircUser);
214 void ircChannelParted();
215 void ircChannelDestroyed();
218 IrcChannel *_ircChannel;
222 /*****************************************
223 * User Category Items (like @vh etc.)
224 *****************************************/
226 class UserCategoryItem : public PropertyMapItem
229 Q_PROPERTY(QString categoryName READ categoryName)
232 UserCategoryItem(int category, AbstractTreeItem *parent);
234 QString categoryName() const;
235 inline int categoryId() const { return _category; }
236 virtual QVariant data(int column, int role) const;
238 IrcUserItem *findIrcUser(IrcUser *ircUser);
239 void addUsers(const QList<IrcUser *> &ircUser);
240 bool removeUser(IrcUser *ircUser);
242 static int categoryFromModes(const QString &modes);
247 static const QList<QChar> categories;
251 /*****************************************
253 *****************************************/
254 class IrcUserItem : public PropertyMapItem
257 Q_PROPERTY(QString nickName READ nickName)
260 IrcUserItem(IrcUser *ircUser, AbstractTreeItem *parent);
262 inline QString nickName() const { return _ircUser ? _ircUser->nick() : QString(); }
263 inline bool isActive() const { return _ircUser ? !_ircUser->isAway() : false; }
265 inline IrcUser *ircUser() { return _ircUser; }
266 virtual QVariant data(int column, int role) const;
267 virtual QString toolTip(int column) const;
270 inline void ircUserQuited() { parent()->removeChild(this); }
273 QPointer<IrcUser> _ircUser;
277 /*****************************************
279 *****************************************/
280 class NetworkModel : public TreeModel
286 BufferTypeRole = TreeModel::UserRole,
296 BufferFirstUnreadMsgIdRole,
301 NetworkItemType = 0x01,
302 BufferItemType = 0x02,
303 UserCategoryItemType = 0x04,
304 IrcUserItemType = 0x08
306 Q_DECLARE_FLAGS(ItemTypes, ItemType)
308 NetworkModel(QObject *parent = 0);
309 static QList<QVariant> defaultHeader();
311 static bool mimeContainsBufferList(const QMimeData *mimeData);
312 static QList<QPair<NetworkId, BufferId> > mimeDataToBufferList(const QMimeData *mimeData);
314 virtual QStringList mimeTypes() const;
315 virtual QMimeData *mimeData(const QModelIndexList &) const;
317 void attachNetwork(Network *network);
319 bool isBufferIndex(const QModelIndex &) const;
320 //Buffer *getBufferByIndex(const QModelIndex &) const;
321 QModelIndex networkIndex(NetworkId networkId);
322 QModelIndex bufferIndex(BufferId bufferId);
324 const Network *networkByIndex(const QModelIndex &index) const;
326 BufferInfo::ActivityLevel bufferActivity(const BufferInfo &buffer) const;
328 //! Finds a buffer with a given name in a given network
329 /** This performs a linear search through all BufferItems, hence it is expensive.
330 * @param networkId The network which we search in
331 * @param bufferName The bufferName we look for
332 * @return The id of the buffer if found, an invalid one else
334 BufferId bufferId(NetworkId networkId, const QString &bufferName, Qt::CaseSensitivity cs = Qt::CaseInsensitive) const;
336 QString bufferName(BufferId bufferId) const;
337 BufferInfo::Type bufferType(BufferId bufferId) const;
338 BufferInfo bufferInfo(BufferId bufferId) const;
339 MsgId lastSeenMsgId(BufferId bufferId) const;
340 MsgId markerLineMsgId(BufferId bufferId) const;
341 NetworkId networkId(BufferId bufferId) const;
342 QString networkName(BufferId bufferId) const;
344 inline QList<BufferId> allBufferIds() const { return _bufferItemCache.keys(); }
345 QList<BufferId> allBufferIdsSorted() const;
346 void sortBufferIds(QList<BufferId> &bufferIds) const;
349 void bufferUpdated(BufferInfo bufferInfo);
350 void removeBuffer(BufferId bufferId);
351 MsgId lastSeenMsgId(const BufferId &bufferId);
352 void setLastSeenMsgId(const BufferId &bufferId, const MsgId &msgId);
353 void setMarkerLineMsgId(const BufferId &bufferId, const MsgId &msgId);
354 void setBufferActivity(const BufferId &bufferId, BufferInfo::ActivityLevel activity);
355 void clearBufferActivity(const BufferId &bufferId);
356 void updateBufferActivity(Message &msg);
357 void networkRemoved(const NetworkId &networkId);
360 void requestSetLastSeenMsg(BufferId buffer, MsgId msg);
361 void lastSeenMsgSet(BufferId buffer, MsgId msg);
362 void markerLineSet(BufferId buffer, MsgId msg);
365 void checkForRemovedBuffers(const QModelIndex &parent, int start, int end);
366 void checkForNewBuffers(const QModelIndex &parent, int start, int end);
367 void messageRedirectionSettingsChanged();
370 int networkRow(NetworkId networkId) const;
371 NetworkItem *findNetworkItem(NetworkId networkId) const;
372 NetworkItem *networkItem(NetworkId networkId);
373 inline BufferItem *findBufferItem(const BufferInfo &bufferInfo) const { return findBufferItem(bufferInfo.bufferId()); }
374 BufferItem *findBufferItem(BufferId bufferId) const;
375 BufferItem *bufferItem(const BufferInfo &bufferInfo);
377 void updateBufferActivity(BufferItem *bufferItem, const Message &msg);
379 static bool bufferItemLessThan(const BufferItem *left, const BufferItem *right);
381 QHash<BufferId, BufferItem *> _bufferItemCache;
383 int _userNoticesTarget;
384 int _serverNoticesTarget;
385 int _errorMsgsTarget;
389 Q_DECLARE_OPERATORS_FOR_FLAGS(NetworkModel::ItemTypes)
391 #endif // NETWORKMODEL_H