newly created buffer views are previewed properly in the settings dialog
[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
38 bool ChatMonitorFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const {
39   Q_UNUSED(sourceParent)
40
41   Message::Flags flags = (Message::Flags)sourceModel()->data(sourceModel()->index(sourceRow, 0), MessageModel::FlagsRole).toInt();
42   if(flags & Message::Backlog || (!_showOwnMessages && flags & Message::Self))
43     return false;
44
45   Message::Type type = (Message::Type)sourceModel()->data(sourceModel()->index(sourceRow, 0), MessageModel::TypeRole).toInt();
46   if(!(type & (Message::Plain | Message::Notice | Message::Action)))
47     return false;
48
49   return true;
50 }
51
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);
56
57   BufferId bufid = data(index, ChatLineModel::BufferIdRole).value<BufferId>();
58   if(!bufid.isValid()) {
59     qDebug() << "ChatMonitorFilter::data(): chatline belongs to an invalid buffer!";
60     return QVariant();
61   }
62
63   QModelIndex source_index = mapToSource(index);
64
65   QStringList fields;
66   if(_showFields & NetworkField) {
67     fields << Client::networkModel()->networkName(bufid);
68   }
69   if(_showFields & BufferField) {
70     fields << Client::networkModel()->bufferName(bufid);
71   }
72
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();
76     fields << sender;
77   }
78   return QString("<%1>").arg(fields.join(":"));
79 }
80
81 void ChatMonitorFilter::addShowField(int field) {
82   if(_showFields & field)
83     return;
84
85   ChatViewSettings(idString()).setValue("showFields", _showFields | field); 
86 }
87
88 void ChatMonitorFilter::removeShowField(int field) {
89   if(!(_showFields & field))
90     return;
91
92   ChatViewSettings(idString()).setValue("showFields", _showFields ^ field);
93 }
94
95 void ChatMonitorFilter::setShowOwnMessages(bool show) {
96   if(_showOwnMessages == show)
97     return;
98
99   ChatViewSettings(idString()).setValue("showOwnMsgs", show);
100 }
101
102 void ChatMonitorFilter::showFieldsSettingsChanged(const QVariant &newValue) {
103   int newFields = newValue.toInt();
104   if(_showFields == newFields)
105     return;
106
107   _showFields = newFields;
108   
109   int rows = rowCount();
110   if(rows == 0)
111     return;
112
113   emit dataChanged(index(0, ChatLineModel::SenderColumn), index(rows - 1, ChatLineModel::SenderColumn));
114 }
115
116 void ChatMonitorFilter::showOwnMessagesSettingChanged(const QVariant &newValue) {
117   _showOwnMessages = newValue.toBool();
118 }