f2e118e194fc5ca864f239c51ac93ccaa733d979
[quassel.git] / src / client / buffertreemodel.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 _BUFFERTREEMODEL_H_
22 #define _BUFFERTREEMODEL_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 BufferTreeItem : 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   BufferTreeItem(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 NetworkTreeItem : 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   NetworkTreeItem(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  * BufferTreeModel
117  *****************************************/
118 class BufferTreeModel : public TreeModel {
119   Q_OBJECT
120
121 public:
122   enum myRoles {
123     BufferTypeRole = Qt::UserRole,
124     BufferActiveRole,
125     BufferUidRole,
126     NetworkIdRole,
127     ItemTypeRole
128   };
129
130   enum itemTypes {
131     AbstractItem,
132     SimpleItem,
133     NetworkItem,
134     BufferItem,
135     NickItem
136   };
137     
138   BufferTreeModel(QObject *parent = 0);
139   static QList<QVariant> defaultHeader();
140
141   inline SelectionModelSynchronizer *selectionModelSynchronizer() { return _selectionModelSynchronizer; }
142   inline ModelPropertyMapper *propertyMapper() { return _propertyMapper; }
143
144   void synchronizeSelectionModel(MappedSelectionModel *selectionModel);
145   void synchronizeView(QAbstractItemView *view);
146   void mapProperty(int column, int role, QObject *target, const QByteArray &property);
147
148   static bool mimeContainsBufferList(const QMimeData *mimeData);
149   static QList< QPair<uint, uint> > mimeDataToBufferList(const QMimeData *mimeData);
150
151   virtual QStringList mimeTypes() const;
152   virtual QMimeData *mimeData(const QModelIndexList &) const;
153   virtual bool dropMimeData(const QMimeData *, Qt::DropAction, int, int, const QModelIndex &);
154
155   void attachNetworkInfo(NetworkInfo *networkInfo);
156                                                   
157 public slots:
158   void bufferUpdated(Buffer *);
159   void setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
160   void selectBuffer(Buffer *buffer);
161   void bufferActivity(Buffer::ActivityLevel, Buffer *buffer);
162
163 signals:
164   void bufferSelected(Buffer *);
165   void selectionChanged(const QModelIndex &);
166
167 private:
168   bool isBufferIndex(const QModelIndex &) const;
169   Buffer *getBufferByIndex(const QModelIndex &) const;
170
171   QModelIndex networkIndex(uint networkId);
172   NetworkTreeItem *network(uint networkId);
173   NetworkTreeItem *newNetwork(uint networkId, const QString &networkName);
174   
175   QModelIndex bufferIndex(BufferInfo bufferInfo);
176   BufferTreeItem *buffer(BufferInfo bufferInfo);
177   BufferTreeItem *newBuffer(BufferInfo bufferInfo);
178
179   QPointer<SelectionModelSynchronizer> _selectionModelSynchronizer;
180   QPointer<ModelPropertyMapper> _propertyMapper;
181   Buffer *currentBuffer;
182 };
183
184 #endif