|
10 | 10 | #include "evdev-lst-layouts.h"
|
11 | 11 | #include "layoutmodel.h"
|
12 | 12 | #include "maindialog.h"
|
| 13 | +#include "settings.h" |
13 | 14 | #include "./ui_maindialog.h"
|
14 | 15 |
|
15 | 16 | extern "C" {
|
16 | 17 | #include "theme.h"
|
17 | 18 | #include "xml.h"
|
18 | 19 | }
|
19 | 20 |
|
20 |
| -MainDialog::MainDialog(QWidget *parent) : QDialog(parent), ui(new Ui::MainDialog) |
| 21 | +MainDialog::MainDialog(std::vector<std::shared_ptr<Setting>> &settings, QWidget *parent) |
| 22 | + : QDialog(parent), ui(new Ui::MainDialog), m_settings(settings) |
21 | 23 | {
|
22 | 24 | ui->setupUi(this);
|
23 | 25 |
|
@@ -81,7 +83,7 @@ void MainDialog::activate()
|
81 | 83 | theme_free_vector(&openbox_themes);
|
82 | 84 |
|
83 | 85 | /* Corner Radius */
|
84 |
| - ui->cornerRadius->setValue(xml_get_int("/labwc_config/theme/cornerradius")); |
| 86 | + ui->cornerRadius->setValue(xml_get_int("/labwc_config/theme/cornerRadius")); |
85 | 87 |
|
86 | 88 | /* Drop Shadows */
|
87 | 89 | ui->dropShadows->addItem("no");
|
@@ -147,16 +149,102 @@ void MainDialog::activate()
|
147 | 149 | }
|
148 | 150 | }
|
149 | 151 |
|
| 152 | +void setInt(std::vector<std::shared_ptr<Setting>> &settings, QString name, int value) |
| 153 | +{ |
| 154 | + std::shared_ptr<Setting> setting = retrieve(settings, name); |
| 155 | + if (setting == nullptr) { |
| 156 | + qDebug() << "warning: no settings with name" << name; |
| 157 | + return; |
| 158 | + } |
| 159 | + if (setting->valueType() != LAB_VALUE_TYPE_INT) { |
| 160 | + qDebug() << "setInt(): not valid int setting" << name << value; |
| 161 | + } |
| 162 | + if (value != std::get<int>(setting->value())) { |
| 163 | + qDebug() << name << "has changed to" << value; |
| 164 | + xml_set_num(name.toStdString().c_str(), value); |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +void setStr(std::vector<std::shared_ptr<Setting>> &settings, QString name, QString value) |
| 169 | +{ |
| 170 | + std::shared_ptr<Setting> setting = retrieve(settings, name); |
| 171 | + if (setting == nullptr) { |
| 172 | + qDebug() << "warning: no settings with name" << name; |
| 173 | + return; |
| 174 | + } |
| 175 | + if (setting->valueType() != LAB_VALUE_TYPE_STRING) { |
| 176 | + qDebug() << "setStr(): not valid string setting" << name << value; |
| 177 | + } |
| 178 | + if (value != std::get<QString>(setting->value())) { |
| 179 | + qDebug() << name << "has changed to" << value; |
| 180 | + xml_set(name.toStdString().c_str(), value.toStdString().c_str()); |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +/** |
| 185 | + * parse_bool() - Parse boolean value of string. |
| 186 | + * @string: String to interpret. This check is case-insensitive. |
| 187 | + * @default_value: Default value to use if string is not a recognised boolean. |
| 188 | + * Use -1 to avoid setting a default value. |
| 189 | + * |
| 190 | + * Return: 0 for false; 1 for true; -1 for non-boolean |
| 191 | + */ |
| 192 | +int parseBool(const char *str, int defaultValue) |
| 193 | +{ |
| 194 | + if (!str) |
| 195 | + goto error_not_a_boolean; |
| 196 | + else if (!strcasecmp(str, "yes")) |
| 197 | + return 1; |
| 198 | + else if (!strcasecmp(str, "true")) |
| 199 | + return 1; |
| 200 | + else if (!strcasecmp(str, "on")) |
| 201 | + return 1; |
| 202 | + else if (!strcmp(str, "1")) |
| 203 | + return 1; |
| 204 | + else if (!strcasecmp(str, "no")) |
| 205 | + return 0; |
| 206 | + else if (!strcasecmp(str, "false")) |
| 207 | + return 0; |
| 208 | + else if (!strcasecmp(str, "off")) |
| 209 | + return 0; |
| 210 | + else if (!strcmp(str, "0")) |
| 211 | + return 0; |
| 212 | +error_not_a_boolean: |
| 213 | + qDebug() << str << "is not a boolean value"; |
| 214 | + return defaultValue; |
| 215 | +} |
| 216 | + |
| 217 | +// TODO: make this more bool-ish |
| 218 | +void setBool(std::vector<std::shared_ptr<Setting>> &settings, QString name, QString value) |
| 219 | +{ |
| 220 | + std::shared_ptr<Setting> setting = retrieve(settings, name); |
| 221 | + if (setting == nullptr) { |
| 222 | + qDebug() << "warning: no settings with name" << name; |
| 223 | + return; |
| 224 | + } |
| 225 | + if (setting->valueType() != LAB_VALUE_TYPE_BOOL) { |
| 226 | + qDebug() << "setBool(): not valid bool setting" << name << value; |
| 227 | + } |
| 228 | + int boolValue = parseBool(value.toStdString().c_str(), -1); |
| 229 | + if (boolValue != std::get<int>(setting->value())) { |
| 230 | + qDebug() << name << "has changed to" << value; |
| 231 | + xml_set(name.toStdString().c_str(), value.toStdString().c_str()); |
| 232 | + } |
| 233 | +} |
| 234 | + |
150 | 235 | void MainDialog::onApply()
|
151 | 236 | {
|
152 | 237 | /* ~/.config/labwc/rc.xml */
|
153 |
| - xml_set_num("/labwc_config/theme/cornerradius", ui->cornerRadius->value()); |
154 |
| - xml_set("/labwc_config/theme/name", ui->openboxTheme->currentText().toLatin1().data()); |
155 |
| - xml_set("/labwc_config/theme/dropShadows", ui->dropShadows->currentText().toLatin1().data()); |
156 |
| - xml_set("/labwc_config/theme/icon", ui->iconTheme->currentText().toLatin1().data()); |
157 |
| - xml_set("/labwc_config/libinput/device/naturalscroll", |
| 238 | + setInt(m_settings, "/labwc_config/theme/cornerRadius", ui->cornerRadius->value()); |
| 239 | + setStr(m_settings, "/labwc_config/theme/name", |
| 240 | + ui->openboxTheme->currentText().toLatin1().data()); |
| 241 | + setBool(m_settings, "/labwc_config/theme/dropShadows", |
| 242 | + ui->dropShadows->currentText().toLatin1().data()); |
| 243 | + setStr(m_settings, "/labwc_config/theme/icon", ui->iconTheme->currentText().toLatin1().data()); |
| 244 | + setBool(m_settings, "/labwc_config/libinput/device/naturalScroll", |
158 | 245 | ui->naturalScroll->currentText().toLatin1().data());
|
159 |
| - xml_set("/labwc_config/placement/policy", ui->placementPolicy->currentText().toLatin1().data()); |
| 246 | + setStr(m_settings, "/labwc_config/placement/policy", |
| 247 | + ui->placementPolicy->currentText().toLatin1().data()); |
160 | 248 | xml_save();
|
161 | 249 |
|
162 | 250 | /* ~/.config/labwc/environment */
|
|
0 commit comments