ChatMonitorFilter: Refactor conditions
[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     // 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 &)));
43
44     // ChatMonitorSettingsPage
45     QString showHighlightsSettingsId = "ShowHighlights";
46     QString operationModeSettingsId = "OperationMode";
47     QString buffersSettingsId = "Buffers";
48     QString showBacklogSettingsId = "ShowBacklog";
49     QString includeReadSettingsId = "IncludeRead";
50
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();
58
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 &)));
64 }
65
66
67 bool ChatMonitorFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
68 {
69     Q_UNUSED(sourceParent)
70
71     QModelIndex source_index = sourceModel()->index(sourceRow, 0);
72     BufferId bufferId = source_index.data(MessageModel::BufferIdRole).value<BufferId>();
73     Message::Flags flags = (Message::Flags)source_index.data(MessageModel::FlagsRole).toInt();
74
75     if (flags & Message::Backlog) {
76         if (!_showBacklog)
77             return false;
78
79         if (!_includeRead && Client::networkModel()->lastSeenMsgId(bufferId) >= sourceModel()->data(source_index, MessageModel::MsgIdRole).value<MsgId>())
80             return false;
81     }
82
83     if (!_showOwnMessages && flags & Message::Self)
84         return false;
85
86     Message::Type type = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
87     if (!(type & (Message::Plain | Message::Notice | Message::Action)))
88         return false;
89
90     // ChatMonitorSettingsPage
91     if (_showHighlights && flags & Message::Highlight)
92         ; // pass
93     else if (_operationMode == ChatViewSettings::OptOut && _bufferIds.contains(bufferId))
94         return false;
95     else if (_operationMode == ChatViewSettings::OptIn && !_bufferIds.contains(bufferId))
96         return false;
97
98     // ignorelist handling
99     // only match if message is not flagged as server msg
100     if (!(flags & Message::ServerMsg) && Client::ignoreListManager()
101         && Client::ignoreListManager()->match(source_index.data(MessageModel::MessageRole).value<Message>(), Client::networkModel()->networkName(bufferId)))
102         return false;
103
104     return true;
105 }
106
107
108 // override this to inject display of network and channel
109 QVariant ChatMonitorFilter::data(const QModelIndex &index, int role) const
110 {
111     if (index.column() != ChatLineModel::SenderColumn || role != ChatLineModel::DisplayRole)
112         return MessageFilter::data(index, role);
113
114     BufferId bufid = data(index, ChatLineModel::BufferIdRole).value<BufferId>();
115     if (!bufid.isValid()) {
116         qDebug() << "ChatMonitorFilter::data(): chatline belongs to an invalid buffer!";
117         return QVariant();
118     }
119
120     QModelIndex source_index = mapToSource(index);
121
122     QStringList fields;
123     if (_showFields & NetworkField) {
124         fields << Client::networkModel()->networkName(bufid);
125     }
126     if (_showFields & BufferField) {
127         fields << Client::networkModel()->bufferName(bufid);
128     }
129
130     Message::Type messageType = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
131     if (messageType & (Message::Plain | Message::Notice)) {
132         QString sender = MessageFilter::data(index, ChatLineModel::EditRole).toString();
133         fields << sender;
134     }
135     if (_showSenderBrackets)
136         return QString("<%1>").arg(fields.join(":"));
137     else
138         return QString("%1").arg(fields.join(":"));
139 }
140
141
142 void ChatMonitorFilter::addShowField(int field)
143 {
144     if (_showFields & field)
145         return;
146
147     ChatViewSettings(idString()).setValue("ShowFields", _showFields | field);
148 }
149
150
151 void ChatMonitorFilter::removeShowField(int field)
152 {
153     if (!(_showFields & field))
154         return;
155
156     ChatViewSettings(idString()).setValue("ShowFields", _showFields ^ field);
157 }
158
159
160 void ChatMonitorFilter::setShowOwnMessages(bool show)
161 {
162     if (_showOwnMessages == show)
163         return;
164
165     ChatViewSettings(idString()).setValue("ShowOwnMsgs", show);
166 }
167
168
169 void ChatMonitorFilter::showFieldsSettingChanged(const QVariant &newValue)
170 {
171     int newFields = newValue.toInt();
172     if (_showFields == newFields)
173         return;
174
175     _showFields = newFields;
176
177     int rows = rowCount();
178     if (rows == 0)
179         return;
180
181     emit dataChanged(index(0, ChatLineModel::SenderColumn), index(rows - 1, ChatLineModel::SenderColumn));
182 }
183
184
185 void ChatMonitorFilter::showOwnMessagesSettingChanged(const QVariant &newValue)
186 {
187     _showOwnMessages = newValue.toBool();
188 }
189
190
191 void ChatMonitorFilter::showHighlightsSettingChanged(const QVariant &newValue)
192 {
193     _showHighlights = newValue.toBool();
194 }
195
196
197 void ChatMonitorFilter::operationModeSettingChanged(const QVariant &newValue)
198 {
199     _operationMode = newValue.toInt();
200 }
201
202
203 void ChatMonitorFilter::buffersSettingChanged(const QVariant &newValue)
204 {
205     _bufferIds.clear();
206     foreach(QVariant v, newValue.toList()) {
207         _bufferIds << v.value<BufferId>();
208     }
209     invalidateFilter();
210 }
211
212 void ChatMonitorFilter::showBacklogSettingChanged(const QVariant &newValue) {
213     _showBacklog = newValue.toBool();
214 }
215
216 void ChatMonitorFilter::includeReadSettingChanged(const QVariant &newValue) {
217     _includeRead = newValue.toBool();
218 }
219
220 void ChatMonitorFilter::showSenderBracketsSettingChanged(const QVariant &newValue)
221 {
222     _showSenderBrackets = newValue.toBool();
223 }