File indexing completed on 2024-04-21 05:50:37

0001 /*
0002     SPDX-FileCopyrightText: 2008, 2009, 2010, 2012, 2013, 2016, 2017 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 "KGpgRootNode.h"
0007 
0008 #include "KGpgGroupNode.h"
0009 #include "kgpginterface.h"
0010 #include "KGpgOrphanNode.h"
0011 #include "kgpgsettings.h"
0012 
0013 #include <QString>
0014 
0015 KGpgRootNode::KGpgRootNode(KGpgItemModel *model)
0016     : KGpgExpandableNode(nullptr),
0017     m_groups(0)
0018 {
0019     m_model = model;
0020 }
0021 
0022 KGpgRootNode::~KGpgRootNode()
0023 {
0024     // clear the parents in all children to signal them not to do any
0025     // update signalling
0026     for (KGpgNode *child: std::as_const(children))
0027         child->setParent(nullptr);
0028 }
0029 
0030 void
0031 KGpgRootNode::readChildren()
0032 {
0033 }
0034 
0035 KgpgCore::KgpgItemType
0036 KGpgRootNode::getType() const
0037 {
0038     return {};
0039 }
0040 
0041 void
0042 KGpgRootNode::addGroups(const QStringList &groups)
0043 {
0044     for (const QString &group : groups) {
0045         const QStringList parts = group.split(QLatin1Char(':'));
0046         if (parts.count() < 2)
0047             continue;
0048         const QString groupName = parts.first();
0049         new KGpgGroupNode(this, groupName, parts.at(1).split(QLatin1Char(';'), Qt::SkipEmptyParts));
0050     }
0051 }
0052 
0053 void
0054 KGpgRootNode::addKeys(const QStringList &ids)
0055 {
0056     const KgpgCore::KgpgKeyList publiclist = KgpgInterface::readPublicKeys(ids);
0057     KgpgCore::KgpgKeyList secretlist = KgpgInterface::readSecretKeys();
0058 
0059     QStringList issec = secretlist;
0060 
0061     for (KgpgCore::KgpgKey key : publiclist) {
0062         int index = issec.indexOf(key.fullId());
0063 
0064         if (index >= 0) {
0065             key.setSecret(true);
0066             issec.removeAt(index);
0067             secretlist.removeAt(index);
0068         }
0069 
0070         KGpgKeyNode *nd = new KGpgKeyNode(this, key);
0071         Q_EMIT newKeyNode(nd);
0072     }
0073 
0074     for (const KgpgCore::KgpgKey &key : std::as_const(secretlist))
0075         new KGpgOrphanNode(this, key);
0076 }
0077 
0078 void
0079 KGpgRootNode::refreshKeys(KGpgKeyNode::List nodes)
0080 {
0081     QStringList ids;
0082     ids.reserve(nodes.count());
0083 
0084     for (const KGpgNode *nd : std::as_const(nodes))
0085         ids << nd->getId();
0086 
0087     const KgpgCore::KgpgKeyList publiclist = KgpgInterface::readPublicKeys(ids);
0088     QStringList issec = KgpgInterface::readSecretKeys(ids);
0089 
0090     for (KgpgCore::KgpgKey key : publiclist) {
0091         int index = issec.indexOf(key.fullId());
0092         if (index != -1) {
0093             key.setSecret(true);
0094             issec.removeAt(index);
0095         }
0096 
0097         for (int j = 0; j < nodes.count(); j++) {
0098             KGpgKeyNode *nd = nodes.at(j);
0099             if (nd->getId() == key.fullId()) {
0100                 nodes.removeAt(j);
0101                 nd->setKey(key);
0102                 break;
0103             }
0104         }
0105     }
0106 }
0107 
0108 KGpgKeyNode *
0109 KGpgRootNode::findKey(const QString &keyId)
0110 {
0111     int i = findKeyRow(keyId);
0112     if (i >= 0) {
0113         return children[i]->toKeyNode();
0114     }
0115 
0116     return nullptr;
0117 }
0118 
0119 int
0120 KGpgRootNode::findKeyRow(const QString &keyId)
0121 {
0122     int i = 0;
0123 
0124     for (const KGpgNode *node : std::as_const(children)) {
0125         if ((node->getType() & ITYPE_PAIR) == 0) {
0126             ++i;
0127             continue;
0128         }
0129 
0130         const KGpgKeyNode *key = node->toKeyNode();
0131 
0132         if (key->compareId(keyId))
0133             return i;
0134         ++i;
0135     }
0136 
0137     return -1;
0138 }
0139 
0140 int
0141 KGpgRootNode::groupChildren() const
0142 {
0143     return m_groups;
0144 }
0145 
0146 int
0147 KGpgRootNode::findKeyRow(const KGpgKeyNode *key)
0148 {
0149     for (int i = 0; i < children.count(); i++) {
0150         if (children[i] == key)
0151             return i;
0152     }
0153     return -1;
0154 }