Commit bd72100e authored by Ahmad Samir's avatar Ahmad Samir
Browse files

ReaderStatus: use a std::function in Transaction struct

This way we can use lambdas instead of method names as char* and get
compile-time type checking, which makes removing Q_PRIVATE_SLOT calls less
worrying, it'll just fail to compile.
parent 04d95345
......@@ -129,7 +129,9 @@ void AuthenticatePIVCardApplicationCommand::Private::authenticate(const QByteArr
const QByteArray plusPercentEncodedAdminKey = adminKey.toPercentEncoding().replace(' ', '+');
const QByteArray command = QByteArray("SCD SETATTR AUTH-ADM-KEY ") + plusPercentEncodedAdminKey;
ReaderStatus::mutableInstance()->startSimpleTransaction(pivCard, command, q, "slotResult");
ReaderStatus::mutableInstance()->startSimpleTransaction(pivCard, command, q, [this](const GpgME::Error &err) {
slotResult(err);
});
}
void AuthenticatePIVCardApplicationCommand::Private::slotResult(const Error &err)
......
......@@ -37,7 +37,6 @@ private:
class Private;
inline Private *d_func();
inline const Private *d_func() const;
Q_PRIVATE_SLOT(d_func(), void slotResult(GpgME::Error))
};
} // namespace Commands
......
......@@ -176,7 +176,14 @@ void CertificateToPIVCardCommand::Private::startCertificateToPIVCard()
const QByteArray command = QByteArrayLiteral("SCD WRITECERT ") + QByteArray::fromStdString(cardSlot);
auto transaction = std::unique_ptr<AssuanTransaction>(new WriteCertAssuanTransaction(certificateData));
ReaderStatus::mutableInstance()->startTransaction(pivCard, command, q_func(), "certificateToPIVCardDone", std::move(transaction));
ReaderStatus::mutableInstance()->startTransaction(
pivCard,
command,
q_func(),
[this](const GpgME::Error &err) {
q->certificateToPIVCardDone(err);
},
std::move(transaction));
}
void CertificateToPIVCardCommand::Private::authenticate()
......
......@@ -131,7 +131,9 @@ void ChangePinCommand::Private::changePin()
command << "--nullpin";
}
command << QByteArray::fromStdString(keyRef);
ReaderStatus::mutableInstance()->startSimpleTransaction(card, command.join(' '), q, "slotResult");
ReaderStatus::mutableInstance()->startSimpleTransaction(card, command.join(' '), q, [this](const GpgME::Error &err) {
slotResult(err);
});
}
namespace {
......
......@@ -44,7 +44,6 @@ private:
class Private;
inline Private *d_func();
inline const Private *d_func() const;
Q_PRIVATE_SLOT(d_func(), void slotResult(GpgME::Error))
};
} // namespace Commands
......
......@@ -264,7 +264,9 @@ void KeyToCardCommand::Private::startKeyToOpenPGPCard() {
.arg(QString::fromLatin1(subkey.keyGrip()), QString::fromStdString(serialNumber()))
.arg(slot)
.arg(timestamp);
ReaderStatus::mutableInstance()->startSimpleTransaction(pgpCard, cmd.toUtf8(), q_func(), "keyToOpenPGPCardDone");
ReaderStatus::mutableInstance()->startSimpleTransaction(pgpCard, cmd.toUtf8(), q_func(), [this](const GpgME::Error &err) {
q->keyToOpenPGPCardDone(err);
});
}
namespace {
......@@ -386,7 +388,9 @@ void KeyToCardCommand::Private::startKeyToPIVCard()
const QString cmd = QStringLiteral("KEYTOCARD --force %1 %2 %3")
.arg(QString::fromLatin1(subkey.keyGrip()), QString::fromStdString(serialNumber()))
.arg(QString::fromStdString(cardSlot));
ReaderStatus::mutableInstance()->startSimpleTransaction(pivCard, cmd.toUtf8(), q_func(), "keyToPIVCardDone");
ReaderStatus::mutableInstance()->startSimpleTransaction(pivCard, cmd.toUtf8(), q_func(), [this](const GpgME::Error &err) {
q->keyToPIVCardDone(err);
});
}
void KeyToCardCommand::Private::authenticate()
......
......@@ -197,7 +197,9 @@ void PIVGenerateCardKeyCommand::Private::generateKey()
command << "--algo=" + QByteArray::fromStdString(algorithm);
}
command << "--" << QByteArray::fromStdString(keyRef);
ReaderStatus::mutableInstance()->startSimpleTransaction(pivCard, command.join(' '), q, "slotResult");
ReaderStatus::mutableInstance()->startSimpleTransaction(pivCard, command.join(' '), q, [this](const GpgME::Error &err) {
slotResult(err);
});
}
void PIVGenerateCardKeyCommand::Private::slotResult(const GpgME::Error& err)
......
......@@ -37,7 +37,6 @@ private:
class Private;
inline Private *d_func();
inline const Private *d_func() const;
Q_PRIVATE_SLOT(d_func(), void slotResult(GpgME::Error))
};
} // namespace Commands
......
......@@ -72,7 +72,7 @@ private:
}
private:
void setInitialPin(const char *pinRef, const char *resultSlot)
void setInitialPin(const char *pinRef, const ReaderStatus::TransactionFunc &resultSlot)
{
const auto nksCard = ReaderStatus::instance()->getCard<NetKeyCard>(serialNumber());
if (!nksCard) {
......@@ -86,12 +86,16 @@ private:
void slotNksPinRequested()
{
setInitialPin("PW1.CH", "setNksPinSettingResult");
setInitialPin("PW1.CH", [this](const GpgME::Error &error) {
dialog->setNksPinSettingResult(error);
});
}
void slotSigGPinRequested()
{
setInitialPin("PW1.CH.SIG", "setSigGPinSettingResult");
setInitialPin("PW1.CH.SIG", [this](const GpgME::Error &error) {
dialog->setSigGPinSettingResult(error);
});
}
void slotDialogRejected()
......
......@@ -191,7 +191,9 @@ void SetPIVCardApplicationAdministrationKeyCommand::Private::slotDialogAccepted(
const QByteArray plusPercentEncodedAdminKey = newAdminKey.toPercentEncoding().replace(' ', '+');
const QByteArray command = QByteArray("SCD SETATTR SET-ADM-KEY ") + plusPercentEncodedAdminKey;
ReaderStatus::mutableInstance()->startSimpleTransaction(pivCard, command, q, "slotResult");
ReaderStatus::mutableInstance()->startSimpleTransaction(pivCard, command, q, [this](const GpgME::Error &err) {
slotResult(err);
});
}
void SetPIVCardApplicationAdministrationKeyCommand::Private::slotDialogRejected()
......
......@@ -35,7 +35,6 @@ private:
class Private;
inline Private *d_func();
inline const Private *d_func() const;
Q_PRIVATE_SLOT(d_func(), void slotResult(GpgME::Error))
};
} // namespace Commands
......
......@@ -629,7 +629,7 @@ struct Transaction {
CardApp cardApp;
QByteArray command;
QPointer<QObject> receiver;
const char *slot;
ReaderStatus::TransactionFunc slot;
AssuanTransaction* assuanTransaction;
};
......@@ -709,8 +709,8 @@ private Q_SLOTS:
KDAB_SYNCHRONIZED(m_mutex)
ft.splice(ft.begin(), m_finishedTransactions);
for (const Transaction &t : std::as_const(ft))
if (t.receiver && t.slot && *t.slot) {
QMetaObject::invokeMethod(t.receiver, t.slot, Qt::DirectConnection, Q_ARG(GpgME::Error, err));
if (t.receiver && t.slot) {
QMetaObject::invokeMethod(t.receiver, [&t, &err]() { t.slot(err); }, Qt::DirectConnection);
}
}
......@@ -973,14 +973,17 @@ bool ReaderStatus::anyCardCanLearnKeys() const
return d->anyCardCanLearnKeysImpl();
}
void ReaderStatus::startSimpleTransaction(const std::shared_ptr<Card> &card, const QByteArray &command, QObject *receiver, const char *slot)
void ReaderStatus::startSimpleTransaction(const std::shared_ptr<Card> &card, const QByteArray &command, QObject *receiver, const TransactionFunc &slot)
{
const CardApp cardApp = { card->serialNumber(), card->appName() };
const Transaction t = { cardApp, command, receiver, slot, nullptr };
d->addTransaction(t);
}
void ReaderStatus::startTransaction(const std::shared_ptr<Card> &card, const QByteArray &command, QObject *receiver, const char *slot,
void ReaderStatus::startTransaction(const std::shared_ptr<Card> &card,
const QByteArray &command,
QObject *receiver,
const TransactionFunc &slot,
std::unique_ptr<AssuanTransaction> transaction)
{
const CardApp cardApp = { card->serialNumber(), card->appName() };
......
......@@ -39,8 +39,12 @@ public:
static const ReaderStatus *instance();
static ReaderStatus *mutableInstance();
void startSimpleTransaction(const std::shared_ptr<Card> &card, const QByteArray &cmd, QObject *receiver, const char *slot);
void startTransaction(const std::shared_ptr<Card> &card, const QByteArray &cmd, QObject *receiver, const char *slot,
using TransactionFunc = std::function<void(const GpgME::Error &)>;
void startSimpleTransaction(const std::shared_ptr<Card> &card, const QByteArray &cmd, QObject *receiver, const TransactionFunc &slot);
void startTransaction(const std::shared_ptr<Card> &card,
const QByteArray &cmd,
QObject *receiver,
const TransactionFunc &slot,
std::unique_ptr<GpgME::AssuanTransaction> transaction);
Card::Status cardStatus(unsigned int slot) const;
......
......@@ -413,7 +413,9 @@ void PGPCardWidget::changeNameRequested()
}
const QByteArray command = QByteArrayLiteral("SCD SETATTR DISP-NAME ") + formatted.toUtf8();
ReaderStatus::mutableInstance()->startSimpleTransaction(pgpCard, command, this, "changeNameResult");
ReaderStatus::mutableInstance()->startSimpleTransaction(pgpCard, command, this, [this](const GpgME::Error &err) {
changeNameResult(err);
});
}
void PGPCardWidget::changeNameResult(const GpgME::Error &err)
......@@ -458,7 +460,9 @@ void PGPCardWidget::changeUrlRequested()
}
const QByteArray command = QByteArrayLiteral("SCD SETATTR PUBKEY-URL ") + text.toUtf8();
ReaderStatus::mutableInstance()->startSimpleTransaction(pgpCard, command, this, "changeUrlResult");
ReaderStatus::mutableInstance()->startSimpleTransaction(pgpCard, command, this, [this](const GpgME::Error &err) {
changeUrlResult(err);
});
}
void PGPCardWidget::changeUrlResult(const GpgME::Error &err)
......
Supports Markdown
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