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 // NOTE: Whenever changing defaults here, also update ChatMonitorSettingsPage::loadSettings()
38 // and ChatMonitorSettingsPage::defaults() to match
40 // Chat Monitor specific configuration
41 ChatViewSettings viewSettings(ChatMonitorFilter::idString());
42 _showFields = viewSettings.value("ShowFields", AllFields).toInt();
43 _showOwnMessages = viewSettings.value("ShowOwnMsgs", true).toBool();
44 viewSettings.notify("ShowFields", this, SLOT(showFieldsSettingChanged(const QVariant &)));
45 viewSettings.notify("ShowOwnMsgs", this, SLOT(showOwnMessagesSettingChanged(const QVariant &)));
47 // ChatMonitorSettingsPage
48 QString showHighlightsSettingsId = "ShowHighlights";
49 QString operationModeSettingsId = "OperationMode";
50 QString buffersSettingsId = "Buffers";
51 QString showBacklogSettingsId = "ShowBacklog";
52 QString includeReadSettingsId = "IncludeRead";
53 QString alwaysOwnSettingsId = "AlwaysOwn";
55 _showHighlights = viewSettings.value(showHighlightsSettingsId, false).toBool();
57 viewSettings.value(operationModeSettingsId, ChatViewSettings::InvalidMode).toInt();
58 // read configured list of buffers to monitor/ignore
59 foreach(QVariant v, viewSettings.value(buffersSettingsId, QVariant()).toList())
60 _bufferIds << v.value<BufferId>();
61 _showBacklog = viewSettings.value(showBacklogSettingsId, true).toBool();
62 _includeRead = viewSettings.value(includeReadSettingsId, false).toBool();
63 _alwaysOwn = viewSettings.value(alwaysOwnSettingsId, false).toBool();
65 viewSettings.notify(showHighlightsSettingsId, this, SLOT(showHighlightsSettingChanged(const QVariant &)));
66 viewSettings.notify(operationModeSettingsId, this, SLOT(operationModeSettingChanged(const QVariant &)));
67 viewSettings.notify(buffersSettingsId, this, SLOT(buffersSettingChanged(const QVariant &)));
68 viewSettings.notify(showBacklogSettingsId, this, SLOT(showBacklogSettingChanged(const QVariant &)));
69 viewSettings.notify(includeReadSettingsId, this, SLOT(includeReadSettingChanged(const QVariant &)));
70 viewSettings.notify(alwaysOwnSettingsId, this, SLOT(alwaysOwnSettingChanged(const QVariant &)));
74 bool ChatMonitorFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
76 Q_UNUSED(sourceParent)
78 QModelIndex source_index = sourceModel()->index(sourceRow, 0);
79 BufferId bufferId = source_index.data(MessageModel::BufferIdRole).value<BufferId>();
80 Message::Flags flags = (Message::Flags)source_index.data(MessageModel::FlagsRole).toInt();
82 if (flags & Message::Backlog) {
86 if (!_includeRead && Client::networkModel()->lastSeenMsgId(bufferId) >= sourceModel()->data(source_index, MessageModel::MsgIdRole).value<MsgId>())
90 if (!_showOwnMessages && flags & Message::Self)
93 Message::Type type = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
94 if (!(type & (Message::Plain | Message::Notice | Message::Action)))
97 // ChatMonitorSettingsPage
98 if (_showHighlights && flags & Message::Highlight)
100 else if (_alwaysOwn && flags & Message::Self)
102 else if (_operationMode == ChatViewSettings::OptOut && _bufferIds.contains(bufferId))
104 else if (_operationMode == ChatViewSettings::OptIn && !_bufferIds.contains(bufferId))
107 // ignorelist handling
108 // only match if message is not flagged as server msg
109 if (!(flags & Message::ServerMsg) && Client::ignoreListManager()
110 && Client::ignoreListManager()->match(source_index.data(MessageModel::MessageRole).value<Message>(), Client::networkModel()->networkName(bufferId)))
117 // override this to inject display of network and channel
118 QVariant ChatMonitorFilter::data(const QModelIndex &index, int role) const
120 if (index.column() != ChatLineModel::SenderColumn || role != ChatLineModel::DisplayRole)
121 return MessageFilter::data(index, role);
123 BufferId bufid = data(index, ChatLineModel::BufferIdRole).value<BufferId>();
124 if (!bufid.isValid()) {
125 qDebug() << "ChatMonitorFilter::data(): chatline belongs to an invalid buffer!";
129 QModelIndex source_index = mapToSource(index);
132 if (_showFields & NetworkField) {
133 fields << Client::networkModel()->networkName(bufid);
135 if (_showFields & BufferField) {
136 fields << Client::networkModel()->bufferName(bufid);
139 Message::Type messageType = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
140 if (messageType & (Message::Plain | Message::Notice)) {
141 QString sender = MessageFilter::data(index, ChatLineModel::EditRole).toString();
144 if (_showSenderBrackets)
145 return QString("<%1>").arg(fields.join(":"));
147 return QString("%1").arg(fields.join(":"));
151 void ChatMonitorFilter::addShowField(int field)
153 if (_showFields & field)
156 ChatViewSettings(idString()).setValue("ShowFields", _showFields | field);
160 void ChatMonitorFilter::removeShowField(int field)
162 if (!(_showFields & field))
165 ChatViewSettings(idString()).setValue("ShowFields", _showFields ^ field);
169 void ChatMonitorFilter::setShowOwnMessages(bool show)
171 if (_showOwnMessages == show)
174 ChatViewSettings(idString()).setValue("ShowOwnMsgs", show);
178 void ChatMonitorFilter::showFieldsSettingChanged(const QVariant &newValue)
180 int newFields = newValue.toInt();
181 if (_showFields == newFields)
184 _showFields = newFields;
186 int rows = rowCount();
190 emit dataChanged(index(0, ChatLineModel::SenderColumn), index(rows - 1, ChatLineModel::SenderColumn));
194 void ChatMonitorFilter::showOwnMessagesSettingChanged(const QVariant &newValue)
196 _showOwnMessages = newValue.toBool();
199 void ChatMonitorFilter::alwaysOwnSettingChanged(const QVariant &newValue) {
200 _alwaysOwn = newValue.toBool();
203 void ChatMonitorFilter::showHighlightsSettingChanged(const QVariant &newValue)
205 _showHighlights = newValue.toBool();
209 void ChatMonitorFilter::operationModeSettingChanged(const QVariant &newValue)
211 _operationMode = newValue.toInt();
215 void ChatMonitorFilter::buffersSettingChanged(const QVariant &newValue)
218 foreach(QVariant v, newValue.toList()) {
219 _bufferIds << v.value<BufferId>();
224 void ChatMonitorFilter::showBacklogSettingChanged(const QVariant &newValue) {
225 _showBacklog = newValue.toBool();
228 void ChatMonitorFilter::includeReadSettingChanged(const QVariant &newValue) {
229 _includeRead = newValue.toBool();
232 void ChatMonitorFilter::showSenderBracketsSettingChanged(const QVariant &newValue)
234 _showSenderBrackets = newValue.toBool();