diff --git a/src/crypto/encryptemailcontroller.cpp b/src/crypto/encryptemailcontroller.cpp index 32d96af76..2aef116e7 100644 --- a/src/crypto/encryptemailcontroller.cpp +++ b/src/crypto/encryptemailcontroller.cpp @@ -1,313 +1,290 @@ /* -*- mode: c++; c-basic-offset:4 -*- crypto/encryptemailcontroller.cpp This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB SPDX-License-Identifier: GPL-2.0-or-later */ #include #include "encryptemailcontroller.h" #include "kleopatra_debug.h" #include "encryptemailtask.h" #include "taskcollection.h" #include #include #include #include #include #include #include "emailoperationspreferences.h" #include #include #include #include #include using namespace Kleo; using namespace Kleo::Crypto; using namespace Kleo::Crypto::Gui; using namespace GpgME; using namespace KMime::Types; class EncryptEMailController::Private { friend class ::Kleo::Crypto::EncryptEMailController; EncryptEMailController *const q; public: explicit Private(Mode mode, EncryptEMailController *qq); private: - void slotWizardRecipientsResolved(); void slotWizardCanceled(); private: - void ensureWizardCreated() const; + void createWizard(); void ensureWizardVisible(); void cancelAllTasks(); void schedule(); std::shared_ptr takeRunnable(GpgME::Protocol proto); private: const Mode mode; std::vector< std::shared_ptr > runnable, completed; std::shared_ptr cms, openpgp; - mutable QPointer wizard; + std::unique_ptr wizard; }; EncryptEMailController::Private::Private(Mode m, EncryptEMailController *qq) : q(qq), mode(m), runnable(), cms(), - openpgp(), - wizard() + openpgp() { - + createWizard(); } EncryptEMailController::EncryptEMailController(const std::shared_ptr &xc, Mode mode, QObject *p) : Controller(xc, p), d(new Private(mode, this)) { } EncryptEMailController::EncryptEMailController(Mode mode, QObject *p) : Controller(p), d(new Private(mode, this)) { } EncryptEMailController::~EncryptEMailController() { - if (d->wizard && !d->wizard->isVisible()) { - delete d->wizard; - } - //d->wizard->close(); ### ? + d->wizard->close(); } EncryptEMailController::Mode EncryptEMailController::mode() const { return d->mode; } void EncryptEMailController::setProtocol(Protocol proto) { - d->ensureWizardCreated(); const Protocol protocol = d->wizard->presetProtocol(); kleo_assert(protocol == UnknownProtocol || protocol == proto); d->wizard->setPresetProtocol(proto); } Protocol EncryptEMailController::protocol() const { - d->ensureWizardCreated(); return d->wizard->selectedProtocol(); } const char *EncryptEMailController::protocolAsString() const { switch (protocol()) { case OpenPGP: return "OpenPGP"; case CMS: return "CMS"; default: throw Kleo::Exception(gpg_error(GPG_ERR_INTERNAL), i18n("Call to EncryptEMailController::protocolAsString() is ambiguous.")); } } void EncryptEMailController::startResolveRecipients() { startResolveRecipients(std::vector(), std::vector()); } void EncryptEMailController::startResolveRecipients(const std::vector &recipients, const std::vector &senders) { - d->ensureWizardCreated(); d->wizard->setRecipients(recipients, senders); d->ensureWizardVisible(); } -void EncryptEMailController::Private::slotWizardRecipientsResolved() -{ - Q_EMIT q->recipientsResolved(); -} - void EncryptEMailController::Private::slotWizardCanceled() { q->setLastError(gpg_error(GPG_ERR_CANCELED), i18n("User cancel")); q->emitDoneOrError(); } void EncryptEMailController::setInputAndOutput(const std::shared_ptr &input, const std::shared_ptr &output) { setInputsAndOutputs(std::vector< std::shared_ptr >(1, input), std::vector< std::shared_ptr >(1, output)); } void EncryptEMailController::setInputsAndOutputs(const std::vector< std::shared_ptr > &inputs, const std::vector< std::shared_ptr > &outputs) { kleo_assert(!inputs.empty()); kleo_assert(outputs.size() == inputs.size()); std::vector< std::shared_ptr > tasks; tasks.reserve(inputs.size()); - d->ensureWizardCreated(); - const std::vector keys = d->wizard->resolvedCertificates(); kleo_assert(!keys.empty()); for (unsigned int i = 0, end = inputs.size(); i < end; ++i) { const std::shared_ptr task(new EncryptEMailTask); task->setInput(inputs[i]); task->setOutput(outputs[i]); if (d->mode == ClipboardMode) { task->setAsciiArmor(true); } task->setRecipients(keys); tasks.push_back(task); } d->runnable.swap(tasks); } void EncryptEMailController::start() { std::shared_ptr coll(new TaskCollection); std::vector > tmp; std::copy(d->runnable.begin(), d->runnable.end(), std::back_inserter(tmp)); coll->setTasks(tmp); - d->ensureWizardCreated(); d->wizard->setTaskCollection(coll); for (const std::shared_ptr &t : std::as_const(tmp)) { connectTask(t); } d->schedule(); } void EncryptEMailController::Private::schedule() { if (!cms) if (const std::shared_ptr t = takeRunnable(CMS)) { t->start(); cms = t; } if (!openpgp) if (const std::shared_ptr t = takeRunnable(OpenPGP)) { t->start(); openpgp = t; } if (cms || openpgp) { return; } kleo_assert(runnable.empty()); q->emitDoneOrError(); } std::shared_ptr EncryptEMailController::Private::takeRunnable(GpgME::Protocol proto) { const auto it = std::find_if(runnable.begin(), runnable.end(), [proto](const std::shared_ptr &task) { return task->protocol() == proto; }); if (it == runnable.end()) { return std::shared_ptr(); } const std::shared_ptr result = *it; runnable.erase(it); return result; } void EncryptEMailController::doTaskDone(const Task *task, const std::shared_ptr &result) { Q_UNUSED(result) Q_ASSERT(task); // We could just delete the tasks here, but we can't use // Qt::QueuedConnection here (we need sender()) and other slots // might not yet have executed. Therefore, we push completed tasks // into a burial container if (task == d->cms.get()) { d->completed.push_back(d->cms); d->cms.reset(); } else if (task == d->openpgp.get()) { d->completed.push_back(d->openpgp); d->openpgp.reset(); } - QTimer::singleShot(0, this, SLOT(schedule())); + QTimer::singleShot(0, this, [this]() { d->schedule(); }); } void EncryptEMailController::cancel() { try { if (d->wizard) { d->wizard->close(); } d->cancelAllTasks(); } catch (const std::exception &e) { qCDebug(KLEOPATRA_LOG) << "Caught exception: " << e.what(); } } void EncryptEMailController::Private::cancelAllTasks() { // we just kill all runnable tasks - this will not result in // signal emissions. runnable.clear(); // a cancel() will result in a call to if (cms) { cms->cancel(); } if (openpgp) { openpgp->cancel(); } } -void EncryptEMailController::Private::ensureWizardCreated() const +void EncryptEMailController::Private::createWizard() { - if (wizard) { - return; - } - - std::unique_ptr w(new EncryptEMailWizard); - w->setAttribute(Qt::WA_DeleteOnClose); + wizard.reset(new EncryptEMailWizard); + wizard->setAttribute(Qt::WA_DeleteOnClose); Kleo::EMailOperationsPreferences prefs; - w->setQuickMode(prefs.quickEncryptEMail()); - connect(w.get(), SIGNAL(recipientsResolved()), q, SLOT(slotWizardRecipientsResolved()), Qt::QueuedConnection); - connect(w.get(), SIGNAL(canceled()), q, SLOT(slotWizardCanceled()), Qt::QueuedConnection); - - wizard = w.release(); + wizard->setQuickMode(prefs.quickEncryptEMail()); + connect(wizard.get(), &EncryptEMailWizard::recipientsResolved, q, &EncryptEMailController::recipientsResolved, Qt::QueuedConnection); + connect(wizard.get(), &EncryptEMailWizard::canceled, q, [this]() { slotWizardCanceled(); }, Qt::QueuedConnection); } void EncryptEMailController::Private::ensureWizardVisible() { - ensureWizardCreated(); - q->bringToForeground(wizard); + q->bringToForeground(wizard.get()); } #include "moc_encryptemailcontroller.cpp" diff --git a/src/crypto/encryptemailcontroller.h b/src/crypto/encryptemailcontroller.h index 7a1da75bb..99ed2d0d0 100644 --- a/src/crypto/encryptemailcontroller.h +++ b/src/crypto/encryptemailcontroller.h @@ -1,94 +1,91 @@ /* -*- mode: c++; c-basic-offset:4 -*- crypto/encryptemailcontroller.h This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once #include #include #include #include #include namespace KMime { namespace Types { class Mailbox; } } namespace Kleo { class Input; class Output; namespace Crypto { class EncryptEMailController : public Controller { Q_OBJECT public: enum Mode { GpgOLMode, ClipboardMode, NumModes }; explicit EncryptEMailController(Mode mode, QObject *parent = nullptr); explicit EncryptEMailController(const std::shared_ptr &xc, Mode mode, QObject *parent = nullptr); ~EncryptEMailController() override; Mode mode() const; static const char *mementoName() { return "EncryptEMailController"; } void setProtocol(GpgME::Protocol proto); const char *protocolAsString() const; GpgME::Protocol protocol() const; void startResolveRecipients(); void startResolveRecipients(const std::vector &recipients, const std::vector &senders); void setInputAndOutput(const std::shared_ptr &input, const std::shared_ptr &output); void setInputsAndOutputs(const std::vector< std::shared_ptr > &inputs, const std::vector< std::shared_ptr > &outputs); void start(); public Q_SLOTS: void cancel(); Q_SIGNALS: void recipientsResolved(); private: void doTaskDone(const Task *task, const std::shared_ptr &) override; class Private; kdtools::pimpl_ptr d; - Q_PRIVATE_SLOT(d, void slotWizardRecipientsResolved()) - Q_PRIVATE_SLOT(d, void slotWizardCanceled()) - Q_PRIVATE_SLOT(d, void schedule()) }; } // Crypto } // Kleo