1 /***************************************************************************
2 * Copyright (C) 2005-09 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #ifndef NETWORKMODEL_H
22 #define NETWORKMODEL_H
24 #include "bufferinfo.h"
25 #include "clientsettings.h"
28 #include "treemodel.h"
32 /*****************************************
34 *****************************************/
35 class NetworkItem : public PropertyMapItem {
37 Q_PROPERTY(QString networkName READ networkName)
38 Q_PROPERTY(QString currentServer READ currentServer)
39 Q_PROPERTY(int nickCount READ nickCount)
42 NetworkItem(const NetworkId &netid, AbstractTreeItem *parent = 0);
44 virtual QVariant data(int column, int row) const;
46 inline bool isActive() const { return (bool)_network ? _network->isConnected() : false; }
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; }
53 virtual QString toolTip(int column) const;
55 BufferItem *findBufferItem(BufferId bufferId);
56 inline BufferItem *findBufferItem(const BufferInfo &bufferInfo) { return findBufferItem(bufferInfo.bufferId()); }
57 BufferItem *bufferItem(const BufferInfo &bufferInfo);
60 void setNetworkName(const QString &networkName);
61 void setCurrentServer(const QString &serverName);
63 void attachNetwork(Network *network);
64 void attachIrcChannel(IrcChannel *channel);
65 void attachIrcUser(IrcUser *ircUser);
68 void networkDataChanged(int column = -1);
73 QPointer<Network> _network;
76 /*****************************************
78 *****************************************/
79 class BufferItem : public PropertyMapItem {
81 Q_PROPERTY(QString bufferName READ bufferName WRITE setBufferName)
82 Q_PROPERTY(QString topic READ topic)
83 Q_PROPERTY(int nickCount READ nickCount)
86 BufferItem(const BufferInfo &bufferInfo, AbstractTreeItem *parent = 0);
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);
92 inline BufferId bufferId() const { return _bufferInfo.bufferId(); }
93 inline BufferInfo::Type bufferType() const { return _bufferInfo.type(); }
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; }
100 virtual inline bool isActive() const { return qobject_cast<NetworkItem *>(parent())->isActive(); }
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);
110 bool isCurrentBuffer() const;
111 virtual QString toolTip(int column) const;
114 virtual inline void setTopic(const QString &) { emit dataChanged(1); }
117 BufferInfo _bufferInfo;
118 BufferInfo::ActivityLevel _activity;
119 MsgId _lastSeenMsgId;
120 MsgId _lastSeenMarkerMsgId;
123 /*****************************************
125 *****************************************/
126 class StatusBufferItem : public BufferItem {
130 StatusBufferItem(const BufferInfo &bufferInfo, NetworkItem *parent);
132 virtual QString toolTip(int column) const;
133 virtual inline QString bufferName() const { return tr("Status Buffer"); }
136 /*****************************************
138 *****************************************/
139 class QueryBufferItem : public BufferItem {
143 QueryBufferItem(const BufferInfo &bufferInfo, NetworkItem *parent);
145 virtual QVariant data(int column, int role) const;
146 virtual bool setData(int column, const QVariant &value, int role);
148 virtual inline bool isActive() const { return (bool)_ircUser; }
149 virtual QString toolTip(int column) const;
151 virtual void setBufferName(const QString &name);
154 void setIrcUser(IrcUser *ircUser);
155 void removeIrcUser();
161 /*****************************************
163 *****************************************/
164 class UserCategoryItem;
166 class ChannelBufferItem : public BufferItem {
170 ChannelBufferItem(const BufferInfo &bufferInfo, AbstractTreeItem *parent);
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;
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; }
179 void attachIrcChannel(IrcChannel *ircChannel);
182 void join(const QList<IrcUser *> &ircUsers);
183 void part(IrcUser *ircUser);
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);
192 void ircChannelParted();
195 IrcChannel *_ircChannel;
198 /*****************************************
199 * User Category Items (like @vh etc.)
200 *****************************************/
202 class UserCategoryItem : public PropertyMapItem {
204 Q_PROPERTY(QString categoryName READ categoryName)
207 UserCategoryItem(int category, AbstractTreeItem *parent);
209 QString categoryName() const;
210 inline int categoryId() const { return _category; }
211 virtual QVariant data(int column, int role) const;
213 IrcUserItem *findIrcUser(IrcUser *ircUser);
214 void addUsers(const QList<IrcUser *> &ircUser);
215 bool removeUser(IrcUser *ircUser);
217 static int categoryFromModes(const QString &modes);
222 static const QList<QChar> categories;
225 /*****************************************
227 *****************************************/
228 class IrcUserItem : public PropertyMapItem {
230 Q_PROPERTY(QString nickName READ nickName)
233 IrcUserItem(IrcUser *ircUser, AbstractTreeItem *parent);
235 inline QString nickName() const { return _ircUser ? _ircUser->nick() : QString(); }
236 inline bool isActive() const { return _ircUser ? !_ircUser->isAway() : false; }
238 inline IrcUser *ircUser() { return _ircUser; }
239 virtual QVariant data(int column, int role) const;
240 virtual QString toolTip(int column) const;
243 inline void ircUserQuited() { parent()->removeChild(this); }
246 QPointer<IrcUser> _ircUser;
250 /*****************************************
252 *****************************************/
253 class NetworkModel : public TreeModel {
258 BufferTypeRole = TreeModel::UserRole,
271 NetworkItemType = 0x01,
272 BufferItemType = 0x02,
273 UserCategoryItemType = 0x04,
274 IrcUserItemType = 0x08
276 Q_DECLARE_FLAGS(ItemTypes, ItemType)
278 NetworkModel(QObject *parent = 0);
279 static QList<QVariant> defaultHeader();
281 static bool mimeContainsBufferList(const QMimeData *mimeData);
282 static QList< QPair<NetworkId, BufferId> > mimeDataToBufferList(const QMimeData *mimeData);
284 virtual QStringList mimeTypes() const;
285 virtual QMimeData *mimeData(const QModelIndexList &) const;
287 void attachNetwork(Network *network);
289 bool isBufferIndex(const QModelIndex &) const;
290 //Buffer *getBufferByIndex(const QModelIndex &) const;
291 QModelIndex networkIndex(NetworkId networkId);
292 QModelIndex bufferIndex(BufferId bufferId);
294 const Network *networkByIndex(const QModelIndex &index) const;
296 BufferInfo::ActivityLevel bufferActivity(const BufferInfo &buffer) const;
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
304 BufferId bufferId(NetworkId networkId, const QString &bufferName, Qt::CaseSensitivity cs = Qt::CaseInsensitive) const;
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;
314 inline QList<BufferId> allBufferIds() const { return _bufferItemCache.keys(); }
315 QList<BufferId> allBufferIdsSorted() const;
316 void sortBufferIds(QList<BufferId> &bufferIds) const;
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);
328 void setLastSeenMsg(BufferId bufferId, MsgId msgId);
331 void checkForRemovedBuffers(const QModelIndex &parent, int start, int end);
332 void checkForNewBuffers(const QModelIndex &parent, int start, int end);
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);
342 static bool bufferItemLessThan(const BufferItem *left, const BufferItem *right);
344 QHash<BufferId, BufferItem *> _bufferItemCache;
346 Q_DECLARE_OPERATORS_FOR_FLAGS(NetworkModel::ItemTypes)
348 #endif // NETWORKMODEL_H