Skip to content

Commit faa6216

Browse files
committed
Only update rc.xml on change from previous state
...which could be either the default or earlier rc.xml state
1 parent d239356 commit faa6216

File tree

5 files changed

+115
-10
lines changed

5 files changed

+115
-10
lines changed

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ int main(int argc, char *argv[])
8686
std::vector<std::shared_ptr<Setting>> settings;
8787
initSettings(settings);
8888

89-
MainDialog w;
89+
MainDialog w(settings);
9090
w.show();
9191

9292
// Make work the window icon also when the application is not (yet) installed

src/maindialog.cpp

Lines changed: 96 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
#include "evdev-lst-layouts.h"
1111
#include "layoutmodel.h"
1212
#include "maindialog.h"
13+
#include "settings.h"
1314
#include "./ui_maindialog.h"
1415

1516
extern "C" {
1617
#include "theme.h"
1718
#include "xml.h"
1819
}
1920

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)
2123
{
2224
ui->setupUi(this);
2325

@@ -81,7 +83,7 @@ void MainDialog::activate()
8183
theme_free_vector(&openbox_themes);
8284

8385
/* Corner Radius */
84-
ui->cornerRadius->setValue(xml_get_int("/labwc_config/theme/cornerradius"));
86+
ui->cornerRadius->setValue(xml_get_int("/labwc_config/theme/cornerRadius"));
8587

8688
/* Drop Shadows */
8789
ui->dropShadows->addItem("no");
@@ -147,16 +149,102 @@ void MainDialog::activate()
147149
}
148150
}
149151

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+
150235
void MainDialog::onApply()
151236
{
152237
/* ~/.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",
158245
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());
160248
xml_save();
161249

162250
/* ~/.config/labwc/environment */

src/maindialog.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define MAINDIALOG_H
33
#include <QDialog>
44
#include "layoutmodel.h"
5+
#include "settings.h"
56

67
QT_BEGIN_NAMESPACE
78
namespace Ui {
@@ -14,7 +15,7 @@ class MainDialog : public QDialog
1415
Q_OBJECT
1516

1617
public:
17-
MainDialog(QWidget *parent = nullptr);
18+
MainDialog(std::vector<std::shared_ptr<Setting>> &settings, QWidget *parent = nullptr);
1819
~MainDialog();
1920
void activate();
2021
QStringList findIconThemes();
@@ -25,6 +26,7 @@ private slots:
2526

2627
private:
2728
LayoutModel *m_model;
29+
std::vector<std::shared_ptr<Setting>> &m_settings;
2830

2931
void onApply();
3032

src/settings.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,13 @@ Setting::Setting(QString name, enum settingFileType fileType, enum settingValueT
7676
}
7777
}
7878
};
79+
80+
std::shared_ptr<Setting> retrieve(std::vector<std::shared_ptr<Setting>> &settings, QString name)
81+
{
82+
for (auto &setting : settings) {
83+
if (name == setting->name()) {
84+
return setting;
85+
}
86+
}
87+
return nullptr;
88+
}

src/settings.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <memory>
33
#include <vector>
44
#include <QString>
5+
#include "settings.h"
56

67
enum settingFileType {
78
LAB_FILE_TYPE_UNKNOWN = 0,
@@ -50,6 +51,10 @@ class Setting
5051
enum settingFileType fileType() const { return m_fileType; }
5152
enum settingValueOrigin valueOrigin() const { return m_valueOrigin; }
5253
enum settingValueType valueType() const { return m_valueType; }
54+
std::variant<int, QString> value() const { return m_value; }
5355
};
5456

5557
void initSettings(std::vector<std::shared_ptr<Setting>> &settings);
58+
59+
std::shared_ptr<Setting> retrieve(std::vector<std::shared_ptr<Setting>> &settings, QString name);
60+

0 commit comments

Comments
 (0)