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