56fcc9189169b2d3db418dee8e0ee41e0c49dfdf
[quassel.git] / src / client / networkmodel.h
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by the Quassel IRC Team                         *
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 #include <QItemSelectionModel>
32
33 class BufferInfo;
34
35 #include "selectionmodelsynchronizer.h"
36 #include "modelpropertymapper.h"
37 class MappedSelectionModel;
38 class QAbstractItemView;
39 class NetworkInfo;
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(Buffer *, AbstractTreeItem *parent = 0);
54
55   virtual quint64 id() const;
56   virtual QVariant data(int column, int role) const;
57
58   void attachIrcChannel(IrcChannel *ircChannel);
59
60   QString bufferName() const;
61   QString topic() const;
62   int nickCount() const;
63
64   
65   Buffer *buffer() const { return buf; }
66   void setActivity(const Buffer::ActivityLevel &);
67
68 public slots:
69   void setTopic(const QString &topic);
70   void join(IrcUser *ircUser);
71   void part(IrcUser *ircUser);
72   
73 private:
74   QColor foreground(int column) const;
75
76   Buffer *buf;
77   Buffer::ActivityLevel activity;
78
79   QPointer<IrcChannel> _ircChannel;
80 };
81
82 /*****************************************
83  *  Network Items
84  *****************************************/
85 class NetworkItem : public PropertyMapItem {
86   Q_OBJECT
87   Q_PROPERTY(QString networkName READ networkName)
88   Q_PROPERTY(QString currentServer READ currentServer)
89   Q_PROPERTY(int nickCount READ nickCount)
90     
91 public:
92   NetworkItem(const uint &netid, const QString &, AbstractTreeItem *parent = 0);
93
94   virtual QVariant data(int column, int row) const;
95   virtual quint64 id() const;
96
97   QString networkName() const;
98   QString currentServer() const;
99   int nickCount() const;
100   
101 public slots:
102   void setNetworkName(const QString &networkName);
103   void setCurrentServer(const QString &serverName);
104
105   void attachNetworkInfo(NetworkInfo *networkInfo);
106   void attachIrcChannel(const QString &channelName);
107   
108 private:
109   uint _networkId;
110   QString _networkName;
111
112   QPointer<NetworkInfo> _networkInfo;
113 };
114
115 /*****************************************
116 *  Irc User Items
117 *****************************************/
118 class IrcUserItem : public PropertyMapItem {
119   Q_OBJECT
120   Q_PROPERTY(QString nickName READ nickName)
121     
122 public:
123   IrcUserItem(IrcUser *ircUser, AbstractTreeItem *parent);
124
125   QString nickName();
126
127 private slots:
128   void setNick(QString newNick);
129   void ircUserDestroyed();
130
131 private:
132   IrcUser *_ircUser;
133 };
134
135
136 /*****************************************
137  * NetworkModel
138  *****************************************/
139 class NetworkModel : public TreeModel {
140   Q_OBJECT
141
142 public:
143   enum myRoles {
144     BufferTypeRole = Qt::UserRole,
145     BufferActiveRole,
146     BufferUidRole,
147     NetworkIdRole,
148     ItemTypeRole
149   };
150
151   enum itemTypes {
152     AbstractItemType,
153     SimpleItemType,
154     NetworkItemType,
155     BufferItemType,
156     NickItemType
157   };
158     
159   NetworkModel(QObject *parent = 0);
160   static QList<QVariant> defaultHeader();
161
162   inline SelectionModelSynchronizer *selectionModelSynchronizer() { return _selectionModelSynchronizer; }
163   inline ModelPropertyMapper *propertyMapper() { return _propertyMapper; }
164
165   void synchronizeSelectionModel(MappedSelectionModel *selectionModel);
166   void synchronizeView(QAbstractItemView *view);
167   void mapProperty(int column, int role, QObject *target, const QByteArray &property);
168
169   static bool mimeContainsBufferList(const QMimeData *mimeData);
170   static QList< QPair<uint, uint> > mimeDataToBufferList(const QMimeData *mimeData);
171
172   virtual QStringList mimeTypes() const;
173   virtual QMimeData *mimeData(const QModelIndexList &) const;
174   virtual bool dropMimeData(const QMimeData *, Qt::DropAction, int, int, const QModelIndex &);
175
176   void attachNetworkInfo(NetworkInfo *networkInfo);
177                                                   
178 public slots:
179   void bufferUpdated(Buffer *);
180   void setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
181   void selectBuffer(Buffer *buffer);
182   void bufferActivity(Buffer::ActivityLevel, Buffer *buffer);
183
184 signals:
185   void bufferSelected(Buffer *);
186   void selectionChanged(const QModelIndex &);
187
188 private:
189   bool isBufferIndex(const QModelIndex &) const;
190   Buffer *getBufferByIndex(const QModelIndex &) const;
191
192   QModelIndex networkIndex(uint networkId);
193   NetworkItem *network(uint networkId);
194   NetworkItem *newNetwork(uint networkId, const QString &networkName);
195   
196   QModelIndex bufferIndex(BufferInfo bufferInfo);
197   BufferItem *buffer(BufferInfo bufferInfo);
198   BufferItem *newBuffer(BufferInfo bufferInfo);
199
200   QPointer<SelectionModelSynchronizer> _selectionModelSynchronizer;
201   QPointer<ModelPropertyMapper> _propertyMapper;
202   Buffer *currentBuffer;
203 };
204
205 #endif // NETWORKMODEL_H