File indexing completed on 2021-12-21 14:36:54

0001 /*
0002     SPDX-FileCopyrightText: 2002, 2003 Jean-Baptiste Mardelle <bj@altern.org>
0003     SPDX-FileCopyrightText: 2010, 2012, 2016, 2017 Rolf Eike Beer <kde@opensource.sf-tec.de>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 
0008 #include "kgpgrevokewidget.h"
0009 
0010 #include "core/KGpgKeyNode.h"
0011 #include "kgpgsettings.h"
0012 
0013 #include <QUrl>
0014 #include <QDir>
0015 #include <KConfigGroup>
0016 #include <QDialogButtonBox>
0017 #include <QPushButton>
0018 #include <QVBoxLayout>
0019 
0020 KgpgRevokeWidget::KgpgRevokeWidget(QWidget* parent)
0021     : QWidget(parent),
0022     Ui_KgpgRevokeWidget()
0023 {
0024     setupUi(this);
0025     connect(cbSave, &QCheckBox::toggled, this, &KgpgRevokeWidget::cbSave_toggled);
0026 }
0027 
0028 void KgpgRevokeWidget::cbSave_toggled(bool isOn)
0029 {
0030     outputFile->setEnabled(isOn);
0031 }
0032 
0033 KGpgRevokeDialog::KGpgRevokeDialog(QWidget* parent, const KGpgKeyNode *node)
0034     : QDialog(parent),
0035     m_revWidget(new KgpgRevokeWidget(this)),
0036     m_id(node->getId())
0037 {
0038     setWindowTitle(i18n("Create Revocation Certificate"));
0039     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
0040     QWidget *mainWidget = new QWidget(this);
0041     QVBoxLayout *mainLayout = new QVBoxLayout(this);
0042     setLayout(mainLayout);
0043     mainLayout->addWidget(mainWidget);
0044     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0045     okButton->setDefault(true);
0046     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0047     connect(buttonBox, &QDialogButtonBox::accepted, this, &KGpgRevokeDialog::accept);
0048     connect(buttonBox, &QDialogButtonBox::rejected, this, &KGpgRevokeDialog::reject);
0049     buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
0050     setModal(true);
0051 
0052     m_revWidget->keyID->setText(i18nc("<Name> (<Email>) ID: <KeyId>", "%1 (%2) ID: %3",
0053                 node->getName(), node->getEmail(), m_id));
0054     m_revWidget->outputFile->setUrl(revokeUrl(node->getName(), node->getEmail()));
0055     m_revWidget->outputFile->setMode(KFile::File);
0056 
0057     setMinimumSize(m_revWidget->sizeHint());
0058     mainLayout->addWidget(m_revWidget);
0059     mainLayout->addWidget(buttonBox);
0060 }
0061 
0062 QString KGpgRevokeDialog::getDescription() const
0063 {
0064     return m_revWidget->textDescription->toPlainText();
0065 }
0066 
0067 int KGpgRevokeDialog::getReason() const
0068 {
0069     return m_revWidget->comboBox1->currentIndex();
0070 }
0071 
0072 QUrl KGpgRevokeDialog::saveUrl() const
0073 {
0074     if (m_revWidget->cbSave->isChecked())
0075         return m_revWidget->outputFile->url();
0076     else
0077         return QUrl();
0078 }
0079 
0080 QString KGpgRevokeDialog::getId() const
0081 {
0082     return m_id;
0083 }
0084 
0085 bool KGpgRevokeDialog::printChecked()
0086 {
0087     return m_revWidget->cbPrint->isChecked();
0088 }
0089 
0090 bool KGpgRevokeDialog::importChecked()
0091 {
0092     return m_revWidget->cbImport->isChecked();
0093 }
0094 
0095 QUrl KGpgRevokeDialog::revokeUrl(const QString& name, const QString& email)
0096 {
0097     QString revurl;
0098     const QString gpgPath(KGpgSettings::gpgConfigPath());
0099     if (!gpgPath.isEmpty())
0100         revurl = QUrl::fromLocalFile(gpgPath).adjusted(QUrl::RemoveFilename).path();
0101     else
0102         revurl = QDir::homePath() + QLatin1Char('/');
0103 
0104     if (!email.isEmpty())
0105         revurl += email.section(QLatin1Char('@'), 0, 0);
0106     else
0107         revurl += name.section(QLatin1Char(' '), 0, 0);
0108 
0109     return QUrl::fromLocalFile(revurl + QLatin1String( ".revoke" ));
0110 }