src: Yearly copyright bump
[quassel.git] / src / uisupport / bufferviewoverlayfilter.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "bufferviewoverlayfilter.h"
22
23 #include "bufferviewoverlay.h"
24 #include "networkmodel.h"
25 #include "types.h"
26
27 BufferViewOverlayFilter::BufferViewOverlayFilter(QAbstractItemModel* model, BufferViewOverlay* overlay)
28     : QSortFilterProxyModel(model)
29     , _overlay(nullptr)
30 {
31     setOverlay(overlay);
32     setSourceModel(model);
33
34     setDynamicSortFilter(true);
35 }
36
37 void BufferViewOverlayFilter::setOverlay(BufferViewOverlay* overlay)
38 {
39     if (_overlay == overlay)
40         return;
41
42     if (_overlay) {
43         disconnect(_overlay, nullptr, this, nullptr);
44     }
45
46     _overlay = overlay;
47
48     if (!overlay) {
49         invalidate();
50         return;
51     }
52
53     connect(overlay, &QObject::destroyed, this, &BufferViewOverlayFilter::overlayDestroyed);
54     connect(overlay, &BufferViewOverlay::hasChanged, this, &QSortFilterProxyModel::invalidate);
55     invalidate();
56 }
57
58 void BufferViewOverlayFilter::overlayDestroyed()
59 {
60     setOverlay(nullptr);
61 }
62
63 bool BufferViewOverlayFilter::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
64 {
65     if (!_overlay)
66         return false;
67
68     QModelIndex source_bufferIndex = sourceModel()->index(source_row, 0, source_parent);
69
70     if (!source_bufferIndex.isValid()) {
71         qWarning() << "filterAcceptsRow has been called with an invalid Child";
72         return false;
73     }
74
75     NetworkModel::ItemType itemType = (NetworkModel::ItemType)sourceModel()->data(source_bufferIndex, NetworkModel::ItemTypeRole).toInt();
76
77     NetworkId networkId = sourceModel()->data(source_bufferIndex, NetworkModel::NetworkIdRole).value<NetworkId>();
78     if (!_overlay->networkIds().contains(networkId) && !_overlay->allNetworks()) {
79         return false;
80     }
81     else if (itemType == NetworkModel::NetworkItemType) {
82         // network items don't need further checks.
83         return true;
84     }
85
86     int activityLevel = sourceModel()->data(source_bufferIndex, NetworkModel::BufferActivityRole).toInt();
87     if (_overlay->minimumActivity() > activityLevel)
88         return false;
89
90     int bufferType = sourceModel()->data(source_bufferIndex, NetworkModel::BufferTypeRole).toInt();
91     if (!(_overlay->allowedBufferTypes() & bufferType))
92         return false;
93
94     BufferId bufferId = sourceModel()->data(source_bufferIndex, NetworkModel::BufferIdRole).value<BufferId>();
95     Q_ASSERT(bufferId.isValid());
96
97     if (_overlay->bufferIds().contains(bufferId))
98         return true;
99
100     if (_overlay->tempRemovedBufferIds().contains(bufferId))
101         return activityLevel > BufferInfo::OtherActivity;
102
103     if (_overlay->removedBufferIds().contains(bufferId))
104         return false;
105
106     // the buffer is not known to us
107     qDebug() << "BufferViewOverlayFilter::filterAcceptsRow()" << bufferId << "is unknown!";
108     return false;
109 }