Checking in WiP on the MessageModel. More cleanly separated code and compiling of...
[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   inline const BufferInfo &bufferInfo() const { return _bufferInfo; }
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   inline BufferId bufferId() const { return _bufferInfo.bufferId(); }
64   inline BufferInfo::Type bufferType() const { return _bufferInfo.type(); }
65
66   void setBufferName(const QString &name);
67   QString topic() const;
68   int nickCount() const;
69
70   // bool isStatusBuffer() const;
71
72   bool isActive() const;
73
74   inline Buffer::ActivityLevel activityLevel() const { return _activity; }
75   bool setActivityLevel(Buffer::ActivityLevel level);
76   void updateActivityLevel(Buffer::ActivityLevel level);
77
78   void setLastMsgInsert(QDateTime msgDate);
79   bool setLastSeen();
80   QDateTime lastSeen();
81
82   virtual QString toolTip(int column) const;
83
84 public slots:
85   void setTopic(const QString &topic);
86   void join(const QList<IrcUser *> &ircUsers);
87   void part(IrcUser *ircUser);
88
89   void addUserToCategory(IrcUser *ircUser);
90   void addUsersToCategory(const QList<IrcUser *> &ircUser);
91   void removeUserFromCategory(IrcUser *ircUser);
92   void userModeChanged(IrcUser *ircUser);
93
94 private slots:
95   void ircChannelDestroyed();
96   void ircUserDestroyed();
97
98 private:
99   BufferInfo _bufferInfo;
100   QString _bufferName;
101   Buffer::ActivityLevel _activity;
102
103   IrcChannel *_ircChannel;
104 };
105
106
107 /*****************************************
108  *  Network Items
109  *****************************************/
110 class NetworkItem : public PropertyMapItem {
111   Q_OBJECT
112   Q_PROPERTY(QString networkName READ networkName)
113   Q_PROPERTY(QString currentServer READ currentServer)
114   Q_PROPERTY(int nickCount READ nickCount)
115     
116 public:
117   NetworkItem(const NetworkId &netid, AbstractTreeItem *parent = 0);
118
119   virtual quint64 id() const;
120   virtual QVariant data(int column, int row) const;
121
122   bool isActive() const;
123
124   inline const NetworkId &networkId() const { return _networkId; }
125   QString networkName() const;
126   QString currentServer() const;
127   int nickCount() const;
128
129   virtual QString toolTip(int column) const;
130
131 public slots:
132   void setNetworkName(const QString &networkName);
133   void setCurrentServer(const QString &serverName);
134
135   void attachNetwork(Network *network);
136   void attachIrcChannel(const QString &channelName);
137
138 private:
139   NetworkId _networkId;
140
141   QPointer<Network> _network;
142 };
143
144 /*****************************************
145 *  User Category Items (like @vh etc.)
146 *****************************************/
147 class UserCategoryItem : public PropertyMapItem {
148   Q_OBJECT
149   Q_PROPERTY(QString categoryName READ categoryName)
150     
151 public:
152   UserCategoryItem(int category, AbstractTreeItem *parent);
153
154   QString categoryName() const;
155   virtual quint64 id() const;
156   virtual QVariant data(int column, int role) const;
157   
158   void addUsers(const QList<IrcUser *> &ircUser);
159   bool removeUser(IrcUser *ircUser);
160
161   static int categoryFromModes(const QString &modes);
162
163 private:
164   int _category;
165
166   static const QList<QChar> categories;
167 };
168
169 /*****************************************
170 *  Irc User Items
171 *****************************************/
172 class IrcUserItem : public PropertyMapItem {
173   Q_OBJECT
174   Q_PROPERTY(QString nickName READ nickName)
175     
176 public:
177   IrcUserItem(IrcUser *ircUser, AbstractTreeItem *parent);
178
179   QString nickName() const;
180   bool isActive() const;
181
182   IrcUser *ircUser();
183   virtual quint64 id() const;
184   virtual QVariant data(int column, int role) const;
185   virtual QString toolTip(int column) const;
186
187 private slots:
188   void setNick(QString newNick);
189   void setAway(bool);
190
191 private:
192   QPointer<IrcUser> _ircUser;
193   quint64 _id;
194 };
195
196
197 /*****************************************
198  * NetworkModel
199  *****************************************/
200 class NetworkModel : public TreeModel {
201   Q_OBJECT
202
203 public:
204   enum myRoles {
205     BufferTypeRole = TreeModel::UserRole,
206     ItemActiveRole,
207     BufferActivityRole,
208     BufferIdRole,
209     NetworkIdRole,
210     BufferInfoRole,
211     ItemTypeRole
212   };
213
214   enum itemTypes {
215     NetworkItemType,
216     BufferItemType,
217     UserCategoryItemType,
218     IrcUserItemType
219   };
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 bufferIndex(BufferId bufferId);
236
237   const Network *networkByIndex(const QModelIndex &index) const;
238
239   Buffer::ActivityLevel bufferActivity(const BufferInfo &buffer) const;
240
241 public slots:
242   void bufferUpdated(BufferInfo bufferInfo);
243   void removeBuffer(BufferId bufferId);
244   void setBufferActivity(const BufferInfo &buffer, Buffer::ActivityLevel activity);
245   void networkRemoved(const NetworkId &networkId);
246   
247 private:
248   QModelIndex networkIndex(NetworkId networkId);
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
256 #endif // NETWORKMODEL_H