From 12006a782900687a7f0158a3f931528cfc5ff451 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingo=20Kl=C3=B6cker?= Date: Tue, 29 Jun 2021 12:12:33 +0200 Subject: [PATCH] qt: Allow retrieving the default value of a config entry * lang/qt/src/cryptoconfig.cpp, lang/qt/src/cryptoconfig.h (CryptoConfigEntry::defaultValue): New. * lang/qt/src/qgpgmenewcryptoconfig.cpp, lang/qt/src/qgpgmenewcryptoconfig.h (QGpgMENewCryptoConfigEntry::defaultValue): New. * lang/qt/tests/t-config.cpp (CryptoConfigTest::testDefault()): Add test of CryptoConfigEntry::defaultValue(). Port away from deprecated CryptoConfig::entry overload. -- GnuPG-bug-id: 5515 --- lang/qt/src/cryptoconfig.cpp | 9 +++++ lang/qt/src/cryptoconfig.h | 7 ++++ lang/qt/src/qgpgmenewcryptoconfig.cpp | 52 +++++++++++++++++++++++++++ lang/qt/src/qgpgmenewcryptoconfig.h | 1 + lang/qt/tests/t-config.cpp | 19 +++++----- 5 files changed, 77 insertions(+), 11 deletions(-) diff --git a/lang/qt/src/cryptoconfig.cpp b/lang/qt/src/cryptoconfig.cpp index d52dce73..4f13d3f0 100644 --- a/lang/qt/src/cryptoconfig.cpp +++ b/lang/qt/src/cryptoconfig.cpp @@ -44,6 +44,15 @@ QStringList CryptoConfigEntry::stringValueList() const return entry->stringValueList(); } +QVariant CryptoConfigEntry::defaultValue() const +{ + const QGpgMENewCryptoConfigEntry *entry = dynamic_cast (this); + if (!entry) { + return {}; + } + return entry->defaultValue(); +} + QGpgME::CryptoConfigEntry *CryptoConfig::entry(const QString &componentName, const QString &entryName) const { const CryptoConfigComponent *comp = component(componentName); diff --git a/lang/qt/src/cryptoconfig.h b/lang/qt/src/cryptoconfig.h index 651e2af1..d87f7d46 100644 --- a/lang/qt/src/cryptoconfig.h +++ b/lang/qt/src/cryptoconfig.h @@ -44,6 +44,8 @@ #include +class QVariant; + /* Start reading this file from the bottom up :) */ namespace QGpgME @@ -258,6 +260,11 @@ public: * @return a stringValueList. */ QStringList stringValueList() const; + + /** + * Return the default value as a variant (available for all argtypes). + */ + QVariant defaultValue() const; }; /** diff --git a/lang/qt/src/qgpgmenewcryptoconfig.cpp b/lang/qt/src/qgpgmenewcryptoconfig.cpp index b93db760..32bf888c 100644 --- a/lang/qt/src/qgpgmenewcryptoconfig.cpp +++ b/lang/qt/src/qgpgmenewcryptoconfig.cpp @@ -43,6 +43,7 @@ #include #include +#include #include "global.h" #include "error.h" @@ -710,6 +711,57 @@ bool QGpgMENewCryptoConfigEntry::isDirty() const return m_option.dirty(); } +QVariant QGpgMENewCryptoConfigEntry::defaultValue() const +{ + const auto defaultValue = m_option.defaultValue(); + if (defaultValue.isNull() || defaultValue.numElements() == 0) { + return {}; + } + if (defaultValue.numElements() == 1) { + switch (m_option.alternateType()) { + case NoType: + return QVariant{defaultValue.boolValue()}; + case StringType: + return QVariant{QString::fromUtf8(defaultValue.stringValue())}; + case IntegerType: + return QVariant{defaultValue.intValue()}; + case UnsignedIntegerType: + return QVariant{defaultValue.uintValue()}; + default: + // alternateType should always be one of the above four types + qCWarning(QGPGME_LOG) << __func__ << ": unsupported alternateType" << m_option.alternateType(); + } + } else { + QList list; + switch (m_option.alternateType()) { + case StringType: { + const auto values = defaultValue.stringValues(); + std::transform(std::begin(values), std::end(values), std::back_inserter(list), + [] (const char *value) { return QVariant{QString::fromUtf8(value)}; }); + break; + } + case IntegerType: { + const auto values = defaultValue.intValues(); + std::transform(std::begin(values), std::end(values), std::back_inserter(list), + [] (int value) { return QVariant{value}; }); + break; + } + case UnsignedIntegerType: { + const auto values = defaultValue.uintValues(); + std::transform(std::begin(values), std::end(values), std::back_inserter(list), + [] (unsigned int value) { return QVariant{value}; }); + break; + } + default: + // alternateType should always be one of the above four types + qCWarning(QGPGME_LOG) << __func__ << ": unsupported alternateType" << m_option.alternateType() << "for list"; + } + return QVariant{list}; + } + + return {}; +} + #if 0 QString QGpgMENewCryptoConfigEntry::toString(bool escape) const { diff --git a/lang/qt/src/qgpgmenewcryptoconfig.h b/lang/qt/src/qgpgmenewcryptoconfig.h index eade8ebc..15579fd7 100644 --- a/lang/qt/src/qgpgmenewcryptoconfig.h +++ b/lang/qt/src/qgpgmenewcryptoconfig.h @@ -95,6 +95,7 @@ public: bool isDirty() const Q_DECL_OVERRIDE; QStringList stringValueList() const; + QVariant defaultValue() const; #if 0 void setDirty(bool b); diff --git a/lang/qt/tests/t-config.cpp b/lang/qt/tests/t-config.cpp index 9ec95669..b84dbfd4 100644 --- a/lang/qt/tests/t-config.cpp +++ b/lang/qt/tests/t-config.cpp @@ -57,27 +57,24 @@ private Q_SLOTS: // be unsupported in older versions. return; } - // First set compliance to de-vs auto conf = cryptoConfig(); QVERIFY(conf); - auto entry = conf->entry(QStringLiteral("gpg"), - QStringLiteral("Configuration"), - QStringLiteral("compliance")); + auto entry = conf->entry(QStringLiteral("gpg"), QStringLiteral("compliance")); QVERIFY(entry); + const auto defaultValue = entry->defaultValue().toString(); + QCOMPARE(defaultValue, QStringLiteral("gnupg")); + entry->setStringValue("de-vs"); conf->sync(true); conf->clear(); - entry = conf->entry(QStringLiteral("gpg"), - QStringLiteral("Configuration"), - QStringLiteral("compliance")); + entry = conf->entry(QStringLiteral("gpg"), QStringLiteral("compliance")); QCOMPARE(entry->stringValue(), QStringLiteral("de-vs")); + entry->resetToDefault(); conf->sync(true); conf->clear(); - entry = conf->entry(QStringLiteral("gpg"), - QStringLiteral("Configuration"), - QStringLiteral("compliance")); - QCOMPARE(entry->stringValue(), QStringLiteral("gnupg")); + entry = conf->entry(QStringLiteral("gpg"), QStringLiteral("compliance")); + QCOMPARE(entry->stringValue(), defaultValue); } void initTestCase()