1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 ***************************************************************************/
23 #include "client-export.h"
25 #include "bufferinfo.h"
26 #include "clientsettings.h"
29 #include "treemodel.h"
32 class StatusBufferItem;
34 /*****************************************
36 *****************************************/
37 class NetworkItem : public PropertyMapItem
40 Q_PROPERTY(QString networkName READ networkName)
41 Q_PROPERTY(QString currentServer READ currentServer)
42 Q_PROPERTY(int nickCount READ nickCount)
45 NetworkItem(const NetworkId &netid, AbstractTreeItem *parent = nullptr);
47 QStringList propertyOrder() const override;
49 QVariant data(int column, int row) const override;
52 * Escapes a string as HTML, ready for Qt markup.
54 * Implementation depends on Qt version - Qt4 uses Qt::escape, while Qt5 uses .toHtmlEscaped().
56 * @param[in] string QString to escape
57 * @param[in] useNonbreakingSpaces
59 * If true, replace spaces with non-breaking spaces (i.e. ' '), otherwise only HTML escape.
62 static QString escapeHTML(const QString &string, bool useNonbreakingSpaces = false);
64 inline bool isActive() const { return (bool)_network ? _network->isConnected() : false; }
66 inline const NetworkId &networkId() const { return _networkId; }
67 inline QString networkName() const { return (bool)_network ? _network->networkName() : QString(); }
68 inline QString currentServer() const { return (bool)_network ? _network->currentServer() : QString(); }
69 inline int nickCount() const { return (bool)_network ? _network->ircUsers().count() : 0; }
71 QString toolTip(int column) const override;
73 BufferItem *findBufferItem(BufferId bufferId);
74 inline BufferItem *findBufferItem(const BufferInfo &bufferInfo) { return findBufferItem(bufferInfo.bufferId()); }
75 BufferItem *bufferItem(const BufferInfo &bufferInfo);
76 inline StatusBufferItem *statusBufferItem() const { return _statusBufferItem; }
79 void setNetworkName(const QString &networkName);
80 void setCurrentServer(const QString &serverName);
82 void attachNetwork(Network *network);
83 void attachIrcChannel(IrcChannel *channel);
84 void attachIrcUser(IrcUser *ircUser);
87 void networkDataChanged(int column = -1);
90 void onBeginRemoveChilds(int start, int end);
91 void onNetworkDestroyed();
95 StatusBufferItem *_statusBufferItem;
97 QPointer<Network> _network;
101 /*****************************************
103 *****************************************/
104 class BufferItem : public PropertyMapItem
107 Q_PROPERTY(QString bufferName READ bufferName WRITE setBufferName)
108 Q_PROPERTY(QString topic READ topic)
109 Q_PROPERTY(int nickCount READ nickCount)
112 BufferItem(BufferInfo bufferInfo, AbstractTreeItem *parent = nullptr);
114 QStringList propertyOrder() const override;
116 inline const BufferInfo &bufferInfo() const { return _bufferInfo; }
117 QVariant data(int column, int role) const override;
118 bool setData(int column, const QVariant &value, int role) override;
120 inline BufferId bufferId() const { return _bufferInfo.bufferId(); }
121 inline BufferInfo::Type bufferType() const { return _bufferInfo.type(); }
123 virtual void setBufferName(const QString &name);
124 virtual inline QString bufferName() const { return _bufferInfo.bufferName(); }
125 virtual inline QString topic() const { return QString(); }
126 virtual inline int nickCount() const { return 0; }
128 virtual inline bool isActive() const { return qobject_cast<NetworkItem *>(parent())->isActive(); }
130 inline MsgId lastSeenMsgId() const { return _lastSeenMsgId; }
131 inline MsgId markerLineMsgId() const { return _markerLineMsgId; }
132 void setLastSeenMsgId(MsgId msgId);
133 void setMarkerLineMsgId(MsgId msgId);
135 inline BufferInfo::ActivityLevel activityLevel() const { return _activity; }
136 void setActivityLevel(BufferInfo::ActivityLevel level);
137 void clearActivityLevel();
138 void updateActivityLevel(const Message &msg);
139 void setActivity(Message::Types msg, bool highlight);
140 bool addActivity(Message::Types msg, bool highlight);
142 inline const MsgId &firstUnreadMsgId() const { return _firstUnreadMsgId; }
144 bool isCurrentBuffer() const;
145 QString toolTip(int column) const override;
148 virtual inline void setTopic(const QString &) { emit dataChanged(1); }
149 virtual inline void setEncrypted(bool) { emit dataChanged(); }
152 BufferInfo _bufferInfo;
153 BufferInfo::ActivityLevel _activity;
154 MsgId _lastSeenMsgId;
155 MsgId _markerLineMsgId;
156 MsgId _firstUnreadMsgId;
160 /*****************************************
162 *****************************************/
163 class StatusBufferItem : public BufferItem
168 StatusBufferItem(const BufferInfo &bufferInfo, NetworkItem *parent);
170 QString toolTip(int column) const override;
171 inline QString bufferName() const override { return tr("Status Buffer"); }
175 /*****************************************
177 *****************************************/
178 class QueryBufferItem : public BufferItem
183 QueryBufferItem(const BufferInfo &bufferInfo, NetworkItem *parent);
185 QVariant data(int column, int role) const override;
186 bool setData(int column, const QVariant &value, int role) override;
188 inline bool isActive() const override { return (bool)_ircUser; }
189 QString toolTip(int column) const override;
191 void setBufferName(const QString &name) override;
194 void setIrcUser(IrcUser *ircUser);
195 void removeIrcUser();
202 /*****************************************
204 *****************************************/
205 class UserCategoryItem;
207 class ChannelBufferItem : public BufferItem
212 ChannelBufferItem(const BufferInfo &bufferInfo, AbstractTreeItem *parent);
214 QVariant data(int column, int role) const override;
215 inline bool isActive() const override { return (bool)_ircChannel; }
216 QString toolTip(int column) const override;
218 inline QString topic() const override { return (bool)_ircChannel ? _ircChannel->topic() : QString(); }
219 inline int nickCount() const override { return (bool)_ircChannel ? _ircChannel->ircUsers().count() : 0; }
221 void attachIrcChannel(IrcChannel *ircChannel);
224 * Gets the list of channel modes for a given nick.
226 * @param[in] nick IrcUser nickname to check
227 * @returns Channel modes as a string if any, otherwise empty string
229 QString nickChannelModes(const QString &nick) const;
232 void join(const QList<IrcUser *> &ircUsers);
233 void part(IrcUser *ircUser);
235 UserCategoryItem *findCategoryItem(int categoryId);
236 void addUserToCategory(IrcUser *ircUser);
237 void addUsersToCategory(const QList<IrcUser *> &ircUser);
238 void removeUserFromCategory(IrcUser *ircUser);
239 void userModeChanged(IrcUser *ircUser);
242 void ircChannelParted();
243 void ircChannelDestroyed();
246 IrcChannel *_ircChannel;
250 /*****************************************
251 * User Category Items (like @vh etc.)
252 *****************************************/
255 class CLIENT_EXPORT UserCategoryItem : public PropertyMapItem
258 Q_PROPERTY(QString categoryName READ categoryName)
261 UserCategoryItem(int category, AbstractTreeItem *parent);
263 QStringList propertyOrder() const override;
265 QString categoryName() const;
266 inline int categoryId() const { return _category; }
267 QVariant data(int column, int role) const override;
269 IrcUserItem *findIrcUser(IrcUser *ircUser);
270 void addUsers(const QList<IrcUser *> &ircUser);
271 bool removeUser(IrcUser *ircUser);
273 static int categoryFromModes(const QString &modes);
278 static const QList<QChar> categories;
282 /*****************************************
284 *****************************************/
285 class IrcUserItem : public PropertyMapItem
288 Q_PROPERTY(QString nickName READ nickName)
291 IrcUserItem(IrcUser *ircUser, AbstractTreeItem *parent);
293 QStringList propertyOrder() const override;
295 inline QString nickName() const { return _ircUser ? _ircUser->nick() : QString(); }
296 inline bool isActive() const { return _ircUser ? !_ircUser->isAway() : false; }
298 inline IrcUser *ircUser() { return _ircUser; }
299 QVariant data(int column, int role) const override;
300 QString toolTip(int column) const override;
303 * Gets the list of channel modes for this nick if parented to channel.
305 * @returns Channel modes as a string if any, otherwise empty string
307 QString channelModes() const;
310 inline void ircUserQuited() { parent()->removeChild(this); }
313 QPointer<IrcUser> _ircUser;
317 /*****************************************
319 *****************************************/
320 class CLIENT_EXPORT NetworkModel : public TreeModel
326 BufferTypeRole = TreeModel::UserRole,
336 BufferFirstUnreadMsgIdRole,
341 NetworkItemType = 0x01,
342 BufferItemType = 0x02,
343 UserCategoryItemType = 0x04,
344 IrcUserItemType = 0x08
346 Q_DECLARE_FLAGS(ItemTypes, ItemType)
348 NetworkModel(QObject *parent = nullptr);
349 static QList<QVariant> defaultHeader();
351 static bool mimeContainsBufferList(const QMimeData *mimeData);
352 static QList<QPair<NetworkId, BufferId> > mimeDataToBufferList(const QMimeData *mimeData);
354 QStringList mimeTypes() const override;
355 QMimeData *mimeData(const QModelIndexList &) const override;
357 void attachNetwork(Network *network);
359 bool isBufferIndex(const QModelIndex &) const;
360 //Buffer *getBufferByIndex(const QModelIndex &) const;
361 QModelIndex networkIndex(NetworkId networkId);
362 QModelIndex bufferIndex(BufferId bufferId);
364 const Network *networkByIndex(const QModelIndex &index) const;
366 BufferInfo::ActivityLevel bufferActivity(const BufferInfo &buffer) const;
368 //! Finds a buffer with a given name in a given network
369 /** This performs a linear search through all BufferItems, hence it is expensive.
370 * @param networkId The network which we search in
371 * @param bufferName The bufferName we look for
372 * @return The id of the buffer if found, an invalid one else
374 BufferId bufferId(NetworkId networkId, const QString &bufferName, Qt::CaseSensitivity cs = Qt::CaseInsensitive) const;
376 QString bufferName(BufferId bufferId) const;
377 BufferInfo::Type bufferType(BufferId bufferId) const;
378 BufferInfo bufferInfo(BufferId bufferId) const;
379 MsgId lastSeenMsgId(BufferId bufferId) const;
380 MsgId markerLineMsgId(BufferId bufferId) const;
381 NetworkId networkId(BufferId bufferId) const;
382 QString networkName(BufferId bufferId) const;
384 inline QList<BufferId> allBufferIds() const { return _bufferItemCache.keys(); }
385 QList<BufferId> allBufferIdsSorted() const;
386 void sortBufferIds(QList<BufferId> &bufferIds) const;
389 void bufferUpdated(BufferInfo bufferInfo);
390 void removeBuffer(BufferId bufferId);
391 MsgId lastSeenMsgId(const BufferId &bufferId);
392 void setLastSeenMsgId(const BufferId &bufferId, const MsgId &msgId);
393 void setMarkerLineMsgId(const BufferId &bufferId, const MsgId &msgId);
394 void setBufferActivity(const BufferId &bufferId, BufferInfo::ActivityLevel activity);
395 void clearBufferActivity(const BufferId &bufferId);
396 void updateBufferActivity(Message &msg);
397 void networkRemoved(const NetworkId &networkId);
398 void bufferActivityChanged(BufferId, Message::Types);
399 void highlightCountChanged(BufferId, int);
402 void requestSetLastSeenMsg(BufferId buffer, MsgId msg);
403 void lastSeenMsgSet(BufferId buffer, MsgId msg);
404 void markerLineSet(BufferId buffer, MsgId msg);
407 void checkForRemovedBuffers(const QModelIndex &parent, int start, int end);
408 void checkForNewBuffers(const QModelIndex &parent, int start, int end);
409 void messageRedirectionSettingsChanged();
412 int networkRow(NetworkId networkId) const;
413 NetworkItem *findNetworkItem(NetworkId networkId) const;
414 NetworkItem *networkItem(NetworkId networkId);
415 inline BufferItem *findBufferItem(const BufferInfo &bufferInfo) const { return findBufferItem(bufferInfo.bufferId()); }
416 BufferItem *findBufferItem(BufferId bufferId) const;
417 BufferItem *bufferItem(const BufferInfo &bufferInfo);
419 void updateBufferActivity(BufferItem *bufferItem, const Message &msg);
421 static bool bufferItemLessThan(const BufferItem *left, const BufferItem *right);
423 QHash<BufferId, BufferItem *> _bufferItemCache;
425 int _userNoticesTarget;
426 int _serverNoticesTarget;
427 int _errorMsgsTarget;
431 Q_DECLARE_OPERATORS_FOR_FLAGS(NetworkModel::ItemTypes)