d3c0810a263d199019914c169d031a37c5071ad0
[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
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>()))))
77         return false;
78
79     if (!_showOwnMessages && flags & Message::Self)
80         return false;
81
82     Message::Type type = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
83     if (!(type & (Message::Plain | Message::Notice | Message::Action)))
84         return false;
85
86     // ChatMonitorSettingsPage
87     if (_operationMode == ChatViewSettings::OptOut
88         && !(_showHighlights && flags & Message::Highlight)
89         &&  _bufferIds.contains(bufferId))
90         return false;
91     if (_operationMode == ChatViewSettings::OptIn
92         && !(_showHighlights && flags & Message::Highlight)
93         && !_bufferIds.contains(bufferId))
94         return false;
95
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)))
100         return false;
101     return true;
102 }
103
104
105 // override this to inject display of network and channel
106 QVariant ChatMonitorFilter::data(const QModelIndex &index, int role) const
107 {
108     if (index.column() != ChatLineModel::SenderColumn || role != ChatLineModel::DisplayRole)
109         return MessageFilter::data(index, role);
110
111     BufferId bufid = data(index, ChatLineModel::BufferIdRole).value<BufferId>();
112     if (!bufid.isValid()) {
113         qDebug() << "ChatMonitorFilter::data(): chatline belongs to an invalid buffer!";
114         return QVariant();
115     }
116
117     QModelIndex source_index = mapToSource(index);
118
119     QStringList fields;
120     if (_showFields & NetworkField) {
121         fields << Client::networkModel()->networkName(bufid);
122     }
123     if (_showFields & BufferField) {
124         fields << Client::networkModel()->bufferName(bufid);
125     }
126
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();
130         fields << sender;
131     }
132     if (_showSenderBrackets)
133         return QString("<%1>").arg(fields.join(":"));
134     else
135         return QString("%1").arg(fields.join(":"));
136 }
137
138
139 void ChatMonitorFilter::addShowField(int field)
140 {
141     if (_showFields & field)
142         return;
143
144     ChatViewSettings(idString()).setValue("ShowFields", _showFields | field);
145 }
146
147
148 void ChatMonitorFilter::removeShowField(int field)
149 {
150     if (!(_showFields & field))
151         return;
152
153     ChatViewSettings(idString()).setValue("ShowFields", _showFields ^ field);
154 }
155
156
157 void ChatMonitorFilter::setShowOwnMessages(bool show)
158 {
159     if (_showOwnMessages == show)
160         return;
161
162     ChatViewSettings(idString()).setValue("ShowOwnMsgs", show);
163 }
164
165
166 void ChatMonitorFilter::showFieldsSettingChanged(const QVariant &newValue)
167 {
168     int newFields = newValue.toInt();
169     if (_showFields == newFields)
170         return;
171
172     _showFields = newFields;
173
174     int rows = rowCount();
175     if (rows == 0)
176         return;
177
178     emit dataChanged(index(0, ChatLineModel::SenderColumn), index(rows - 1, ChatLineModel::SenderColumn));
179 }
180
181
182 void ChatMonitorFilter::showOwnMessagesSettingChanged(const QVariant &newValue)
183 {
184     _showOwnMessages = newValue.toBool();
185 }
186
187
188 void ChatMonitorFilter::showHighlightsSettingChanged(const QVariant &newValue)
189 {
190     _showHighlights = newValue.toBool();
191 }
192
193
194 void ChatMonitorFilter::operationModeSettingChanged(const QVariant &newValue)
195 {
196     _operationMode = newValue.toInt();
197 }
198
199
200 void ChatMonitorFilter::buffersSettingChanged(const QVariant &newValue)
201 {
202     _bufferIds.clear();
203     foreach(QVariant v, newValue.toList()) {
204         _bufferIds << v.value<BufferId>();
205     }
206     invalidateFilter();
207 }
208
209 void ChatMonitorFilter::showBacklogSettingChanged(const QVariant &newValue) {
210     _showBacklog = newValue.toBool();
211 }
212
213 void ChatMonitorFilter::includeReadSettingChanged(const QVariant &newValue) {
214     _includeRead = newValue.toBool();
215 }
216
217 void ChatMonitorFilter::showSenderBracketsSettingChanged(const QVariant &newValue)
218 {
219     _showSenderBrackets = newValue.toBool();
220 }