Commit 4853bdf5 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Update the signing actions when the keys change

Use a lazy check for checking for suitable signing keys. This way the
clipboard menu doesn't block waiting for the initialization of the key
cache when it is created.
parent d3b99279
Pipeline #103865 passed with stage
in 1 minute and 48 seconds
......@@ -20,8 +20,6 @@
#include <utils/input.h>
#include <utils/output.h>
#include <Libkleo/Algorithm>
#include <Libkleo/KeyCache>
#include <Libkleo/Stl_Util>
#include <KLocalizedString>
......@@ -31,8 +29,6 @@
#include <QClipboard>
#include <QMimeData>
#include <gpgme++/key.h>
#include <exception>
using namespace Kleo;
......@@ -123,7 +119,7 @@ SignClipboardCommand::~SignClipboardCommand()
}
// static
bool SignClipboardCommand::canSignCurrentClipboard(GpgME::Protocol protocol)
bool SignClipboardCommand::canSignCurrentClipboard()
{
bool canSign = false;
if (const QClipboard *const clip = QApplication::clipboard()) {
......@@ -131,10 +127,6 @@ bool SignClipboardCommand::canSignCurrentClipboard(GpgME::Protocol protocol)
canSign = mime->hasText();
}
}
if (canSign) {
canSign &= Kleo::any_of(KeyCache::instance()->secretKeys(),
[](const auto &k) { return k.hasSecret(); });
}
return canSign;
}
......
......@@ -30,7 +30,7 @@ public:
explicit SignClipboardCommand(GpgME::Protocol protocol, KeyListController *parent);
~SignClipboardCommand() override;
static bool canSignCurrentClipboard(GpgME::Protocol protocol);
static bool canSignCurrentClipboard();
private:
void doStart() override;
......
......@@ -16,6 +16,9 @@
#include <commands/signclipboardcommand.h>
#include <commands/decryptverifyclipboardcommand.h>
#include <Libkleo/Algorithm>
#include <Libkleo/KeyCache>
#include <KLocalizedString>
#include <KActionMenu>
......@@ -24,6 +27,8 @@
#include <QClipboard>
#include <QSignalBlocker>
#include <gpgme++/key.h>
using namespace Kleo;
using namespace Kleo::Commands;
......@@ -63,6 +68,7 @@ ClipboardMenu::ClipboardMenu(QObject *parent)
mClipboardMenu->addAction(mOpenPGPSignClipboardAction);
mClipboardMenu->addAction(mDecryptVerifyClipboardAction);
connect(QApplication::clipboard(), &QClipboard::changed, this, &ClipboardMenu::slotEnableDisableActions);
connect(KeyCache::instance().get(), &KeyCache::keyListingDone, this, &ClipboardMenu::slotEnableDisableActions);
slotEnableDisableActions();
}
......@@ -110,14 +116,29 @@ void ClipboardMenu::slotDecryptVerifyClipboard()
startCommand(new DecryptVerifyClipboardCommand(nullptr));
}
namespace
{
bool hasSigningKeys(GpgME::Protocol protocol) {
if (!KeyCache::instance()->initialized()) {
return false;
}
return Kleo::any_of(KeyCache::instance()->keys(),
[protocol](const auto &k) {
return k.hasSecret() && k.canReallySign() && (k.protocol() == protocol);
});
}
}
void ClipboardMenu::slotEnableDisableActions()
{
const QSignalBlocker blocker(QApplication::clipboard());
mImportClipboardAction->setEnabled(ImportCertificateFromClipboardCommand::canImportCurrentClipboard());
mEncryptClipboardAction->setEnabled(EncryptClipboardCommand::canEncryptCurrentClipboard());
mOpenPGPSignClipboardAction->setEnabled(SignClipboardCommand::canSignCurrentClipboard(GpgME::OpenPGP));
mOpenPGPSignClipboardAction->setEnabled(SignClipboardCommand::canSignCurrentClipboard() && hasSigningKeys(GpgME::OpenPGP));
if (mSmimeSignClipboardAction) {
mSmimeSignClipboardAction->setEnabled(SignClipboardCommand::canSignCurrentClipboard(GpgME::CMS));
mSmimeSignClipboardAction->setEnabled(SignClipboardCommand::canSignCurrentClipboard() && hasSigningKeys(GpgME::CMS));
}
mDecryptVerifyClipboardAction->setEnabled(DecryptVerifyClipboardCommand::canDecryptVerifyCurrentClipboard());
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment