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

Factor switching of card and app into common helper function

parent d4fef80c
......@@ -131,26 +131,10 @@ void CreateOpenPGPKeyFromCardKeysCommand::Private::start()
void CreateOpenPGPKeyFromCardKeysCommand::Private::slotDialogAccepted()
{
if (!(engineInfo(GpgEngine).engineVersion() < "2.3.0")) {
Error err;
std::unique_ptr<Context> c = Context::createForEngine(AssuanEngine, &err);
if (err.code() == GPG_ERR_NOT_SUPPORTED) {
finished();
return;
}
auto assuanContext = std::shared_ptr<Context>(c.release());
const auto resultSerialNumber = ReaderStatus::switchCard(assuanContext, serialNumber(), err);
if (err || resultSerialNumber != serialNumber()) {
qCWarning(KLEOPATRA_LOG) << "Switching to card" << QString::fromStdString(serialNumber()) << "failed";
finished();
return;
}
const auto resultAppName = ReaderStatus::switchApp(assuanContext, serialNumber(), appName, err);
if (err || resultAppName != appName) {
qCWarning(KLEOPATRA_LOG) << "Switching card to" << QString::fromStdString(appName) << "app failed";
finished();
return;
}
const Error err = ReaderStatus::switchCardAndApp(serialNumber(), appName);
if (err) {
finished();
return;
}
const auto backend = openpgp();
......
......@@ -1088,4 +1088,34 @@ std::string ReaderStatus::switchApp(std::shared_ptr<Context>& ctx, const std::st
return ::switchApp(ctx, serialNumber, appName, err);
}
// static
Error ReaderStatus::switchCardAndApp(const std::string &serialNumber, const std::string &appName)
{
Error err;
if (!(engineInfo(GpgEngine).engineVersion() < "2.3.0")) {
std::unique_ptr<Context> c = Context::createForEngine(AssuanEngine, &err);
if (err.code() == GPG_ERR_NOT_SUPPORTED) {
return err;
}
auto assuanContext = std::shared_ptr<Context>(c.release());
const auto resultSerialNumber = switchCard(assuanContext, serialNumber, err);
if (err || resultSerialNumber != serialNumber) {
qCWarning(KLEOPATRA_LOG) << "Switching to card" << QString::fromStdString(serialNumber) << "failed";
if (!err) {
err = Error::fromCode(GPG_ERR_UNEXPECTED);
}
return err;
}
const auto resultAppName = switchApp(assuanContext, serialNumber, appName, err);
if (err || resultAppName != appName) {
qCWarning(KLEOPATRA_LOG) << "Switching card to" << QString::fromStdString(appName) << "app failed";
if (!err) {
err = Error::fromCode(GPG_ERR_UNEXPECTED);
}
return err;
}
}
return err;
}
#include "readerstatus.moc"
......@@ -61,6 +61,7 @@ public:
static std::string switchCard(std::shared_ptr<GpgME::Context> &ctx, const std::string &serialNumber, GpgME::Error &err);
static std::string switchApp(std::shared_ptr<GpgME::Context> &ctx, const std::string &serialNumber,
const std::string &appName, GpgME::Error &err);
static GpgME::Error switchCardAndApp(const std::string &serialNumber, const std::string &appName);
public Q_SLOTS:
void updateStatus();
......
......@@ -261,23 +261,9 @@ void PGPCardWidget::doChangePin(const std::string &keyRef)
void PGPCardWidget::doGenKey(GenCardKeyDialog *dlg)
{
if (!(engineInfo(GpgME::GpgEngine).engineVersion() < "2.3.0")) {
GpgME::Error err;
std::unique_ptr<GpgME::Context> c = GpgME::Context::createForEngine(GpgME::AssuanEngine, &err);
if (err.code() == GPG_ERR_NOT_SUPPORTED) {
return;
}
auto assuanContext = std::shared_ptr<GpgME::Context>(c.release());
const auto serialNumber = ReaderStatus::switchCard(assuanContext, mRealSerial, err);
if (err || serialNumber != mRealSerial) {
qCWarning(KLEOPATRA_LOG) << "Switching to card" << QString::fromStdString(mRealSerial) << "failed";
return;
}
const auto appName = ReaderStatus::switchApp(assuanContext, mRealSerial, OpenPGPCard::AppName, err);
if (err || appName != OpenPGPCard::AppName) {
qCWarning(KLEOPATRA_LOG) << "Switching card to" << QString::fromStdString(OpenPGPCard::AppName) << "app failed";
return;
}
const GpgME::Error err = ReaderStatus::switchCardAndApp(mRealSerial, OpenPGPCard::AppName);
if (err) {
return;
}
const auto params = dlg->getKeyParams();
......
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