e15cdd8e8bc477ddb77fe4eaa3fce54a31ba2fdf
[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(Buffer *, AbstractTreeItem *parent = 0);
52
53   virtual quint64 id() const;
54   virtual QVariant data(int column, int role) const;
55
56   void attachIrcChannel(IrcChannel *ircChannel);
57
58   QString bufferName() const;
59   QString topic() const;
60   int nickCount() const;
61
62   
63   Buffer *buffer() const { return buf; }
64   void setActivity(const Buffer::ActivityLevel &);
65
66 public slots:
67   void setTopic(const QString &topic);
68   void join(IrcUser *ircUser);
69   void part(IrcUser *ircUser);
70   
71 private:
72   QColor foreground(int column) const;
73
74   Buffer *buf;
75   Buffer::ActivityLevel activity;
76
77   QPointer<IrcChannel> _ircChannel;
78 };
79
80 /*****************************************
81  *  Network Items
82  *****************************************/
83 class NetworkItem : public PropertyMapItem {
84   Q_OBJECT
85   Q_PROPERTY(QString networkName READ networkName)
86   Q_PROPERTY(QString currentServer READ currentServer)
87   Q_PROPERTY(int nickCount READ nickCount)
88     
89 public:
90   NetworkItem(const uint &netid, const QString &, AbstractTreeItem *parent = 0);
91
92   virtual QVariant data(int column, int row) const;
93   virtual quint64 id() const;
94
95   QString networkName() const;
96   QString currentServer() const;
97   int nickCount() const;
98   
99 public slots:
100   void setNetworkName(const QString &networkName);
101   void setCurrentServer(const QString &serverName);
102
103   void attachNetwork(Network *network);
104   void attachIrcChannel(const QString &channelName);
105   
106 private:
107   uint _networkId;
108   QString _networkName;
109
110   QPointer<Network> _network;
111 };
112
113 /*****************************************
114 *  Irc User Items
115 *****************************************/
116 class IrcUserItem : public PropertyMapItem {
117   Q_OBJECT
118   Q_PROPERTY(QString nickName READ nickName)
119     
120 public:
121   IrcUserItem(IrcUser *ircUser, AbstractTreeItem *parent);
122
123   QString nickName();
124
125 private slots:
126   void setNick(QString newNick);
127   void ircUserDestroyed();
128
129 private:
130   IrcUser *_ircUser;
131 };
132
133
134 /*****************************************
135  * NetworkModel
136  *****************************************/
137 class NetworkModel : public TreeModel {
138   Q_OBJECT
139
140 public:
141   enum myRoles {
142     BufferTypeRole = Qt::UserRole,
143     BufferActiveRole,
144     BufferUidRole,
145     NetworkIdRole,
146     ItemTypeRole
147   };
148
149   enum itemTypes {
150     AbstractItemType,
151     SimpleItemType,
152     NetworkItemType,
153     BufferItemType,
154     NickItemType
155   };
156     
157   NetworkModel(QObject *parent = 0);
158   static QList<QVariant> defaultHeader();
159
160   static bool mimeContainsBufferList(const QMimeData *mimeData);
161   static QList< QPair<uint, uint> > mimeDataToBufferList(const QMimeData *mimeData);
162
163   virtual QStringList mimeTypes() const;
164   virtual QMimeData *mimeData(const QModelIndexList &) const;
165   virtual bool dropMimeData(const QMimeData *, Qt::DropAction, int, int, const QModelIndex &);
166
167   void attachNetwork(Network *network);
168
169   bool isBufferIndex(const QModelIndex &) const;
170   Buffer *getBufferByIndex(const QModelIndex &) const;
171   QModelIndex bufferIndex(BufferInfo bufferInfo);
172
173 public slots:
174   void bufferUpdated(Buffer *);
175   void bufferActivity(Buffer::ActivityLevel, Buffer *buffer);
176
177 private:
178   QModelIndex networkIndex(uint networkId);
179   NetworkItem *network(uint networkId);
180   NetworkItem *newNetwork(uint networkId, const QString &networkName);
181   
182   BufferItem *buffer(BufferInfo bufferInfo);
183   BufferItem *newBuffer(BufferInfo bufferInfo);
184
185 };
186
187 #endif // NETWORKMODEL_H