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