File indexing completed on 2025-01-19 04:46:49

0001 /*
0002   SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "pgpkeymemento.h"
0008 
0009 #include <QGpgME/KeyListJob>
0010 #include <QGpgME/Protocol>
0011 #include <gpgme++/keylistresult.h>
0012 
0013 PgpKeyMemento::PgpKeyMemento()
0014     : QObject(nullptr)
0015     , MimeTreeParser::Interface::BodyPartMemento()
0016 {
0017 }
0018 
0019 PgpKeyMemento::~PgpKeyMemento() = default;
0020 
0021 void PgpKeyMemento::detach()
0022 {
0023     disconnect(this, SIGNAL(update(MimeTreeParser::UpdateMode)), nullptr, nullptr);
0024 }
0025 
0026 void PgpKeyMemento::notify()
0027 {
0028     Q_EMIT update(MimeTreeParser::Force);
0029 }
0030 
0031 bool PgpKeyMemento::isRunning() const
0032 {
0033     return mIsRunning;
0034 }
0035 
0036 void PgpKeyMemento::setRunning(bool running)
0037 {
0038     mIsRunning = running;
0039 }
0040 
0041 GpgME::Key PgpKeyMemento::key() const
0042 {
0043     return mKey;
0044 }
0045 
0046 QString PgpKeyMemento::error() const
0047 {
0048     return mError;
0049 }
0050 
0051 bool PgpKeyMemento::start(const QString &fingerprint)
0052 {
0053     auto job = QGpgME::openpgp()->keyListJob(false, false, true);
0054     connect(job, &QGpgME::KeyListJob::nextKey, this, &PgpKeyMemento::onKeyReceived);
0055     connect(job, &QGpgME::KeyListJob::result, this, &PgpKeyMemento::onListJobFinished);
0056     job->start({fingerprint});
0057 
0058     setRunning(true);
0059     return true;
0060 }
0061 
0062 void PgpKeyMemento::exec(const QString &fingerprint)
0063 {
0064     auto job = QGpgME::openpgp()->keyListJob(false, false, true);
0065     std::vector<GpgME::Key> outKeys;
0066     auto result = job->exec({fingerprint}, false, outKeys);
0067     if (result.error()) {
0068         mError = QString::fromStdString(result.error().asString());
0069     } else if (!outKeys.empty()) {
0070         mKey = outKeys[0];
0071     }
0072 }
0073 
0074 void PgpKeyMemento::onKeyReceived(const GpgME::Key &key)
0075 {
0076     mKey = key;
0077 }
0078 
0079 void PgpKeyMemento::onListJobFinished(const GpgME::KeyListResult &result)
0080 {
0081     if (result.error()) {
0082         mError = QString::fromStdString(result.error().asString());
0083     }
0084 
0085     setRunning(false);
0086     notify();
0087 }
0088 
0089 #include "moc_pgpkeymemento.cpp"