65d898f4a33a4dd7de0f5b9fc761b727e56c3c8d
[quassel.git] / src / client / messagefilter.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 "messagefilter.h"
22
23 #include "buffersettings.h"
24 #include "client.h"
25 #include "messagemodel.h"
26
27 MessageFilter::MessageFilter(QAbstractItemModel *source, QObject *parent)
28   : QSortFilterProxyModel(parent),
29     _messageTypeFilter(0)
30 {
31   init();
32   setSourceModel(source);
33 }
34
35 MessageFilter::MessageFilter(MessageModel *source, const QList<BufferId> &buffers, QObject *parent)
36   : QSortFilterProxyModel(parent),
37     _validBuffers(buffers.toSet()),
38     _messageTypeFilter(0)
39 {
40   init();
41   setSourceModel(source);
42 }
43
44 void MessageFilter::init() {
45   BufferSettings defaultSettings;
46   _messageTypeFilter = defaultSettings.messageFilter();
47   defaultSettings.notify("MessageTypeFilter", this, SLOT(messageTypeFilterChanged()));
48
49   BufferSettings mySettings(idString());
50   if(mySettings.hasFilter())
51     _messageTypeFilter = mySettings.messageFilter();
52   mySettings.notify("MessageTypeFilter", this, SLOT(messageTypeFilterChanged()));
53 }
54
55 void MessageFilter::messageTypeFilterChanged() {
56   int newFilter;
57   BufferSettings defaultSettings;
58   newFilter = BufferSettings().messageFilter();
59
60   BufferSettings mySettings(idString());
61   if(mySettings.hasFilter())
62     newFilter = mySettings.messageFilter();
63
64   if(_messageTypeFilter != newFilter) {
65     _messageTypeFilter = newFilter;
66     invalidateFilter();
67   }
68 }
69
70 QString MessageFilter::idString() const {
71   if(_validBuffers.isEmpty())
72     return "*";
73
74   QList<BufferId> bufferIds = _validBuffers.toList();;
75   qSort(bufferIds);
76   
77   QStringList bufferIdStrings;
78   foreach(BufferId id, bufferIds)
79     bufferIdStrings << QString::number(id.toInt());
80
81   return bufferIdStrings.join("|");
82 }
83
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())
88     return false;
89
90   if(_validBuffers.isEmpty())
91     return true;
92
93   BufferId id = sourceModel()->data(sourceIdx, MessageModel::BufferIdRole).value<BufferId>();
94   if(!id.isValid()) {
95     return true;
96   }
97   return _validBuffers.contains(id);
98 }
99
100 void MessageFilter::requestBacklog() {
101   QSet<BufferId>::const_iterator bufferIdIter = _validBuffers.constBegin();
102   while(bufferIdIter != _validBuffers.constEnd()) {
103     Client::messageModel()->requestBacklog(*bufferIdIter);
104     bufferIdIter++;
105   }
106 }