qa: Avoid deprecation warnings for QList/QSet conversions
[quassel.git] / src / qtui / chatmonitorfilter.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 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 "chatlinemodel.h"
24 #include "chatviewsettings.h"
25 #include "client.h"
26 #include "clientignorelistmanager.h"
27 #include "networkmodel.h"
28
29 ChatMonitorFilter::ChatMonitorFilter(MessageModel* model, QObject* parent)
30     : MessageFilter(model, parent)
31 {
32     // Global configuration
33     ChatViewSettings defaultSettings;
34     defaultSettings.initAndNotify("ShowSenderBrackets", this, &ChatMonitorFilter::showSenderBracketsSettingChanged);
35
36     // NOTE: Whenever changing defaults here, also update ChatMonitorSettingsPage::loadSettings()
37     // and ChatMonitorSettingsPage::defaults() to match
38
39     // Chat Monitor specific configuration
40     ChatViewSettings viewSettings(ChatMonitorFilter::idString());
41     _showFields = viewSettings.value("ShowFields", AllFields).toInt();
42     _showOwnMessages = viewSettings.value("ShowOwnMsgs", true).toBool();
43     viewSettings.notify("ShowFields", this, &ChatMonitorFilter::showFieldsSettingChanged);
44     viewSettings.notify("ShowOwnMsgs", this, &ChatMonitorFilter::showOwnMessagesSettingChanged);
45
46     // ChatMonitorSettingsPage
47     QString showHighlightsSettingsId = "ShowHighlights";
48     QString operationModeSettingsId = "OperationMode";
49     QString buffersSettingsId = "Buffers";
50     QString showBacklogSettingsId = "ShowBacklog";
51     QString includeReadSettingsId = "IncludeRead";
52     QString alwaysOwnSettingsId = "AlwaysOwn";
53
54     _showHighlights = viewSettings.value(showHighlightsSettingsId, false).toBool();
55     _operationMode = viewSettings.value(operationModeSettingsId, ChatViewSettings::InvalidMode).toInt();
56     // read configured list of buffers to monitor/ignore
57     foreach (QVariant v, viewSettings.value(buffersSettingsId, QVariant()).toList())
58         _bufferIds << v.value<BufferId>();
59     _showBacklog = viewSettings.value(showBacklogSettingsId, true).toBool();
60     _includeRead = viewSettings.value(includeReadSettingsId, false).toBool();
61     _alwaysOwn = viewSettings.value(alwaysOwnSettingsId, false).toBool();
62
63     viewSettings.notify(showHighlightsSettingsId, this, &ChatMonitorFilter::showHighlightsSettingChanged);
64     viewSettings.notify(operationModeSettingsId, this, &ChatMonitorFilter::operationModeSettingChanged);
65     viewSettings.notify(buffersSettingsId, this, &ChatMonitorFilter::buffersSettingChanged);
66     viewSettings.notify(showBacklogSettingsId, this, &ChatMonitorFilter::showBacklogSettingChanged);
67     viewSettings.notify(includeReadSettingsId, this, &ChatMonitorFilter::includeReadSettingChanged);
68     viewSettings.notify(alwaysOwnSettingsId, this, &ChatMonitorFilter::alwaysOwnSettingChanged);
69 }
70
71 bool ChatMonitorFilter::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
72 {
73     Q_UNUSED(sourceParent)
74
75     QModelIndex source_index = sourceModel()->index(sourceRow, 0);
76     BufferId bufferId = source_index.data(MessageModel::BufferIdRole).value<BufferId>();
77     Message::Flags flags = (Message::Flags)source_index.data(MessageModel::FlagsRole).toInt();
78
79     if (flags & Message::Backlog) {
80         if (!_showBacklog)
81             return false;
82
83         if (!_includeRead
84             && Client::networkModel()->lastSeenMsgId(bufferId) >= sourceModel()->data(source_index, MessageModel::MsgIdRole).value<MsgId>())
85             return false;
86     }
87
88     if (!_showOwnMessages && flags & Message::Self)
89         return false;
90
91     Message::Type type = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
92     if (!(type & (Message::Plain | Message::Notice | Message::Action)))
93         return false;
94
95     // ChatMonitorSettingsPage
96     if (_showHighlights && flags & Message::Highlight)
97         ;  // pass
98     else if (_alwaysOwn && flags & Message::Self)
99         ;  // pass
100     else if (_operationMode == ChatViewSettings::OptOut && _bufferIds.contains(bufferId))
101         return false;
102     else if (_operationMode == ChatViewSettings::OptIn && !_bufferIds.contains(bufferId))
103         return false;
104
105     // ignorelist handling
106     // only match if message is not flagged as server msg
107     if (!(flags & Message::ServerMsg) && Client::ignoreListManager()
108         && Client::ignoreListManager()->match(source_index.data(MessageModel::MessageRole).value<Message>(),
109                                               Client::networkModel()->networkName(bufferId)))
110         return false;
111
112     return true;
113 }
114
115 // override this to inject display of network and channel
116 QVariant ChatMonitorFilter::data(const QModelIndex& index, int role) const
117 {
118     if (index.column() != ChatLineModel::SenderColumn || role != ChatLineModel::DisplayRole)
119         return MessageFilter::data(index, role);
120
121     BufferId bufid = data(index, ChatLineModel::BufferIdRole).value<BufferId>();
122     if (!bufid.isValid()) {
123         qDebug() << "ChatMonitorFilter::data(): chatline belongs to an invalid buffer!";
124         return QVariant();
125     }
126
127     QModelIndex source_index = mapToSource(index);
128
129     QStringList fields;
130     if (_showFields & NetworkField) {
131         fields << Client::networkModel()->networkName(bufid);
132     }
133     if (_showFields & BufferField) {
134         fields << Client::networkModel()->bufferName(bufid);
135     }
136
137     Message::Type messageType = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
138     if (messageType & (Message::Plain | Message::Notice)) {
139         QString sender = MessageFilter::data(index, ChatLineModel::EditRole).toString();
140         fields << sender;
141     }
142     if (_showSenderBrackets)
143         return QString("<%1>").arg(fields.join(":"));
144     else
145         return QString("%1").arg(fields.join(":"));
146 }
147
148 void ChatMonitorFilter::addShowField(int field)
149 {
150     if (_showFields & field)
151         return;
152
153     ChatViewSettings(idString()).setValue("ShowFields", _showFields | field);
154 }
155
156 void ChatMonitorFilter::removeShowField(int field)
157 {
158     if (!(_showFields & field))
159         return;
160
161     ChatViewSettings(idString()).setValue("ShowFields", _showFields ^ field);
162 }
163
164 void ChatMonitorFilter::setShowOwnMessages(bool show)
165 {
166     if (_showOwnMessages == show)
167         return;
168
169     ChatViewSettings(idString()).setValue("ShowOwnMsgs", show);
170 }
171
172 void ChatMonitorFilter::showFieldsSettingChanged(const QVariant& newValue)
173 {
174     int newFields = newValue.toInt();
175     if (_showFields == newFields)
176         return;
177
178     _showFields = newFields;
179
180     int rows = rowCount();
181     if (rows == 0)
182         return;
183
184     emit dataChanged(index(0, ChatLineModel::SenderColumn), index(rows - 1, ChatLineModel::SenderColumn));
185 }
186
187 void ChatMonitorFilter::showOwnMessagesSettingChanged(const QVariant& newValue)
188 {
189     _showOwnMessages = newValue.toBool();
190 }
191
192 void ChatMonitorFilter::alwaysOwnSettingChanged(const QVariant& newValue)
193 {
194     _alwaysOwn = newValue.toBool();
195 }
196
197 void ChatMonitorFilter::showHighlightsSettingChanged(const QVariant& newValue)
198 {
199     _showHighlights = newValue.toBool();
200 }
201
202 void ChatMonitorFilter::operationModeSettingChanged(const QVariant& newValue)
203 {
204     _operationMode = newValue.toInt();
205 }
206
207 void ChatMonitorFilter::buffersSettingChanged(const QVariant& newValue)
208 {
209     _bufferIds.clear();
210     foreach (QVariant v, newValue.toList()) {
211         _bufferIds << v.value<BufferId>();
212     }
213     invalidateFilter();
214 }
215
216 void ChatMonitorFilter::showBacklogSettingChanged(const QVariant& newValue)
217 {
218     _showBacklog = newValue.toBool();
219 }
220
221 void ChatMonitorFilter::includeReadSettingChanged(const QVariant& newValue)
222 {
223     _includeRead = newValue.toBool();
224 }
225
226 void ChatMonitorFilter::showSenderBracketsSettingChanged(const QVariant& newValue)
227 {
228     _showSenderBrackets = newValue.toBool();
229 }