common: Port remote nicks to NickHighlightMatcher
[quassel.git] / src / qtui / chatmonitorfilter.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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     // Global configuration
33     ChatViewSettings defaultSettings;
34     _showSenderBrackets = defaultSettings.showSenderBrackets();
35     defaultSettings.notify("ShowSenderBrackets", this, SLOT(showSenderBracketsSettingChanged(const QVariant &)));
36
37     // NOTE: Whenever changing defaults here, also update ChatMonitorSettingsPage::loadSettings()
38     // and ChatMonitorSettingsPage::defaults() to match
39
40     // Chat Monitor specific configuration
41     ChatViewSettings viewSettings(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 &)));
46
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";
54
55     _showHighlights = viewSettings.value(showHighlightsSettingsId, false).toBool();
56     _operationMode =
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();
64
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 &)));
71 }
72
73
74 bool ChatMonitorFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
75 {
76     Q_UNUSED(sourceParent)
77
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();
81
82     if (flags & Message::Backlog) {
83         if (!_showBacklog)
84             return false;
85
86         if (!_includeRead && Client::networkModel()->lastSeenMsgId(bufferId) >= sourceModel()->data(source_index, MessageModel::MsgIdRole).value<MsgId>())
87             return false;
88     }
89
90     if (!_showOwnMessages && flags & Message::Self)
91         return false;
92
93     Message::Type type = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
94     if (!(type & (Message::Plain | Message::Notice | Message::Action)))
95         return false;
96
97     // ChatMonitorSettingsPage
98     if (_showHighlights && flags & Message::Highlight)
99         ; // pass
100     else if (_alwaysOwn && flags & Message::Self)
101         ; // pass
102     else if (_operationMode == ChatViewSettings::OptOut && _bufferIds.contains(bufferId))
103         return false;
104     else if (_operationMode == ChatViewSettings::OptIn && !_bufferIds.contains(bufferId))
105         return false;
106
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)))
111         return false;
112
113     return true;
114 }
115
116
117 // override this to inject display of network and channel
118 QVariant ChatMonitorFilter::data(const QModelIndex &index, int role) const
119 {
120     if (index.column() != ChatLineModel::SenderColumn || role != ChatLineModel::DisplayRole)
121         return MessageFilter::data(index, role);
122
123     BufferId bufid = data(index, ChatLineModel::BufferIdRole).value<BufferId>();
124     if (!bufid.isValid()) {
125         qDebug() << "ChatMonitorFilter::data(): chatline belongs to an invalid buffer!";
126         return QVariant();
127     }
128
129     QModelIndex source_index = mapToSource(index);
130
131     QStringList fields;
132     if (_showFields & NetworkField) {
133         fields << Client::networkModel()->networkName(bufid);
134     }
135     if (_showFields & BufferField) {
136         fields << Client::networkModel()->bufferName(bufid);
137     }
138
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();
142         fields << sender;
143     }
144     if (_showSenderBrackets)
145         return QString("<%1>").arg(fields.join(":"));
146     else
147         return QString("%1").arg(fields.join(":"));
148 }
149
150
151 void ChatMonitorFilter::addShowField(int field)
152 {
153     if (_showFields & field)
154         return;
155
156     ChatViewSettings(idString()).setValue("ShowFields", _showFields | field);
157 }
158
159
160 void ChatMonitorFilter::removeShowField(int field)
161 {
162     if (!(_showFields & field))
163         return;
164
165     ChatViewSettings(idString()).setValue("ShowFields", _showFields ^ field);
166 }
167
168
169 void ChatMonitorFilter::setShowOwnMessages(bool show)
170 {
171     if (_showOwnMessages == show)
172         return;
173
174     ChatViewSettings(idString()).setValue("ShowOwnMsgs", show);
175 }
176
177
178 void ChatMonitorFilter::showFieldsSettingChanged(const QVariant &newValue)
179 {
180     int newFields = newValue.toInt();
181     if (_showFields == newFields)
182         return;
183
184     _showFields = newFields;
185
186     int rows = rowCount();
187     if (rows == 0)
188         return;
189
190     emit dataChanged(index(0, ChatLineModel::SenderColumn), index(rows - 1, ChatLineModel::SenderColumn));
191 }
192
193
194 void ChatMonitorFilter::showOwnMessagesSettingChanged(const QVariant &newValue)
195 {
196     _showOwnMessages = newValue.toBool();
197 }
198
199 void ChatMonitorFilter::alwaysOwnSettingChanged(const QVariant &newValue) {
200     _alwaysOwn = newValue.toBool();
201 }
202
203 void ChatMonitorFilter::showHighlightsSettingChanged(const QVariant &newValue)
204 {
205     _showHighlights = newValue.toBool();
206 }
207
208
209 void ChatMonitorFilter::operationModeSettingChanged(const QVariant &newValue)
210 {
211     _operationMode = newValue.toInt();
212 }
213
214
215 void ChatMonitorFilter::buffersSettingChanged(const QVariant &newValue)
216 {
217     _bufferIds.clear();
218     foreach(QVariant v, newValue.toList()) {
219         _bufferIds << v.value<BufferId>();
220     }
221     invalidateFilter();
222 }
223
224 void ChatMonitorFilter::showBacklogSettingChanged(const QVariant &newValue) {
225     _showBacklog = newValue.toBool();
226 }
227
228 void ChatMonitorFilter::includeReadSettingChanged(const QVariant &newValue) {
229     _includeRead = newValue.toBool();
230 }
231
232 void ChatMonitorFilter::showSenderBracketsSettingChanged(const QVariant &newValue)
233 {
234     _showSenderBrackets = newValue.toBool();
235 }