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()
39 for (
auto i = 0; i < q->
sourceModel()->rowCount(); ++i) {
40 const auto key = q->
sourceModel()->index(i, 0).data(KeyList::KeyRole).value<GpgME::Key>();
43 mIds += q->
sourceModel()->index(i, 0).data(KeyList::GroupRole).value<KeyGroup>();
44 }
else if (key.protocol() == GpgME::OpenPGP) {
45 for (
const auto &userID : key.userIDs()) {
50 for (
const auto &userID : key.userIDs()) {
51 const auto exists = Kleo::contains_if(ids, [userID](
const auto &other) {
52 return !qstrcmp(std::get<GpgME::UserID>(other).email(), userID.email());
54 if (!exists && userID.email() && *userID.email()) {
58 if (ids.
count() > 0) {
61 mIds.
append(key.userID(0));
68UserIDProxyModel::Private::Private(UserIDProxyModel *qq)
71 connect(q, &UserIDProxyModel::sourceModelChanged, q, [
this]() {
73 disconnect(oldSourceModel,
nullptr, q,
nullptr);
84 oldSourceModel = q->sourceModel();
89UserIDProxyModel::UserIDProxyModel(
QObject *parent)
90 : AbstractKeyListSortFilterProxyModel(parent)
91 , d{new Private(this)}
95UserIDProxyModel::~UserIDProxyModel() =
default;
111 const auto &sourceKey = sourceIndex.
data(KeyList::KeyRole).
value<GpgME::Key>();
112 if (sourceKey.isNull()) {
113 const auto &sourceKeyGroup = sourceIndex.
data(KeyList::GroupRole).
value<KeyGroup>();
114 for (
int i = 0; i < d->mIds.count(); ++i) {
115 if (std::holds_alternative<KeyGroup>(d->mIds[i]) && std::get<KeyGroup>(d->mIds[i]).id() == sourceKeyGroup.id()) {
116 return index(i, sourceIndex.
column(), {});
120 const auto &fingerprint = sourceKey.primaryFingerprint();
121 for (
int i = 0; i < d->mIds.count(); ++i) {
122 if (std::holds_alternative<GpgME::UserID>(d->mIds[i]) && !qstrcmp(fingerprint, std::get<GpgME::UserID>(d->mIds[i]).parent().primaryFingerprint())) {
123 return index(i, sourceIndex.
column(), {});
136 const auto &entry = d->mIds[proxyIndex.
row()];
138 if (std::holds_alternative<KeyGroup>(entry)) {
139 const auto &
id = std::get<KeyGroup>(entry).id();
140 for (
int i = 0; i <
sourceModel()->rowCount(); ++i) {
141 if (
sourceModel()->index(i, 0).data(KeyList::GroupRole).value<KeyGroup>().
id() ==
id) {
146 const auto &fingerprint = std::get<GpgME::UserID>(entry).parent().primaryFingerprint();
147 for (
int i = 0; i <
sourceModel()->rowCount(); ++i) {
148 if (!qstrcmp(
sourceModel()->index(i, 0).data(KeyList::KeyRole).value<GpgME::Key>().primaryFingerprint(), fingerprint)) {
157int UserIDProxyModel::rowCount(
const QModelIndex &parent)
const
162 return d->mIds.count();
178int UserIDProxyModel::columnCount(
const QModelIndex &index)
const
183 return sourceModel()->columnCount(mapToSource(index));
188 const auto &entry = d->mIds[index.
row()];
189 if (std::holds_alternative<KeyGroup>(entry)) {
192 const auto &userId = std::get<GpgME::UserID>(entry);
193 const auto &key = userId.parent();
194 if (role == KeyList::UserIDRole) {
198 if (index.
column() == KeyList::Columns::PrettyName) {
199 if (key.protocol() == GpgME::OpenPGP) {
200 return Formatting::prettyName(userId);
202 return Formatting::prettyName(key);
205 if (index.
column() == KeyList::Columns::PrettyEMail) {
206 return Formatting::prettyEMail(userId);
208 if (index.
column() == KeyList::Columns::Validity) {
209 return Formatting::complianceStringShort(userId);
211 if (index.
column() == KeyList::Columns::Summary) {
212 return Formatting::summaryLine(userId);
214 if (index.
column() == KeyList::Columns::Origin) {
215 return Formatting::origin(userId.origin());
217 if (index.
column() == KeyList::Columns::LastUpdate) {
219 return Formatting::accessibleDate(userId.lastUpdate());
221 return Formatting::dateString(userId.lastUpdate());
226 if (!SystemInfo::isHighContrastModeActive()) {
227 return returnIfValid(KeyFilterManager::instance()->bgColor(userId));
230 if (!SystemInfo::isHighContrastModeActive()) {
231 return returnIfValid(KeyFilterManager::instance()->fgColor(userId));
237UserIDProxyModel *UserIDProxyModel::clone()
const
244QModelIndex UserIDProxyModel::index(
const KeyGroup &group)
const
250QModelIndex UserIDProxyModel::index(
const GpgME::Key &key)
const
256#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)
qsizetype count() const const
void reserve(qsizetype size)
QVariant data(int role) const const
bool isValid() const const
QObject * parent() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QVariant fromValue(T &&value)