chatmonitor shows own messages again (per default) (configurable via context menu)
[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 "buffer.h"
24 #include "client.h"
25 #include "chatlinemodel.h"
26 #include "networkmodel.h"
27
28 ChatMonitorFilter::ChatMonitorFilter(MessageModel *model, QObject *parent)
29   : MessageFilter(model, parent)
30 {
31   QtUiSettings qtUiSettings;
32   QString showFieldSettingId = QString("ChatView/%1/showFields").arg(idString());
33   QString showOwnMessagesSettingId = QString("ChatView/%1/showOwnMsgs").arg(idString());
34
35   _showFields = qtUiSettings.value(showFieldSettingId, AllFields).toInt();
36   _showOwnMessages = qtUiSettings.value(showOwnMessagesSettingId, true).toBool();
37   qtUiSettings.notify(showFieldSettingId, this, SLOT(showFieldsSettingsChanged(const QVariant &)));
38   qtUiSettings.notify(showOwnMessagesSettingId, this, SLOT(showOwnMessagesSettingChanged(const QVariant &)));
39 }
40
41 bool ChatMonitorFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const {
42   Q_UNUSED(sourceParent)
43
44   Message::Flags flags = (Message::Flags)sourceModel()->data(sourceModel()->index(sourceRow, 0), MessageModel::FlagsRole).toInt();
45   if(flags & Message::Backlog || (!_showOwnMessages && flags & Message::Self))
46     return false;
47
48   Message::Type type = (Message::Type)sourceModel()->data(sourceModel()->index(sourceRow, 0), MessageModel::TypeRole).toInt();
49   if(!(type & (Message::Plain | Message::Notice | Message::Action)))
50     return false;
51
52   return true;
53 }
54
55 // override this to inject display of network and channel
56 QVariant ChatMonitorFilter::data(const QModelIndex &index, int role) const {
57   if(index.column() != ChatLineModel::SenderColumn || role != ChatLineModel::DisplayRole)
58     return MessageFilter::data(index, role);
59
60   BufferId bufid = data(index, ChatLineModel::BufferIdRole).value<BufferId>();
61   if(!bufid.isValid()) {
62     qDebug() << "ChatMonitorFilter::data(): chatline belongs to an invalid buffer!";
63     return QVariant();
64   }
65
66   QModelIndex source_index = mapToSource(index);
67
68   QStringList fields;
69   if(_showFields & NetworkField) {
70     fields << Client::networkModel()->networkName(bufid);
71   }
72   if(_showFields & BufferField) {
73     fields << Client::networkModel()->bufferName(bufid);
74   }
75
76   Message::Type messageType = (Message::Type)sourceModel()->data(source_index, MessageModel::TypeRole).toInt();
77   if(messageType & (Message::Plain | Message::Notice)) {
78     QString sender = MessageFilter::data(index, role).toString();
79     // we have to strip leading and traling < / >
80     fields << sender.mid(1, sender.count() - 2);
81   }
82   return QString("<%1>").arg(fields.join(":"));
83 }
84
85 void ChatMonitorFilter::addShowField(int field) {
86   if(_showFields & field)
87     return;
88
89   QtUiSettings().setValue(QString("ChatView/%1/showFields").arg(idString()), _showFields | field); 
90 }
91
92 void ChatMonitorFilter::removeShowField(int field) {
93   if(!(_showFields & field))
94     return;
95
96   QtUiSettings().setValue(QString("ChatView/%1/showFields").arg(idString()), _showFields ^ field);
97 }
98
99 void ChatMonitorFilter::setShowOwnMessages(bool show) {
100   if(_showOwnMessages == show)
101     return;
102
103   QtUiSettings().setValue(QString("ChatView/%1/showOwnMsgs").arg(idString()), show);
104 }
105
106 void ChatMonitorFilter::showFieldsSettingsChanged(const QVariant &newValue) {
107   int newFields = newValue.toInt();
108   if(_showFields == newFields)
109     return;
110
111   _showFields = newFields;
112   
113   int rows = rowCount();
114   if(rows == 0)
115     return;
116
117   emit dataChanged(index(0, ChatLineModel::SenderColumn), index(rows - 1, ChatLineModel::SenderColumn));
118 }
119
120 void ChatMonitorFilter::showOwnMessagesSettingChanged(const QVariant &newValue) {
121   _showOwnMessages = newValue.toBool();
122 }