Refactoring the Tree- and Networkmodel (internal stuff only).
[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
78 private:
79   NetworkId _networkId;
80
81   QPointer<Network> _network;
82 };
83
84 /*****************************************
85  *  Fancy Buffer Items
86  *****************************************/
87 class BufferItem : public PropertyMapItem {
88   Q_OBJECT
89   Q_PROPERTY(QString bufferName READ bufferName WRITE setBufferName)
90   Q_PROPERTY(QString topic READ topic)
91   Q_PROPERTY(int nickCount READ nickCount)
92
93 public:
94   BufferItem(const BufferInfo &bufferInfo, AbstractTreeItem *parent = 0);
95
96   inline const BufferInfo &bufferInfo() const { return _bufferInfo; }
97   virtual QVariant data(int column, int role) const;
98   virtual bool setData(int column, const QVariant &value, int role);
99
100   inline BufferId bufferId() const { return _bufferInfo.bufferId(); }
101   inline BufferInfo::Type bufferType() const { return _bufferInfo.type(); }
102
103   void setBufferName(const QString &name);
104   virtual inline QString bufferName() const { return _bufferInfo.bufferName(); }
105   virtual inline QString topic() const { return QString(); }
106   virtual inline int nickCount() const { return 0; }
107
108   virtual inline bool isActive() const { return qobject_cast<NetworkItem *>(parent())->isActive(); }
109
110   inline Buffer::ActivityLevel activityLevel() const { return _activity; }
111   void setActivityLevel(Buffer::ActivityLevel level);
112   void updateActivityLevel(Buffer::ActivityLevel level);
113
114   void setLastMsgInsert(QDateTime msgDate);
115   bool setLastSeen();
116   QDateTime lastSeen();
117
118   virtual QString toolTip(int column) const;
119
120 public slots:
121   virtual inline void setTopic(const QString &) { emit dataChanged(1); }
122
123 private:
124   BufferInfo _bufferInfo;
125   Buffer::ActivityLevel _activity;
126 };
127
128 /*****************************************
129 *  StatusBufferItem
130 *****************************************/
131 class StatusBufferItem : public BufferItem {
132   Q_OBJECT
133
134 public:
135   StatusBufferItem(const BufferInfo &bufferInfo, NetworkItem *parent);
136
137   virtual QString toolTip(int column) const;
138   virtual inline QString bufferName() const { return tr("Status Buffer"); }
139 };
140
141 /*****************************************
142 *  QueryBufferItem
143 *****************************************/
144 class QueryBufferItem : public BufferItem {
145   Q_OBJECT
146
147 public:
148   QueryBufferItem(const BufferInfo &bufferInfo, NetworkItem *parent);
149
150   virtual QString toolTip(int column) const;
151 };
152
153 /*****************************************
154 *  ChannelBufferItem
155 *****************************************/
156 class UserCategoryItem;
157
158 class ChannelBufferItem : public BufferItem {
159   Q_OBJECT
160
161 public:
162   ChannelBufferItem(const BufferInfo &bufferInfo, AbstractTreeItem *parent);
163
164   virtual inline bool isActive() const { return (bool)_ircChannel; }
165   virtual QString toolTip(int column) const;
166
167   virtual inline QString topic() const { return (bool)_ircChannel ? _ircChannel->topic() : QString(); }
168   virtual inline int nickCount() const { return (bool)_ircChannel ? _ircChannel->ircUsers().count() : 0; }
169   
170   void attachIrcChannel(IrcChannel *ircChannel);
171
172 public slots:
173   void join(const QList<IrcUser *> &ircUsers);
174   void part(IrcUser *ircUser);
175
176   UserCategoryItem *findCategoryItem(int categoryId);
177   void addUserToCategory(IrcUser *ircUser);
178   void addUsersToCategory(const QList<IrcUser *> &ircUser);
179   void removeUserFromCategory(IrcUser *ircUser);
180   void userModeChanged(IrcUser *ircUser);
181
182 private slots:
183   void ircChannelDestroyed();
184   void ircUserDestroyed();
185
186 private:
187   IrcChannel *_ircChannel;
188 };
189
190 /*****************************************
191 *  User Category Items (like @vh etc.)
192 *****************************************/
193 class IrcUserItem;
194 class UserCategoryItem : public PropertyMapItem {
195   Q_OBJECT
196   Q_PROPERTY(QString categoryName READ categoryName)
197     
198 public:
199   UserCategoryItem(int category, AbstractTreeItem *parent);
200
201   QString categoryName() const;
202   inline int categoryId() const { return _category; }
203   virtual QVariant data(int column, int role) const;
204
205   IrcUserItem *findIrcUser(IrcUser *ircUser);
206   void addUsers(const QList<IrcUser *> &ircUser);
207   bool removeUser(IrcUser *ircUser);
208
209   static int categoryFromModes(const QString &modes);
210
211 private:
212   int _category;
213
214   static const QList<QChar> categories;
215 };
216
217 /*****************************************
218 *  Irc User Items
219 *****************************************/
220 class IrcUserItem : public PropertyMapItem {
221   Q_OBJECT
222   Q_PROPERTY(QString nickName READ nickName)
223     
224 public:
225   IrcUserItem(IrcUser *ircUser, AbstractTreeItem *parent);
226
227   QString nickName() const;
228   bool isActive() const;
229
230   inline IrcUser *ircUser() { return _ircUser; }
231   virtual QVariant data(int column, int role) const;
232   virtual QString toolTip(int column) const;
233
234 private slots:
235   void setNick(QString newNick);
236   void setAway(bool);
237
238 private:
239   QPointer<IrcUser> _ircUser;
240 };
241
242
243 /*****************************************
244  * NetworkModel
245  *****************************************/
246 class NetworkModel : public TreeModel {
247   Q_OBJECT
248
249 public:
250   enum myRoles {
251     BufferTypeRole = TreeModel::UserRole,
252     ItemActiveRole,
253     BufferActivityRole,
254     BufferIdRole,
255     NetworkIdRole,
256     BufferInfoRole,
257     ItemTypeRole
258   };
259
260   enum itemType {
261     NetworkItemType = 0x01,
262     BufferItemType = 0x02,
263     UserCategoryItemType = 0x04,
264     IrcUserItemType = 0x08
265   };
266   Q_DECLARE_FLAGS(itemTypes, itemType);
267
268   NetworkModel(QObject *parent = 0);
269   static QList<QVariant> defaultHeader();
270
271   static bool mimeContainsBufferList(const QMimeData *mimeData);
272   static QList< QPair<NetworkId, BufferId> > mimeDataToBufferList(const QMimeData *mimeData);
273
274   virtual QStringList mimeTypes() const;
275   virtual QMimeData *mimeData(const QModelIndexList &) const;
276   virtual bool dropMimeData(const QMimeData *, Qt::DropAction, int, int, const QModelIndex &);
277
278   void attachNetwork(Network *network);
279
280   bool isBufferIndex(const QModelIndex &) const;
281   //Buffer *getBufferByIndex(const QModelIndex &) const;
282   QModelIndex networkIndex(NetworkId networkId);
283   QModelIndex bufferIndex(BufferId bufferId);
284
285   const Network *networkByIndex(const QModelIndex &index) const;
286
287   Buffer::ActivityLevel bufferActivity(const BufferInfo &buffer) const;
288
289 public slots:
290   void bufferUpdated(BufferInfo bufferInfo);
291   void removeBuffer(BufferId bufferId);
292   void setBufferActivity(const BufferInfo &buffer, Buffer::ActivityLevel activity);
293   void networkRemoved(const NetworkId &networkId);
294   
295 private:
296   int networkRow(NetworkId networkId);
297   NetworkItem *findNetworkItem(NetworkId networkId);
298   NetworkItem *networkItem(NetworkId networkId);
299   BufferItem *findBufferItem(const BufferInfo &bufferInfo);
300   BufferItem *findBufferItem(BufferId bufferId);
301   BufferItem *bufferItem(const BufferInfo &bufferInfo);
302
303 };
304 Q_DECLARE_OPERATORS_FOR_FLAGS(NetworkModel::itemTypes);
305
306 #endif // NETWORKMODEL_H