/*! * * Copyright (c) 2021-2022 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) 05-Apr-2022 * \author (original) Behrouz NematiPour * \date (original) 29-Mar-2021 * */ #include "MSettings.h" // Qt #include // Project // name spaces using namespace Storage; /*! * \brief MSettings::MSettings * \details The constructor * \param parent */ 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) { const QMutexLocker locker(&_mutex); return _settings[vGroup].keys; } QString MSettings::key(const QString &vGroup, uint vIndex) { QStringList mKeys = keys(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) { QMutexLocker locker(&_mutex); return _settings[vGroup].values; } /*! * \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 */ QVariant MSettings::value(const QString &vGroup, const QString &vKey) { QMutexLocker locker(&_mutex); int index = _settings[vGroup].keys.indexOf(vKey); if ( index >= 0 ) return _settings[vGroup].values.at(index); return QVariant(); } /*! * \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 */ QString MSettings::location(const QString &vGroup) { QMutexLocker locker(&_mutex); return _settings[vGroup].location; } /*! * \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 _category[vCategory]; } /*! * \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 _category.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. * \param vLocation - the category of the setting data which by default is the base location if has extra folder + filename. */ void MSettings::add(const QString &vGroup, const QString &vKey, const QVariant &vValue, const QString &vLocation, const QString &vCategory, bool vEnableDuplicateKeys) { QMutexLocker locker(&_mutex); if ( ! _category[vCategory].contains(vGroup) ) _category[vCategory] += vGroup; KeyValue mGroup = _settings[vGroup]; if ( mGroup.location != vLocation ) mGroup.location = vLocation; if ( mGroup.category != vCategory ) mGroup.category = vCategory; if ( vEnableDuplicateKeys ) { mGroup.keys += vKey ; mGroup.values += vValue ; } else { if ( mGroup.keys.contains(vKey)) { int index = mGroup.keys.indexOf(vKey); mGroup.values[index] = vValue; } else { mGroup.keys += vKey ; mGroup.values += vValue ; } } _settings[vGroup] = mGroup; } /********** 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() { QVariant dateFotmat = _Settings.value("Date", "Format"); QVariant timeFotmat = _Settings.value("Time", "Format"); if (dateFotmat.isValid() && timeFotmat.isValid()) _datetimeFormat = dateFotmat.toString() + " " + timeFotmat.toString(); }