1 /***************************************************************************
2 * Copyright (C) 2005-08 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(showFieldsSettingsChanged(const QVariant &)));
35 viewSettings.notify("showOwnMsgs", this, SLOT(showOwnMessagesSettingChanged(const QVariant &)));
38 bool ChatMonitorFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const {
39 Q_UNUSED(sourceParent)
41 Message::Flags flags = (Message::Flags)sourceModel()->data(sourceModel()->index(sourceRow, 0), MessageModel::FlagsRole).toInt();
42 if(flags & Message::Backlog || (!_showOwnMessages && flags & Message::Self))
45 Message::Type type = (Message::Type)sourceModel()->data(sourceModel()->index(sourceRow, 0), MessageModel::TypeRole).toInt();
46 if(!(type & (Message::Plain | Message::Notice | Message::Action)))
52 // override this to inject display of network and channel
53 QVariant ChatMonitorFilter::data(const QModelIndex &index, int role) const {
54 if(index.column() != ChatLineModel::SenderColumn || role != ChatLineModel::DisplayRole)
55 return MessageFilter::data(index, role);
57 BufferId bufid = data(index, ChatLineModel::BufferIdRole).value<BufferId>();
58 if(!bufid.isValid()) {
59 qDebug() << "ChatMonitorFilter::data(): chatline belongs to an invalid buffer!";
63 QModelIndex source_index = mapToSource(index);
66 if(_showFields & NetworkField) {
67 fields << Client::networkModel()->networkName(bufid);
69 if(_showFields & BufferField) {
70 fields << Client::networkModel()->bufferName(bufid);
73 Message::Type messageType = (Message::Type)sourceModel()->data(source_index, MessageModel::TypeRole).toInt();
74 if(messageType & (Message::Plain | Message::Notice)) {
75 QString sender = MessageFilter::data(index, ChatLineModel::EditRole).toString();
78 return QString("<%1>").arg(fields.join(":"));
81 void ChatMonitorFilter::addShowField(int field) {
82 if(_showFields & field)
85 ChatViewSettings(idString()).setValue("showFields", _showFields | field);
88 void ChatMonitorFilter::removeShowField(int field) {
89 if(!(_showFields & field))
92 ChatViewSettings(idString()).setValue("showFields", _showFields ^ field);
95 void ChatMonitorFilter::setShowOwnMessages(bool show) {
96 if(_showOwnMessages == show)
99 ChatViewSettings(idString()).setValue("showOwnMsgs", show);
102 void ChatMonitorFilter::showFieldsSettingsChanged(const QVariant &newValue) {
103 int newFields = newValue.toInt();
104 if(_showFields == newFields)
107 _showFields = newFields;
109 int rows = rowCount();
113 emit dataChanged(index(0, ChatLineModel::SenderColumn), index(rows - 1, ChatLineModel::SenderColumn));
116 void ChatMonitorFilter::showOwnMessagesSettingChanged(const QVariant &newValue) {
117 _showOwnMessages = newValue.toBool();