diff --git a/src/commands/adduseridcommand.cpp b/src/commands/adduseridcommand.cpp index b7b2555cf..0c99c0d3d 100644 --- a/src/commands/adduseridcommand.cpp +++ b/src/commands/adduseridcommand.cpp @@ -1,233 +1,214 @@ /* -*- mode: c++; c-basic-offset:4 -*- commands/adduseridcommand.cpp This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB + SPDX-FileCopyrightText: 2022 g10 Code GmbH + SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ #include #include "adduseridcommand.h" #include "command_p.h" #include "dialogs/adduseriddialog.h" #include #include #include +#include + #include #include #include "kleopatra_debug.h" - using namespace Kleo; using namespace Kleo::Commands; using namespace GpgME; class AddUserIDCommand::Private : public Command::Private { friend class ::Kleo::Commands::AddUserIDCommand; AddUserIDCommand *q_func() const { return static_cast(q); } public: explicit Private(AddUserIDCommand *qq, KeyListController *c); ~Private() override; - void init(); - private: void slotDialogAccepted(); void slotDialogRejected(); void slotResult(const Error &err); private: void ensureDialogCreated(); void createJob(); void showErrorDialog(const Error &error); void showSuccessDialog(); private: GpgME::Key key; + QObjectCleanupHandler cleaner; QPointer dialog; QPointer job; }; AddUserIDCommand::Private *AddUserIDCommand::d_func() { return static_cast(d.get()); } const AddUserIDCommand::Private *AddUserIDCommand::d_func() const { return static_cast(d.get()); } #define d d_func() #define q q_func() AddUserIDCommand::Private::Private(AddUserIDCommand *qq, KeyListController *c) - : Command::Private(qq, c), - key(), - dialog(), - job() + : Command::Private{qq, c} { - -} - -AddUserIDCommand::Private::~Private() -{ - qCDebug(KLEOPATRA_LOG); - if (dialog) { - delete dialog; - } } -AddUserIDCommand::AddUserIDCommand(KeyListController *c) - : Command(new Private(this, c)) -{ - d->init(); -} +AddUserIDCommand::Private::~Private() = default; AddUserIDCommand::AddUserIDCommand(QAbstractItemView *v, KeyListController *c) - : Command(v, new Private(this, c)) + : Command{v, new Private{this, c}} { - d->init(); } AddUserIDCommand::AddUserIDCommand(const GpgME::Key &key) - : Command(key, new Private(this, nullptr)) + : Command{key, new Private{this, nullptr}} { - d->init(); -} - -void AddUserIDCommand::Private::init() -{ - } AddUserIDCommand::~AddUserIDCommand() { - qCDebug(KLEOPATRA_LOG); + qCDebug(KLEOPATRA_LOG).nospace() << this << "::" << __func__; } void AddUserIDCommand::doStart() { const std::vector keys = d->keys(); - if (keys.size() != 1 || - keys.front().protocol() != GpgME::OpenPGP || - !keys.front().hasSecret()) { + if (keys.size() != 1) { d->finished(); return; } d->key = keys.front(); + if (d->key.protocol() != GpgME::OpenPGP + || !d->key.hasSecret()) { + d->finished(); + return; + } d->ensureDialogCreated(); - Q_ASSERT(d->dialog); const UserID uid = d->key.userID(0); d->dialog->setName(QString::fromUtf8(uid.name())); d->dialog->setEmail(Formatting::prettyEMail(uid.email(), uid.id())); d->dialog->show(); } void AddUserIDCommand::Private::slotDialogAccepted() { Q_ASSERT(dialog); createJob(); if (!job) { finished(); return; } job->startAddUid(key, dialog->userID()); } void AddUserIDCommand::Private::slotDialogRejected() { Q_EMIT q->canceled(); finished(); } void AddUserIDCommand::Private::slotResult(const Error &err) { - if (err.isCanceled()) - ; - else if (err) { + if (err.isCanceled()) { + } else if (err) { showErrorDialog(err); } else { showSuccessDialog(); } finished(); } void AddUserIDCommand::doCancel() { - qCDebug(KLEOPATRA_LOG); + qCDebug(KLEOPATRA_LOG).nospace() << this << "::" << __func__; if (d->job) { d->job->slotCancel(); } } void AddUserIDCommand::Private::ensureDialogCreated() { if (dialog) { return; } dialog = new AddUserIDDialog; + cleaner.add(dialog); applyWindowID(dialog); - connect(dialog, SIGNAL(accepted()), q, SLOT(slotDialogAccepted())); - connect(dialog, SIGNAL(rejected()), q, SLOT(slotDialogRejected())); + connect(dialog, &QDialog::accepted, q, [this]() { slotDialogAccepted(); }); + connect(dialog, &QDialog::rejected, q, [this]() { slotDialogRejected(); }); } void AddUserIDCommand::Private::createJob() { Q_ASSERT(!job); const auto backend = QGpgME::openpgp(); if (!backend) { return; } const auto j = backend->quickJob(); if (!j) { return; } connect(j, &QGpgME::Job::progress, q, &Command::progress); - connect(j, SIGNAL(result(GpgME::Error)), - q, SLOT(slotResult(GpgME::Error))); + connect(j, &QGpgME::QuickJob::result, + q, [this](const GpgME::Error &err) { slotResult(err); }); job = j; } void AddUserIDCommand::Private::showErrorDialog(const Error &err) { error(xi18nc("@info", "An error occurred while trying to add the user-id: " "%1", QString::fromLocal8Bit(err.asString())), i18nc("@title:window", "Add User-ID Error")); } void AddUserIDCommand::Private::showSuccessDialog() { information(i18nc("@info", "User-ID successfully added."), i18nc("@title:window", "Add User-ID Succeeded")); } #undef d #undef q #include "moc_adduseridcommand.cpp" diff --git a/src/commands/adduseridcommand.h b/src/commands/adduseridcommand.h index f13a270c6..0565472fd 100644 --- a/src/commands/adduseridcommand.h +++ b/src/commands/adduseridcommand.h @@ -1,57 +1,54 @@ /* -*- mode: c++; c-basic-offset:4 -*- commands/adduseridcommand.h This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB + SPDX-FileCopyrightText: 2022 g10 Code GmbH + SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once -#include +#include "command.h" namespace Kleo { namespace Commands { class AddUserIDCommand : public Command { Q_OBJECT public: explicit AddUserIDCommand(QAbstractItemView *view, KeyListController *parent); - explicit AddUserIDCommand(KeyListController *parent); explicit AddUserIDCommand(const GpgME::Key &key); ~AddUserIDCommand() override; /* reimp */ static Restrictions restrictions() { return OnlyOneKey | MustBeOpenPGP | NeedSecretKey; } void setName(const QString &name); const QString &name() const; void setEmail(const QString &email); const QString &email() const; void setComment(const QString &comment); const QString &comment() const; private: void doStart() override; void doCancel() override; private: class Private; inline Private *d_func(); inline const Private *d_func() const; - Q_PRIVATE_SLOT(d_func(), void slotResult(GpgME::Error)) - Q_PRIVATE_SLOT(d_func(), void slotDialogAccepted()) - Q_PRIVATE_SLOT(d_func(), void slotDialogRejected()) }; } } -