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
This commit is contained in:
Ingo Klöcker 2021-06-29 12:12:33 +02:00
parent d8638ed0aa
commit 12006a7829
5 changed files with 77 additions and 11 deletions

View File

@ -44,6 +44,15 @@ QStringList CryptoConfigEntry::stringValueList() const
return entry->stringValueList(); return entry->stringValueList();
} }
QVariant CryptoConfigEntry::defaultValue() const
{
const QGpgMENewCryptoConfigEntry *entry = dynamic_cast <const QGpgMENewCryptoConfigEntry*>(this);
if (!entry) {
return {};
}
return entry->defaultValue();
}
QGpgME::CryptoConfigEntry *CryptoConfig::entry(const QString &componentName, const QString &entryName) const QGpgME::CryptoConfigEntry *CryptoConfig::entry(const QString &componentName, const QString &entryName) const
{ {
const CryptoConfigComponent *comp = component(componentName); const CryptoConfigComponent *comp = component(componentName);

View File

@ -44,6 +44,8 @@
#include <vector> #include <vector>
class QVariant;
/* Start reading this file from the bottom up :) */ /* Start reading this file from the bottom up :) */
namespace QGpgME namespace QGpgME
@ -258,6 +260,11 @@ public:
* @return a stringValueList. * @return a stringValueList.
*/ */
QStringList stringValueList() const; QStringList stringValueList() const;
/**
* Return the default value as a variant (available for all argtypes).
*/
QVariant defaultValue() const;
}; };
/** /**

View File

@ -43,6 +43,7 @@
#include <QFile> #include <QFile>
#include <QDir> #include <QDir>
#include <QList>
#include "global.h" #include "global.h"
#include "error.h" #include "error.h"
@ -710,6 +711,57 @@ bool QGpgMENewCryptoConfigEntry::isDirty() const
return m_option.dirty(); 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<QVariant> 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 #if 0
QString QGpgMENewCryptoConfigEntry::toString(bool escape) const QString QGpgMENewCryptoConfigEntry::toString(bool escape) const
{ {

View File

@ -95,6 +95,7 @@ public:
bool isDirty() const Q_DECL_OVERRIDE; bool isDirty() const Q_DECL_OVERRIDE;
QStringList stringValueList() const; QStringList stringValueList() const;
QVariant defaultValue() const;
#if 0 #if 0
void setDirty(bool b); void setDirty(bool b);

View File

@ -57,27 +57,24 @@ private Q_SLOTS:
// be unsupported in older versions. // be unsupported in older versions.
return; return;
} }
// First set compliance to de-vs
auto conf = cryptoConfig(); auto conf = cryptoConfig();
QVERIFY(conf); QVERIFY(conf);
auto entry = conf->entry(QStringLiteral("gpg"), auto entry = conf->entry(QStringLiteral("gpg"), QStringLiteral("compliance"));
QStringLiteral("Configuration"),
QStringLiteral("compliance"));
QVERIFY(entry); QVERIFY(entry);
const auto defaultValue = entry->defaultValue().toString();
QCOMPARE(defaultValue, QStringLiteral("gnupg"));
entry->setStringValue("de-vs"); entry->setStringValue("de-vs");
conf->sync(true); conf->sync(true);
conf->clear(); conf->clear();
entry = conf->entry(QStringLiteral("gpg"), entry = conf->entry(QStringLiteral("gpg"), QStringLiteral("compliance"));
QStringLiteral("Configuration"),
QStringLiteral("compliance"));
QCOMPARE(entry->stringValue(), QStringLiteral("de-vs")); QCOMPARE(entry->stringValue(), QStringLiteral("de-vs"));
entry->resetToDefault(); entry->resetToDefault();
conf->sync(true); conf->sync(true);
conf->clear(); conf->clear();
entry = conf->entry(QStringLiteral("gpg"), entry = conf->entry(QStringLiteral("gpg"), QStringLiteral("compliance"));
QStringLiteral("Configuration"), QCOMPARE(entry->stringValue(), defaultValue);
QStringLiteral("compliance"));
QCOMPARE(entry->stringValue(), QStringLiteral("gnupg"));
} }
void initTestCase() void initTestCase()