Index: sources/storage/Settings.cpp =================================================================== diff -u -r61f16c988a159401c92730b4cbfca5085c77222f -r6c6f1f5d466badd9b4fd67be7c907234c342b2a2 --- sources/storage/Settings.cpp (.../Settings.cpp) (revision 61f16c988a159401c92730b4cbfca5085c77222f) +++ sources/storage/Settings.cpp (.../Settings.cpp) (revision 6c6f1f5d466badd9b4fd67be7c907234c342b2a2) @@ -1,15 +1,15 @@ /*! * - * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. + * Copyright (c) 2021-2023 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 Settings.cpp * \author (last) Behrouz NematiPour - * \date (last) 16-Oct-2020 + * \date (last) 01-Mar-2023 * \author (original) Behrouz NematiPour - * \date (original) 17-Jul-2020 + * \date (original) 29-Mar-2021 * */ @@ -31,36 +31,55 @@ bool Settings::isValid(const QString &vSettingFile) { + int err = Settings::Settings_Error::eError_None; if (! QFileInfo::exists(vSettingFile)) { - LOG_DEBUG("Setting file " + vSettingFile + " does not exist."); + err = Settings::Settings_Error::eError_SettingNotExists; + LOG_DEBUG(errorMessage(err).arg(vSettingFile)); return false; } return true; } /*! - * \brief Settings::doRead - reads all the configuration files in the home folder - * \details + * \brief Settings::fileName + * \details returns the conf file by the settings information provided. + * \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); +} + +/*! + * \brief Settings::doRead + * \details Reads all the configuration files + */ int Settings::read() { + int err = Settings::Settings_Error::eError_None; QStringList fileFilter = QStringList() << QString("*.%1").arg(_settingsExt); - QFileInfoList settingFiles = FileHandler::find (Storage::Settings_Path_Name, fileFilter); - QStringList settingFolders = FileHandler::subFolders(Storage::Settings_Path_Name); + QFileInfoList settingFiles = FileHandler::find (Storage::Settings_Path(), fileFilter); + QStringList settingFolders = FileHandler::subFolders(Storage::Settings_Path()); if ( ! settingFolders.count() ) { - LOG_DEBUG(QObject::tr("No setting folder in the %1").arg(Storage::Settings_Path_Name)); - return 1; // TODO : Define an error enum when completed + err = Settings::Settings_Error::eError_No_SettingsFolder; + LOG_DEBUG(errorMessage(err).arg(Storage::Settings_Path())); + return err; } for ( QString &settingFolder : settingFolders ) { - QString folder = settingFolder.prepend(Storage::Settings_Path_Name); + 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() ) { - LOG_DEBUG(QObject::tr("No setting files in the %1").arg(Storage::Settings_Path_Name)); - return 2; // TODO : Define an error enum when completed + err = Settings::Settings_Error::eError_No_SettingsFile; + LOG_DEBUG(errorMessage(err).arg(Storage::Settings_Path())); + return err; } QList details; @@ -73,53 +92,155 @@ detail.content = file.readAll().trimmed(); if (detail.content.isEmpty()) continue; detail.location = settingFile.absolutePath() + "/"; - detail.category = QString(detail.location + settingFile.baseName()).remove(Storage::Settings_Path_Name); + detail.category = QString(detail.location + settingFile.baseName()).remove(Storage::Settings_Path()); details += detail; } } 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) { - // ignore empty lines - if ( line.trimmed().isEmpty() ) continue; + // ----- trim the line + line = line.trimmed(); - // remove comments + // ----- ignore empty lines + if ( line.isEmpty() ) continue; + + // ----- find comments int commentPosition = line.indexOf('#'); - if ( commentPosition == 0 ) continue; // comment line - if ( commentPosition > 0 ) // inline comment - line.truncate(commentPosition); - if (line.contains("[") && line.contains("]")) { + + // ----- 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 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_UI(( "Unknown '" + line + "' attribute in %1").arg(detail.category)); + } + } + + // next line + continue; + } + + // ----- remove inline comment + if ( commentPosition > 0 ) line.truncate(commentPosition); + line = line.trimmed(); + + // ----- find group + if (line.startsWith("[") && line.endsWith("]")) { line.replace("[","").replace("]", ""); - group = line.trimmed(); + group = line; } else { if ( group.isEmpty() ) { continue; } else { - line = line.trimmed(); if ( ! line.isEmpty() ) { QString key = ""; QString value = ""; if ( line.contains('=') ) { QStringList keyValue = line.split('='); - key = keyValue[0].trimmed(); - value = keyValue[1].trimmed(); + key = keyValue[0].trimmed().replace("\\n","\n"); + value = keyValue[1].trimmed().replace("\\n","\n"); } else { key = line; } - _Settings.add(group, key, QVariant(value), detail.location, detail.category); + _Settings.add(detail.category, group, key, QVariant(value), enableDuplicateKey); // DEBUG: qDebug() << group << key << value << location << category; } } } - // qDebug() << group << line; + // DEBUG: qDebug() << group << line; } } - return 0; + return err; } +/*! + * \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) +{ + // qDebug() << vCategory + // << vGroup + // << vKey + // << vValue; + QString mFileName = fileName(vCategory); + 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); + QString mPath = QFileInfo(mFileName).absolutePath(); + if ( mPath.trimmed().isEmpty() ) { + err = Settings_Error::eError_PathEmpty; + LOG_DEBUG(errorMessage(err)); + return err; + } + + if ( ! FileHandler::makeFolder(mPath) ) { + err = Settings_Error::eError_MkDir; + LOG_DEBUG(errorMessage(err).arg(mPath)); + return err; + } + + for ( const auto &group : _Settings.groups(vCategory) ) { + 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()); + } + } + + if ( ! FileHandler::write(mFileName,mContent, false) ) { + err = Settings_Error::eError_Write; + LOG_DEBUG(errorMessage(err).arg(mFileName)); + return err; + } + + return err; +} + +/*! + * \brief Settings::configurationsMove + * \details After the encrypted partition is ready the configuration files are moved from the root home to denali home. + * \param vMessage + * \return + */ +int Settings::configurationsMove(QString *vMessage) +{ + int err = Settings_Error::eError_None ; + Location_Enum loc = Location_Enum ::eInit; + QString src = Settings::location(Location_Enum ::eInit ) ; + QString dst = Settings::location(Location_Enum ::eSecured ) ; + QString msg = ""; + if ( ! Settings ::configurationsPOST(loc )) { msg = errorMessage(err ); LOG_DEBUG(msg); err = Settings_Error::eError_POST ; goto lOut; } + if ( ! FileHandler ::makeFolder ( dst )) { msg = errorMessage(err ); LOG_DEBUG(msg); err = Settings_Error::eError_MkDir ; goto lOut; } + for( QString dir : FileHandler::subFolders(src)) { + QString sub = src + dir; + if ( FileHandler ::copyFolder (sub, dst )) { msg = errorMessage(err, dir); LOG_DEBUG(msg); err = Settings_Error::eError_Copy ; goto lOut; } + if ( FileHandler ::removeFolder (sub )) { msg = errorMessage(err, dir); LOG_DEBUG(msg); err = Settings_Error::eError_Remove ; goto lOut; } + } + + Storage::Settings_Secured(); + +lOut: + if ( vMessage ) { *vMessage = msg; } + return err; +}