e51b2cd9e1c2916c42d3071f83de7b6e49f5197e
[quassel.git] / src / qtui / chatmonitorfilter.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-08 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 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, 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
28 ChatMonitorFilter::ChatMonitorFilter(MessageModel *model, QObject *parent)
29   : MessageFilter(model, parent)
30 {
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 &)));
36
37   // ChatMonitorSettingsPage
38   QString highlightAlwaysSettingsId = "HighlightAlways";
39   QString operationModeSettingsId = "OperationMode";
40   QString buffersSettingsId = "Buffers";
41
42   _highlightAlways = viewSettings.value(highlightAlwaysSettingsId, 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>();
47
48   viewSettings.notify(highlightAlwaysSettingsId, this, SLOT(highlightAlwaysSettingsChanged(const QVariant &)));
49   viewSettings.notify(operationModeSettingsId, this, SLOT(operationModeSettingsChanged(const QVariant &)));
50   viewSettings.notify(buffersSettingsId, this, SLOT(buffersSettingsChanged(const QVariant &)));
51 }
52
53 bool ChatMonitorFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const {
54   Q_UNUSED(sourceParent)
55
56   Message::Flags flags = (Message::Flags)sourceModel()->data(sourceModel()->index(sourceRow, 0), MessageModel::FlagsRole).toInt();
57   if(flags & Message::Backlog || (!_showOwnMessages && flags & Message::Self))
58     return false;
59
60   Message::Type type = (Message::Type)sourceModel()->data(sourceModel()->index(sourceRow, 0), MessageModel::TypeRole).toInt();
61   if(!(type & (Message::Plain | Message::Notice | Message::Action)))
62     return false;
63
64   // ChatMonitorSettingsPage
65   if (_operationMode == ChatViewSettings::OptOut && !(_highlightAlways && flags & Message::Highlight) &&  _bufferIds.contains(sourceModel()->data(sourceModel()->index(sourceRow, 0), MessageModel::BufferIdRole).value<BufferId>()))
66     return false;
67   if (_operationMode == ChatViewSettings::OptIn && !(_highlightAlways && flags & Message::Highlight) && !_bufferIds.contains(sourceModel()->data(sourceModel()->index(sourceRow, 0), MessageModel::BufferIdRole).value<BufferId>()))
68     return false;
69
70   return true;
71 }
72
73 // override this to inject display of network and channel
74 QVariant ChatMonitorFilter::data(const QModelIndex &index, int role) const {
75   if(index.column() != ChatLineModel::SenderColumn || role != ChatLineModel::DisplayRole)
76     return MessageFilter::data(index, role);
77
78   BufferId bufid = data(index, ChatLineModel::BufferIdRole).value<BufferId>();
79   if(!bufid.isValid()) {
80     qDebug() << "ChatMonitorFilter::data(): chatline belongs to an invalid buffer!";
81     return QVariant();
82   }
83
84   QModelIndex source_index = mapToSource(index);
85
86   QStringList fields;
87   if(_showFields & NetworkField) {
88     fields << Client::networkModel()->networkName(bufid);
89   }
90   if(_showFields & BufferField) {
91     fields << Client::networkModel()->bufferName(bufid);
92   }
93
94   Message::Type messageType = (Message::Type)sourceModel()->data(source_index, MessageModel::TypeRole).toInt();
95   if(messageType & (Message::Plain | Message::Notice)) {
96     QString sender = MessageFilter::data(index, ChatLineModel::EditRole).toString();
97     fields << sender;
98   }
99   return QString("<%1>").arg(fields.join(":"));
100 }
101
102 void ChatMonitorFilter::addShowField(int field) {
103   if(_showFields & field)
104     return;
105
106   ChatViewSettings(idString()).setValue("showFields", _showFields | field); 
107 }
108
109 void ChatMonitorFilter::removeShowField(int field) {
110   if(!(_showFields & field))
111     return;
112
113   ChatViewSettings(idString()).setValue("showFields", _showFields ^ field);
114 }
115
116 void ChatMonitorFilter::setShowOwnMessages(bool show) {
117   if(_showOwnMessages == show)
118     return;
119
120   ChatViewSettings(idString()).setValue("showOwnMsgs", show);
121 }
122
123 void ChatMonitorFilter::showFieldsSettingsChanged(const QVariant &newValue) {
124   int newFields = newValue.toInt();
125   if(_showFields == newFields)
126     return;
127
128   _showFields = newFields;
129   
130   int rows = rowCount();
131   if(rows == 0)
132     return;
133
134   emit dataChanged(index(0, ChatLineModel::SenderColumn), index(rows - 1, ChatLineModel::SenderColumn));
135 }
136
137 void ChatMonitorFilter::showOwnMessagesSettingChanged(const QVariant &newValue) {
138   _showOwnMessages = newValue.toBool();
139 }
140
141 void ChatMonitorFilter::highlightAlwaysSettingsChanged(const QVariant &newValue) {
142   _highlightAlways = newValue.toBool();
143 }
144
145 void ChatMonitorFilter::operationModeSettingsChanged(const QVariant &newValue) {
146   _operationMode = newValue.toInt();
147 }
148
149 void ChatMonitorFilter::buffersSettingsChanged(const QVariant &newValue) {
150   _bufferIds.clear();
151   foreach (QVariant v, newValue.toList()) {
152     _bufferIds << v.value<BufferId>();
153   }
154 }