1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "chatmonitorfilter.h"
24 #include "chatlinemodel.h"
25 #include "networkmodel.h"
26 #include "chatviewsettings.h"
28 ChatMonitorFilter::ChatMonitorFilter(MessageModel *model, QObject *parent)
29 : MessageFilter(model, parent)
31 ChatViewSettings viewSettings(idString());
32 _showFields = viewSettings.value("ShowFields", AllFields).toInt();
33 _showOwnMessages = viewSettings.value("ShowOwnMsgs", true).toBool();
34 viewSettings.notify("ShowFields", this, SLOT(showFieldsSettingChanged(const QVariant &)));
35 viewSettings.notify("ShowOwnMsgs", this, SLOT(showOwnMessagesSettingChanged(const QVariant &)));
37 // ChatMonitorSettingsPage
38 QString showHighlightsSettingsId = "ShowHighlights";
39 QString operationModeSettingsId = "OperationMode";
40 QString buffersSettingsId = "Buffers";
42 _showHighlights = viewSettings.value(showHighlightsSettingsId, false).toBool();
43 _operationMode = viewSettings.value(operationModeSettingsId, 0).toInt();
44 // read configured list of buffers to monitor/ignore
45 foreach(QVariant v, viewSettings.value(buffersSettingsId, QVariant()).toList())
46 _bufferIds << v.value<BufferId>();
48 viewSettings.notify(showHighlightsSettingsId, this, SLOT(showHighlightsSettingChanged(const QVariant &)));
49 viewSettings.notify(operationModeSettingsId, this, SLOT(operationModeSettingChanged(const QVariant &)));
50 viewSettings.notify(buffersSettingsId, this, SLOT(buffersSettingChanged(const QVariant &)));
53 bool ChatMonitorFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const {
54 Q_UNUSED(sourceParent)
56 QModelIndex source_index = sourceModel()->index(sourceRow, 0);
58 Message::Flags flags = (Message::Flags)source_index.data(MessageModel::FlagsRole).toInt();
59 if(flags & Message::Backlog || (!_showOwnMessages && flags & Message::Self))
62 Message::Type type = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
63 if(!(type & (Message::Plain | Message::Notice | Message::Action)))
66 // ChatMonitorSettingsPage
67 if(_operationMode == ChatViewSettings::OptOut
68 && !(_showHighlights && flags & Message::Highlight)
69 && _bufferIds.contains(source_index.data(MessageModel::BufferIdRole).value<BufferId>()))
71 if(_operationMode == ChatViewSettings::OptIn
72 && !(_showHighlights && flags & Message::Highlight)
73 && !_bufferIds.contains(source_index.data(MessageModel::BufferIdRole).value<BufferId>()))
79 // override this to inject display of network and channel
80 QVariant ChatMonitorFilter::data(const QModelIndex &index, int role) const {
81 if(index.column() != ChatLineModel::SenderColumn || role != ChatLineModel::DisplayRole)
82 return MessageFilter::data(index, role);
84 BufferId bufid = data(index, ChatLineModel::BufferIdRole).value<BufferId>();
85 if(!bufid.isValid()) {
86 qDebug() << "ChatMonitorFilter::data(): chatline belongs to an invalid buffer!";
90 QModelIndex source_index = mapToSource(index);
93 if(_showFields & NetworkField) {
94 fields << Client::networkModel()->networkName(bufid);
96 if(_showFields & BufferField) {
97 fields << Client::networkModel()->bufferName(bufid);
100 Message::Type messageType = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
101 if(messageType & (Message::Plain | Message::Notice)) {
102 QString sender = MessageFilter::data(index, ChatLineModel::EditRole).toString();
105 return QString("<%1>").arg(fields.join(":"));
108 void ChatMonitorFilter::addShowField(int field) {
109 if(_showFields & field)
112 ChatViewSettings(idString()).setValue("ShowFields", _showFields | field);
115 void ChatMonitorFilter::removeShowField(int field) {
116 if(!(_showFields & field))
119 ChatViewSettings(idString()).setValue("ShowFields", _showFields ^ field);
122 void ChatMonitorFilter::setShowOwnMessages(bool show) {
123 if(_showOwnMessages == show)
126 ChatViewSettings(idString()).setValue("ShowOwnMsgs", show);
129 void ChatMonitorFilter::showFieldsSettingChanged(const QVariant &newValue) {
130 int newFields = newValue.toInt();
131 if(_showFields == newFields)
134 _showFields = newFields;
136 int rows = rowCount();
140 emit dataChanged(index(0, ChatLineModel::SenderColumn), index(rows - 1, ChatLineModel::SenderColumn));
143 void ChatMonitorFilter::showOwnMessagesSettingChanged(const QVariant &newValue) {
144 _showOwnMessages = newValue.toBool();
147 void ChatMonitorFilter::showHighlightsSettingChanged(const QVariant &newValue) {
148 _showHighlights = newValue.toBool();
151 void ChatMonitorFilter::operationModeSettingChanged(const QVariant &newValue) {
152 _operationMode = newValue.toInt();
155 void ChatMonitorFilter::buffersSettingChanged(const QVariant &newValue) {
157 foreach (QVariant v, newValue.toList()) {
158 _bufferIds << v.value<BufferId>();