groupeditproxymodel.cpp 4.14 KB
Newer Older
1
/* Copyright 2008,2010,2012,2013,2014  Rolf Eike Beer <kde@opensource.sf-tec.de>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License or (at your option) version 3 or any later version
 * accepted by the membership of KDE e.V. (or its successor approved
 * by the membership of KDE e.V.), which shall act as a proxy
 * defined in Section 14 of version 3 of the license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
19
#include "groupeditproxymodel.h"
Rolf Eike Beer's avatar
Rolf Eike Beer committed
20
#include "model/kgpgitemnode.h"
21 22 23
#include "kgpgitemmodel.h"
#include "core/kgpgkey.h"

Andrius Štikonas's avatar
Andrius Štikonas committed
24
#include <KLocalizedString>
25
#include <QIcon>
26 27 28

using namespace KgpgCore;

29
GroupEditProxyModel::GroupEditProxyModel(QObject *parent, const bool invert, QList<KGpgNode *> *ids, const KgpgCore::KgpgKeyTrust mintrust)
30
	: QSortFilterProxyModel(parent),
Rolf Eike Beer's avatar
Rolf Eike Beer committed
31
	m_model(nullptr),
32 33 34
	m_invert(invert),
	m_ids(ids),
	m_mintrust(mintrust)
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
{
}

void
GroupEditProxyModel::setKeyModel(KGpgItemModel *md)
{
	m_model = md;
	setSourceModel(md);
}

bool
GroupEditProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
	QModelIndex idx = m_model->index(source_row, 0, source_parent);
	KGpgNode *l = m_model->nodeForIndex(idx);

51
	if (l->getType() & ~ITYPE_PAIR)
52 53
		return false;

54
	if (l->getTrust() < m_mintrust)
55 56
		return false;

57
	const KGpgKeyNode * const lk = l->toKeyNode();
58
	for (int i = 0; i < m_ids->count(); i++)
59
		if (lk->compareId(m_ids->at(i)->getId()))
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
			return !m_invert;

	return m_invert;
}

KGpgNode *
GroupEditProxyModel::nodeForIndex(const QModelIndex &index) const
{
	return m_model->nodeForIndex(mapToSource(index));
}

int
GroupEditProxyModel::columnCount(const QModelIndex &) const
{
	return 3;
}

Rolf Eike Beer's avatar
Rolf Eike Beer committed
77 78 79 80 81 82 83
int
GroupEditProxyModel::rowCount(const QModelIndex &parent) const
{
	if (parent.column() > 0)
		return 0;
	if (parent.isValid())
		return 0;
Rolf Eike Beer's avatar
Rolf Eike Beer committed
84
	if (m_model == nullptr)
Rolf Eike Beer's avatar
Rolf Eike Beer committed
85 86 87 88
		return 0;
	return QSortFilterProxyModel::rowCount(parent);
}

89 90 91
QVariant
GroupEditProxyModel::data(const QModelIndex &index, int role) const
{
92 93 94
	if (!index.isValid() || (index.column() >= 3))
		return QVariant();

95
	KGpgNode *nd = m_model->nodeForIndex(mapToSource(index));
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

	switch (role) {
	case Qt::ToolTipRole:
	case Qt::DisplayRole:
		switch (index.column()) {
		case 0:
			if (role == Qt::ToolTipRole)
				return nd->getNameComment();
			else
				return nd->getName();
		case 1:
			if (role == Qt::ToolTipRole) {
				if (nd->toKeyNode()->getExpiration().isValid() && (nd->toKeyNode()->getExpiration() <= QDateTime::currentDateTime()))
					return i18nc("Expired key", "Expired");
				break;
			} else {
				return nd->getEmail();
			}
		case 2:
			if (role == Qt::ToolTipRole)
				return nd->toKeyNode()->getBeautifiedFingerprint();
			else
				return nd->getId().right(8);
		default:
			break;
		}
122
		break;
123 124 125 126 127 128
	case Qt::DecorationRole:
		if (index.column() != 1)
			break;

		if (nd->toKeyNode()->getExpiration().isValid() && (nd->toKeyNode()->getExpiration() <= QDateTime::currentDateTime()))
			return QIcon::fromTheme(QLatin1String("dialog-warning"));
129 130 131 132 133
	}

	return QVariant();
}

134
bool
Rolf Eike Beer's avatar
Rolf Eike Beer committed
135
GroupEditProxyModel::hasChildren(const QModelIndex &parent) const
136
{
Rolf Eike Beer's avatar
Rolf Eike Beer committed
137
	if (m_model == nullptr)
Rolf Eike Beer's avatar
Rolf Eike Beer committed
138 139 140 141
		return false;
	if (parent.column() > 0)
		return false;
	return !parent.isValid();
142 143
}

144 145
QVariant
GroupEditProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
146
{
147 148
	if (role != Qt::DisplayRole)
		return QVariant();
149

150 151 152
	if (orientation != Qt::Horizontal)
		return QVariant();

Rolf Eike Beer's avatar
Rolf Eike Beer committed
153
	if (m_model == nullptr)
154 155
		return QVariant();

156
	switch (section) {
Rolf Eike Beer's avatar
Rolf Eike Beer committed
157 158 159 160 161 162 163 164
	case 0:
		return m_model->headerData(KEYCOLUMN_NAME, orientation, role);
	case 1:
		return m_model->headerData(KEYCOLUMN_EMAIL, orientation, role);
	case 2:
		return m_model->headerData(KEYCOLUMN_ID, orientation, role);
	default:
		return QVariant();
165
	}
166
}