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