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