Index: sources/model/settings/MSettings.cpp =================================================================== diff -u -r94fc1cd187816ecbf176df26f9dc5601bf379f13 -r2ef03b2ce51b4dc507f66e9671953a8e0824bde9 --- sources/model/settings/MSettings.cpp (.../MSettings.cpp) (revision 94fc1cd187816ecbf176df26f9dc5601bf379f13) +++ sources/model/settings/MSettings.cpp (.../MSettings.cpp) (revision 2ef03b2ce51b4dc507f66e9671953a8e0824bde9) @@ -1,15 +1,15 @@ /*! * - * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. + * Copyright (c) 2021-2024 Diality Inc. - All Rights Reserved. * \copyright * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * \file MSettings.cpp * \author (last) Behrouz NematiPour - * \date (last) 28-Mar-2021 + * \date (last) 27-May-2023 * \author (original) Behrouz NematiPour - * \date (original) 28-Mar-2021 + * \date (original) 29-Mar-2021 * */ #include "MSettings.h" @@ -18,6 +18,7 @@ #include // Project +#include "StorageGlobals.h" // name spaces using namespace Storage; @@ -30,64 +31,132 @@ MSettings::MSettings(QObject *parent) : QObject(parent) { } /*! - * \brief MSettings::groups - * \details Returns all the groups of the settings - * \return list of the groups as list of strings - */ -QStringList MSettings::groups() -{ - QMutexLocker locker(&_mutex); - return _settings.keys(); -} - -/*! * \brief MSettings::keys * \details returns all keys in a group * \param vGroup - the group to look for all the keys * \return list of the keys in QString for the group vGroup */ -QStringList MSettings::keys(const QString &vGroup) +QStringList MSettings::keys(const QString &vCategory, const QString &vGroup) { const QMutexLocker locker(&_mutex); - return _settings[vGroup].keys; + QStringList mKeys; + TKeyValues keyValues = _settings[vCategory][vGroup]; + for (const auto &keyValue: keyValues ) { + mKeys += keyValue.key(); + } + return mKeys; } +QString MSettings::key(const QString &vCategory, const QString &vGroup, uint vIndex) +{ + QStringList mKeys = keys(vCategory, vGroup); + QString mKey = "[Unknown %1]"; + if ( vIndex < (unsigned)mKeys.count() ) { + mKey = mKeys [vIndex]; + } else { + mKey = mKey.arg(vIndex); + } + return mKey; +} + /*! * \brief MSettings::values * \details returns all values in a group * \param vGroup - the group to look for all the values * \return list of the values in QString for the group vGroup */ -QVariantList MSettings::values(const QString &vGroup) +QVariantList MSettings::values(const QString &vCategory, const QString &vGroup) { QMutexLocker locker(&_mutex); - return _settings[vGroup].values; + QVariantList mValues; + TKeyValues keyValues = _settings[vCategory][vGroup]; + for (const auto &keyValue: keyValues ) { + mValues += keyValue.val(); + } + return mValues; } /*! - * \brief MSettings::location - * \details The locations where the settings have been read for the group vGroup - * \param vGroup - the group to look for the location - * \return the location in QString + * \brief MSettings::value + * \details returns value of a key for the given group + * \param vGroup - the group to look for the value + * \param vKey - the key to look for the value + * \return the values in QString for the group vGroup */ -QString MSettings::location(const QString &vGroup) +QVariant MSettings::value(const QString &vCategory, const QString &vGroup, const QString &vKey) { QMutexLocker locker(&_mutex); - return _location[vGroup]; + TKeyValues mKeyValues = _settings[vCategory][vGroup]; + for (const auto &keyValue: mKeyValues ) { + if ( vKey == keyValue.key() ) return keyValue.val(); + } + return QVariant(); } /*! + * \brief MSettings::groups + * \details The groups where have been read from the location vLocation + * \param vLocation - The location to look for the group(s) + * \return the groups in QString + */ +QStringList MSettings::groups(const QString &vCategory) +{ + QMutexLocker locker(&_mutex); + return _settings[vCategory].keys(); +} + +/*! + * \brief MSettings::categorys + * \details The list of all the categories in the setting + * \return the categories in QStringList + */ +QStringList MSettings::categorys() +{ + QMutexLocker locker(&_mutex); + return _settings.keys(); +} + +/*! * \brief MSettings::add * \details The function to be used to add elements in the settings * \param vGroup - the group of the settings * \param vKey - the key to be added under the group vGroup * \param vValue - the value of the key to be added under group vGroup for the key vKey - * \param vLocation - the location of the setting data if is different will be set. */ -void MSettings::add(const QString &vGroup, const QString &vKey, const QVariant &vValue, const QString &vLocation) +void MSettings::add(const QString &vCategory, const QString &vGroup, const QString &vKey, const QVariant &vValue, bool vEnableDuplicateKeys) { QMutexLocker locker(&_mutex); - if ( ! _location.contains(vGroup) ) _location[vGroup] = vLocation; - _settings[vGroup].keys += vKey ; - _settings[vGroup].values += vValue; + if ( vEnableDuplicateKeys ) { + _settings[vCategory][vGroup] += TKeyValue (vKey, vValue); + } + else { + TKeyValue keyValue (vKey, vValue); + TKeyValues keyValues = _settings[vCategory][vGroup]; + int index = keyValues.indexOf(keyValue); + if ( index >= 0 ) { + _settings[vCategory][vGroup].replace( index, keyValue ); + } + else { + _settings[vCategory][vGroup] += keyValue; + } + } } + +/********** The common helper functions **********/ + +/*! + * \brief Settings::getDatetimeFormat + * \details Get the date time format + * \return The String output of the data/time format. + */ +void MSettings::datetimeFormat() +{ + QString category = Storage::Settings_Category_SettingsSystem; + QVariant dateFotmat = _Settings.value(category, "Date", "Format"); + QVariant timeFotmat = _Settings.value(category, "Time", "Format"); + if (dateFotmat.isValid() && timeFotmat.isValid()) { + _dateFormat = dateFotmat.toString(); + _timeFormat = timeFotmat.toString(); + _datetimeFormat = _dateFormat + " " + _timeFormat; + } +}