Make compile on top of the current master branch
[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 quint64 id() const { return qHash(_networkId); }
57   virtual QVariant data(int column, int row) const;
58
59   inline bool isActive() const { return (bool)_network ? _network->isConnected() : false; }
60
61   inline const NetworkId &networkId() const { return _networkId; }
62   inline QString networkName() const { return (bool)_network ? _network->networkName() : QString(); }
63   inline QString currentServer() const { return (bool)_network ? _network->currentServer() : QString(); }
64   inline int nickCount() const { return (bool)_network ? _network->ircUsers().count() : 0; }
65
66   virtual QString toolTip(int column) const;
67
68   BufferItem *bufferItem(const BufferInfo &bufferInfo);
69
70 public slots:
71   void setNetworkName(const QString &networkName);
72   void setCurrentServer(const QString &serverName);
73
74   void attachNetwork(Network *network);
75   void attachIrcChannel(IrcChannel *channel);
76   void attachIrcUser(IrcUser *ircUser);
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 inline quint64 id() const { return qHash(_bufferInfo.bufferId()); }
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 Buffer::ActivityLevel activityLevel() const { return _activity; }
112   void setActivityLevel(Buffer::ActivityLevel level);
113   void updateActivityLevel(Buffer::ActivityLevel level);
114
115   void setLastMsgInsert(QDateTime msgDate);
116   bool setLastSeen();
117   QDateTime lastSeen();
118
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 };
128
129 /*****************************************
130 *  StatusBufferItem
131 *****************************************/
132 class StatusBufferItem : public BufferItem {
133   Q_OBJECT
134
135 public:
136   StatusBufferItem(const BufferInfo &bufferInfo, NetworkItem *parent);
137
138   virtual QString toolTip(int column) const;
139   virtual inline QString bufferName() const { return tr("Status Buffer"); }
140 };
141
142 /*****************************************
143 *  QueryBufferItem
144 *****************************************/
145 class QueryBufferItem : public BufferItem {
146   Q_OBJECT
147
148 public:
149   QueryBufferItem(const BufferInfo &bufferInfo, NetworkItem *parent);
150
151   virtual bool isActive() const;
152   virtual QString toolTip(int column) const;
153
154 public slots:
155   void attachIrcUser(IrcUser *ircUser);
156   void ircUserDestroyed();
157
158 private:
159   IrcUser *_ircUser;
160 };
161
162 /*****************************************
163 *  ChannelBufferItem
164 *****************************************/
165 class ChannelBufferItem : public BufferItem {
166   Q_OBJECT
167
168 public:
169   ChannelBufferItem(const BufferInfo &bufferInfo, AbstractTreeItem *parent);
170
171   virtual inline bool isActive() const { return (bool)_ircChannel; }
172   virtual QString toolTip(int column) const;
173
174   virtual inline QString topic() const { return (bool)_ircChannel ? _ircChannel->topic() : QString(); }
175   virtual inline int nickCount() const { return (bool)_ircChannel ? _ircChannel->ircUsers().count() : 0; }
176   
177   void attachIrcChannel(IrcChannel *ircChannel);
178
179 public slots:
180   void join(const QList<IrcUser *> &ircUsers);
181   void part(IrcUser *ircUser);
182
183   void addUserToCategory(IrcUser *ircUser);
184   void addUsersToCategory(const QList<IrcUser *> &ircUser);
185   void removeUserFromCategory(IrcUser *ircUser);
186   void userModeChanged(IrcUser *ircUser);
187
188 private slots:
189   void ircChannelDestroyed();
190   void ircUserDestroyed();
191
192 private:
193   IrcChannel *_ircChannel;
194 };
195
196 /*****************************************
197 *  User Category Items (like @vh etc.)
198 *****************************************/
199 class UserCategoryItem : public PropertyMapItem {
200   Q_OBJECT
201   Q_PROPERTY(QString categoryName READ categoryName)
202     
203 public:
204   UserCategoryItem(int category, AbstractTreeItem *parent);
205
206   QString categoryName() const;
207   virtual quint64 id() const;
208   virtual QVariant data(int column, int role) const;
209   
210   void addUsers(const QList<IrcUser *> &ircUser);
211   bool removeUser(IrcUser *ircUser);
212
213   static int categoryFromModes(const QString &modes);
214
215 private:
216   int _category;
217
218   static const QList<QChar> categories;
219 };
220
221 /*****************************************
222 *  Irc User Items
223 *****************************************/
224 class IrcUserItem : public PropertyMapItem {
225   Q_OBJECT
226   Q_PROPERTY(QString nickName READ nickName)
227     
228 public:
229   IrcUserItem(IrcUser *ircUser, AbstractTreeItem *parent);
230
231   QString nickName() const;
232   bool isActive() const;
233
234   inline IrcUser *ircUser() { return _ircUser; }
235   inline virtual quint64 id() const { return _id; }
236   virtual QVariant data(int column, int role) const;
237   virtual QString toolTip(int column) const;
238
239 private:
240   QPointer<IrcUser> _ircUser;
241   quint64 _id;
242 };
243
244
245 /*****************************************
246  * NetworkModel
247  *****************************************/
248 class NetworkModel : public TreeModel {
249   Q_OBJECT
250
251 public:
252   enum myRoles {
253     BufferTypeRole = TreeModel::UserRole,
254     ItemActiveRole,
255     BufferActivityRole,
256     BufferIdRole,
257     NetworkIdRole,
258     BufferInfoRole,
259     ItemTypeRole
260   };
261
262   enum itemType {
263     NetworkItemType = 0x01,
264     BufferItemType = 0x02,
265     UserCategoryItemType = 0x04,
266     IrcUserItemType = 0x08
267   };
268   Q_DECLARE_FLAGS(itemTypes, itemType);
269
270   NetworkModel(QObject *parent = 0);
271   static QList<QVariant> defaultHeader();
272
273   static bool mimeContainsBufferList(const QMimeData *mimeData);
274   static QList< QPair<NetworkId, BufferId> > mimeDataToBufferList(const QMimeData *mimeData);
275
276   virtual QStringList mimeTypes() const;
277   virtual QMimeData *mimeData(const QModelIndexList &) const;
278   virtual bool dropMimeData(const QMimeData *, Qt::DropAction, int, int, const QModelIndex &);
279
280   void attachNetwork(Network *network);
281
282   bool isBufferIndex(const QModelIndex &) const;
283   //Buffer *getBufferByIndex(const QModelIndex &) const;
284   QModelIndex networkIndex(NetworkId networkId);
285   QModelIndex bufferIndex(BufferId bufferId);
286
287   const Network *networkByIndex(const QModelIndex &index) const;
288
289   Buffer::ActivityLevel bufferActivity(const BufferInfo &buffer) const;
290
291 public slots:
292   void bufferUpdated(BufferInfo bufferInfo);
293   void removeBuffer(BufferId bufferId);
294   void setBufferActivity(const BufferInfo &buffer, Buffer::ActivityLevel activity);
295   void networkRemoved(const NetworkId &networkId);
296   
297 private:
298   NetworkItem *networkItem(NetworkId networkId);
299   NetworkItem *existsNetworkItem(NetworkId networkId);
300   BufferItem *bufferItem(const BufferInfo &bufferInfo);
301   BufferItem *existsBufferItem(const BufferInfo &bufferInfo);
302
303 };
304 Q_DECLARE_OPERATORS_FOR_FLAGS(NetworkModel::itemTypes);
305
306 #endif // NETWORKMODEL_H