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

0001 /*
0002     SPDX-FileCopyrightText: 2008, 2009, 2010, 2012, 2014 Rolf Eike Beer <kde@opensource.sf-tec.de>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "kgpgchangekey.h"
0007 
0008 #include "model/kgpgitemnode.h"
0009 #include "transactions/kgpgchangetrust.h"
0010 #include "transactions/kgpgchangeexpire.h"
0011 #include "transactions/kgpgchangedisable.h"
0012 
0013 #include <QWidget>
0014 
0015 KGpgChangeKey::KGpgChangeKey(KGpgKeyNode *node, QWidget *widget)
0016     : QObject(nullptr),
0017     m_expiration(node->getExpiration()),
0018     m_key(*node->copyKey()),
0019     m_node(node),
0020     m_current(nullptr),
0021     m_parentWidget(widget),
0022     m_step(0),
0023     m_failed(0),
0024     m_autodestroy(false)
0025 {
0026     m_disable = !m_key.valid();
0027     m_owtrust = m_key.ownerTrust();
0028 }
0029 
0030 KGpgChangeKey::~KGpgChangeKey()
0031 {
0032     Q_ASSERT(m_current == nullptr);
0033 }
0034 
0035 void KGpgChangeKey::setExpiration(const QDateTime &date)
0036 {
0037     m_expiration = date;
0038 }
0039 
0040 void KGpgChangeKey::setDisable(const bool disable)
0041 {
0042     m_disable = disable;
0043 }
0044 
0045 void KGpgChangeKey::setOwTrust(const gpgme_validity_t trust)
0046 {
0047     m_owtrust = trust;
0048 }
0049 
0050 bool KGpgChangeKey::apply()
0051 {
0052     if (!wasChanged()) {
0053         Q_EMIT done(0);
0054         return true;
0055     }
0056 
0057     if (m_step != 0)
0058         return false;
0059 
0060     m_step = 0;
0061     m_failed = 0;
0062 
0063     nextStep(KGpgTransaction::TS_OK);
0064 
0065     return true;
0066 }
0067 
0068 void KGpgChangeKey::nextStep(int result)
0069 {
0070     if (m_step == 0) {
0071         Q_ASSERT(sender() == nullptr);
0072         Q_ASSERT(m_current == nullptr);
0073     } else {
0074         Q_ASSERT(sender() != nullptr);
0075         Q_ASSERT(sender() == m_current);
0076         sender()->deleteLater();
0077         m_current = nullptr;
0078     }
0079 
0080     m_step++;
0081 
0082     switch (m_step) {
0083     case 1:
0084         if (m_expiration != m_key.expirationDate()) {
0085             m_current = new KGpgChangeExpire(m_parentWidget, m_key.fingerprint(), m_expiration);
0086 
0087             connect(m_current, &KGpgTransaction::done, this, &KGpgChangeKey::nextStep);
0088 
0089             m_current->start();
0090             break;
0091         } else {
0092             m_step++;
0093         }
0094         // fall through
0095         Q_FALLTHROUGH();
0096     case 2:
0097         if (result == KGpgTransaction::TS_OK) {
0098             m_key.setExpiration(m_expiration);
0099         } else {
0100             m_failed |= 1;
0101         }
0102         if (m_owtrust != m_key.ownerTrust()) {
0103             m_current = new KGpgChangeTrust(m_parentWidget, m_key.fingerprint(), m_owtrust);
0104 
0105             connect(m_current, &KGpgTransaction::done, this, &KGpgChangeKey::nextStep);
0106 
0107             m_current->start();
0108             break;
0109         } else {
0110             m_step++;
0111         }
0112         // fall through
0113         Q_FALLTHROUGH();
0114     case 3:
0115         if (result == KGpgTransaction::TS_OK) {
0116             m_key.setOwnerTrust(m_owtrust);
0117         } else {
0118             m_failed |= 2;
0119         }
0120         if (m_key.valid() == m_disable) {
0121             m_current = new KGpgChangeDisable(m_parentWidget, m_key.fingerprint(), m_disable);
0122 
0123             connect(m_current, &KGpgTransaction::done, this, &KGpgChangeKey::nextStep);
0124 
0125             m_current->start();
0126             break;
0127         } else {
0128             m_step++;
0129         }
0130         // fall through
0131         Q_FALLTHROUGH();
0132     default:
0133         if (result == KGpgTransaction::TS_OK) {
0134             m_key.setValid(!m_disable);
0135         } else {
0136             m_failed |= 4;
0137         }
0138         m_step = 0;
0139         Q_EMIT done(m_failed);
0140         if (m_autodestroy) {
0141             if (m_node)
0142                 Q_EMIT keyNeedsRefresh(m_node);
0143             deleteLater();
0144         }
0145     }
0146 }
0147 
0148 bool KGpgChangeKey::wasChanged()
0149 {
0150     if (m_key.expirationDate() != m_expiration)
0151         return true;
0152 
0153     if (m_key.ownerTrust() != m_owtrust)
0154         return true;
0155 
0156     if (m_key.valid() == m_disable)
0157         return true;
0158 
0159     return false;
0160 }
0161 
0162 void KGpgChangeKey::selfdestruct(const bool applyChanges)
0163 {
0164     m_autodestroy = true;
0165 
0166     // if apply is already running it will take care of everything
0167     if (m_step != 0)
0168         return;
0169 
0170     if (applyChanges && wasChanged())
0171         apply();
0172     else
0173         deleteLater();
0174 }
0175 
0176 void KGpgChangeKey::setParentWidget(QWidget *widget)
0177 {
0178     m_parentWidget = widget;
0179     if (m_current != nullptr)
0180         m_current->setParent(widget);
0181 }