Added BanHandler.
[quassel.git] / src / uisupport / bufferviewfilter.cpp
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 #include "bufferviewfilter.h"
22
23 #include <QColor>
24
25 #include "networkmodel.h"
26
27 /*****************************************
28 * The Filter for the Tree View
29 *****************************************/
30 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, const Modes &filtermode, const QList<NetworkId> &nets)
31   : QSortFilterProxyModel(model),
32     mode(filtermode),
33     networks(QSet<NetworkId>::fromList(nets))
34 {
35   setSourceModel(model);
36   setSortCaseSensitivity(Qt::CaseInsensitive);
37
38   // FIXME
39   // ok the following basically sucks. therfore it's commented out. Justice served.
40   // a better solution would use dataChanged()
41   
42   // I have this feeling that this resulted in a fuckup once... no clue though right now and invalidateFilter isn't a slot -.-
43   //connect(model, SIGNAL(invalidateFilter()), this, SLOT(invalidate()));
44   // connect(model, SIGNAL(invalidateFilter()), this, SLOT(invalidateFilter_()));
45 }
46
47 void BufferViewFilter::invalidateFilter_() {
48   QSortFilterProxyModel::invalidateFilter();
49 }
50
51 Qt::ItemFlags BufferViewFilter::flags(const QModelIndex &index) const {
52   Qt::ItemFlags flags = mapToSource(index).flags();
53   if(mode & FullCustom) {
54     if(index == QModelIndex() || index.parent() == QModelIndex())
55       flags |= Qt::ItemIsDropEnabled;
56   }
57   return flags;
58 }
59
60 bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
61   // drops have to occur in the open field
62   if(parent != QModelIndex())
63     return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
64
65   if(!NetworkModel::mimeContainsBufferList(data))
66     return false;
67
68   QList< QPair<NetworkId, BufferId> > bufferList = NetworkModel::mimeDataToBufferList(data);
69
70   NetworkId netId;
71   BufferId bufferId;
72   for(int i = 0; i < bufferList.count(); i++) {
73     netId = bufferList[i].first;
74     bufferId = bufferList[i].second;
75     if(!networks.contains(netId)) {
76       networks << netId;
77     }
78     addBuffer(bufferId);
79   }
80   return true;
81 }
82
83 void BufferViewFilter::addBuffer(const BufferId &bufferuid) {
84   if(!buffers.contains(bufferuid)) {
85     buffers << bufferuid;
86     invalidateFilter();
87   }
88 }
89
90 void BufferViewFilter::removeBuffer(const QModelIndex &index) {
91   if(!(mode & FullCustom))
92     return; // only custom buffers can be customized... obviously... :)
93   
94   if(index.parent() == QModelIndex())
95     return; // only child elements can be deleted
96
97   bool lastBuffer = (rowCount(index.parent()) == 1);
98   NetworkId netId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
99   BufferId bufferuid = index.data(NetworkModel::BufferIdRole).value<BufferId>();
100
101   if(buffers.contains(bufferuid)) {
102     buffers.remove(bufferuid);
103     
104     if(lastBuffer) {
105       networks.remove(netId);
106       Q_ASSERT(!networks.contains(netId));
107     }
108
109     invalidateFilter();
110   }
111   
112 }
113
114
115 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
116   BufferItem::Type bufferType = (BufferItem::Type) source_bufferIndex.data(NetworkModel::BufferTypeRole).toInt();
117   
118   if((mode & NoChannels) && bufferType == BufferItem::ChannelType)
119     return false;
120   if((mode & NoQueries) && bufferType == BufferItem::QueryType)
121     return false;
122   if((mode & NoServers) && bufferType == BufferItem::StatusType)
123     return false;
124
125 //   bool isActive = source_bufferIndex.data(NetworkModel::BufferActiveRole).toBool();
126 //   if((mode & NoActive) && isActive)
127 //     return false;
128 //   if((mode & NoInactive) && !isActive)
129 //     return false;
130
131   if((mode & FullCustom)) {
132     BufferId bufferuid = source_bufferIndex.data(NetworkModel::BufferIdRole).value<BufferId>();
133     return buffers.contains(bufferuid);
134   }
135     
136   return true;
137 }
138
139 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
140   NetworkId net = source_index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
141   return !((mode & (SomeNets | FullCustom)) && !networks.contains(net));
142 }
143
144 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
145   QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
146   
147   if(!child.isValid()) {
148     qDebug() << "filterAcceptsRow has been called with an invalid Child";
149     return false;
150   }
151
152   if(source_parent == QModelIndex())
153     return filterAcceptNetwork(child);
154   else
155     return filterAcceptBuffer(child);
156 }
157
158 bool BufferViewFilter::lessThan(const QModelIndex &left, const QModelIndex &right) const {
159   int lefttype = left.data(NetworkModel::BufferTypeRole).toInt();
160   int righttype = right.data(NetworkModel::BufferTypeRole).toInt();
161
162   if(lefttype != righttype)
163     return lefttype < righttype;
164   else
165     return QSortFilterProxyModel::lessThan(left, right);
166 }
167
168 QVariant BufferViewFilter::data(const QModelIndex &index, int role) const {
169   if(role == Qt::ForegroundRole)
170     return foreground(index);
171   else
172     return QSortFilterProxyModel::data(index, role);
173 }
174
175 QVariant BufferViewFilter::foreground(const QModelIndex &index) const {
176   if(!index.data(NetworkModel::ItemActiveRole).toBool())
177     return QColor(Qt::gray);
178
179   // FIXME:: show colors depending on activity level
180   return QColor(Qt::black);
181 }