File indexing completed on 2024-05-12 03:47:27

0001 #include "Settings.h"
0002 
0003 #include <KConfigGroup>
0004 
0005 #include <QLatin1String>
0006 
0007 /*!
0008  * Setup new setting.
0009  * setting_name: the name of the function used after the read/write prefix
0010  * datatype: the datatype of the setting
0011  * settings_datatype: the datatype used in the settings, for example an enum class cannot be directly stored in the settings, it must be converted to an integer
0012  * setting_group: the setting group in which the setting shall be located
0013  * config_name: the actual name used in the settings for reading/writing. This value must be unique within one setting_group!
0014  * default_value: the default value used, if no value for the setting is stored in the settings file
0015  */
0016 #define SETUP_SETTING(setting_name, datatype, settings_datatype, setting_group, config_name, default_value)                                                    \
0017     namespace {                                                                                                                                                \
0018     const QLatin1String config_name##ConfigName(#config_name);                                                                                                 \
0019     }                                                                                                                                                          \
0020     /* read config */                                                                                                                                          \
0021     datatype read##setting_name() {                                                                                                                            \
0022         return static_cast<datatype>(setting_group.readEntry(config_name##ConfigName, static_cast<settings_datatype>(default_value)));                         \
0023     }                                                                                                                                                          \
0024     /* write config */                                                                                                                                         \
0025     void write##setting_name(const datatype& value) {                                                                                                          \
0026         setting_group.writeEntry(config_name##ConfigName, static_cast<settings_datatype>(value));                                                              \
0027     }
0028 
0029 namespace Settings {
0030 
0031 namespace {
0032 KSharedConfig::Ptr confPtr;
0033 } // anonymous namespace
0034 
0035 KSharedConfig::Ptr config() {
0036     if (!confPtr)
0037         confPtr = KSharedConfig::openConfig();
0038     return confPtr;
0039 }
0040 
0041 KConfigGroup group(const QString& name) {
0042     return config()->group(name);
0043 }
0044 
0045 bool sync() {
0046     return config()->sync();
0047 }
0048 
0049 KConfigGroup settingsGeneral() {
0050     const QLatin1String settingsGeneralConfigName("Settings_General");
0051     return group(settingsGeneralConfigName);
0052 }
0053 
0054 SETUP_SETTING(DockPosBehaviour, DockPosBehaviour, int, settingsGeneral(), DockReopenPositionAfterClose, DockPosBehaviour::AboveLastActive)
0055 
0056 } // namespace Settings