1 /***************************************************************************
2 * Copyright (C) 2005-2016 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";
51 _showHighlights = viewSettings.value(showHighlightsSettingsId, false).toBool();
52 _operationMode = viewSettings.value(operationModeSettingsId, 0).toInt();
53 // read configured list of buffers to monitor/ignore
54 foreach(QVariant v, viewSettings.value(buffersSettingsId, QVariant()).toList())
55 _bufferIds << v.value<BufferId>();
56 _showBacklog = viewSettings.value(showBacklogSettingsId, true).toBool();
57 _includeRead = viewSettings.value(includeReadSettingsId, true).toBool();
59 viewSettings.notify(showHighlightsSettingsId, this, SLOT(showHighlightsSettingChanged(const QVariant &)));
60 viewSettings.notify(operationModeSettingsId, this, SLOT(operationModeSettingChanged(const QVariant &)));
61 viewSettings.notify(buffersSettingsId, this, SLOT(buffersSettingChanged(const QVariant &)));
62 viewSettings.notify(showBacklogSettingsId, this, SLOT(showBacklogSettingChanged(const QVariant &)));
63 viewSettings.notify(includeReadSettingsId, this, SLOT(includeReadSettingChanged(const QVariant &)));
67 bool ChatMonitorFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
69 Q_UNUSED(sourceParent)
71 QModelIndex source_index = sourceModel()->index(sourceRow, 0);
72 BufferId bufferId = source_index.data(MessageModel::BufferIdRole).value<BufferId>();
74 Message::Flags flags = (Message::Flags)source_index.data(MessageModel::FlagsRole).toInt();
75 if ((flags & Message::Backlog) && (!_showBacklog || (!_includeRead &&
76 (Client::networkModel()->lastSeenMsgId(bufferId) >= sourceModel()->data(source_index, MessageModel::MsgIdRole).value<MsgId>()))))
79 if (!_showOwnMessages && flags & Message::Self)
82 Message::Type type = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
83 if (!(type & (Message::Plain | Message::Notice | Message::Action)))
86 // ChatMonitorSettingsPage
87 if (_operationMode == ChatViewSettings::OptOut
88 && !(_showHighlights && flags & Message::Highlight)
89 && _bufferIds.contains(bufferId))
91 if (_operationMode == ChatViewSettings::OptIn
92 && !(_showHighlights && flags & Message::Highlight)
93 && !_bufferIds.contains(bufferId))
96 // ignorelist handling
97 // only match if message is not flagged as server msg
98 if (!(flags & Message::ServerMsg) && Client::ignoreListManager()
99 && Client::ignoreListManager()->match(source_index.data(MessageModel::MessageRole).value<Message>(), Client::networkModel()->networkName(bufferId)))
105 // override this to inject display of network and channel
106 QVariant ChatMonitorFilter::data(const QModelIndex &index, int role) const
108 if (index.column() != ChatLineModel::SenderColumn || role != ChatLineModel::DisplayRole)
109 return MessageFilter::data(index, role);
111 BufferId bufid = data(index, ChatLineModel::BufferIdRole).value<BufferId>();
112 if (!bufid.isValid()) {
113 qDebug() << "ChatMonitorFilter::data(): chatline belongs to an invalid buffer!";
117 QModelIndex source_index = mapToSource(index);
120 if (_showFields & NetworkField) {
121 fields << Client::networkModel()->networkName(bufid);
123 if (_showFields & BufferField) {
124 fields << Client::networkModel()->bufferName(bufid);
127 Message::Type messageType = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
128 if (messageType & (Message::Plain | Message::Notice)) {
129 QString sender = MessageFilter::data(index, ChatLineModel::EditRole).toString();
132 if (_showSenderBrackets)
133 return QString("<%1>").arg(fields.join(":"));
135 return QString("%1").arg(fields.join(":"));
139 void ChatMonitorFilter::addShowField(int field)
141 if (_showFields & field)
144 ChatViewSettings(idString()).setValue("ShowFields", _showFields | field);
148 void ChatMonitorFilter::removeShowField(int field)
150 if (!(_showFields & field))
153 ChatViewSettings(idString()).setValue("ShowFields", _showFields ^ field);
157 void ChatMonitorFilter::setShowOwnMessages(bool show)
159 if (_showOwnMessages == show)
162 ChatViewSettings(idString()).setValue("ShowOwnMsgs", show);
166 void ChatMonitorFilter::showFieldsSettingChanged(const QVariant &newValue)
168 int newFields = newValue.toInt();
169 if (_showFields == newFields)
172 _showFields = newFields;
174 int rows = rowCount();
178 emit dataChanged(index(0, ChatLineModel::SenderColumn), index(rows - 1, ChatLineModel::SenderColumn));
182 void ChatMonitorFilter::showOwnMessagesSettingChanged(const QVariant &newValue)
184 _showOwnMessages = newValue.toBool();
188 void ChatMonitorFilter::showHighlightsSettingChanged(const QVariant &newValue)
190 _showHighlights = newValue.toBool();
194 void ChatMonitorFilter::operationModeSettingChanged(const QVariant &newValue)
196 _operationMode = newValue.toInt();
200 void ChatMonitorFilter::buffersSettingChanged(const QVariant &newValue)
203 foreach(QVariant v, newValue.toList()) {
204 _bufferIds << v.value<BufferId>();
209 void ChatMonitorFilter::showBacklogSettingChanged(const QVariant &newValue) {
210 _showBacklog = newValue.toBool();
213 void ChatMonitorFilter::includeReadSettingChanged(const QVariant &newValue) {
214 _includeRead = newValue.toBool();
217 void ChatMonitorFilter::showSenderBracketsSettingChanged(const QVariant &newValue)
219 _showSenderBrackets = newValue.toBool();