1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "chatmonitorfilter.h"
24 #include "chatlinemodel.h"
25 #include "networkmodel.h"
26 #include "chatviewsettings.h"
27 #include "clientignorelistmanager.h"
29 ChatMonitorFilter::ChatMonitorFilter(MessageModel *model, QObject *parent)
30 : MessageFilter(model, parent)
32 // Global configuration
33 ChatViewSettings defaultSettings;
34 _showSenderBrackets = defaultSettings.showSenderBrackets();
35 defaultSettings.notify("ShowSenderBrackets", this, SLOT(showSenderBracketsSettingChanged(const QVariant &)));
37 // Chat Monitor specific configuration
38 ChatViewSettings viewSettings(idString());
39 _showFields = viewSettings.value("ShowFields", AllFields).toInt();
40 _showOwnMessages = viewSettings.value("ShowOwnMsgs", true).toBool();
41 viewSettings.notify("ShowFields", this, SLOT(showFieldsSettingChanged(const QVariant &)));
42 viewSettings.notify("ShowOwnMsgs", this, SLOT(showOwnMessagesSettingChanged(const QVariant &)));
44 // ChatMonitorSettingsPage
45 QString showHighlightsSettingsId = "ShowHighlights";
46 QString operationModeSettingsId = "OperationMode";
47 QString buffersSettingsId = "Buffers";
48 QString showBacklogSettingsId = "ShowBacklog";
49 QString includeReadSettingsId = "IncludeRead";
50 QString alwaysOwnSettingsId = "AlwaysOwn";
52 _showHighlights = viewSettings.value(showHighlightsSettingsId, false).toBool();
53 _operationMode = viewSettings.value(operationModeSettingsId, 0).toInt();
54 // read configured list of buffers to monitor/ignore
55 foreach(QVariant v, viewSettings.value(buffersSettingsId, QVariant()).toList())
56 _bufferIds << v.value<BufferId>();
57 _showBacklog = viewSettings.value(showBacklogSettingsId, true).toBool();
58 _includeRead = viewSettings.value(includeReadSettingsId, true).toBool();
59 _alwaysOwn = viewSettings.value(alwaysOwnSettingsId, false).toBool();
61 viewSettings.notify(showHighlightsSettingsId, this, SLOT(showHighlightsSettingChanged(const QVariant &)));
62 viewSettings.notify(operationModeSettingsId, this, SLOT(operationModeSettingChanged(const QVariant &)));
63 viewSettings.notify(buffersSettingsId, this, SLOT(buffersSettingChanged(const QVariant &)));
64 viewSettings.notify(showBacklogSettingsId, this, SLOT(showBacklogSettingChanged(const QVariant &)));
65 viewSettings.notify(includeReadSettingsId, this, SLOT(includeReadSettingChanged(const QVariant &)));
66 viewSettings.notify(alwaysOwnSettingsId, this, SLOT(alwaysOwnSettingChanged(const QVariant &)));
70 bool ChatMonitorFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
72 Q_UNUSED(sourceParent)
74 QModelIndex source_index = sourceModel()->index(sourceRow, 0);
75 BufferId bufferId = source_index.data(MessageModel::BufferIdRole).value<BufferId>();
76 Message::Flags flags = (Message::Flags)source_index.data(MessageModel::FlagsRole).toInt();
78 if (flags & Message::Backlog) {
82 if (!_includeRead && Client::networkModel()->lastSeenMsgId(bufferId) >= sourceModel()->data(source_index, MessageModel::MsgIdRole).value<MsgId>())
86 if (!_showOwnMessages && flags & Message::Self)
89 Message::Type type = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
90 if (!(type & (Message::Plain | Message::Notice | Message::Action)))
93 // ChatMonitorSettingsPage
94 if (_showHighlights && flags & Message::Highlight)
96 else if (_alwaysOwn && flags & Message::Self)
98 else if (_operationMode == ChatViewSettings::OptOut && _bufferIds.contains(bufferId))
100 else if (_operationMode == ChatViewSettings::OptIn && !_bufferIds.contains(bufferId))
103 // ignorelist handling
104 // only match if message is not flagged as server msg
105 if (!(flags & Message::ServerMsg) && Client::ignoreListManager()
106 && Client::ignoreListManager()->match(source_index.data(MessageModel::MessageRole).value<Message>(), Client::networkModel()->networkName(bufferId)))
113 // override this to inject display of network and channel
114 QVariant ChatMonitorFilter::data(const QModelIndex &index, int role) const
116 if (index.column() != ChatLineModel::SenderColumn || role != ChatLineModel::DisplayRole)
117 return MessageFilter::data(index, role);
119 BufferId bufid = data(index, ChatLineModel::BufferIdRole).value<BufferId>();
120 if (!bufid.isValid()) {
121 qDebug() << "ChatMonitorFilter::data(): chatline belongs to an invalid buffer!";
125 QModelIndex source_index = mapToSource(index);
128 if (_showFields & NetworkField) {
129 fields << Client::networkModel()->networkName(bufid);
131 if (_showFields & BufferField) {
132 fields << Client::networkModel()->bufferName(bufid);
135 Message::Type messageType = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
136 if (messageType & (Message::Plain | Message::Notice)) {
137 QString sender = MessageFilter::data(index, ChatLineModel::EditRole).toString();
140 if (_showSenderBrackets)
141 return QString("<%1>").arg(fields.join(":"));
143 return QString("%1").arg(fields.join(":"));
147 void ChatMonitorFilter::addShowField(int field)
149 if (_showFields & field)
152 ChatViewSettings(idString()).setValue("ShowFields", _showFields | field);
156 void ChatMonitorFilter::removeShowField(int field)
158 if (!(_showFields & field))
161 ChatViewSettings(idString()).setValue("ShowFields", _showFields ^ field);
165 void ChatMonitorFilter::setShowOwnMessages(bool show)
167 if (_showOwnMessages == show)
170 ChatViewSettings(idString()).setValue("ShowOwnMsgs", show);
174 void ChatMonitorFilter::showFieldsSettingChanged(const QVariant &newValue)
176 int newFields = newValue.toInt();
177 if (_showFields == newFields)
180 _showFields = newFields;
182 int rows = rowCount();
186 emit dataChanged(index(0, ChatLineModel::SenderColumn), index(rows - 1, ChatLineModel::SenderColumn));
190 void ChatMonitorFilter::showOwnMessagesSettingChanged(const QVariant &newValue)
192 _showOwnMessages = newValue.toBool();
195 void ChatMonitorFilter::alwaysOwnSettingChanged(const QVariant &newValue) {
196 _alwaysOwn = newValue.toBool();
199 void ChatMonitorFilter::showHighlightsSettingChanged(const QVariant &newValue)
201 _showHighlights = newValue.toBool();
205 void ChatMonitorFilter::operationModeSettingChanged(const QVariant &newValue)
207 _operationMode = newValue.toInt();
211 void ChatMonitorFilter::buffersSettingChanged(const QVariant &newValue)
214 foreach(QVariant v, newValue.toList()) {
215 _bufferIds << v.value<BufferId>();
220 void ChatMonitorFilter::showBacklogSettingChanged(const QVariant &newValue) {
221 _showBacklog = newValue.toBool();
224 void ChatMonitorFilter::includeReadSettingChanged(const QVariant &newValue) {
225 _includeRead = newValue.toBool();
228 void ChatMonitorFilter::showSenderBracketsSettingChanged(const QVariant &newValue)
230 _showSenderBrackets = newValue.toBool();