Index: sources/view/settings/VSettings.cpp =================================================================== diff -u -rf68b226e67eb500758ee94fe015df48931240013 -r6c6f1f5d466badd9b4fd67be7c907234c342b2a2 --- sources/view/settings/VSettings.cpp (.../VSettings.cpp) (revision f68b226e67eb500758ee94fe015df48931240013) +++ sources/view/settings/VSettings.cpp (.../VSettings.cpp) (revision 6c6f1f5d466badd9b4fd67be7c907234c342b2a2) @@ -1,13 +1,13 @@ /*! * - * Copyright (c) 2021-2022 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 VSettings.cpp * \author (last) Behrouz NematiPour - * \date (last) 08-Nov-2022 + * \date (last) 02-Apr-2023 * \author (original) Behrouz NematiPour * \date (original) 29-Mar-2021 * @@ -16,36 +16,61 @@ // Qt #include +#include // Project #include "GuiController.h" #include "MSettings.h" #include "Settings.h" +#include VIEW_DEF_CLASS(VSettings) void VSettings::initConnections() { ACTION_VIEW_CONNECTION (SettingsData ); + connect(&_GuiController, SIGNAL(didActionReceive (GuiActionType, const QVariantList &)), + this , SLOT( onActionReceive (GuiActionType, const QVariantList &))); + +} + +void VSettings::initConnectionsSettings() +{ + static bool init = false; if ( init ) return; + PROPERTY_POST_CONNECTION(VSettings, servicePass ); PROPERTY_POST_CONNECTION(VSettings, alarmVolume ); + PROPERTY_POST_CONNECTION(VSettings, roWaterMode ); PROPERTY_POST_CONNECTION(VSettings, noCANBus ); - connect(&_GuiController, SIGNAL(didActionReceive (GuiActionType, const QVariantList &)), - this , SLOT( onActionReceive (GuiActionType, const QVariantList &))); - + init = true; } void VSettings::servicePass_post(const QString &vservicePass) { + //TODO The Settings shall be the Singleton SettingsController and modify the MSettings like the others. Storage::Settings settings; - settings.save(servicePassCategory(), servicePassGroup(), servicePassKey(), vservicePass); + if ( settings.save(servicePassCategory(), servicePassGroup(), servicePassKey(), vservicePass) != 0 ) { + servicePass(""); + // FIXME: Notify UI with a message + } } +void VSettings::roWaterMode_post(const bool &vroWaterMode_post) { + //TODO The Settings shall be the Singleton SettingsController and modify the MSettings like the others. + Storage::Settings settings; + if ( settings.save(roWaterModeCategory(), roWaterModeGroup(), roWaterModeKey(), QString::number(vroWaterMode_post)) != 0 ) { + roWaterMode(false); + // FIXME: Notify UI with a message + } +} + void VSettings::alarmVolume_post(const quint8 &valarmVolume) { + //TODO The Settings shall be the Singleton SettingsController and modify the MSettings like the others. Storage::Settings settings; settings.save(alarmVolumeCategory(), alarmVolumeGroup(), alarmVolumeKey(), QString::number(valarmVolume)); } void VSettings::noCANBus_post(const bool &vnoCANBus) { + //TODO The Settings shall be the Singleton SettingsController and modify the MSettings like the others. Storage::Settings settings; settings.save(noCANBusCategory(), noCANBusGroup(), noCANBusKey(), QString::number(vnoCANBus)); } @@ -75,6 +100,12 @@ keyValue[key] = mServicePass ; servicePass ( mServicePass); } + else if ( isroWaterMode (category, group, key) ) { + bool mRoWaterMode; + mRoWaterMode = _Settings.value(category, group, key).toBool (); + keyValue[key] = mRoWaterMode ; + roWaterMode ( mRoWaterMode); + } else if ( isalarmVolume (category, group, key) ) { quint8 mAlarmVolume; mAlarmVolume = _Settings.value(category, group, key).toInt (); // returns 0 if fails, so no error checking needed. @@ -104,9 +135,13 @@ // otherwise will use the default value and will notify the update. servicePass ( _servicePass ); alarmVolume ( _alarmVolume ); + roWaterMode ( _roWaterMode ); // noCANBus ( _noCANBus ); // This line has been put here to remind developers that it is intentionally removed, to not to add a default value. + //DEBUG qDebug() << servicePass() << roWaterMode() << alarmVolume() << noCANBus(); adjustment(true); + + initConnectionsSettings(); } VSettings::TKeysList VSettings::updateReplacements(const QString &vGroup, const QStringList &vKeys) @@ -207,3 +242,82 @@ } if ( isChanged ) emit instructionsChanged(_instructions); } + +/*! + * \brief VSettings::isPasswordValid + * \details Validates the passed string for conforming to the + * - at least one capital letter, + * - at least one digit, + * - at least one symbol + * rule + * \param vPassword - the string to check + * \returns true if string pass conforms, false otherwise + */ +bool View::VSettings::isPasswordValid(const QString &vPassword) { + int minLen = 8 ; + QString pla = "(?=.*[%1])" ; // positive look ahead + QString regSntnc = "^%1$" ; // regular expression sentence with has a start/end + QString regUpper = "A-Z" ; + QString regLower = "a-z" ; + QString regDigit = "0-9" ; + QString regSymbl = "@$!%*?&.,_-" ; + QString rln = "[%1]{%2,}" ; + + // "^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[@$!%*?&])[A-Za-z0-9@$!%*?&]{8,}$" + QString regStr = regSntnc.arg( + pla.arg(regUpper) + + pla.arg(regLower) + + pla.arg(regDigit) + + pla.arg(regSymbl) + + rln.arg(regUpper + + regLower + + regDigit + + regSymbl) + .arg(minLen ) + ); + QRegularExpression passwordRegex(regStr); + bool ok; + ok = passwordRegex.match(vPassword).hasMatch(); + return ok; +} + +QString View::VSettings::hashedPassword(const QString &vPassword, bool vIsService) +{ + bool ok; + QString hashed = encryption::hashedString(vPassword, ok, ! vIsService); + encryptionPass(ok); + return ok ? hashed : ""; +} + +/*! + * \brief View::VSettings::isServicePasswordMatch + * \details matches the given password string, vPassword, with the service password. + * \param vPassword - the given password string + * \return true if it matches. + */ +bool View::VSettings::isServicePasswordMatch(const QString &vPassword) +{ + //DEBUG qDebug() << __FUNCTION__ << _servicePass << hashedPassword(vPassword, true); + bool isNotEmpty = ( ! vPassword.trimmed().isEmpty() ); + bool isMatch = ( _servicePass == hashedPassword(vPassword, true) ); + return isMatch && isNotEmpty; +} + +/*! + * \brief View::VSettings::updateServicePassword + * \details Updatest the service password with the given password. + * \param vPassword - the password to be saved as the service password. + */ +void View::VSettings::updateServicePassword(const QString &vPassword) +{ + servicePass(hashedPassword(vPassword, true)); +} + +void View::VSettings::checkServicePasswordSet() +{ + bool ok; + isDefaultServicePassword(encryption::isDefaultServicePassword(servicePass(), ok)); + //DEBUG qDebug() << __FUNCTION__ << ok; + encryptionPass(ok); +} +