1 /*
2    Drawpile - a collaborative drawing program.
3 
4    Copyright (C) 2019 Calle Laakkonen
5 
6    Drawpile is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation, either version 3 of the License, or
9    (at your option) any later version.
10 
11    Drawpile is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with Drawpile.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "sessionfilterproxymodel.h"
21 #include "../net/sessionlistingmodel.h"
22 #include "../net/loginsessions.h"
23 
SessionFilterProxyModel(QObject * parent)24 SessionFilterProxyModel::SessionFilterProxyModel(QObject *parent)
25 	: QSortFilterProxyModel(parent),
26 	m_showPassworded(true),
27 	m_showNsfw(true),
28 	m_showClosed(true)
29 {
30 }
31 
setShowPassworded(bool show)32 void SessionFilterProxyModel::setShowPassworded(bool show)
33 {
34 	if(m_showPassworded != show) {
35 		m_showPassworded = show;
36 		invalidateFilter();
37 	}
38 }
39 
setShowNsfw(bool show)40 void SessionFilterProxyModel::setShowNsfw(bool show)
41 {
42 	if(m_showNsfw != show) {
43 		m_showNsfw = show;
44 		invalidateFilter();
45 	}
46 }
47 
setShowClosed(bool show)48 void SessionFilterProxyModel::setShowClosed(bool show)
49 {
50 	if(m_showClosed != show) {
51 		m_showClosed = show;
52 		invalidateFilter();
53 	}
54 }
55 
filterAcceptsRow(int source_row,const QModelIndex & source_parent) const56 bool SessionFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
57 {
58 	const QModelIndex i = sourceModel()->index(source_row, 0, source_parent);
59 	int nsfwRole=0, pwRole=0, closedRole=0;
60 
61 	if(sourceModel()->inherits(SessionListingModel::staticMetaObject.className())) {
62 		// Always show the top level items (listin servers)
63 		if(!source_parent.isValid())
64 			return true;
65 
66 		nsfwRole = SessionListingModel::IsNsfwRole;
67 		pwRole = SessionListingModel::IsPasswordedRole;
68 		closedRole = SessionListingModel::IsClosedRole;
69 
70 	} else if(sourceModel()->inherits(net::LoginSessionModel::staticMetaObject.className())) {
71 		nsfwRole = net::LoginSessionModel::NsfmRole;
72 		pwRole = net::LoginSessionModel::NeedPasswordRole;
73 		closedRole = net::LoginSessionModel::ClosedRole;
74 	}
75 
76 	if(!m_showNsfw && nsfwRole) {
77 		if(i.data(nsfwRole).toBool())
78 			return false;
79 	}
80 
81 	if(!m_showPassworded && pwRole) {
82 		if(i.data(pwRole).toBool())
83 			return false;
84 	}
85 
86 	if(!m_showClosed && closedRole) {
87 		if(i.data(pwRole).toBool())
88 			return false;
89 	}
90 
91 	return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
92 }
93 
94