9359ad39d731cfb8f8420500439e2f3f3eeec6eb
[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 "ircuser.h"
37
38 class MappedSelectionModel;
39 class QAbstractItemView;
40 class Network;
41 class IrcChannel;
42
43 /*****************************************
44  *  Network Items
45  *****************************************/
46 class NetworkItem : public PropertyMapItem {
47   Q_OBJECT
48   Q_PROPERTY(QString networkName READ networkName)
49   Q_PROPERTY(QString currentServer READ currentServer)
50   Q_PROPERTY(int nickCount READ nickCount)
51     
52 public:
53   NetworkItem(const NetworkId &netid, AbstractTreeItem *parent = 0);
54
55   virtual quint64 id() const;
56   virtual QVariant data(int column, int row) const;
57
58   bool isActive() const;
59
60   inline const NetworkId &networkId() const { return _networkId; }
61   QString networkName() const;
62   QString currentServer() const;
63   int nickCount() const;
64
65   virtual QString toolTip(int column) const;
66
67 public slots:
68   void setNetworkName(const QString &networkName);
69   void setCurrentServer(const QString &serverName);
70
71   void attachNetwork(Network *network);
72   void attachIrcChannel(const QString &channelName);
73
74 private:
75   NetworkId _networkId;
76
77   QPointer<Network> _network;
78 };
79
80 /*****************************************
81  *  Fancy Buffer Items
82  *****************************************/
83 class BufferItem : public PropertyMapItem {
84   Q_OBJECT
85   Q_PROPERTY(QString bufferName READ bufferName WRITE setBufferName)
86   Q_PROPERTY(QString topic READ topic)
87   Q_PROPERTY(int nickCount READ nickCount)
88
89 public:
90   BufferItem(BufferInfo bufferInfo, AbstractTreeItem *parent = 0);
91
92   inline const BufferInfo &bufferInfo() const { return _bufferInfo; }
93   virtual quint64 id() const;
94   virtual QVariant data(int column, int role) const;
95   virtual bool setData(int column, const QVariant &value, int role);
96
97   void attachIrcChannel(IrcChannel *ircChannel);
98
99   QString bufferName() const;
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   QString topic() const;
105   int nickCount() const;
106
107   // bool isStatusBuffer() const;
108
109   bool isActive() const;
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   void setTopic(const QString &topic);
123   void join(const QList<IrcUser *> &ircUsers);
124   void part(IrcUser *ircUser);
125
126   void addUserToCategory(IrcUser *ircUser);
127   void addUsersToCategory(const QList<IrcUser *> &ircUser);
128   void removeUserFromCategory(IrcUser *ircUser);
129   void userModeChanged(IrcUser *ircUser);
130
131 private slots:
132   void ircChannelDestroyed();
133   void ircUserDestroyed();
134
135 private:
136   BufferInfo _bufferInfo;
137   QString _bufferName;
138   Buffer::ActivityLevel _activity;
139
140   IrcChannel *_ircChannel;
141 };
142
143 /*****************************************
144 *  User Category Items (like @vh etc.)
145 *****************************************/
146 class UserCategoryItem : public PropertyMapItem {
147   Q_OBJECT
148   Q_PROPERTY(QString categoryName READ categoryName)
149     
150 public:
151   UserCategoryItem(int category, AbstractTreeItem *parent);
152
153   QString categoryName() const;
154   virtual quint64 id() const;
155   virtual QVariant data(int column, int role) const;
156   
157   void addUsers(const QList<IrcUser *> &ircUser);
158   bool removeUser(IrcUser *ircUser);
159
160   static int categoryFromModes(const QString &modes);
161
162 private:
163   int _category;
164
165   static const QList<QChar> categories;
166 };
167
168 /*****************************************
169 *  Irc User Items
170 *****************************************/
171 class IrcUserItem : public PropertyMapItem {
172   Q_OBJECT
173   Q_PROPERTY(QString nickName READ nickName)
174     
175 public:
176   IrcUserItem(IrcUser *ircUser, AbstractTreeItem *parent);
177
178   QString nickName() const;
179   bool isActive() const;
180
181   inline IrcUser *ircUser() { return _ircUser; }
182   inline virtual quint64 id() const { return _id; }
183   virtual QVariant data(int column, int role) const;
184   virtual QString toolTip(int column) const;
185
186 private slots:
187   void setNick(QString newNick);
188   void setAway(bool);
189
190 private:
191   QPointer<IrcUser> _ircUser;
192   quint64 _id;
193 };
194
195
196 /*****************************************
197  * NetworkModel
198  *****************************************/
199 class NetworkModel : public TreeModel {
200   Q_OBJECT
201
202 public:
203   enum myRoles {
204     BufferTypeRole = TreeModel::UserRole,
205     ItemActiveRole,
206     BufferActivityRole,
207     BufferIdRole,
208     NetworkIdRole,
209     BufferInfoRole,
210     ItemTypeRole
211   };
212
213   enum itemType {
214     NetworkItemType = 0x01,
215     BufferItemType = 0x02,
216     UserCategoryItemType = 0x04,
217     IrcUserItemType = 0x08
218   };
219   Q_DECLARE_FLAGS(itemTypes, itemType);
220
221   NetworkModel(QObject *parent = 0);
222   static QList<QVariant> defaultHeader();
223
224   static bool mimeContainsBufferList(const QMimeData *mimeData);
225   static QList< QPair<NetworkId, BufferId> > mimeDataToBufferList(const QMimeData *mimeData);
226
227   virtual QStringList mimeTypes() const;
228   virtual QMimeData *mimeData(const QModelIndexList &) const;
229   virtual bool dropMimeData(const QMimeData *, Qt::DropAction, int, int, const QModelIndex &);
230
231   void attachNetwork(Network *network);
232
233   bool isBufferIndex(const QModelIndex &) const;
234   //Buffer *getBufferByIndex(const QModelIndex &) const;
235   QModelIndex networkIndex(NetworkId networkId);
236   QModelIndex bufferIndex(BufferId bufferId);
237
238   const Network *networkByIndex(const QModelIndex &index) const;
239
240   Buffer::ActivityLevel bufferActivity(const BufferInfo &buffer) const;
241
242 public slots:
243   void bufferUpdated(BufferInfo bufferInfo);
244   void removeBuffer(BufferId bufferId);
245   void setBufferActivity(const BufferInfo &buffer, Buffer::ActivityLevel activity);
246   void networkRemoved(const NetworkId &networkId);
247   
248 private:
249   NetworkItem *networkItem(NetworkId networkId);
250   NetworkItem *existsNetworkItem(NetworkId networkId);
251   BufferItem *bufferItem(const BufferInfo &bufferInfo);
252   BufferItem *existsBufferItem(const BufferInfo &bufferInfo);
253
254 };
255 Q_DECLARE_OPERATORS_FOR_FLAGS(NetworkModel::itemTypes);
256
257 #endif // NETWORKMODEL_H