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