Add option for chat monitor to look in the backlog
[quassel.git] / src / qtui / chatmonitorfilter.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2014 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     ChatViewSettings viewSettings(idString());
33     _showFields = viewSettings.value("ShowFields", AllFields).toInt();
34     _showOwnMessages = viewSettings.value("ShowOwnMsgs", true).toBool();
35     viewSettings.notify("ShowFields", this, SLOT(showFieldsSettingChanged(const QVariant &)));
36     viewSettings.notify("ShowOwnMsgs", this, SLOT(showOwnMessagesSettingChanged(const QVariant &)));
37
38     // ChatMonitorSettingsPage
39     QString showHighlightsSettingsId = "ShowHighlights";
40     QString operationModeSettingsId = "OperationMode";
41     QString buffersSettingsId = "Buffers";
42     QString showBacklogSettingsId = "ShowBacklog";
43
44     _showHighlights = viewSettings.value(showHighlightsSettingsId, false).toBool();
45     _operationMode = viewSettings.value(operationModeSettingsId, 0).toInt();
46     // read configured list of buffers to monitor/ignore
47     foreach(QVariant v, viewSettings.value(buffersSettingsId, QVariant()).toList())
48     _bufferIds << v.value<BufferId>();
49     _showBacklog = viewSettings.value(showBacklogSettingsId, true).toBool();
50
51     viewSettings.notify(showHighlightsSettingsId, this, SLOT(showHighlightsSettingChanged(const QVariant &)));
52     viewSettings.notify(operationModeSettingsId, this, SLOT(operationModeSettingChanged(const QVariant &)));
53     viewSettings.notify(buffersSettingsId, this, SLOT(buffersSettingChanged(const QVariant &)));
54     viewSettings.notify(showBacklogSettingsId, this, SLOT(showBacklogSettingChanged(const QVariant &)));
55 }
56
57
58 bool ChatMonitorFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
59 {
60     Q_UNUSED(sourceParent)
61
62     QModelIndex source_index = sourceModel()->index(sourceRow, 0);
63     BufferId bufferId = source_index.data(MessageModel::BufferIdRole).value<BufferId>();
64
65     Message::Flags flags = (Message::Flags)source_index.data(MessageModel::FlagsRole).toInt();
66     if ((flags & Message::Backlog) && (!_showBacklog ||
67         (Client::networkModel()->lastSeenMsgId(bufferId) >= sourceModel()->data(source_index, MessageModel::MsgIdRole).value<MsgId>())))
68         return false;
69
70     if (!_showOwnMessages && flags & Message::Self)
71         return false;
72
73     Message::Type type = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
74     if (!(type & (Message::Plain | Message::Notice | Message::Action)))
75         return false;
76
77     // ChatMonitorSettingsPage
78     if (_operationMode == ChatViewSettings::OptOut
79         && !(_showHighlights && flags & Message::Highlight)
80         &&  _bufferIds.contains(bufferId))
81         return false;
82     if (_operationMode == ChatViewSettings::OptIn
83         && !(_showHighlights && flags & Message::Highlight)
84         && !_bufferIds.contains(bufferId))
85         return false;
86
87     // ignorelist handling
88     // only match if message is not flagged as server msg
89     if (!(flags & Message::ServerMsg) && Client::ignoreListManager()
90         && Client::ignoreListManager()->match(source_index.data(MessageModel::MessageRole).value<Message>(), Client::networkModel()->networkName(bufferId)))
91         return false;
92     return true;
93 }
94
95
96 // override this to inject display of network and channel
97 QVariant ChatMonitorFilter::data(const QModelIndex &index, int role) const
98 {
99     if (index.column() != ChatLineModel::SenderColumn || role != ChatLineModel::DisplayRole)
100         return MessageFilter::data(index, role);
101
102     BufferId bufid = data(index, ChatLineModel::BufferIdRole).value<BufferId>();
103     if (!bufid.isValid()) {
104         qDebug() << "ChatMonitorFilter::data(): chatline belongs to an invalid buffer!";
105         return QVariant();
106     }
107
108     QModelIndex source_index = mapToSource(index);
109
110     QStringList fields;
111     if (_showFields & NetworkField) {
112         fields << Client::networkModel()->networkName(bufid);
113     }
114     if (_showFields & BufferField) {
115         fields << Client::networkModel()->bufferName(bufid);
116     }
117
118     Message::Type messageType = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
119     if (messageType & (Message::Plain | Message::Notice)) {
120         QString sender = MessageFilter::data(index, ChatLineModel::EditRole).toString();
121         fields << sender;
122     }
123     return QString("<%1>").arg(fields.join(":"));
124 }
125
126
127 void ChatMonitorFilter::addShowField(int field)
128 {
129     if (_showFields & field)
130         return;
131
132     ChatViewSettings(idString()).setValue("ShowFields", _showFields | field);
133 }
134
135
136 void ChatMonitorFilter::removeShowField(int field)
137 {
138     if (!(_showFields & field))
139         return;
140
141     ChatViewSettings(idString()).setValue("ShowFields", _showFields ^ field);
142 }
143
144
145 void ChatMonitorFilter::setShowOwnMessages(bool show)
146 {
147     if (_showOwnMessages == show)
148         return;
149
150     ChatViewSettings(idString()).setValue("ShowOwnMsgs", show);
151 }
152
153
154 void ChatMonitorFilter::showFieldsSettingChanged(const QVariant &newValue)
155 {
156     int newFields = newValue.toInt();
157     if (_showFields == newFields)
158         return;
159
160     _showFields = newFields;
161
162     int rows = rowCount();
163     if (rows == 0)
164         return;
165
166     emit dataChanged(index(0, ChatLineModel::SenderColumn), index(rows - 1, ChatLineModel::SenderColumn));
167 }
168
169
170 void ChatMonitorFilter::showOwnMessagesSettingChanged(const QVariant &newValue)
171 {
172     _showOwnMessages = newValue.toBool();
173 }
174
175
176 void ChatMonitorFilter::showHighlightsSettingChanged(const QVariant &newValue)
177 {
178     _showHighlights = newValue.toBool();
179 }
180
181
182 void ChatMonitorFilter::operationModeSettingChanged(const QVariant &newValue)
183 {
184     _operationMode = newValue.toInt();
185 }
186
187
188 void ChatMonitorFilter::buffersSettingChanged(const QVariant &newValue)
189 {
190     _bufferIds.clear();
191     foreach(QVariant v, newValue.toList()) {
192         _bufferIds << v.value<BufferId>();
193     }
194     invalidateFilter();
195 }
196
197 void ChatMonitorFilter::showBacklogSettingChanged(const QVariant &newValue) {
198     _showBacklog = newValue.toBool();
199 }