Introduce IgnoreList backend
[quassel.git] / src / qtui / chatmonitorfilter.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-09 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 "chatmonitorfilter.h"
22
23 #include "client.h"
24 #include "chatlinemodel.h"
25 #include "networkmodel.h"
26 #include "chatviewsettings.h"
27 #include "clientignorelistmanager.h"
28
29 ChatMonitorFilter::ChatMonitorFilter(MessageModel *model, QObject *parent)
30   : MessageFilter(model, parent)
31 {
32   ChatViewSettings viewSettings(idString());
33   _showFields = viewSettings.value("ShowFields", AllFields).toInt();
34   _showOwnMessages = viewSettings.value("ShowOwnMsgs", true).toBool();
35   viewSettings.notify("ShowFields", this, SLOT(showFieldsSettingChanged(const QVariant &)));
36   viewSettings.notify("ShowOwnMsgs", this, SLOT(showOwnMessagesSettingChanged(const QVariant &)));
37
38   // ChatMonitorSettingsPage
39   QString showHighlightsSettingsId = "ShowHighlights";
40   QString operationModeSettingsId = "OperationMode";
41   QString buffersSettingsId = "Buffers";
42
43   _showHighlights = viewSettings.value(showHighlightsSettingsId, false).toBool();
44   _operationMode = viewSettings.value(operationModeSettingsId, 0).toInt();
45   // read configured list of buffers to monitor/ignore
46   foreach(QVariant v, viewSettings.value(buffersSettingsId, QVariant()).toList())
47     _bufferIds << v.value<BufferId>();
48
49   viewSettings.notify(showHighlightsSettingsId, this, SLOT(showHighlightsSettingChanged(const QVariant &)));
50   viewSettings.notify(operationModeSettingsId, this, SLOT(operationModeSettingChanged(const QVariant &)));
51   viewSettings.notify(buffersSettingsId, this, SLOT(buffersSettingChanged(const QVariant &)));
52 }
53
54 bool ChatMonitorFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const {
55   Q_UNUSED(sourceParent)
56
57   QModelIndex source_index = sourceModel()->index(sourceRow, 0);
58
59   Message::Flags flags = (Message::Flags)source_index.data(MessageModel::FlagsRole).toInt();
60   if(flags & Message::Backlog || (!_showOwnMessages && flags & Message::Self))
61     return false;
62
63   Message::Type type = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
64   if(!(type & (Message::Plain | Message::Notice | Message::Action)))
65     return false;
66
67   // ChatMonitorSettingsPage
68   if(_operationMode == ChatViewSettings::OptOut
69     && !(_showHighlights && flags & Message::Highlight)
70     &&  _bufferIds.contains(source_index.data(MessageModel::BufferIdRole).value<BufferId>()))
71     return false;
72   if(_operationMode == ChatViewSettings::OptIn
73     && !(_showHighlights && flags & Message::Highlight)
74     && !_bufferIds.contains(source_index.data(MessageModel::BufferIdRole).value<BufferId>()))
75     return false;
76
77   // ignorelist handling
78   const MessageModelItem *item = const_cast<const MessageModel*>(static_cast<MessageModel*>(sourceModel()))->messageItemAt(sourceRow);
79   // only match if message is not flagged as server msg
80   if(!(item->message().flags() & Message::ServerMsg) &&
81      Client::ignoreListManager()->match(item->message(), Client::networkModel()->networkName(item->bufferId())))
82       return false;
83   return true;
84 }
85
86 // override this to inject display of network and channel
87 QVariant ChatMonitorFilter::data(const QModelIndex &index, int role) const {
88   if(index.column() != ChatLineModel::SenderColumn || role != ChatLineModel::DisplayRole)
89     return MessageFilter::data(index, role);
90
91   BufferId bufid = data(index, ChatLineModel::BufferIdRole).value<BufferId>();
92   if(!bufid.isValid()) {
93     qDebug() << "ChatMonitorFilter::data(): chatline belongs to an invalid buffer!";
94     return QVariant();
95   }
96
97   QModelIndex source_index = mapToSource(index);
98
99   QStringList fields;
100   if(_showFields & NetworkField) {
101     fields << Client::networkModel()->networkName(bufid);
102   }
103   if(_showFields & BufferField) {
104     fields << Client::networkModel()->bufferName(bufid);
105   }
106
107   Message::Type messageType = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
108   if(messageType & (Message::Plain | Message::Notice)) {
109     QString sender = MessageFilter::data(index, ChatLineModel::EditRole).toString();
110     fields << sender;
111   }
112   return QString("<%1>").arg(fields.join(":"));
113 }
114
115 void ChatMonitorFilter::addShowField(int field) {
116   if(_showFields & field)
117     return;
118
119   ChatViewSettings(idString()).setValue("ShowFields", _showFields | field);
120 }
121
122 void ChatMonitorFilter::removeShowField(int field) {
123   if(!(_showFields & field))
124     return;
125
126   ChatViewSettings(idString()).setValue("ShowFields", _showFields ^ field);
127 }
128
129 void ChatMonitorFilter::setShowOwnMessages(bool show) {
130   if(_showOwnMessages == show)
131     return;
132
133   ChatViewSettings(idString()).setValue("ShowOwnMsgs", show);
134 }
135
136 void ChatMonitorFilter::showFieldsSettingChanged(const QVariant &newValue) {
137   int newFields = newValue.toInt();
138   if(_showFields == newFields)
139     return;
140
141   _showFields = newFields;
142
143   int rows = rowCount();
144   if(rows == 0)
145     return;
146
147   emit dataChanged(index(0, ChatLineModel::SenderColumn), index(rows - 1, ChatLineModel::SenderColumn));
148 }
149
150 void ChatMonitorFilter::showOwnMessagesSettingChanged(const QVariant &newValue) {
151   _showOwnMessages = newValue.toBool();
152 }
153
154 void ChatMonitorFilter::showHighlightsSettingChanged(const QVariant &newValue) {
155   _showHighlights = newValue.toBool();
156 }
157
158 void ChatMonitorFilter::operationModeSettingChanged(const QVariant &newValue) {
159   _operationMode = newValue.toInt();
160 }
161
162 void ChatMonitorFilter::buffersSettingChanged(const QVariant &newValue) {
163   _bufferIds.clear();
164   foreach (QVariant v, newValue.toList()) {
165     _bufferIds << v.value<BufferId>();
166   }
167   invalidate();
168 }