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