Index: sources/storage/Settings.cpp =================================================================== diff -u -r2ef03b2ce51b4dc507f66e9671953a8e0824bde9 -ra75bc5b80ad4ebde50f028b550cd534e589bd059 --- sources/storage/Settings.cpp (.../Settings.cpp) (revision 2ef03b2ce51b4dc507f66e9671953a8e0824bde9) +++ sources/storage/Settings.cpp (.../Settings.cpp) (revision a75bc5b80ad4ebde50f028b550cd534e589bd059) @@ -29,7 +29,7 @@ #include "Settings.h" -bool Settings::isValid(const QString &vSettingFile) +bool Settings::exists(const QString &vSettingFile) { int err = Settings::Settings_Error::eError_None; if (! QFileInfo::exists(vSettingFile)) { @@ -43,10 +43,12 @@ /*! * \brief Settings::fileName * \details returns the conf file by the settings information provided. + * \param vCategory - the settings file category + * \param vLocale - the settings file locale * \return QString configuration/settings file name */ -QString Settings::fileName(const QString &vCategory) { - return QString("%1%2.%3").arg(Storage::Settings_Path()).arg(vCategory).arg(_settingsExt); +QString Settings::fileName(const QString &vCategory, const QString &vLocale) { + return QString("%1%2%4.%3").arg(Storage::Settings_Path()).arg(vCategory).arg(_settingsExt).arg(vLocale.trimmed().isEmpty() ? "" : _settingsLocalSeparator + vLocale); } /*! @@ -56,151 +58,155 @@ */ int Settings::read() { - int err = Settings::Settings_Error::eError_None; - QStringList fileFilter = QStringList() << QString("*.%1").arg(_settingsExt); - QFileInfoList settingFiles = FileHandler::find (Storage::Settings_Path(), fileFilter); - QStringList settingFolders = FileHandler::subFolders(Storage::Settings_Path()); + int err = Settings::Settings_Error::eError_None; + QFileInfo mSettingFile; + QString mCategory; + for ( quint8 i = 0; i <= _categoryCount; i++ ) { + mCategory = category(static_cast(i)); + switch (i) { // NOTE: don't use default case + case eSettingsSystem : + case eMessagesUnhandled : + mSettingFile.setFile(fileName(mCategory, "")); + // FALLTHROUGH + break; + case eInstructions : + case eConfigurationsDataList : + case eAlarms : + case eEvents : + case eGenericConfirm : + mSettingFile.setFile(fileName(mCategory, _Settings.systemLocale())); + // FALLTHROUGH + break; + } - if ( ! settingFolders.count() ) { - err = Settings::Settings_Error::eError_No_SettingsFolder; - LOG_APPED_PO(errorMessage(err).arg(Storage::Settings_Path())); - return err; - } - - for ( QString &settingFolder : settingFolders ) { - QString folder = settingFolder.prepend(Storage::Settings_Path()); - settingFiles += FileHandler::find(folder, fileFilter); - } - - // DEBUG: - // settingFiles = - // { - // QFileInfo("/home/denali/Projects/application/resources/settings/Confirm/Confirm.conf"), - // QFileInfo("/home/denali/Projects/application/resources/settings/Alarms/Alarms.conf") - // }; - - if ( ! settingFiles.count() ) { - err = Settings::Settings_Error::eError_No_SettingsFile; - LOG_APPED_PO(errorMessage(err).arg(Storage::Settings_Path())); - return err; - } - - QList details; - for (const auto &settingFile: settingFiles) { - if (! isValid(settingFile.absoluteFilePath())) continue; - - QFile file(settingFile.absoluteFilePath()); + if (! exists(mSettingFile.absoluteFilePath())) continue; // error handling is done in exists(), so just continue. + QFile file(mSettingFile.absoluteFilePath()); if (! file.open(QIODevice::ReadOnly | QIODevice::Text)) { err = Settings::Settings_Error::eError_Read; LOG_APPED_PO(errorMessage(err).arg(Storage::Settings_Path())); return err; } Detail detail; - detail.content = file.readAll().trimmed(); + detail.content = file.readAll().trimmed(); if (detail.content.isEmpty()) { //TODO Do not error out for now, the list of the config files which can be empty or not needs to be defined. // err = Settings::Settings_Error::eError_Empty; - LOG_APPED_PO(errorMessage(Settings::Settings_Error::eError_Empty).arg(settingFile.fileName())); + LOG_APPED_PO(errorMessage(Settings::Settings_Error::eError_Empty).arg(mSettingFile.fileName())); continue; } - detail.location = settingFile.absolutePath() + "/"; - detail.category = QString(detail.location + settingFile.baseName()).remove(Storage::Settings_Path()); - details += detail; + detail.location = mSettingFile.absolutePath() + "/"; + detail.category = mCategory; + + if ( parse(detail) ) { + LOG_DEBUG( (QString("Configuration file '%1' successfully loaded").arg(mSettingFile.fileName())) ); + } + else { + LOG_DEBUG( (QString("Configuration file '%1' failed to load").arg(mSettingFile.fileName())) ); + } } + return err; +} - for (const auto &detail : details) { - bool enableDuplicateKey = false; - QString attribute = QString("%1%2").arg(_config_attribute_tag); - QString group = ""; - QStringList lines = detail.content.split('\n'); - for (QString line : lines) { - // ----- trim the line - line = line.trimmed(); +/*! + * \brief Settings::parse + * \details The function to parse the content of the conf file and fill in the Settings model. + * \param vDetail - the conf file detail has been read. + * \return bool - true on success. + */ +bool Settings::parse(const Detail &vDetail) { + bool enableDuplicateKey = false; + QString attribute = QString("%1%2").arg(_config_attribute_tag); + QString group = ""; + QStringList lines = vDetail.content.split('\n'); + for (QString line : lines) { + // ----- trim the line + line = line.trimmed(); - // ----- ignore empty lines - if ( line.isEmpty() ) continue; + // ----- ignore empty lines + if ( line.isEmpty() ) continue; - // ----- find comments - int commentPosition = line.indexOf('#'); + // ----- find comments + int commentPosition = line.indexOf('#'); - // ----- ignore comment line or find attributes - if ( commentPosition == 0 ) { + // ----- ignore comment line or find attributes + if ( commentPosition == 0 ) { - // ----- find the configuration file attribute - int attributeTagPosition = line.indexOf(_config_attribute_tag); - if ( attributeTagPosition == 0 ) { + // ----- find the configuration file attribute + int attributeTagPosition = line.indexOf(_config_attribute_tag); + if ( attributeTagPosition == 0 ) { - // ----- find the attribute : duplicate_key_... - if ( line == attribute.arg( _duplicate_key_on ) ) { enableDuplicateKey = true ;} - else if ( line == attribute.arg(_duplicate_key_off ) ) { enableDuplicateKey = false ;} + // ----- find the attribute : duplicate_key_... + if ( line == attribute.arg( _duplicate_key_on ) ) { enableDuplicateKey = true ;} + else if ( line == attribute.arg(_duplicate_key_off ) ) { enableDuplicateKey = false ;} - else { - LOG_APPED_PO(( "Unknown '" + line + "' attribute in %1").arg(detail.category)); - } + else { + LOG_APPED_PO(( "Unknown '" + line + "' attribute in %1").arg(vDetail.category)); + return false; } - - // next line - continue; } - // ----- remove inline comment - if ( commentPosition > 0 ) line.truncate(commentPosition); - line = line.trimmed(); + // next line + continue; + } - // ----- find group - if (line.startsWith("[") && line.endsWith("]")) { - line.replace("[","").replace("]", ""); - group = line; + // ----- remove inline comment + if ( commentPosition > 0 ) line.truncate(commentPosition); + line = line.trimmed(); + + // ----- find group + if (line.startsWith("[") && line.endsWith("]")) { + line.replace("[","").replace("]", ""); + group = line; + } + else { + if ( group.isEmpty() ) { + continue; } else { - if ( group.isEmpty() ) { - continue; - } - else { - if ( ! line.isEmpty() ) { - QString key = ""; - QString value = ""; - if ( line.contains('=') ) { - QStringList keyValue = line.split('='); - key = keyValue[0].trimmed().replace("\\n","\n"); - value = keyValue[1].trimmed().replace("\\n","\n"); - } - else { - key = line; - } - _Settings.add(detail.category, group, key, QVariant(value), enableDuplicateKey); - // DEBUG: qDebug() << group << key << value << location << category; + if ( ! line.isEmpty() ) { + QString key = ""; + QString value = ""; + if ( line.contains('=') ) { + QStringList keyValue = line.split('='); + key = keyValue[0].trimmed().replace("\\n","\n"); + value = keyValue[1].trimmed().replace("\\n","\n"); } + else { + key = line; + } + _Settings.add(vDetail.category, group, key, QVariant(value), enableDuplicateKey); + // DEBUG: qDebug() << group << key << value << location << category; } } - // DEBUG: qDebug() << group << line; } + // DEBUG: qDebug() << group << line; } - return err; + return true; } /*! * \brief Settings::save * \details Writes the setting in the configuration files * \return */ -int Settings::save(const QString &vCategory, const QString &vGroup, const QString &vKey, const QString &vValue) +int Settings::saveSystem(const QString &vGroup, const QString &vKey, const QString &vValue) { // qDebug() << vCategory // << vGroup // << vKey // << vValue; - QString mFileName = fileName(vCategory); + QString mCategory = Storage::Settings_Category_SettingsSystem; + QString mFileName = fileName(mCategory, ""); QString mContent; int err = Settings_Error::eError_None; // -------------------------------------------------------------------------------------------------------------- //Note: the configuration files which can be saved, are like settings and should not have duplicate values. // as an example the Alarm volume can't have two separate duplicate entry in the settings. // -------------------------------------------------------------------------------------------------------------- - _Settings.add(vCategory, vGroup, vKey, vValue, false); + _Settings.add(mCategory, vGroup, vKey, vValue, false); QString mPath = QFileInfo(mFileName).absolutePath(); if ( mPath.trimmed().isEmpty() ) { err = Settings_Error::eError_PathEmpty; @@ -214,10 +220,10 @@ return err; } - for ( const auto &group : _Settings.groups(vCategory) ) { + for ( const auto &group : _Settings.groups(mCategory) ) { mContent += QString("\n[%1]\n").arg(group); - for ( const auto &key : _Settings.keys(vCategory, group) ) { - mContent += QString("%1 = %2\n").arg(key).arg(_Settings.value(vCategory, group, key).toString()); + for ( const auto &key : _Settings.keys(mCategory, group) ) { + mContent += QString("%1 = %2\n").arg(key).arg(_Settings.value(mCategory, group, key).toString()); } }