fixing the creating of new buffers in the client. bye bye Client::buffer(BufferId)
[quassel.git] / src / client / networkmodel.h
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #ifndef NETWORKMODEL_H
22 #define NETWORKMODEL_H
23
24 #include <QtCore>
25
26 #include "treemodel.h"
27 #include "buffer.h"
28
29 #include <QPointer>
30
31 class BufferInfo;
32
33 #include "selectionmodelsynchronizer.h"
34 #include "modelpropertymapper.h"
35 #include "clientsettings.h"
36 #include "ircchannel.h"
37 #include "ircuser.h"
38 #include "network.h"
39
40 class MappedSelectionModel;
41 class QAbstractItemView;
42 class BufferItem;
43
44 /*****************************************
45  *  Network Items
46  *****************************************/
47 class NetworkItem : public PropertyMapItem {
48   Q_OBJECT
49   Q_PROPERTY(QString networkName READ networkName)
50   Q_PROPERTY(QString currentServer READ currentServer)
51   Q_PROPERTY(int nickCount READ nickCount)
52     
53 public:
54   NetworkItem(const NetworkId &netid, AbstractTreeItem *parent = 0);
55
56   virtual QVariant data(int column, int row) const;
57
58   inline bool isActive() const { return (bool)_network ? _network->isConnected() : false; }
59
60   inline const NetworkId &networkId() const { return _networkId; }
61   inline QString networkName() const { return (bool)_network ? _network->networkName() : QString(); }
62   inline QString currentServer() const { return (bool)_network ? _network->currentServer() : QString(); }
63   inline int nickCount() const { return (bool)_network ? _network->ircUsers().count() : 0; }
64
65   virtual QString toolTip(int column) const;
66
67   BufferItem *findBufferItem(BufferId bufferId);
68   inline BufferItem *findBufferItem(const BufferInfo &bufferInfo) { return findBufferItem(bufferInfo.bufferId()); }
69   BufferItem *bufferItem(const BufferInfo &bufferInfo);
70
71 public slots:
72   void setNetworkName(const QString &networkName);
73   void setCurrentServer(const QString &serverName);
74
75   void attachNetwork(Network *network);
76   void attachIrcChannel(IrcChannel *channel);
77   void attachIrcUser(IrcUser *ircUser);
78
79 private:
80   NetworkId _networkId;
81
82   QPointer<Network> _network;
83 };
84
85 /*****************************************
86  *  Fancy Buffer Items
87  *****************************************/
88 class BufferItem : public PropertyMapItem {
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   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 const MsgId &lastSeenMsgId() const { return _lastSeenMsgId; }
112   inline void setLastSeenMsgId(const MsgId &msgId) { _lastSeenMsgId = msgId; }
113   inline Buffer::ActivityLevel activityLevel() const { return _activity; }
114   void setActivityLevel(Buffer::ActivityLevel level);
115   //void updateActivityLevel(Buffer::ActivityLevel level);
116   void updateActivityLevel(const Message &msg);
117
118   bool isCurrentBuffer() const;
119   virtual QString toolTip(int column) const;
120
121 public slots:
122   virtual inline void setTopic(const QString &) { emit dataChanged(1); }
123
124 private:
125   BufferInfo _bufferInfo;
126   Buffer::ActivityLevel _activity;
127   MsgId _lastSeenMsgId;
128 };
129
130 /*****************************************
131 *  StatusBufferItem
132 *****************************************/
133 class StatusBufferItem : public BufferItem {
134   Q_OBJECT
135
136 public:
137   StatusBufferItem(const BufferInfo &bufferInfo, NetworkItem *parent);
138
139   virtual QString toolTip(int column) const;
140   virtual inline QString bufferName() const { return tr("Status Buffer"); }
141 };
142
143 /*****************************************
144 *  QueryBufferItem
145 *****************************************/
146 class QueryBufferItem : public BufferItem {
147   Q_OBJECT
148
149 public:
150   QueryBufferItem(const BufferInfo &bufferInfo, NetworkItem *parent);
151
152   virtual QVariant data(int column, int role) const;
153   virtual inline bool isActive() const { return (bool)_ircUser; }
154   virtual QString toolTip(int column) const;
155
156 public slots:
157   void attachIrcUser(IrcUser *ircUser);
158   void ircUserDestroyed();
159
160 private:
161   IrcUser *_ircUser;
162 };
163
164 /*****************************************
165 *  ChannelBufferItem
166 *****************************************/
167 class UserCategoryItem;
168
169 class ChannelBufferItem : public BufferItem {
170   Q_OBJECT
171
172 public:
173   ChannelBufferItem(const BufferInfo &bufferInfo, AbstractTreeItem *parent);
174
175   virtual inline bool isActive() const { return (bool)_ircChannel; }
176   virtual QString toolTip(int column) const;
177
178   virtual inline QString topic() const { return (bool)_ircChannel ? _ircChannel->topic() : QString(); }
179   virtual inline int nickCount() const { return (bool)_ircChannel ? _ircChannel->ircUsers().count() : 0; }
180   
181   void attachIrcChannel(IrcChannel *ircChannel);
182
183 public slots:
184   void join(const QList<IrcUser *> &ircUsers);
185   void part(IrcUser *ircUser);
186
187   UserCategoryItem *findCategoryItem(int categoryId);
188   void addUserToCategory(IrcUser *ircUser);
189   void addUsersToCategory(const QList<IrcUser *> &ircUser);
190   void removeUserFromCategory(IrcUser *ircUser);
191   void userModeChanged(IrcUser *ircUser);
192
193 private slots:
194   void ircChannelDestroyed();
195
196 private:
197   IrcChannel *_ircChannel;
198 };
199
200 /*****************************************
201 *  User Category Items (like @vh etc.)
202 *****************************************/
203 class IrcUserItem;
204 class UserCategoryItem : public PropertyMapItem {
205   Q_OBJECT
206   Q_PROPERTY(QString categoryName READ categoryName)
207     
208 public:
209   UserCategoryItem(int category, AbstractTreeItem *parent);
210
211   QString categoryName() const;
212   inline int categoryId() const { return _category; }
213   virtual QVariant data(int column, int role) const;
214
215   IrcUserItem *findIrcUser(IrcUser *ircUser);
216   void addUsers(const QList<IrcUser *> &ircUser);
217   bool removeUser(IrcUser *ircUser);
218
219   static int categoryFromModes(const QString &modes);
220
221 private:
222   int _category;
223
224   static const QList<QChar> categories;
225 };
226
227 /*****************************************
228 *  Irc User Items
229 *****************************************/
230 class IrcUserItem : public PropertyMapItem {
231   Q_OBJECT
232   Q_PROPERTY(QString nickName READ nickName)
233     
234 public:
235   IrcUserItem(IrcUser *ircUser, AbstractTreeItem *parent);
236
237   inline QString nickName() const { return _ircUser ? _ircUser->nick() : QString(); }
238   inline bool isActive() const { return _ircUser ? !_ircUser->isAway() : false; }
239
240   inline IrcUser *ircUser() { return _ircUser; }
241   virtual QVariant data(int column, int role) const;
242   virtual QString toolTip(int column) const;
243
244 private slots:
245   inline void ircUserDestroyed() { parent()->removeChild(this); }
246
247 private:
248   QPointer<IrcUser> _ircUser;
249 };
250
251
252 /*****************************************
253  * NetworkModel
254  *****************************************/
255 class NetworkModel : public TreeModel {
256   Q_OBJECT
257
258 public:
259   enum myRoles {
260     BufferTypeRole = TreeModel::UserRole,
261     ItemActiveRole,
262     BufferActivityRole,
263     BufferIdRole,
264     NetworkIdRole,
265     BufferInfoRole,
266     ItemTypeRole,
267     UserAwayRole
268   };
269
270   enum itemType {
271     NetworkItemType = 0x01,
272     BufferItemType = 0x02,
273     UserCategoryItemType = 0x04,
274     IrcUserItemType = 0x08
275   };
276   Q_DECLARE_FLAGS(itemTypes, itemType);
277
278   NetworkModel(QObject *parent = 0);
279   static QList<QVariant> defaultHeader();
280
281   static bool mimeContainsBufferList(const QMimeData *mimeData);
282   static QList< QPair<NetworkId, BufferId> > mimeDataToBufferList(const QMimeData *mimeData);
283
284   virtual QStringList mimeTypes() const;
285   virtual QMimeData *mimeData(const QModelIndexList &) const;
286   virtual bool dropMimeData(const QMimeData *, Qt::DropAction, int, int, const QModelIndex &);
287
288   void attachNetwork(Network *network);
289
290   bool isBufferIndex(const QModelIndex &) const;
291   //Buffer *getBufferByIndex(const QModelIndex &) const;
292   QModelIndex networkIndex(NetworkId networkId);
293   QModelIndex bufferIndex(BufferId bufferId);
294
295   const Network *networkByIndex(const QModelIndex &index) const;
296
297   Buffer::ActivityLevel bufferActivity(const BufferInfo &buffer) const;
298
299   QString bufferName(BufferId bufferId);
300   BufferInfo::Type bufferType(BufferId bufferId);
301   BufferInfo bufferInfo(BufferId bufferId);
302   MsgId lastSeenMsgId(BufferId bufferId);
303   NetworkId networkId(BufferId bufferId);
304   QString networkName(BufferId bufferId);
305
306 public slots:
307   void bufferUpdated(BufferInfo bufferInfo);
308   void removeBuffer(BufferId bufferId);
309   void setLastSeenMsgId(const BufferId &bufferId, const MsgId &msgId);
310   void setBufferActivity(const BufferId &bufferId, Buffer::ActivityLevel activity);
311   void updateBufferActivity(const Message &msg);
312   void networkRemoved(const NetworkId &networkId);
313
314 private slots:
315   void checkForRemovedBuffers(const QModelIndex &parent, int start, int end);
316   void checkForNewBuffers(const QModelIndex &parent, int start, int end);
317
318 private:
319   int networkRow(NetworkId networkId);
320   NetworkItem *findNetworkItem(NetworkId networkId);
321   NetworkItem *networkItem(NetworkId networkId);
322   inline BufferItem *findBufferItem(const BufferInfo &bufferInfo) { return findBufferItem(bufferInfo.bufferId()); }
323   BufferItem *findBufferItem(BufferId bufferId);
324   BufferItem *bufferItem(const BufferInfo &bufferInfo);
325
326   QHash<BufferId, BufferItem *> _bufferItemCache;
327 };
328 Q_DECLARE_OPERATORS_FOR_FLAGS(NetworkModel::itemTypes);
329
330 #endif // NETWORKMODEL_H