Make sure NickView::customEvent() has a valid model
[quassel.git] / src / uisupport / bufferhotlistfilter.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 "bufferhotlistfilter.h"
22
23 #include "networkmodel.h"
24
25 BufferHotListFilter::BufferHotListFilter(QAbstractItemModel *source, QObject *parent)
26   : QSortFilterProxyModel(parent)
27 {
28   setSourceModel(source);
29   setDynamicSortFilter(true);
30   sort(0, Qt::DescendingOrder); // enable sorting... this is "usually" triggered by a enabling setSortingEnabled(true) on a view;
31 }
32
33 bool BufferHotListFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
34   Q_ASSERT(sourceModel());
35   QModelIndex source_index = sourceModel()->index(source_row, 0, source_parent);
36
37   MsgId firstUnreadMsgId = sourceModel()->data(source_index, NetworkModel::BufferFirstUnreadMsgIdRole).value<MsgId>();
38   if(!firstUnreadMsgId.isValid())
39     return false;
40
41   // filter out statusbuffers (it's accessable as networkitem)
42   BufferInfo::Type bufferType = (BufferInfo::Type)sourceModel()->data(source_index, NetworkModel::BufferTypeRole).toInt();
43   if(bufferType == BufferInfo::StatusBuffer) {
44     NetworkModel::ItemType itemType = (NetworkModel::ItemType)sourceModel()->data(source_index, NetworkModel::ItemTypeRole).toInt();
45     return itemType == NetworkModel::NetworkItemType;
46   }
47
48   return true;
49 }
50
51 bool BufferHotListFilter::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
52   int leftActivity = sourceModel()->data(source_left, NetworkModel::BufferActivityRole).toInt();
53   int rightActivity = sourceModel()->data(source_right, NetworkModel::BufferActivityRole).toInt();
54   if(leftActivity != rightActivity)
55     return leftActivity < rightActivity;
56
57   MsgId leftUnreadMsgId = sourceModel()->data(source_left, NetworkModel::BufferFirstUnreadMsgIdRole).value<MsgId>();
58   MsgId rightUnreadMsgId = sourceModel()->data(source_right, NetworkModel::BufferFirstUnreadMsgIdRole).value<MsgId>();
59   return leftUnreadMsgId > rightUnreadMsgId; // newer messages are treated to be "less"
60 }
61
62 // QVariant BufferHotListFilter::data(const QModelIndex &index, int role) const {
63 //   QVariant d = QSortFilterProxyModel::data(index, role);
64
65 //   if(role == Qt::DisplayRole) {
66 //     int activity = QSortFilterProxyModel::data(index, NetworkModel::BufferActivityRole).toInt();    
67 //     MsgId unreadMsgId = QSortFilterProxyModel::data(index, NetworkModel::BufferFirstUnreadMsgIdRole).value<MsgId>();
68 //     return QString("%1 %2 %3").arg(d.toString()).arg(activity).arg(unreadMsgId.toInt());
69 //   }
70 //   return d;
71 // }