7#include "useridproxymodel.h"
10#include "keylistmodel.h"
11#include "kleo/keyfiltermanager.h"
12#include "utils/algorithm.h"
13#include "utils/formatting.h"
14#include "utils/systeminfo.h"
16#include <gpgme++/global.h>
24class UserIDProxyModel::Private
27 Private(UserIDProxyModel *qq);
34void UserIDProxyModel::Private::loadUserIDs()
37 auto oldIndicesSize = mSourceIndices.
size();
39 mSourceIndices.
clear();
41 mSourceIndices.
reserve(std::max(mIds.
size(), oldIndicesSize));
42 for (
auto i = 0; i < q->
sourceModel()->rowCount(); ++i) {
43 const auto key = q->
sourceModel()->index(i, 0).data(KeyList::KeyRole).value<GpgME::Key>();
45 mIds += q->
sourceModel()->index(i, 0).data(KeyList::GroupRole).value<KeyGroup>();
47 }
else if (key.protocol() == GpgME::OpenPGP) {
48 for (
const auto &userID : key.userIDs()) {
55 for (
const auto &userID : key.userIDs()) {
56 const auto exists = Kleo::contains_if(ids, [userID](
const auto &other) {
57 return !qstrcmp(std::get<GpgME::UserID>(other).email(), userID.email());
59 if (!exists && userID.email() && *userID.email()) {
66 mSourceIndices.
append(indices);
68 mIds.
append(key.userID(0));
76UserIDProxyModel::Private::Private(UserIDProxyModel *qq)
81UserIDProxyModel::UserIDProxyModel(
QObject *parent)
82 : AbstractKeyListSortFilterProxyModel(parent)
83 , d{new Private(this)}
87UserIDProxyModel::~UserIDProxyModel() =
default;
103 const auto &sourceKey = sourceIndex.
data(KeyList::KeyRole).
value<GpgME::Key>();
104 if (sourceKey.isNull()) {
105 const auto &sourceKeyGroup = sourceIndex.
data(KeyList::GroupRole).
value<KeyGroup>();
106 for (
int i = 0; i < d->mIds.count(); ++i) {
107 if (std::holds_alternative<KeyGroup>(d->mIds[i]) && std::get<KeyGroup>(d->mIds[i]).id() == sourceKeyGroup.id()) {
108 return index(i, sourceIndex.
column(), {});
112 const auto &fingerprint = sourceKey.primaryFingerprint();
113 for (
int i = 0; i < d->mIds.count(); ++i) {
114 if (std::holds_alternative<GpgME::UserID>(d->mIds[i]) && !qstrcmp(fingerprint, std::get<GpgME::UserID>(d->mIds[i]).parent().primaryFingerprint())) {
115 return index(i, sourceIndex.
column(), {});
132int UserIDProxyModel::rowCount(
const QModelIndex &parent)
const
137 return d->mIds.count();
153int UserIDProxyModel::columnCount(
const QModelIndex &index)
const
158 return sourceModel()->columnCount(mapToSource(index));
163 const auto &entry = d->mIds[index.
row()];
164 if (std::holds_alternative<KeyGroup>(entry)) {
167 const auto &userId = std::get<GpgME::UserID>(entry);
168 const auto &key = userId.parent();
169 if (role == KeyList::UserIDRole) {
173 if (index.
column() == KeyList::Columns::PrettyName) {
174 if (key.protocol() == GpgME::OpenPGP) {
175 return Formatting::prettyName(userId);
177 return Formatting::prettyName(key);
180 if (index.
column() == KeyList::Columns::PrettyEMail) {
181 return Formatting::prettyEMail(userId);
183 if (index.
column() == KeyList::Columns::Validity) {
184 return Formatting::complianceStringShort(userId);
186 if (index.
column() == KeyList::Columns::Summary) {
187 return Formatting::summaryLine(userId);
189 if (index.
column() == KeyList::Columns::Origin) {
190 return Formatting::origin(userId.origin());
192 if (index.
column() == KeyList::Columns::LastUpdate) {
194 return Formatting::accessibleDate(userId.lastUpdate());
196 return Formatting::dateString(userId.lastUpdate());
201 if (!SystemInfo::isHighContrastModeActive()) {
202 return returnIfValid(KeyFilterManager::instance()->bgColor(userId));
205 if (!SystemInfo::isHighContrastModeActive()) {
206 return returnIfValid(KeyFilterManager::instance()->fgColor(userId));
212UserIDProxyModel *UserIDProxyModel::clone()
const
219QModelIndex UserIDProxyModel::index(
const KeyGroup &group)
const
225QModelIndex UserIDProxyModel::index(
const GpgME::Key &key)
const
254#include "moc_useridproxymodel.cpp"
QModelIndex createIndex(int row, int column, const void *ptr) const const
virtual QVariant data(const QModelIndex &index, int role) const const=0
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
void rowsInserted(const QModelIndex &parent, int first, int last)
bool isValid() const const
void append(QList< T > &&value)
bool isEmpty() const const
void push_back(parameter_type value)
void reserve(qsizetype size)
qsizetype size() const const
QVariant data(int role) const const
bool isValid() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
QObject * parent() const const
virtual void setSourceModel(QAbstractItemModel *sourceModel) override
QVariant fromValue(T &&value)