Fixes #682 - Core crashes on client connection
[quassel.git] / src / uisupport / nickviewfilter.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 "nickviewfilter.h"
22
23 #include "buffersettings.h"
24 #include "iconloader.h"
25 #include "networkmodel.h"
26
27 /******************************************************************************************
28  * NickViewFilter
29  ******************************************************************************************/
30 NickViewFilter::NickViewFilter(const BufferId &bufferId, NetworkModel *parent)
31   : QSortFilterProxyModel(parent),
32     _bufferId(bufferId),
33     _userOnlineIcon(SmallIcon("im-user")),
34     _userAwayIcon(SmallIcon("im-user-away")),
35     _categoryOpIcon(SmallIcon("irc-operator")),
36     _categoryVoiceIcon(SmallIcon("irc-voice")),
37     _opIconLimit(UserCategoryItem::categoryFromModes("o")),
38     _voiceIconLimit(UserCategoryItem::categoryFromModes("v"))
39 {
40   setSourceModel(parent);
41   setDynamicSortFilter(true);
42   setSortCaseSensitivity(Qt::CaseInsensitive);
43   setSortRole(TreeModel::SortRole);
44
45   BufferSettings bufferSettings;
46   _showUserStateIcons = bufferSettings.showUserStateIcons();
47   bufferSettings.notify("ShowUserStateIcons", this, SLOT(showUserStateIconsChanged()));
48 }
49
50 bool NickViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
51   // root node, networkindexes, the bufferindex of the buffer this filter is active for and it's childs are accepted
52   if(!source_parent.isValid())
53     return true;
54
55   QModelIndex source_child = source_parent.child(source_row, 0);
56   return (sourceModel()->data(source_child, NetworkModel::BufferIdRole).value<BufferId>() == _bufferId);
57 }
58
59 QVariant NickViewFilter::data(const QModelIndex &index, int role) const {
60   switch(role) {
61   case Qt::DecorationRole:
62     return icon(index);
63   default:
64     return QSortFilterProxyModel::data(index, role);
65   }
66 }
67
68 QVariant NickViewFilter::icon(const QModelIndex &index) const {
69   if(!_showUserStateIcons)
70     return QVariant();
71
72   if(index.column() != 0)
73     return QVariant();
74
75   QModelIndex source_index = mapToSource(index);
76   NetworkModel::ItemType itemType = (NetworkModel::ItemType)sourceModel()->data(source_index, NetworkModel::ItemTypeRole).toInt();
77   switch(itemType) {
78   case NetworkModel::UserCategoryItemType:
79     {
80       int categoryId = sourceModel()->data(source_index, TreeModel::SortRole).toInt();
81       if(categoryId <= _opIconLimit)
82         return _categoryOpIcon;
83       if(categoryId <= _voiceIconLimit)
84         return _categoryVoiceIcon;
85       return _userOnlineIcon;
86     }
87   case NetworkModel::IrcUserItemType:
88     if(sourceModel()->data(source_index, NetworkModel::ItemActiveRole).toBool())
89       return _userOnlineIcon;
90     else
91       return _userAwayIcon;
92     break;
93   default:
94     return QVariant();
95   };
96 }
97
98 void NickViewFilter::showUserStateIconsChanged() {
99   BufferSettings bufferSettings;
100   _showUserStateIcons = bufferSettings.showUserStateIcons();
101 }