/**************************************************************************** ** Copyright (c) 2021, Fougue Ltd. ** All rights reserved. ** See license at https://github.com/fougue/mayo/blob/master/LICENSE.txt ****************************************************************************/ #include "qsettings_storage.h" #include "../qtcommon/qstring_conv.h" #include "../qtcommon/qtcore_utils.h" namespace Mayo { QSettingsStorage::QSettingsStorage(const QString& fileName, QSettings::Format format) : m_storage(fileName, format) { } bool QSettingsStorage::contains(std::string_view key) const { return m_storage.contains(to_QString(key)); } Settings::Variant QSettingsStorage::value(std::string_view key) const { const QVariant value = m_storage.value(to_QString(key)); switch (value.type()) { case QVariant::ByteArray: return value.toByteArray().toStdString(); case QVariant::String: { const QString strval = value.toString(); if (strval == "true") return true; else if (strval == "false") return false; bool ok = false; const int ival = strval.toInt(&ok); if (ok) return ival; const double dval = strval.toDouble(&ok); if (ok) return dval; return to_stdString(strval); } case QVariant::Int: case QVariant::UInt: case QVariant::LongLong: case QVariant::ULongLong: { bool ok = true; const int ivalue = value.toInt(&ok); return ok ? Settings::Variant(ivalue) : Settings::Variant{}; } case QVariant::Double: { bool ok = true; const double dvalue = value.toDouble(&ok); return ok ? Settings::Variant(dvalue) : Settings::Variant{}; } case QVariant::Bool: return value.toBool(); default: return {}; } // endswitch return {}; } void QSettingsStorage::setValue(std::string_view key, const Settings::Variant& value) { QVariant qvalue; if (std::holds_alternative(value)) { qvalue = std::get(value); } else if (std::holds_alternative(value)) { qvalue = std::get(value); } else if (std::holds_alternative(value)) { qvalue = std::get(value); } else if (std::holds_alternative(value)) { const std::string& str = value.toConstRefString(); if (value.isByteArray()) qvalue = QByteArray::fromStdString(str); // Don't use QtCoreUtils::QByteArray_frowRawData(str) else qvalue = to_QString(str); } if (!qvalue.isNull()) m_storage.setValue(to_QString(key), qvalue); } void QSettingsStorage::sync() { m_storage.sync(); } } // namespace Mayo