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