41e5f1d76f4da34464a7c0249abf5c5d73e0975b
[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   inline const NetworkId &networkId() const { return _networkId; }
114   virtual QVariant data(int column, int row) const;
115
116   bool isActive() const;
117   
118   QString networkName() const;
119   QString currentServer() const;
120   int nickCount() const;
121   
122 public slots:
123   void setNetworkName(const QString &networkName);
124   void setCurrentServer(const QString &serverName);
125
126   void attachNetwork(Network *network);
127   void attachIrcChannel(const QString &channelName);
128   
129 private:
130   NetworkId _networkId;
131
132   QPointer<Network> _network;
133 };
134
135 /*****************************************
136 *  User Category Items (like @vh etc.)
137 *****************************************/
138 class UserCategoryItem : public PropertyMapItem {
139   Q_OBJECT
140   Q_PROPERTY(QString categoryId READ categoryId)
141     
142 public:
143   UserCategoryItem(int category, AbstractTreeItem *parent);
144
145   QString categoryId();
146   virtual quint64 id() const;
147   virtual QVariant data(int column, int role) const;
148   
149   void addUser(IrcUser *ircUser);
150   bool removeUser(IrcUser *ircUser);
151
152   static int categoryFromModes(const QString &modes);
153
154 private:
155   int _category;
156
157   struct Category {
158     QChar mode;
159     QString displayString;
160     inline Category(QChar mode_, QString displayString_) : mode(mode_), displayString(displayString_) {};
161   };
162
163   static const QList<Category> categories;
164 };
165
166 /*****************************************
167 *  Irc User Items
168 *****************************************/
169 class IrcUserItem : public PropertyMapItem {
170   Q_OBJECT
171   Q_PROPERTY(QString nickName READ nickName)
172     
173 public:
174   IrcUserItem(IrcUser *ircUser, AbstractTreeItem *parent);
175
176   QString nickName() const;
177   bool isActive() const;
178
179   IrcUser *ircUser();
180   virtual quint64 id() const;
181   virtual QVariant data(int column, int role) const;
182   virtual QString toolTip(int column) const;
183                                    
184 private slots:
185   void setNick(QString newNick);
186
187 private:
188   QPointer<IrcUser> _ircUser;
189   quint64 _id;
190 };
191
192
193 /*****************************************
194  * NetworkModel
195  *****************************************/
196 class NetworkModel : public TreeModel {
197   Q_OBJECT
198
199 public:
200   enum myRoles {
201     BufferTypeRole = Qt::UserRole,
202     ItemActiveRole,
203     BufferActivityRole,
204     BufferIdRole,
205     NetworkIdRole,
206     BufferInfoRole,
207     ItemTypeRole
208   };
209
210   enum itemTypes {
211     NetworkItemType,
212     BufferItemType,
213     UserCategoryItemType,
214     IrcUserItemType
215   };
216
217   NetworkModel(QObject *parent = 0);
218   static QList<QVariant> defaultHeader();
219
220   static bool mimeContainsBufferList(const QMimeData *mimeData);
221   static QList< QPair<NetworkId, BufferId> > mimeDataToBufferList(const QMimeData *mimeData);
222
223   virtual QStringList mimeTypes() const;
224   virtual QMimeData *mimeData(const QModelIndexList &) const;
225   virtual bool dropMimeData(const QMimeData *, Qt::DropAction, int, int, const QModelIndex &);
226
227   void attachNetwork(Network *network);
228
229   bool isBufferIndex(const QModelIndex &) const;
230   //Buffer *getBufferByIndex(const QModelIndex &) const;
231   QModelIndex bufferIndex(BufferId bufferId);
232
233   const Network *networkByIndex(const QModelIndex &index) const;
234
235   Buffer::ActivityLevel bufferActivity(const BufferInfo &buffer) const;
236
237 public slots:
238   void bufferUpdated(BufferInfo bufferInfo);
239   void setBufferActivity(const BufferInfo &buffer, Buffer::ActivityLevel activity);
240   void networkRemoved(const NetworkId &networkId);
241   
242 private:
243   QModelIndex networkIndex(NetworkId networkId);
244   NetworkItem *networkItem(NetworkId networkId);
245   NetworkItem *existsNetworkItem(NetworkId networkId);
246   BufferItem *bufferItem(const BufferInfo &bufferInfo);
247   BufferItem *existsBufferItem(const BufferInfo &bufferInfo);
248
249 };
250
251 #endif // NETWORKMODEL_H