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"
26 #include "networkmodel.h"
28 MessageFilter::MessageFilter(QAbstractItemModel *source, QObject *parent)
29 : QSortFilterProxyModel(parent),
33 setSourceModel(source);
36 MessageFilter::MessageFilter(MessageModel *source, const QList<BufferId> &buffers, QObject *parent)
37 : QSortFilterProxyModel(parent),
38 _validBuffers(buffers.toSet()),
42 setSourceModel(source);
45 void MessageFilter::init() {
46 BufferSettings defaultSettings;
47 _messageTypeFilter = defaultSettings.messageFilter();
48 defaultSettings.notify("MessageTypeFilter", this, SLOT(messageTypeFilterChanged()));
50 BufferSettings mySettings(idString());
51 if(mySettings.hasFilter())
52 _messageTypeFilter = mySettings.messageFilter();
53 mySettings.notify("MessageTypeFilter", this, SLOT(messageTypeFilterChanged()));
56 void MessageFilter::messageTypeFilterChanged() {
58 BufferSettings defaultSettings;
59 newFilter = BufferSettings().messageFilter();
61 BufferSettings mySettings(idString());
62 if(mySettings.hasFilter())
63 newFilter = mySettings.messageFilter();
65 if(_messageTypeFilter != newFilter) {
66 _messageTypeFilter = newFilter;
67 _filteredQuitMsgs.clear();
72 QString MessageFilter::idString() const {
73 if(_validBuffers.isEmpty())
76 QList<BufferId> bufferIds = _validBuffers.toList();;
79 QStringList bufferIdStrings;
80 foreach(BufferId id, bufferIds)
81 bufferIdStrings << QString::number(id.toInt());
83 return bufferIdStrings.join("|");
86 bool MessageFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const {
87 Q_UNUSED(sourceParent);
88 QModelIndex sourceIdx = sourceModel()->index(sourceRow, 2);
89 Message::Type messageType = (Message::Type)sourceModel()->data(sourceIdx, MessageModel::TypeRole).toInt();
91 // apply message type filter
92 if(_messageTypeFilter & messageType)
95 if(_validBuffers.isEmpty())
98 BufferId id = sourceModel()->data(sourceIdx, MessageModel::BufferIdRole).value<BufferId>();
103 if(_validBuffers.contains(id)) {
106 // show Quit messages in Query buffers:
107 if(bufferType() != BufferInfo::QueryBuffer)
109 if(!(messageType & Message::Quit))
111 if(networkId() != Client::networkModel()->networkId(id))
114 uint messageTimestamp = sourceModel()->data(sourceIdx, MessageModel::TimestampRole).value<QDateTime>().toTime_t();
115 QString quiter = sourceModel()->data(sourceIdx, Qt::DisplayRole).toString().section(' ', 0, 0, QString::SectionSkipEmpty).toLower();
116 if(quiter != bufferName().toLower())
119 if(_filteredQuitMsgs.contains(quiter, messageTimestamp))
122 MessageFilter *that = const_cast<MessageFilter *>(this);
123 that->_filteredQuitMsgs.insert(quiter, messageTimestamp);
128 void MessageFilter::requestBacklog() {
129 QSet<BufferId>::const_iterator bufferIdIter = _validBuffers.constBegin();
130 while(bufferIdIter != _validBuffers.constEnd()) {
131 Client::messageModel()->requestBacklog(*bufferIdIter);