1 /***************************************************************************
2 * Copyright (C) 2005-2020 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"
23 #include "chatlinemodel.h"
24 #include "chatviewsettings.h"
26 #include "clientignorelistmanager.h"
27 #include "networkmodel.h"
29 ChatMonitorFilter::ChatMonitorFilter(MessageModel* model, QObject* parent)
30 : MessageFilter(model, parent)
32 // Global configuration
33 ChatViewSettings defaultSettings;
34 defaultSettings.initAndNotify("ShowSenderBrackets", this, &ChatMonitorFilter::showSenderBracketsSettingChanged);
36 // NOTE: Whenever changing defaults here, also update ChatMonitorSettingsPage::loadSettings()
37 // and ChatMonitorSettingsPage::defaults() to match
39 // Chat Monitor specific configuration
40 ChatViewSettings viewSettings(ChatMonitorFilter::idString());
41 _showFields = viewSettings.value("ShowFields", AllFields).toInt();
42 _showOwnMessages = viewSettings.value("ShowOwnMsgs", true).toBool();
43 viewSettings.notify("ShowFields", this, &ChatMonitorFilter::showFieldsSettingChanged);
44 viewSettings.notify("ShowOwnMsgs", this, &ChatMonitorFilter::showOwnMessagesSettingChanged);
46 // ChatMonitorSettingsPage
47 QString showHighlightsSettingsId = "ShowHighlights";
48 QString operationModeSettingsId = "OperationMode";
49 QString buffersSettingsId = "Buffers";
50 QString showBacklogSettingsId = "ShowBacklog";
51 QString includeReadSettingsId = "IncludeRead";
52 QString alwaysOwnSettingsId = "AlwaysOwn";
54 _showHighlights = viewSettings.value(showHighlightsSettingsId, false).toBool();
55 _operationMode = viewSettings.value(operationModeSettingsId, ChatViewSettings::InvalidMode).toInt();
56 // read configured list of buffers to monitor/ignore
57 foreach (QVariant v, viewSettings.value(buffersSettingsId, QVariant()).toList())
58 _bufferIds << v.value<BufferId>();
59 _showBacklog = viewSettings.value(showBacklogSettingsId, true).toBool();
60 _includeRead = viewSettings.value(includeReadSettingsId, false).toBool();
61 _alwaysOwn = viewSettings.value(alwaysOwnSettingsId, false).toBool();
63 viewSettings.notify(showHighlightsSettingsId, this, &ChatMonitorFilter::showHighlightsSettingChanged);
64 viewSettings.notify(operationModeSettingsId, this, &ChatMonitorFilter::operationModeSettingChanged);
65 viewSettings.notify(buffersSettingsId, this, &ChatMonitorFilter::buffersSettingChanged);
66 viewSettings.notify(showBacklogSettingsId, this, &ChatMonitorFilter::showBacklogSettingChanged);
67 viewSettings.notify(includeReadSettingsId, this, &ChatMonitorFilter::includeReadSettingChanged);
68 viewSettings.notify(alwaysOwnSettingsId, this, &ChatMonitorFilter::alwaysOwnSettingChanged);
71 bool ChatMonitorFilter::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
73 Q_UNUSED(sourceParent)
75 QModelIndex source_index = sourceModel()->index(sourceRow, 0);
76 BufferId bufferId = source_index.data(MessageModel::BufferIdRole).value<BufferId>();
77 Message::Flags flags = (Message::Flags)source_index.data(MessageModel::FlagsRole).toInt();
79 if (flags & Message::Backlog) {
84 && Client::networkModel()->lastSeenMsgId(bufferId) >= sourceModel()->data(source_index, MessageModel::MsgIdRole).value<MsgId>())
88 if (!_showOwnMessages && flags & Message::Self)
91 Message::Type type = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
92 if (!(type & (Message::Plain | Message::Notice | Message::Action)))
95 // ChatMonitorSettingsPage
96 if (_showHighlights && flags & Message::Highlight)
98 else if (_alwaysOwn && flags & Message::Self)
100 else if (_operationMode == ChatViewSettings::OptOut && _bufferIds.contains(bufferId))
102 else if (_operationMode == ChatViewSettings::OptIn && !_bufferIds.contains(bufferId))
105 // ignorelist handling
106 // only match if message is not flagged as server msg
107 if (!(flags & Message::ServerMsg) && Client::ignoreListManager()
108 && Client::ignoreListManager()->match(source_index.data(MessageModel::MessageRole).value<Message>(),
109 Client::networkModel()->networkName(bufferId)))
115 // override this to inject display of network and channel
116 QVariant ChatMonitorFilter::data(const QModelIndex& index, int role) const
118 if (index.column() != ChatLineModel::SenderColumn || role != ChatLineModel::DisplayRole)
119 return MessageFilter::data(index, role);
121 BufferId bufid = data(index, ChatLineModel::BufferIdRole).value<BufferId>();
122 if (!bufid.isValid()) {
123 qDebug() << "ChatMonitorFilter::data(): chatline belongs to an invalid buffer!";
127 QModelIndex source_index = mapToSource(index);
130 if (_showFields & NetworkField) {
131 fields << Client::networkModel()->networkName(bufid);
133 if (_showFields & BufferField) {
134 fields << Client::networkModel()->bufferName(bufid);
137 Message::Type messageType = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
138 if (messageType & (Message::Plain | Message::Notice)) {
139 QString sender = MessageFilter::data(index, ChatLineModel::EditRole).toString();
142 if (_showSenderBrackets)
143 return QString("<%1>").arg(fields.join(":"));
145 return QString("%1").arg(fields.join(":"));
148 void ChatMonitorFilter::addShowField(int field)
150 if (_showFields & field)
153 ChatViewSettings(idString()).setValue("ShowFields", _showFields | field);
156 void ChatMonitorFilter::removeShowField(int field)
158 if (!(_showFields & field))
161 ChatViewSettings(idString()).setValue("ShowFields", _showFields ^ field);
164 void ChatMonitorFilter::setShowOwnMessages(bool show)
166 if (_showOwnMessages == show)
169 ChatViewSettings(idString()).setValue("ShowOwnMsgs", show);
172 void ChatMonitorFilter::showFieldsSettingChanged(const QVariant& newValue)
174 int newFields = newValue.toInt();
175 if (_showFields == newFields)
178 _showFields = newFields;
180 int rows = rowCount();
184 emit dataChanged(index(0, ChatLineModel::SenderColumn), index(rows - 1, ChatLineModel::SenderColumn));
187 void ChatMonitorFilter::showOwnMessagesSettingChanged(const QVariant& newValue)
189 _showOwnMessages = newValue.toBool();
192 void ChatMonitorFilter::alwaysOwnSettingChanged(const QVariant& newValue)
194 _alwaysOwn = newValue.toBool();
197 void ChatMonitorFilter::showHighlightsSettingChanged(const QVariant& newValue)
199 _showHighlights = newValue.toBool();
202 void ChatMonitorFilter::operationModeSettingChanged(const QVariant& newValue)
204 _operationMode = newValue.toInt();
207 void ChatMonitorFilter::buffersSettingChanged(const QVariant& newValue)
210 foreach (QVariant v, newValue.toList()) {
211 _bufferIds << v.value<BufferId>();
216 void ChatMonitorFilter::showBacklogSettingChanged(const QVariant& newValue)
218 _showBacklog = newValue.toBool();
221 void ChatMonitorFilter::includeReadSettingChanged(const QVariant& newValue)
223 _includeRead = newValue.toBool();
226 void ChatMonitorFilter::showSenderBracketsSettingChanged(const QVariant& newValue)
228 _showSenderBrackets = newValue.toBool();