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