File indexing completed on 2024-04-28 09:46:00

0001 /*
0002     SPDX-FileCopyrightText: 2008, 2010, 2012, 2013, 2014 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 "groupeditproxymodel.h"
0007 #include "model/kgpgitemnode.h"
0008 #include "kgpgitemmodel.h"
0009 
0010 #include <KLocalizedString>
0011 #include <QIcon>
0012 
0013 using namespace KgpgCore;
0014 
0015 GroupEditProxyModel::GroupEditProxyModel(QObject *parent, const bool invert, QList<KGpgNode *> *ids, const KgpgCore::KgpgKeyTrust mintrust)
0016     : QSortFilterProxyModel(parent),
0017     m_model(nullptr),
0018     m_invert(invert),
0019     m_ids(ids),
0020     m_mintrust(mintrust)
0021 {
0022 }
0023 
0024 void
0025 GroupEditProxyModel::setKeyModel(KGpgItemModel *md)
0026 {
0027     m_model = md;
0028     setSourceModel(md);
0029 }
0030 
0031 bool
0032 GroupEditProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0033 {
0034     QModelIndex idx = m_model->index(source_row, 0, source_parent);
0035     KGpgNode *l = m_model->nodeForIndex(idx);
0036 
0037     if (l->getType() & ~ITYPE_PAIR)
0038         return false;
0039 
0040     if (l->getTrust() < m_mintrust)
0041         return false;
0042 
0043     const KGpgKeyNode * const lk = l->toKeyNode();
0044     for (int i = 0; i < m_ids->count(); i++)
0045         if (lk->compareId(m_ids->at(i)->getId()))
0046             return !m_invert;
0047 
0048     return m_invert;
0049 }
0050 
0051 KGpgNode *
0052 GroupEditProxyModel::nodeForIndex(const QModelIndex &index) const
0053 {
0054     return m_model->nodeForIndex(mapToSource(index));
0055 }
0056 
0057 int
0058 GroupEditProxyModel::columnCount(const QModelIndex &) const
0059 {
0060     return 3;
0061 }
0062 
0063 int
0064 GroupEditProxyModel::rowCount(const QModelIndex &parent) const
0065 {
0066     if (parent.column() > 0)
0067         return 0;
0068     if (parent.isValid())
0069         return 0;
0070     if (m_model == nullptr)
0071         return 0;
0072     return QSortFilterProxyModel::rowCount(parent);
0073 }
0074 
0075 QVariant
0076 GroupEditProxyModel::data(const QModelIndex &index, int role) const
0077 {
0078     if (!index.isValid() || (index.column() >= 3))
0079         return QVariant();
0080 
0081     KGpgNode *nd = m_model->nodeForIndex(mapToSource(index));
0082 
0083     switch (role) {
0084     case Qt::ToolTipRole:
0085     case Qt::DisplayRole:
0086         switch (index.column()) {
0087         case 0:
0088             if (role == Qt::ToolTipRole)
0089                 return nd->getNameComment();
0090             else
0091                 return nd->getName();
0092         case 1:
0093             if (role == Qt::ToolTipRole) {
0094                 if (nd->toKeyNode()->getExpiration().isValid() && (nd->toKeyNode()->getExpiration() <= QDateTime::currentDateTime()))
0095                     return i18nc("Expired key", "Expired");
0096                 break;
0097             } else {
0098                 return nd->getEmail();
0099             }
0100         case 2:
0101             if (role == Qt::ToolTipRole)
0102                 return nd->toKeyNode()->getBeautifiedFingerprint();
0103             else
0104                 return nd->getId().right(8);
0105         default:
0106             break;
0107         }
0108         break;
0109     case Qt::DecorationRole:
0110         if (index.column() != 1)
0111             break;
0112 
0113         if (nd->toKeyNode()->getExpiration().isValid() && (nd->toKeyNode()->getExpiration() <= QDateTime::currentDateTime()))
0114             return QIcon::fromTheme(QLatin1String("dialog-warning"));
0115     }
0116 
0117     return QVariant();
0118 }
0119 
0120 bool
0121 GroupEditProxyModel::hasChildren(const QModelIndex &parent) const
0122 {
0123     if (m_model == nullptr)
0124         return false;
0125     if (parent.column() > 0)
0126         return false;
0127     return !parent.isValid();
0128 }
0129 
0130 QVariant
0131 GroupEditProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
0132 {
0133     if (role != Qt::DisplayRole)
0134         return QVariant();
0135 
0136     if (orientation != Qt::Horizontal)
0137         return QVariant();
0138 
0139     if (m_model == nullptr)
0140         return QVariant();
0141 
0142     switch (section) {
0143     case 0:
0144         return m_model->headerData(KEYCOLUMN_NAME, orientation, role);
0145     case 1:
0146         return m_model->headerData(KEYCOLUMN_EMAIL, orientation, role);
0147     case 2:
0148         return m_model->headerData(KEYCOLUMN_ID, orientation, role);
0149     default:
0150         return QVariant();
0151     }
0152 }