Index: sources/storage/Settings.h =================================================================== diff -u -rb794e2e09c4a06e6faaa722f2dc35f50e5f2e8e0 -rd9b9df9b23da89b4c27f4672ff6e7f570adcc48a --- sources/storage/Settings.h (.../Settings.h) (revision b794e2e09c4a06e6faaa722f2dc35f50e5f2e8e0) +++ sources/storage/Settings.h (.../Settings.h) (revision d9b9df9b23da89b4c27f4672ff6e7f570adcc48a) @@ -42,15 +42,14 @@ { private: // Settings - const char *_config_attribute_tag = "#--"; - const char *_duplicate_key_on = "duplicate_key_on"; - const char *_duplicate_key_off = "duplicate_key_off"; - const char *_settingsExt = "conf"; - const char *_settingsFormat = "%1/%2.%3"; - const char *_settingsLocalSeparator = "_"; + static constexpr const char *_config_attribute_tag = "#--"; + static constexpr const char *_duplicate_key_on = "duplicate_key_on"; + static constexpr const char *_duplicate_key_off = "duplicate_key_off"; + static constexpr const char*_settingsExt = "conf"; + static constexpr const char *_settingsFormat = "%1/%2.%3"; + static constexpr const char*_settingsLocalSeparator = "_"; + static constexpr const char*_translationExt = "qm"; - QTranslator _translator; - struct Detail { QString category; QString location; @@ -68,31 +67,37 @@ eError_Write , eError_Read , eError_Empty , + eError_Parse , eError_Copy , eError_Remove , eError_No_SettingsFolder , eError_No_SettingsFile , eError_SettingNotExists , eError_No_SettingsLocale , + eError_Not_Writable , + eError_TranslationNotExists , }; + private: - const QHash settingsError_Message { // no translation for the error. My experience shows the error messages if translated is not useful for serviceability and debugging. - { eError_None , "" }, - { eError_POST , "The configuration source check failed." }, - { eError_PathEmpty , "The settings path is empty." }, - { eError_MkDir , "The configuration folder '%1' cannot be created." }, - { eError_Write , "The settings file %1 can't be written." }, - { eError_Read , "The settings file %1 can't be read." }, - { eError_Empty , "The settings file %1 is empty." }, - { eError_Copy , "The configuration folder '%1' cannot be copied." }, - { eError_Remove , "The configuration folder '%1' cannot be removed." }, - { eError_No_SettingsFolder , "No settings folder in the %1 path found." }, - { eError_No_SettingsFile , "No settings file in the %1 folder found." }, - { eError_SettingNotExists , "The setting file %1 doesn't exists." }, - { eError_No_SettingsLocale , "The system locale no defined." }, + static inline QHash settingsError_Message { // no translation for the error. My experience shows the error messages if translated is not useful for serviceability and debugging. + { eError_None , "The configuration file '%1' successfully loaded," }, + { eError_POST , "The configuration source check failed." }, + { eError_PathEmpty , "The settings path is empty." }, + { eError_MkDir , "The configuration folder '%1' cannot be created." }, + { eError_Write , "The settings file %1 can't be written." }, + { eError_Read , "The settings file %1 can't be read." }, + { eError_Empty , "The settings file %1 is empty." }, + { eError_Parse , "The settings file %1 parse error." }, + { eError_Copy , "The configuration folder '%1' cannot be copied." }, + { eError_Remove , "The configuration folder '%1' cannot be removed." }, + { eError_No_SettingsFolder , "No settings folder in the %1 path found." }, + { eError_No_SettingsFile , "No settings file in the %1 folder found." }, + { eError_SettingNotExists , "The setting file %1 doesn't exists." }, + { eError_No_SettingsLocale , "The system locale not defined. The default en_US used." }, + { eError_Not_Writable , "The setting file %1 is not writable." }, + { eError_TranslationNotExists , "The translation file %1 doesn't exists." }, }; -public: - const QString errorMessage(int vErr, QString vArg1 = "", QString vArg2 = "") { + static const QString errorMessage(int vErr, QString vArg1 = "", QString vArg2 = "") { if ( settingsError_Message.contains(vErr) ) { if ( ! vArg1.isEmpty() ) return QString(settingsError_Message[vErr]).arg(vArg1); if ( ! vArg2.isEmpty() ) return QString(settingsError_Message[vErr]).arg(vArg1).arg(vArg2); @@ -102,63 +107,108 @@ } // } Class SettingsError -private: - const QString makeSetting(const char *vPath, const char *vFile) { - QString literal(_settingsFormat); - return QString(literal.arg(vPath).arg(vFile).arg(_settingsExt)); - } - - bool exists(const QString &vSettingFile); - - QString fileName(const QString &vCategory, const QString &vLocale); - - bool parse(const Detail &vDetail); - - -public: Settings() {} +public: enum Category_Enum { eSettingsSystem = 0, // has to be the first one to read the other configs according to the locale set in the Systems.conf eInstructions , - eConfigurationsDataList , // TODO: the category for this conf is not used. may need to be merged into the Settings, but since it is list, It needs a little more thought. + eParametersDataList , // TODO: the category for this conf is not used. may need to be merged into the Settings, but since it is list, It needs a little more thought. eAlarms , eEvents , eRejects , eMessagesUnhandled , eGenericConfirm , + eTranslation , + eConfigurationsLocale , + }; // IMPORTANT: not having a eCount in the enum is intentional. to make sure in switch no enum is missed when not using the default. - const int _categoryCount = eGenericConfirm; + static constexpr int _configurationsCount = eGenericConfirm; enum Key_Enum { - eKeyTitle , - eKeyMessage , - eKeyListTitle , - eKeyConfirm , - eKeyCancel , + eKeyTitle , + eKeyMessage , + eKeyListTitle , + eKeyConfirm , + eKeyCancel , }; enum Location_Enum { eInit , eSecured , }; - int read (); - int saveSystem (const QString &vGroup, const QString &vKey, const QString &vValue); - int configurationsMove (QString *vMessage = nullptr, bool vIsUpdate = false); - int configurationsPOST (Location_Enum vLoc = Location_Enum::eSecured) { Q_UNUSED(vLoc); return true; } - int loadTranslation (); + static int readConfigurations (); + static int save (const QString &vGroup, const QString &vKey, const QString &vValue, Category_Enum vCategory = eSettingsSystem); + static int configurationsMove (QString *vMessage = nullptr, bool vIsUpdate = false); + static int readLocale (); + static int loadTranslation (QTranslator &vTranslator); + static QString locale(Category_Enum vCategory, bool vSeparator = false) { + auto mLocale = [=](){ + QString s = _Settings.systemLocale(); + QString l = s.isEmpty() ? "" : QString( vSeparator ? _settingsLocalSeparator : "") + s ; + return l; + }; + switch (vCategory) { // NOTE: don't use default case + case eSettingsSystem : return "" ; + case eInstructions : return mLocale() ; + case eParametersDataList : return mLocale() ; + case eAlarms : return mLocale() ; + case eEvents : return mLocale() ; + case eRejects : return mLocale() ; + case eMessagesUnhandled : return "" ; + case eGenericConfirm : return mLocale() ; + case eTranslation : return mLocale() ; + case eConfigurationsLocale : return "" ; + } + return ""; + } + + static QString extention(Category_Enum vCategory) { + switch (vCategory) { // NOTE: don't use default case + case eSettingsSystem : return _settingsExt ; + case eInstructions : return _settingsExt ; + case eParametersDataList : return _settingsExt ; + case eAlarms : return _settingsExt ; + case eEvents : return _settingsExt ; + case eRejects : return _settingsExt ; + case eMessagesUnhandled : return _settingsExt ; + case eGenericConfirm : return _settingsExt ; + case eTranslation : return _translationExt ; + case eConfigurationsLocale : return _settingsExt ; + } + return ""; + } + + static QString path(Category_Enum vCategory) { + switch (vCategory) { // NOTE: don't use default case + case eSettingsSystem : return Storage::Settings_Path() ; + case eInstructions : return Storage::Settings_Path() ; + case eParametersDataList : return Storage::Settings_Path() ; + case eAlarms : return Storage::Settings_Path() ; + case eEvents : return Storage::Settings_Path() ; + case eRejects : return Storage::Settings_Path() ; + case eMessagesUnhandled : return Storage::Settings_Path() ; + case eGenericConfirm : return Storage::Settings_Path() ; + case eTranslation : return Storage::Translations_Path() ; + case eConfigurationsLocale : return Storage::Configurations_Path() ; + } + return ""; + } + static QString category(Category_Enum vCategory) { switch (vCategory) { // NOTE: don't use default case case eSettingsSystem : return Storage::Settings_Category_SettingsSystem ; case eInstructions : return Storage::Settings_Category_Instructions ; - case eConfigurationsDataList : return Storage::Settings_Category_ConfigurationsDataList ; + case eParametersDataList : return Storage::Settings_Category_DataList ; case eAlarms : return Storage::Settings_Category_Alarms ; case eEvents : return Storage::Settings_Category_Events ; case eRejects : return Storage::Settings_Category_Rejects ; case eMessagesUnhandled : return Storage::Settings_Category_MessagesUnhandled ; case eGenericConfirm : return Storage::Settings_Category_GenericConfirm ; + case eTranslation : return Storage::Settings_Category_Translation ; + case eConfigurationsLocale : return Storage::Settings_Category_Locale ; } return ""; } @@ -185,8 +235,9 @@ static bool isCategorySettingsSystem (const QString &vCategory) { return vCategory == category( eSettingsSystem ); } static bool isCategoryInstructions (const QString &vCategory) { return vCategory == category( eInstructions ); } - static bool isCategoryConfigurationsDataList (const QString &vCategory) { return vCategory == category( eConfigurationsDataList ); } + static bool isCategoryParametersDataList (const QString &vCategory) { return vCategory == category( eParametersDataList ); } static bool isCategoryAlarms (const QString &vCategory) { return vCategory == category( eAlarms ); } + static bool isCategoryEvents (const QString &vCategory) { return vCategory == category( eEvents ); } static bool isCategoryRejects (const QString &vCategory) { return vCategory == category( eRejects ); } static bool isCategoryMessagesUnhandled (const QString &vCategory) { return vCategory == category( eMessagesUnhandled ); } static bool isCategoryConfirm (const QString &vCategory) { return vCategory == category( eGenericConfirm ); } @@ -196,6 +247,28 @@ static bool isKeyConfirm (const QString &vKey ) { return vKey == key ( eKeyConfirm ); } static bool isKeyCancel (const QString &vKey ) { return vKey == key ( eKeyCancel ); } +private: // using the Category_Enum so have to be defined after. + /*! + * \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 + */ + static QString fileName ( Category_Enum vCategory ) { + return QString("%1%2%4.%3").arg(path(vCategory)).arg(category(vCategory)).arg(extention(vCategory)).arg(locale(vCategory,true)); + } + static bool isCategoryWritable ( Category_Enum vCategory ) { + const QVector mCategory_Writable { + eSettingsSystem , + eConfigurationsLocale , + }; + return mCategory_Writable.contains(vCategory); + } + + static bool parse (const Detail &vDetail ); + static int readCategory (Category_Enum vCategory ); + static int configurationsPOST (Location_Enum vLoc = Location_Enum::eSecured) { Q_UNUSED(vLoc); return true; } }; }