introducing autocached settings
[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 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 QVariant data(int column, int role) const;
152   virtual inline bool isActive() const { return (bool)_ircUser; }
153   virtual QString toolTip(int column) const;
154
155 public slots:
156   void attachIrcUser(IrcUser *ircUser);
157   void ircUserDestroyed();
158
159 private:
160   IrcUser *_ircUser;
161 };
162
163 /*****************************************
164 *  ChannelBufferItem
165 *****************************************/
166 class UserCategoryItem;
167
168 class ChannelBufferItem : public BufferItem {
169   Q_OBJECT
170
171 public:
172   ChannelBufferItem(const BufferInfo &bufferInfo, AbstractTreeItem *parent);
173
174   virtual inline bool isActive() const { return (bool)_ircChannel; }
175   virtual QString toolTip(int column) const;
176
177   virtual inline QString topic() const { return (bool)_ircChannel ? _ircChannel->topic() : QString(); }
178   virtual inline int nickCount() const { return (bool)_ircChannel ? _ircChannel->ircUsers().count() : 0; }
179   
180   void attachIrcChannel(IrcChannel *ircChannel);
181
182 public slots:
183   void join(const QList<IrcUser *> &ircUsers);
184   void part(IrcUser *ircUser);
185
186   UserCategoryItem *findCategoryItem(int categoryId);
187   void addUserToCategory(IrcUser *ircUser);
188   void addUsersToCategory(const QList<IrcUser *> &ircUser);
189   void removeUserFromCategory(IrcUser *ircUser);
190   void userModeChanged(IrcUser *ircUser);
191
192 private slots:
193   void ircChannelDestroyed();
194
195 private:
196   IrcChannel *_ircChannel;
197 };
198
199 /*****************************************
200 *  User Category Items (like @vh etc.)
201 *****************************************/
202 class IrcUserItem;
203 class UserCategoryItem : public PropertyMapItem {
204   Q_OBJECT
205   Q_PROPERTY(QString categoryName READ categoryName)
206     
207 public:
208   UserCategoryItem(int category, AbstractTreeItem *parent);
209
210   QString categoryName() const;
211   inline int categoryId() const { return _category; }
212   virtual QVariant data(int column, int role) const;
213
214   IrcUserItem *findIrcUser(IrcUser *ircUser);
215   void addUsers(const QList<IrcUser *> &ircUser);
216   bool removeUser(IrcUser *ircUser);
217
218   static int categoryFromModes(const QString &modes);
219
220 private:
221   int _category;
222
223   static const QList<QChar> categories;
224 };
225
226 /*****************************************
227 *  Irc User Items
228 *****************************************/
229 class IrcUserItem : public PropertyMapItem {
230   Q_OBJECT
231   Q_PROPERTY(QString nickName READ nickName)
232     
233 public:
234   IrcUserItem(IrcUser *ircUser, AbstractTreeItem *parent);
235
236   inline QString nickName() const { return _ircUser ? _ircUser->nick() : QString(); }
237   inline bool isActive() const { return _ircUser ? !_ircUser->isAway() : false; }
238
239   inline IrcUser *ircUser() { return _ircUser; }
240   virtual QVariant data(int column, int role) const;
241   virtual QString toolTip(int column) const;
242
243 private slots:
244   inline void ircUserDestroyed() { parent()->removeChild(this); }
245
246 private:
247   QPointer<IrcUser> _ircUser;
248 };
249
250
251 /*****************************************
252  * NetworkModel
253  *****************************************/
254 class NetworkModel : public TreeModel {
255   Q_OBJECT
256
257 public:
258   enum myRoles {
259     BufferTypeRole = TreeModel::UserRole,
260     ItemActiveRole,
261     BufferActivityRole,
262     BufferIdRole,
263     NetworkIdRole,
264     BufferInfoRole,
265     ItemTypeRole,
266     UserAwayRole
267   };
268
269   enum itemType {
270     NetworkItemType = 0x01,
271     BufferItemType = 0x02,
272     UserCategoryItemType = 0x04,
273     IrcUserItemType = 0x08
274   };
275   Q_DECLARE_FLAGS(itemTypes, itemType);
276
277   NetworkModel(QObject *parent = 0);
278   static QList<QVariant> defaultHeader();
279
280   static bool mimeContainsBufferList(const QMimeData *mimeData);
281   static QList< QPair<NetworkId, BufferId> > mimeDataToBufferList(const QMimeData *mimeData);
282
283   virtual QStringList mimeTypes() const;
284   virtual QMimeData *mimeData(const QModelIndexList &) const;
285   virtual bool dropMimeData(const QMimeData *, Qt::DropAction, int, int, const QModelIndex &);
286
287   void attachNetwork(Network *network);
288
289   bool isBufferIndex(const QModelIndex &) const;
290   //Buffer *getBufferByIndex(const QModelIndex &) const;
291   QModelIndex networkIndex(NetworkId networkId);
292   QModelIndex bufferIndex(BufferId bufferId);
293
294   const Network *networkByIndex(const QModelIndex &index) const;
295
296   Buffer::ActivityLevel bufferActivity(const BufferInfo &buffer) const;
297
298   QString bufferName(BufferId bufferId);
299   NetworkId networkId(BufferId bufferId);
300   QString networkName(BufferId bufferId);
301   BufferInfo::Type bufferType(BufferId bufferId);
302
303 public slots:
304   void bufferUpdated(BufferInfo bufferInfo);
305   void removeBuffer(BufferId bufferId);
306   void setBufferActivity(const BufferInfo &buffer, Buffer::ActivityLevel activity);
307   void networkRemoved(const NetworkId &networkId);
308
309 private slots:
310   void checkForRemovedBuffers(const QModelIndex &parent, int start, int end);
311   void checkForNewBuffers(const QModelIndex &parent, int start, int end);
312
313 private:
314   int networkRow(NetworkId networkId);
315   NetworkItem *findNetworkItem(NetworkId networkId);
316   NetworkItem *networkItem(NetworkId networkId);
317   BufferItem *findBufferItem(const BufferInfo &bufferInfo);
318   BufferItem *findBufferItem(BufferId bufferId);
319   BufferItem *bufferItem(const BufferInfo &bufferInfo);
320
321   QHash<BufferId, BufferItem *> _bufferItemCache;
322 };
323 Q_DECLARE_OPERATORS_FOR_FLAGS(NetworkModel::itemTypes);
324
325 #endif // NETWORKMODEL_H