- Improved the speed of IrcServerHandler (and other BasicHandler
[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 class MappedSelectionModel;
36 class QAbstractItemView;
37 class Network;
38 class IrcChannel;
39 class IrcUser;
40
41 /*****************************************
42  *  Fancy Buffer Items
43  *****************************************/
44 class BufferItem : public PropertyMapItem {
45   Q_OBJECT
46   Q_PROPERTY(QString bufferName READ bufferName)
47   Q_PROPERTY(QString topic READ topic)
48   Q_PROPERTY(int nickCount READ nickCount)
49
50 public:
51   BufferItem(BufferInfo bufferInfo, AbstractTreeItem *parent = 0);
52
53   const BufferInfo &bufferInfo() const;
54   virtual quint64 id() const;
55   virtual QVariant data(int column, int role) const;
56
57   void attachIrcChannel(IrcChannel *ircChannel);
58
59   QString bufferName() const;
60   QString topic() const;
61   int nickCount() const;
62
63   enum Type {
64     StatusType,
65     ChannelType,
66     QueryType
67   };
68   
69   bool isStatusBuffer() const;
70   Type bufferType() const;
71
72   bool isActive() const;
73   
74   enum Activity {
75     NoActivity = 0x00,
76     OtherActivity = 0x01,
77     NewMessage = 0x02,
78     Highlight = 0x40
79   };
80   Q_DECLARE_FLAGS(ActivityLevel, Activity)
81
82   ActivityLevel activity() const;
83   void setActivity(const ActivityLevel &level);
84   void addActivity(const ActivityLevel &level);
85
86 public slots:
87   void setTopic(const QString &topic);
88   void join(IrcUser *ircUser);
89   void part(IrcUser *ircUser);
90
91 private slots:
92   void ircChannelDestroyed();
93   
94 private:
95   BufferInfo _bufferInfo;
96   ActivityLevel _activity;
97   Type _type;
98
99   QPointer<IrcChannel> _ircChannel;
100 };
101 Q_DECLARE_OPERATORS_FOR_FLAGS(BufferItem::ActivityLevel)
102
103 /*****************************************
104  *  Network Items
105  *****************************************/
106 class NetworkItem : public PropertyMapItem {
107   Q_OBJECT
108   Q_PROPERTY(QString networkName READ networkName)
109   Q_PROPERTY(QString currentServer READ currentServer)
110   Q_PROPERTY(int nickCount READ nickCount)
111     
112 public:
113   NetworkItem(const uint &netid, const QString &, AbstractTreeItem *parent = 0);
114
115   virtual QVariant data(int column, int row) const;
116   virtual quint64 id() const;
117
118   bool isActive() const;
119   
120   QString networkName() const;
121   QString currentServer() const;
122   int nickCount() const;
123   
124 public slots:
125   void setNetworkName(const QString &networkName);
126   void setCurrentServer(const QString &serverName);
127
128   void attachNetwork(Network *network);
129   void attachIrcChannel(const QString &channelName);
130   
131 private:
132   uint _networkId;
133   QString _networkName;
134
135   QPointer<Network> _network;
136 };
137
138 /*****************************************
139 *  Irc User Items
140 *****************************************/
141 class IrcUserItem : public PropertyMapItem {
142   Q_OBJECT
143   Q_PROPERTY(QString nickName READ nickName)
144     
145 public:
146   IrcUserItem(IrcUser *ircUser, AbstractTreeItem *parent);
147
148   QString nickName();
149
150 private slots:
151   void setNick(QString newNick);
152   void ircUserDestroyed();
153
154 private:
155   IrcUser *_ircUser;
156 };
157
158
159 /*****************************************
160  * NetworkModel
161  *****************************************/
162 class NetworkModel : public TreeModel {
163   Q_OBJECT
164
165 public:
166   enum myRoles {
167     BufferTypeRole = Qt::UserRole,
168     ItemActiveRole,
169     BufferIdRole,
170     NetworkIdRole,
171     ItemTypeRole
172   };
173
174   enum itemTypes {
175     AbstractItemType,
176     SimpleItemType,
177     NetworkItemType,
178     BufferItemType,
179     NickItemType
180   };
181     
182   NetworkModel(QObject *parent = 0);
183   static QList<QVariant> defaultHeader();
184
185   static bool mimeContainsBufferList(const QMimeData *mimeData);
186   static QList< QPair<uint, uint> > mimeDataToBufferList(const QMimeData *mimeData);
187
188   virtual QStringList mimeTypes() const;
189   virtual QMimeData *mimeData(const QModelIndexList &) const;
190   virtual bool dropMimeData(const QMimeData *, Qt::DropAction, int, int, const QModelIndex &);
191
192   void attachNetwork(Network *network);
193
194   bool isBufferIndex(const QModelIndex &) const;
195   Buffer *getBufferByIndex(const QModelIndex &) const;
196   QModelIndex bufferIndex(BufferId bufferId);
197
198 public slots:
199   void bufferUpdated(BufferInfo bufferInfo);
200   void bufferActivity(BufferItem::ActivityLevel, BufferInfo bufferInfo);
201
202 private:
203   QModelIndex networkIndex(uint networkId);
204   NetworkItem *network(uint networkId);
205   NetworkItem *newNetwork(uint networkId, const QString &networkName);
206   
207   BufferItem *buffer(BufferInfo bufferInfo);
208   BufferItem *newBuffer(BufferInfo bufferInfo);
209
210 };
211
212 #endif // NETWORKMODEL_H