File indexing completed on 2024-04-21 09:41:37

0001 /*
0002     SPDX-FileCopyrightText: 2008, 2010, 2012, 2013 Rolf Eike Beer <kde@opensource.sf-tec.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 #include "selectkeyproxymodel.h"
0007 #include "model/kgpgitemnode.h"
0008 #include "kgpgitemmodel.h"
0009 #include "core/kgpgkey.h"
0010 
0011 #include <QLocale>
0012 #include <QRegularExpression>
0013 
0014 #include <KLocalizedString>
0015 
0016 using namespace KgpgCore;
0017 
0018 SelectKeyProxyModel::SelectKeyProxyModel(QObject *parent)
0019     : QSortFilterProxyModel(parent),
0020     m_model(nullptr),
0021     m_showUntrusted(false)
0022 {
0023 }
0024 
0025 void
0026 SelectKeyProxyModel::setKeyModel(KGpgItemModel *md)
0027 {
0028     m_model = md;
0029     setSourceModel(md);
0030 }
0031 
0032 bool
0033 SelectKeyProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0034 {
0035     QModelIndex idx = m_model->index(source_row, 0, source_parent);
0036     KGpgNode *l = m_model->nodeForIndex(idx);
0037 
0038     switch (l->getType()) {
0039     case ITYPE_GROUP:
0040         break;
0041     case ITYPE_PAIR:
0042     case ITYPE_PUBLIC:
0043         if (!l->toKeyNode()->canEncrypt())
0044             return false;
0045         break;
0046     default:
0047         return false;
0048     }
0049 
0050     if (!m_showUntrusted && ((l->getTrust() != TRUST_FULL) && (l->getTrust() != TRUST_ULTIMATE)))
0051         return false;
0052 
0053     // there is probably a better place to do this
0054     QRegularExpression rx = filterRegularExpression();
0055     rx.setPatternOptions(rx.patternOptions() | QRegularExpression::CaseInsensitiveOption);
0056 
0057     if (l->getName().contains(rx))
0058         return true;
0059 
0060     if (l->getEmail().contains(rx))
0061         return true;
0062 
0063     if (l->getId().contains(rx))
0064         return true;
0065 
0066     return false;
0067 }
0068 
0069 KGpgNode *
0070 SelectKeyProxyModel::nodeForIndex(const QModelIndex &index) const
0071 {
0072     return m_model->nodeForIndex(mapToSource(index));
0073 }
0074 
0075 int
0076 SelectKeyProxyModel::columnCount(const QModelIndex &) const
0077 {
0078     return 3;
0079 }
0080 
0081 int
0082 SelectKeyProxyModel::rowCount(const QModelIndex &parent) const
0083 {
0084     if (parent.column() > 0)
0085         return 0;
0086     if (parent.isValid())
0087         return 0;
0088     if (m_model == nullptr)
0089         return 0;
0090     return QSortFilterProxyModel::rowCount(parent);
0091 }
0092 
0093 QVariant
0094 SelectKeyProxyModel::data(const QModelIndex &index, int role) const
0095 {
0096     if (index.column() >= 3)
0097         return QVariant();
0098 
0099     QModelIndex sidx = mapToSource(index);
0100     KGpgNode *nd = m_model->nodeForIndex(sidx);
0101 
0102     if ((index.column() == 2) && (role == Qt::ToolTipRole))
0103         return nd->getId();
0104 
0105     if ((role != Qt::DisplayRole) && (index.column() <= 1))
0106         return m_model->data(sidx, role);
0107 
0108     if (role != Qt::DisplayRole)
0109         return QVariant();
0110 
0111     switch (index.column()) {
0112         case 0: return nd->getName();
0113         case 1: return nd->getEmail();
0114         case 2: return nd->getId().right(8);
0115     }
0116 
0117     return QVariant();
0118 }
0119 
0120 bool
0121 SelectKeyProxyModel::hasChildren(const QModelIndex &parent) const
0122 {
0123     if (m_model == nullptr)
0124         return false;
0125     return !parent.isValid();
0126 }
0127 
0128 QVariant
0129 SelectKeyProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
0130 {
0131     if (role != Qt::DisplayRole)
0132         return QVariant();
0133 
0134     if (orientation != Qt::Horizontal)
0135         return QVariant();
0136 
0137     if (m_model == nullptr)
0138         return QVariant();
0139 
0140     switch (section) {
0141     case 0:
0142         return m_model->headerData(KEYCOLUMN_NAME, orientation, role);
0143     case 1:
0144         return m_model->headerData(KEYCOLUMN_EMAIL, orientation, role);
0145     case 2:
0146         return m_model->headerData(KEYCOLUMN_ID, orientation, role);
0147     default:
0148         return QVariant();
0149     }
0150 }
0151 
0152 bool
0153 SelectKeyProxyModel::showUntrusted() const
0154 {
0155     return m_showUntrusted;
0156 }
0157 
0158 void
0159 SelectKeyProxyModel::setShowUntrusted(const bool b)
0160 {
0161     m_showUntrusted = b;
0162     invalidate();
0163 }
0164 
0165 SelectSecretKeyProxyModel::SelectSecretKeyProxyModel(QObject *parent)
0166     : SelectKeyProxyModel(parent)
0167 {
0168 }
0169 
0170 bool
0171 SelectSecretKeyProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0172 {
0173     QModelIndex idx = m_model->index(source_row, 0, source_parent);
0174     KGpgNode *l = m_model->nodeForIndex(idx);
0175 
0176     return ((l->getType() == ITYPE_PAIR) && !(l->getTrust() == TRUST_EXPIRED) && !(l->getTrust() == TRUST_DISABLED));
0177 }
0178 
0179 int
0180 SelectSecretKeyProxyModel::columnCount(const QModelIndex &) const
0181 {
0182     return 4;
0183 }
0184 
0185 int
0186 SelectSecretKeyProxyModel::rowCount(const QModelIndex &parent) const
0187 {
0188     if (parent.column() > 0)
0189         return 0;
0190     if (parent.isValid())
0191         return 0;
0192     if (m_model == nullptr)
0193         return 0;
0194     return QSortFilterProxyModel::rowCount(parent);
0195 }
0196 
0197 QVariant
0198 SelectSecretKeyProxyModel::data(const QModelIndex &index, int role) const
0199 {
0200     if (index.column() >= 4)
0201         return QVariant();
0202 
0203     QModelIndex sidx = mapToSource(index);
0204     KGpgNode *nd = m_model->nodeForIndex(sidx);
0205 
0206     if ((index.column() == 3) && (role == Qt::ToolTipRole))
0207         return nd->getId();
0208 
0209     if ((role != Qt::DisplayRole) && (index.column() <= 1))
0210         return m_model->data(sidx, role);
0211 
0212     if (role != Qt::DisplayRole)
0213         return QVariant();
0214 
0215     switch (index.column()) {
0216     case 0:
0217         return nd->getName();
0218     case 1:
0219         return nd->getEmail();
0220     case 2:
0221         return QLocale().toString(nd->getExpiration().date(), QLocale::ShortFormat);
0222     case 3:
0223         return nd->getId().right(8);
0224     }
0225 
0226     return QVariant();
0227 }
0228 
0229 bool
0230 SelectSecretKeyProxyModel::hasChildren(const QModelIndex &parent) const
0231 {
0232     if (m_model == nullptr)
0233         return false;
0234     return !parent.isValid();
0235 }
0236 
0237 QVariant
0238 SelectSecretKeyProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
0239 {
0240     if (role != Qt::DisplayRole)
0241         return QVariant();
0242 
0243     if (orientation != Qt::Horizontal)
0244         return QVariant();
0245 
0246     if (m_model == nullptr)
0247         return QVariant();
0248 
0249     switch (section) {
0250     case 0:
0251         return m_model->headerData(KEYCOLUMN_NAME, orientation, role);
0252     case 1:
0253         return m_model->headerData(KEYCOLUMN_EMAIL, orientation, role);
0254     case 2:
0255         return m_model->headerData(KEYCOLUMN_EXPIR, orientation, role);
0256     case 3:
0257         return m_model->headerData(KEYCOLUMN_ID, orientation, role);
0258     default:
0259         return QVariant();
0260     }
0261 }