1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "messagefilter.h"
23 #include "buffersettings.h"
25 #include "messagemodel.h"
27 MessageFilter::MessageFilter(QAbstractItemModel *source, QObject *parent)
28 : QSortFilterProxyModel(parent),
32 setSourceModel(source);
35 MessageFilter::MessageFilter(MessageModel *source, const QList<BufferId> &buffers, QObject *parent)
36 : QSortFilterProxyModel(parent),
37 _validBuffers(buffers.toSet()),
41 setSourceModel(source);
44 void MessageFilter::init() {
45 BufferSettings defaultSettings;
46 _messageTypeFilter = defaultSettings.messageFilter();
47 defaultSettings.notify("MessageTypeFilter", this, SLOT(messageTypeFilterChanged()));
49 BufferSettings mySettings(idString());
50 if(mySettings.hasFilter())
51 _messageTypeFilter = mySettings.messageFilter();
52 mySettings.notify("MessageTypeFilter", this, SLOT(messageTypeFilterChanged()));
55 void MessageFilter::messageTypeFilterChanged() {
57 BufferSettings defaultSettings;
58 newFilter = BufferSettings().messageFilter();
60 BufferSettings mySettings(idString());
61 if(mySettings.hasFilter())
62 newFilter = mySettings.messageFilter();
64 if(_messageTypeFilter != newFilter) {
65 _messageTypeFilter = newFilter;
70 QString MessageFilter::idString() const {
71 if(_validBuffers.isEmpty())
74 QList<BufferId> bufferIds = _validBuffers.toList();;
77 QStringList bufferIdStrings;
78 foreach(BufferId id, bufferIds)
79 bufferIdStrings << QString::number(id.toInt());
81 return bufferIdStrings.join("|");
84 bool MessageFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const {
85 Q_UNUSED(sourceParent);
86 QModelIndex sourceIdx = sourceModel()->index(sourceRow, 0);
87 if(_messageTypeFilter & sourceModel()->data(sourceIdx, MessageModel::TypeRole).toInt())
90 if(_validBuffers.isEmpty())
93 BufferId id = sourceModel()->data(sourceIdx, MessageModel::BufferIdRole).value<BufferId>();
97 return _validBuffers.contains(id);
100 void MessageFilter::requestBacklog() {
101 QSet<BufferId>::const_iterator bufferIdIter = _validBuffers.constBegin();
102 while(bufferIdIter != _validBuffers.constEnd()) {
103 Client::messageModel()->requestBacklog(*bufferIdIter);