File indexing completed on 2024-04-28 17:08:18

0001 /*
0002  * SPDX-FileCopyrightText: 2017 Elvis Angelaccio <elvis.angelaccio@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  *
0006  */
0007 
0008 #include "encryptjob.h"
0009 #include "symmydebug.h"
0010 
0011 #include <KIO/CopyJob>
0012 
0013 #include <QTemporaryFile>
0014 
0015 #include <QGpgME/EncryptJob>
0016 #include <QGpgME/Protocol>
0017 
0018 #include <gpgme++/key.h>
0019 
0020 using namespace GpgME;
0021 using namespace QGpgME;
0022 
0023 namespace Symmy
0024 {
0025 
0026 EncryptJob::EncryptJob(const QString &passphrase, const QString &plaintextFilename) : Job {passphrase}
0027 {
0028     m_plaintext = std::make_shared<QFile>(plaintextFilename);
0029     m_ciphertext = std::make_shared<QTemporaryFile>(ciphertextFilename());
0030 }
0031 
0032 EncryptJob::~EncryptJob()
0033 {
0034 }
0035 
0036 QString EncryptJob::ciphertextFilename() const
0037 {
0038     return QStringLiteral("%1.gpg").arg(plaintextFilename());
0039 }
0040 
0041 QString EncryptJob::plaintextFilename() const
0042 {
0043     return m_plaintext->fileName();
0044 }
0045 
0046 void EncryptJob::doWork()
0047 {
0048     if (not m_plaintext->open(QIODevice::ReadOnly | QIODevice::Unbuffered)) {
0049         qCDebug(SYMMY) << "Could not open plaintext file" << m_plaintext->fileName();
0050         emitResult();
0051         return;
0052     }
0053 
0054     if (not m_ciphertext->open()) {
0055         qCDebug(SYMMY) << "Could not open ciphertext file" << m_ciphertext->fileName();
0056         emitResult();
0057         return;
0058     }
0059 
0060     auto encryptJob = openpgp()->encryptJob(false, false);
0061     auto context = QGpgME::Job::context(encryptJob);
0062     context->setPassphraseProvider(this);
0063     context->setPinentryMode(GpgME::Context::PinentryLoopback);
0064 
0065     setJob(encryptJob);
0066 
0067     connect(encryptJob, &QGpgME::EncryptJob::result, this, &EncryptJob::slotResult);
0068     connect(encryptJob, &QGpgME::EncryptJob::progress, this, [this](const QString &, int current, int total) {
0069         const auto ratio = static_cast<float>(current) / total;
0070         setPercent(static_cast<unsigned long>(100 * ratio));
0071     });
0072 
0073 
0074     encryptJob->start({}, m_plaintext, m_ciphertext, Context::None);
0075 }
0076 
0077 void EncryptJob::slotResult(const EncryptionResult &, const QByteArray &, const QString &, const Error &)
0078 {
0079     qCDebug(SYMMY) << "Encryption job finished, ciphertext size:" << m_ciphertext->size();
0080 
0081     if (m_ciphertext->size() == 0) {
0082         setError(UserDefinedError);
0083         emitResult();
0084         return;
0085     }
0086 
0087     auto ciphertextPath = m_ciphertext->fileName();
0088     // Drop the ".XXXXXX" suffix of QTemporaryFile.
0089     ciphertextPath.chop(7);
0090     qCDebug(SYMMY) << "Moving temporary file" << QUrl::fromLocalFile(m_ciphertext->fileName()) << "to" << QUrl::fromLocalFile(ciphertextPath);
0091     auto job = KIO::move(QUrl::fromLocalFile(m_ciphertext->fileName()), QUrl::fromLocalFile(ciphertextPath));
0092     connect(job, &KJob::result, this, &EncryptJob::emitResult);
0093 }
0094 
0095 }