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