Index: sources/storage/Settings.cpp =================================================================== diff -u -r2ef03b2ce51b4dc507f66e9671953a8e0824bde9 -rd9b9df9b23da89b4c27f4672ff6e7f570adcc48a --- sources/storage/Settings.cpp (.../Settings.cpp) (revision 2ef03b2ce51b4dc507f66e9671953a8e0824bde9) +++ sources/storage/Settings.cpp (.../Settings.cpp) (revision d9b9df9b23da89b4c27f4672ff6e7f570adcc48a) @@ -29,204 +29,187 @@ #include "Settings.h" -bool Settings::isValid(const QString &vSettingFile) +/*! + * \brief Settings::readCategory + * \details Reads, parses and adds the read value to the settins. + * \param vCategory - the category of the configuration file to read. + * \return non-zero, error value on error, and zero on success. + */ +int Settings::readCategory(Category_Enum vCategory) { int err = Settings::Settings_Error::eError_None; - if (! QFileInfo::exists(vSettingFile)) { - err = Settings::Settings_Error::eError_SettingNotExists; - LOG_DEBUG(errorMessage(err).arg(vSettingFile)); - return false; - } - return true; -} + QString msg; + QFileInfo mSettingFile; + QString mCategory; + QString mFileName; + Detail mDetail; -/*! - * \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); + mCategory = category(vCategory); + mFileName = fileName(vCategory); + qDebug() << mCategory << mFileName; + mSettingFile.setFile(mFileName); + QFile mFile(mSettingFile.absoluteFilePath()); + if (! mFile.exists() ) { err = Settings::Settings_Error::eError_SettingNotExists ; msg = errorMessage(err, mSettingFile.fileName ()) ; goto lOut; } + if (! mFile.open(QIODevice::ReadOnly | QIODevice::Text) ) { err = Settings::Settings_Error::eError_Read ; msg = errorMessage(err, Storage::Settings_Path()) ; goto lOut; } + + mDetail.content = mFile.readAll().trimmed(); + if (mDetail.content.isEmpty() ) { err = Settings::Settings_Error::eError_Empty ; msg = errorMessage(err, mSettingFile.fileName ()) ; goto lOut; } + + mDetail.location = mSettingFile.absolutePath() + "/"; + mDetail.category = mCategory; + + if ( ! parse(mDetail) ) { err = Settings::Settings_Error::eError_Parse ; msg = errorMessage(err, mSettingFile.fileName ()) ; goto lOut; } + else { msg = errorMessage(err, mSettingFile.fileName ()) ; } + +lOut: + LOG_APPED_PO( msg ); + return err; } /*! - * \brief Settings::doRead + * \brief Settings::readConfigurations * \details Reads all the configuration files * \return non-zero, error value on error, and zero on success. */ -int Settings::read() +int Settings::readConfigurations() { - 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()); - - if ( ! settingFolders.count() ) { - err = Settings::Settings_Error::eError_No_SettingsFolder; - LOG_APPED_PO(errorMessage(err).arg(Storage::Settings_Path())); - return err; + int err = Settings::Settings_Error::eError_None; + for ( quint8 i = 0; i <= _configurationsCount; i++ ) { + readCategory(static_cast(i)); } + return err; +} - for ( QString &settingFolder : settingFolders ) { - QString folder = settingFolder.prepend(Storage::Settings_Path()); - settingFiles += FileHandler::find(folder, fileFilter); - } +/*! + * \brief Settings::readLocale + * \return + */ +int Settings::readLocale() +{ + int err = Settings_Error::eError_None; + err = readCategory(eConfigurationsLocale); + return err; +} - // DEBUG: - // settingFiles = - // { - // QFileInfo("/home/denali/Projects/application/resources/settings/Confirm/Confirm.conf"), - // QFileInfo("/home/denali/Projects/application/resources/settings/Alarms/Alarms.conf") - // }; +/*! + * \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(); - if ( ! settingFiles.count() ) { - err = Settings::Settings_Error::eError_No_SettingsFile; - LOG_APPED_PO(errorMessage(err).arg(Storage::Settings_Path())); - return err; - } + // ----- ignore empty lines + if ( line.isEmpty() ) continue; - QList details; - for (const auto &settingFile: settingFiles) { - if (! isValid(settingFile.absoluteFilePath())) continue; + // ----- find comments + int commentPosition = line.indexOf('#'); - QFile file(settingFile.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(); - 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())); - continue; - } + // ----- ignore comment line or find attributes + if ( commentPosition == 0 ) { - detail.location = settingFile.absolutePath() + "/"; - detail.category = QString(detail.location + settingFile.baseName()).remove(Storage::Settings_Path()); - details += detail; - } + // ----- find the configuration file attribute + int attributeTagPosition = line.indexOf(_config_attribute_tag); + if ( attributeTagPosition == 0 ) { - 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(); + // ----- find the attribute : duplicate_key_... + if ( line == attribute.arg( _duplicate_key_on ) ) { enableDuplicateKey = true ;} + else if ( line == attribute.arg(_duplicate_key_off ) ) { enableDuplicateKey = false ;} - // ----- ignore empty lines - if ( line.isEmpty() ) continue; + else { + LOG_APPED_PO(( "Unknown '" + line + "' attribute in %1").arg(vDetail.category)); + return false; + } + } - // ----- find comments - int commentPosition = line.indexOf('#'); + // next line + continue; + } + // ----- remove inline comment + if ( commentPosition > 0 ) line.truncate(commentPosition); + line = line.trimmed(); - // ----- 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_PO(( "Unknown '" + line + "' attribute in %1").arg(detail.category)); - } - } - - // next line + // ----- find group + if (line.startsWith("[") && line.endsWith("]")) { + line.replace("[","").replace("]", ""); + group = line; + } + else { + if ( group.isEmpty() ) { 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; - } 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::save(const QString &vGroup, const QString &vKey, const QString &vValue, Category_Enum vCategory) { // qDebug() << vCategory // << vGroup // << vKey // << vValue; - QString mFileName = fileName(vCategory); + + int err = Settings_Error::eError_None; + QString msg; + QString mCategory; + QString mPath; + QString mFileName; QString mContent; - int err = Settings_Error::eError_None; + + if ( ! isCategoryWritable(vCategory) ) { err = Settings_Error::eError_Not_Writable ; msg = errorMessage(err ); goto lOut; } + + mCategory = category(vCategory); + mFileName = fileName(vCategory); // -------------------------------------------------------------------------------------------------------------- //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) ) { + _Settings.add(mCategory, vGroup, vKey, vValue, false); + mPath = QFileInfo(mFileName).absolutePath(); + if ( mPath.trimmed().isEmpty() ) { err = Settings_Error::eError_PathEmpty ; msg = errorMessage(err ); goto lOut; } + if ( ! FileHandler::makeFolder(mPath) ) { err = Settings_Error::eError_MkDir ; msg = errorMessage(err, mPath ); goto lOut; } + 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()); } } + if ( ! FileHandler::write(mFileName,mContent, false) ) { err = Settings_Error::eError_Write ; msg = errorMessage(err, mFileName ); goto lOut; } - if ( ! FileHandler::write(mFileName,mContent, false) ) { - err = Settings_Error::eError_Write; - LOG_DEBUG(errorMessage(err).arg(mFileName)); - return err; - } - +lOut: + if ( err ) LOG_DEBUG( msg ); return err; } @@ -248,19 +231,47 @@ lstExclude << QFileInfo( src + Storage::Settings_Category_SettingsSystem ).absolutePath(); } - if ( ! Settings ::configurationsPOST(loc )) { err = Settings_Error::eError_Remove; msg = errorMessage(err ); LOG_DEBUG(msg); goto lOut; } - if ( ! FileHandler ::makeFolder ( dst )) { err = Settings_Error::eError_Copy ; msg = errorMessage(err ); LOG_DEBUG(msg); goto lOut; } + if ( ! Settings ::configurationsPOST(loc )) { err = Settings_Error::eError_Remove; msg = errorMessage(err ); goto lOut; } + if ( ! FileHandler ::makeFolder ( dst )) { err = Settings_Error::eError_Copy ; msg = errorMessage(err ); goto lOut; } for( QString dir : FileHandler::subFolders(src)) { QString sub = src + dir; if ( ! lstExclude.contains( sub ) ) { - if ( FileHandler ::copyFolder (sub, dst )) { err = Settings_Error::eError_MkDir ; msg = errorMessage(err, dir); LOG_DEBUG(msg); goto lOut; } + if ( FileHandler ::copyFolder (sub, dst )) { err = Settings_Error::eError_MkDir ; msg = errorMessage(err, dir); goto lOut; } } - if ( FileHandler ::removeFolder (sub )) { err = Settings_Error::eError_POST ; msg = errorMessage(err, dir); LOG_DEBUG(msg); goto lOut; } + if ( FileHandler ::removeFolder (sub )) { err = Settings_Error::eError_POST ; msg = errorMessage(err, dir); goto lOut; } } Storage::Settings_Secured(); lOut: + if ( err ) LOG_DEBUG ( msg ); if ( vMessage ) { *vMessage = msg; } return err; } + +/*! + * \brief Settings::loadTranslation + * \return + */ +int Settings::loadTranslation(QTranslator &vTranslator) +{ + int err = Settings_Error::eError_None; + QString msg; + QString translationFile; + bool ok = true; + + QString locale = _Settings.systemLocale(); + if( locale.isEmpty() ) { err = Settings_Error::eError_No_SettingsLocale ; msg = errorMessage(err ); goto lOut; } + + translationFile = fileName(eTranslation); + ok = vTranslator.load(translationFile); + + if ( ! ok ) { err = Settings_Error::eError_TranslationNotExists ; msg = errorMessage(err, translationFile ); goto lOut; } + else { msg = errorMessage(err, translationFile ); } + + QCoreApplication::installTranslator(&vTranslator); + +lOut: + LOG_APPED_PO( msg ); + return err; +}