959d89a465a49b9340d19a1509ece0279793e34c
[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   IrcUser *ircUser();
195   virtual quint64 id() const;
196   virtual QVariant data(int column, int role) const;
197   virtual QString toolTip(int column) const;
198                                    
199 private slots:
200   void setNick(QString newNick);
201
202 private:
203   IrcUser *_ircUser;
204 };
205
206
207 /*****************************************
208  * NetworkModel
209  *****************************************/
210 class NetworkModel : public TreeModel {
211   Q_OBJECT
212
213 public:
214   enum myRoles {
215     BufferTypeRole = Qt::UserRole,
216     ItemActiveRole,
217     BufferActivityRole,
218     BufferIdRole,
219     NetworkIdRole,
220     BufferInfoRole,
221     ItemTypeRole,
222     LastSeenRole
223   };
224
225   enum itemTypes {
226     NetworkItemType,
227     BufferItemType,
228     UserCategoryItemType,
229     IrcUserItemType
230   };
231     
232   NetworkModel(QObject *parent = 0);
233   static QList<QVariant> defaultHeader();
234
235   static bool mimeContainsBufferList(const QMimeData *mimeData);
236   static QList< QPair<NetworkId, BufferId> > mimeDataToBufferList(const QMimeData *mimeData);
237
238   virtual QStringList mimeTypes() const;
239   virtual QMimeData *mimeData(const QModelIndexList &) const;
240   virtual bool dropMimeData(const QMimeData *, Qt::DropAction, int, int, const QModelIndex &);
241
242   void attachNetwork(Network *network);
243
244   bool isBufferIndex(const QModelIndex &) const;
245   //Buffer *getBufferByIndex(const QModelIndex &) const;
246   QModelIndex bufferIndex(BufferId bufferId);
247
248   const Network *networkByIndex(const QModelIndex &index) const;
249
250 public slots:
251   void bufferUpdated(BufferInfo bufferInfo);
252   void updateBufferActivity(const Message &msg);
253
254 private:
255   QModelIndex networkIndex(NetworkId networkId);
256   NetworkItem *networkItem(NetworkId networkId);
257   NetworkItem *existsNetworkItem(NetworkId networkId);
258   BufferItem *bufferItem(const BufferInfo &bufferInfo);
259   BufferItem *existsBufferItem(const BufferInfo &bufferInfo);
260
261 };
262
263 #endif // NETWORKMODEL_H