File indexing completed on 2024-06-23 05:14:06

0001 /*  SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB
0002 
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "trustchainwidget.h"
0007 #include "ui_trustchainwidget.h"
0008 
0009 #include "kleopatra_debug.h"
0010 
0011 #include <QDialogButtonBox>
0012 #include <QPushButton>
0013 #include <QTreeWidget>
0014 #include <QTreeWidgetItem>
0015 
0016 #include <gpgme++/key.h>
0017 
0018 #include <Libkleo/Dn>
0019 #include <Libkleo/KeyCache>
0020 
0021 class TrustChainWidget::Private
0022 {
0023 public:
0024     Private(TrustChainWidget *qq)
0025         : q(qq)
0026     {
0027         Q_UNUSED(q);
0028     }
0029 
0030     GpgME::Key key;
0031     Ui::TrustChainWidget ui;
0032 
0033 private:
0034     TrustChainWidget *const q;
0035 };
0036 
0037 TrustChainWidget::TrustChainWidget(QWidget *parent)
0038     : QWidget(parent)
0039     , d(new Private(this))
0040 {
0041     d->ui.setupUi(this);
0042 }
0043 
0044 TrustChainWidget::~TrustChainWidget()
0045 {
0046 }
0047 
0048 void TrustChainWidget::setKey(const GpgME::Key &key)
0049 {
0050     if (key.protocol() != GpgME::CMS) {
0051         qCDebug(KLEOPATRA_LOG) << "Trust chain is only supported for CMS keys";
0052         return;
0053     }
0054 
0055     d->key = key;
0056     d->ui.treeWidget->clear();
0057     const auto chain = Kleo::KeyCache::instance()->findIssuers(key, Kleo::KeyCache::RecursiveSearch | Kleo::KeyCache::IncludeSubject);
0058     if (chain.empty()) {
0059         return;
0060     }
0061     QTreeWidgetItem *last = nullptr;
0062     if (!chain.back().isRoot()) {
0063         last = new QTreeWidgetItem(d->ui.treeWidget);
0064         last->setText(0, i18n("Issuer Certificate Not Found (%1)", Kleo::DN(chain.back().issuerName()).prettyDN()));
0065         const QBrush &fg = d->ui.treeWidget->palette().brush(QPalette::Disabled, QPalette::WindowText);
0066         last->setForeground(0, fg);
0067     }
0068     for (auto it = chain.rbegin(), end = chain.rend(); it != end; ++it) {
0069         last = last ? new QTreeWidgetItem(last) : new QTreeWidgetItem(d->ui.treeWidget);
0070         last->setText(0, Kleo::DN(it->userID(0).id()).prettyDN());
0071     }
0072     d->ui.treeWidget->expandAll();
0073 }
0074 
0075 GpgME::Key TrustChainWidget::key() const
0076 {
0077     return d->key;
0078 }
0079 
0080 TrustChainDialog::TrustChainDialog(QWidget *parent)
0081     : QDialog(parent)
0082 {
0083     resize(650, 330);
0084     setWindowTitle(i18nc("@title:window", "Trust Chain"));
0085 
0086     auto l = new QVBoxLayout(this);
0087     l->addWidget(new TrustChainWidget(this));
0088 
0089     auto bbox = new QDialogButtonBox(this);
0090     auto btn = bbox->addButton(QDialogButtonBox::Close);
0091     connect(btn, &QPushButton::pressed, this, &QDialog::accept);
0092     l->addWidget(bbox);
0093 }
0094 
0095 TrustChainDialog::~TrustChainDialog()
0096 {
0097 }
0098 
0099 void TrustChainDialog::setKey(const GpgME::Key &key)
0100 {
0101     auto w = findChild<TrustChainWidget *>();
0102     Q_ASSERT(w);
0103     w->setKey(key);
0104 }
0105 
0106 GpgME::Key TrustChainDialog::key() const
0107 {
0108     auto w = findChild<TrustChainWidget *>();
0109     Q_ASSERT(w);
0110     return w->key();
0111 }
0112 
0113 #include "moc_trustchainwidget.cpp"