Merge branch 'network-sync'
[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
77 private:
78   NetworkId _networkId;
79
80   QPointer<Network> _network;
81 };
82
83 /*****************************************
84  *  Fancy Buffer Items
85  *****************************************/
86 class BufferItem : public PropertyMapItem {
87   Q_OBJECT
88   Q_PROPERTY(QString bufferName READ bufferName WRITE setBufferName)
89   Q_PROPERTY(QString topic READ topic)
90   Q_PROPERTY(int nickCount READ nickCount)
91
92 public:
93   BufferItem(const BufferInfo &bufferInfo, AbstractTreeItem *parent = 0);
94
95   inline const BufferInfo &bufferInfo() const { return _bufferInfo; }
96   virtual inline quint64 id() const { return qHash(_bufferInfo.bufferId()); }
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 ChannelBufferItem : public BufferItem {
157   Q_OBJECT
158
159 public:
160   ChannelBufferItem(const BufferInfo &bufferInfo, AbstractTreeItem *parent);
161
162   virtual inline bool isActive() const { return (bool)_ircChannel; }
163   virtual QString toolTip(int column) const;
164
165   virtual inline QString topic() const { return (bool)_ircChannel ? _ircChannel->topic() : QString(); }
166   virtual inline int nickCount() const { return (bool)_ircChannel ? _ircChannel->ircUsers().count() : 0; }
167   
168   void attachIrcChannel(IrcChannel *ircChannel);
169
170 public slots:
171   void join(const QList<IrcUser *> &ircUsers);
172   void part(IrcUser *ircUser);
173
174   void addUserToCategory(IrcUser *ircUser);
175   void addUsersToCategory(const QList<IrcUser *> &ircUser);
176   void removeUserFromCategory(IrcUser *ircUser);
177   void userModeChanged(IrcUser *ircUser);
178
179 private slots:
180   void ircChannelDestroyed();
181   void ircUserDestroyed();
182
183 private:
184   IrcChannel *_ircChannel;
185 };
186
187 /*****************************************
188 *  User Category Items (like @vh etc.)
189 *****************************************/
190 class UserCategoryItem : public PropertyMapItem {
191   Q_OBJECT
192   Q_PROPERTY(QString categoryName READ categoryName)
193     
194 public:
195   UserCategoryItem(int category, AbstractTreeItem *parent);
196
197   QString categoryName() const;
198   virtual quint64 id() const;
199   virtual QVariant data(int column, int role) const;
200   
201   void addUsers(const QList<IrcUser *> &ircUser);
202   bool removeUser(IrcUser *ircUser);
203
204   static int categoryFromModes(const QString &modes);
205
206 private:
207   int _category;
208
209   static const QList<QChar> categories;
210 };
211
212 /*****************************************
213 *  Irc User Items
214 *****************************************/
215 class IrcUserItem : public PropertyMapItem {
216   Q_OBJECT
217   Q_PROPERTY(QString nickName READ nickName)
218     
219 public:
220   IrcUserItem(IrcUser *ircUser, AbstractTreeItem *parent);
221
222   QString nickName() const;
223   bool isActive() const;
224
225   inline IrcUser *ircUser() { return _ircUser; }
226   inline virtual quint64 id() const { return _id; }
227   virtual QVariant data(int column, int role) const;
228   virtual QString toolTip(int column) const;
229
230 private slots:
231   void setNick(QString newNick);
232   void setAway(bool);
233
234 private:
235   QPointer<IrcUser> _ircUser;
236   quint64 _id;
237 };
238
239
240 /*****************************************
241  * NetworkModel
242  *****************************************/
243 class NetworkModel : public TreeModel {
244   Q_OBJECT
245
246 public:
247   enum myRoles {
248     BufferTypeRole = TreeModel::UserRole,
249     ItemActiveRole,
250     BufferActivityRole,
251     BufferIdRole,
252     NetworkIdRole,
253     BufferInfoRole,
254     ItemTypeRole
255   };
256
257   enum itemType {
258     NetworkItemType = 0x01,
259     BufferItemType = 0x02,
260     UserCategoryItemType = 0x04,
261     IrcUserItemType = 0x08
262   };
263   Q_DECLARE_FLAGS(itemTypes, itemType);
264
265   NetworkModel(QObject *parent = 0);
266   static QList<QVariant> defaultHeader();
267
268   static bool mimeContainsBufferList(const QMimeData *mimeData);
269   static QList< QPair<NetworkId, BufferId> > mimeDataToBufferList(const QMimeData *mimeData);
270
271   virtual QStringList mimeTypes() const;
272   virtual QMimeData *mimeData(const QModelIndexList &) const;
273   virtual bool dropMimeData(const QMimeData *, Qt::DropAction, int, int, const QModelIndex &);
274
275   void attachNetwork(Network *network);
276
277   bool isBufferIndex(const QModelIndex &) const;
278   //Buffer *getBufferByIndex(const QModelIndex &) const;
279   QModelIndex networkIndex(NetworkId networkId);
280   QModelIndex bufferIndex(BufferId bufferId);
281
282   const Network *networkByIndex(const QModelIndex &index) const;
283
284   Buffer::ActivityLevel bufferActivity(const BufferInfo &buffer) const;
285
286 public slots:
287   void bufferUpdated(BufferInfo bufferInfo);
288   void removeBuffer(BufferId bufferId);
289   void setBufferActivity(const BufferInfo &buffer, Buffer::ActivityLevel activity);
290   void networkRemoved(const NetworkId &networkId);
291   
292 private:
293   NetworkItem *networkItem(NetworkId networkId);
294   NetworkItem *existsNetworkItem(NetworkId networkId);
295   BufferItem *bufferItem(const BufferInfo &bufferInfo);
296   BufferItem *existsBufferItem(const BufferInfo &bufferInfo);
297
298 };
299 Q_DECLARE_OPERATORS_FOR_FLAGS(NetworkModel::itemTypes);
300
301 #endif // NETWORKMODEL_H