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:
parent
d8638ed0aa
commit
12006a7829
@ -44,6 +44,15 @@ QStringList CryptoConfigEntry::stringValueList() const
|
||||
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
|
||||
{
|
||||
const CryptoConfigComponent *comp = component(componentName);
|
||||
|
@ -44,6 +44,8 @@
|
||||
|
||||
#include <vector>
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -43,6 +43,7 @@
|
||||
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
#include <QList>
|
||||
|
||||
#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<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
|
||||
QString QGpgMENewCryptoConfigEntry::toString(bool escape) const
|
||||
{
|
||||
|
@ -95,6 +95,7 @@ public:
|
||||
bool isDirty() const Q_DECL_OVERRIDE;
|
||||
|
||||
QStringList stringValueList() const;
|
||||
QVariant defaultValue() const;
|
||||
|
||||
#if 0
|
||||
void setDirty(bool b);
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user