79d6e9ddcecd879ebba9798c3a7f0bf10ae946c2
[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 #include "networkmodel.h"
27
28 MessageFilter::MessageFilter(QAbstractItemModel *source, QObject *parent)
29   : QSortFilterProxyModel(parent),
30     _messageTypeFilter(0)
31 {
32   init();
33   setSourceModel(source);
34 }
35
36 MessageFilter::MessageFilter(MessageModel *source, const QList<BufferId> &buffers, QObject *parent)
37   : QSortFilterProxyModel(parent),
38     _validBuffers(buffers.toSet()),
39     _messageTypeFilter(0)
40 {
41   init();
42   setSourceModel(source);
43 }
44
45 void MessageFilter::init() {
46   BufferSettings defaultSettings;
47   _messageTypeFilter = defaultSettings.messageFilter();
48   defaultSettings.notify("MessageTypeFilter", this, SLOT(messageTypeFilterChanged()));
49
50   BufferSettings mySettings(idString());
51   if(mySettings.hasFilter())
52     _messageTypeFilter = mySettings.messageFilter();
53   mySettings.notify("MessageTypeFilter", this, SLOT(messageTypeFilterChanged()));
54 }
55
56 void MessageFilter::messageTypeFilterChanged() {
57   int newFilter;
58   BufferSettings defaultSettings;
59   newFilter = BufferSettings().messageFilter();
60
61   BufferSettings mySettings(idString());
62   if(mySettings.hasFilter())
63     newFilter = mySettings.messageFilter();
64
65   if(_messageTypeFilter != newFilter) {
66     _messageTypeFilter = newFilter;
67     _filteredQuitMsgs.clear();
68     invalidateFilter();
69   }
70 }
71
72 QString MessageFilter::idString() const {
73   if(_validBuffers.isEmpty())
74     return "*";
75
76   QList<BufferId> bufferIds = _validBuffers.toList();;
77   qSort(bufferIds);
78   
79   QStringList bufferIdStrings;
80   foreach(BufferId id, bufferIds)
81     bufferIdStrings << QString::number(id.toInt());
82
83   return bufferIdStrings.join("|");
84 }
85
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();
90
91   // apply message type filter
92   if(_messageTypeFilter & messageType)
93     return false;
94
95   if(_validBuffers.isEmpty())
96     return true;
97
98   BufferId id = sourceModel()->data(sourceIdx, MessageModel::BufferIdRole).value<BufferId>();
99   if(!id.isValid()) {
100     return true;
101   }
102
103   if(_validBuffers.contains(id)) {
104     return true;
105   } else {
106     // show Quit messages in Query buffers:
107     if(bufferType() != BufferInfo::QueryBuffer)
108       return false;
109     if(!(messageType & Message::Quit))
110       return false;
111     if(networkId() != Client::networkModel()->networkId(id))
112       return false;
113
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())
117       return false;
118
119     if(_filteredQuitMsgs.contains(quiter, messageTimestamp))
120       return false;
121
122     MessageFilter *that = const_cast<MessageFilter *>(this);
123     that->_filteredQuitMsgs.insert(quiter,  messageTimestamp);
124     return true;
125   }
126 }
127
128 void MessageFilter::requestBacklog() {
129   QSet<BufferId>::const_iterator bufferIdIter = _validBuffers.constBegin();
130   while(bufferIdIter != _validBuffers.constEnd()) {
131     Client::messageModel()->requestBacklog(*bufferIdIter);
132     bufferIdIter++;
133   }
134 }