Skip to content

Commit 53468f9

Browse files
committed
Only add nodes to rc.xml if values are different from default
1 parent effbd5c commit 53468f9

File tree

5 files changed

+61
-36
lines changed

5 files changed

+61
-36
lines changed

src/main.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,6 @@ void initConfig(std::string &configFile)
6565
msgBox.exec();
6666
exit(EXIT_FAILURE);
6767
}
68-
69-
/* Ensure all relevant nodes exist before we start getting/setting */
70-
xpath_add_node("/labwc_config/theme/cornerRadius");
71-
xpath_add_node("/labwc_config/theme/name");
72-
xpath_add_node("/labwc_config/theme/dropShadows");
73-
xpath_add_node("/labwc_config/theme/icon");
74-
xpath_add_node("/labwc_config/placement/policy");
75-
xpath_add_node("/labwc_config/libinput/device/naturalScroll");
76-
77-
xml_save();
7868
}
7969

8070
int main(int argc, char *argv[])

src/maindialog.cpp

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,45 +61,72 @@ void MainDialog::deleteSelectedLayout(void)
6161
m_model->deleteLayout(ui->layoutView->currentIndex().row());
6262
}
6363

64+
static QString getStr(std::vector<std::shared_ptr<Setting>> &settings, QString name)
65+
{
66+
std::shared_ptr<Setting> setting = retrieve(settings, name);
67+
if (setting == nullptr) {
68+
qDebug() << "warning: no settings with name" << name;
69+
return nullptr;
70+
}
71+
if (setting->valueType() != LAB_VALUE_TYPE_STRING) {
72+
qDebug() << "getStr(): not valid int setting" << name;
73+
}
74+
return std::get<QString>(setting->value());
75+
}
76+
77+
static int getInt(std::vector<std::shared_ptr<Setting>> &settings, QString name)
78+
{
79+
std::shared_ptr<Setting> setting = retrieve(settings, name);
80+
if (setting == nullptr) {
81+
qDebug() << "warning: no settings with name" << name;
82+
return -65535;
83+
}
84+
if (setting->valueType() != LAB_VALUE_TYPE_INT) {
85+
qDebug() << "getInt(): not valid int setting" << name;
86+
}
87+
return std::get<int>(setting->value());
88+
}
89+
90+
/* Return -1 for error, which works will with setCurrentIndex() */
91+
static int getBool(std::vector<std::shared_ptr<Setting>> &settings, QString name)
92+
{
93+
std::shared_ptr<Setting> setting = retrieve(settings, name);
94+
if (setting == nullptr) {
95+
qDebug() << "warning: no settings with name" << name;
96+
return -1;
97+
}
98+
if (setting->valueType() != LAB_VALUE_TYPE_BOOL) {
99+
qDebug() << "getBool(): not valid int setting" << name;
100+
}
101+
return std::get<int>(setting->value());
102+
}
103+
64104
void MainDialog::activate()
65105
{
66106
/* # APPEARANCE */
67107

68-
// TODO: Use retrieve() instead of xml_get(), etc.
69-
70108
/* Labwc Theme */
71109
QStringList labwcThemes = findLabwcThemes();
72110
ui->openboxTheme->addItems(labwcThemes);
73-
ui->openboxTheme->setCurrentIndex(labwcThemes.indexOf(xml_get("/labwc_config/theme/name")));
111+
ui->openboxTheme->setCurrentIndex(labwcThemes.indexOf(getStr(m_settings, "/labwc_config/theme/name")));
74112

75113
/* Corner Radius */
76-
ui->cornerRadius->setValue(xml_get_int("/labwc_config/theme/cornerRadius"));
114+
ui->cornerRadius->setValue(getInt(m_settings, "/labwc_config/theme/cornerRadius"));
77115

78116
/* Drop Shadows */
79117
ui->dropShadows->addItem("no");
80118
ui->dropShadows->addItem("yes");
81-
ui->dropShadows->setCurrentIndex(xml_get_bool_text("/labwc_config/theme/dropShadows"));
119+
ui->dropShadows->setCurrentIndex(getBool(m_settings, "/labwc_config/theme/dropShadows"));
82120

83121
/* Icon Theme */
84122
QStringList themes = findIconThemes(LAB_ICON_THEME_TYPE_ICON);
85123
ui->iconTheme->addItems(themes);
86-
ui->iconTheme->setCurrentIndex(themes.indexOf(xml_get("/labwc_config/theme/icon")));
124+
ui->iconTheme->setCurrentIndex(themes.indexOf(getStr(m_settings, "/labwc_config/theme/icon")));
87125

88126
/* # BEHAVIOUR */
89-
std::vector policies = { "", "Automatic", "Cascade", "Center", "Cursor" };
90-
int active = -1;
91-
const char *active_id = xml_get("/labwc_config/placement/policy");
92-
int i = 0;
93-
for (auto policy : policies) {
94-
if (active_id && !strcasecmp(policy, active_id)) {
95-
active = i;
96-
}
97-
ui->placementPolicy->addItem(policy);
98-
++i;
99-
}
100-
if (active != -1) {
101-
ui->placementPolicy->setCurrentIndex(active);
102-
}
127+
QStringList policies = { "", "Automatic", "Cascade", "Center", "Cursor" };
128+
ui->placementPolicy->addItems(policies);
129+
ui->placementPolicy->setCurrentIndex(policies.indexOf(getStr(m_settings, "/labwc_config/placement/policy")));
103130

104131
/* # MOUSE & TOUCHPAD */
105132

@@ -114,8 +141,7 @@ void MainDialog::activate()
114141
/* Natural Scroll */
115142
ui->naturalScroll->addItem("no");
116143
ui->naturalScroll->addItem("yes");
117-
ui->naturalScroll->setCurrentIndex(
118-
xml_get_bool_text("/labwc_config/libinput/device/naturalscroll"));
144+
ui->naturalScroll->setCurrentIndex(getBool(m_settings, "/labwc_config/libinput/device/naturalScroll"));
119145

120146
/* # LANGUAGE */
121147

@@ -138,6 +164,8 @@ void setInt(std::vector<std::shared_ptr<Setting>> &settings, QString name, int v
138164
}
139165
if (value != std::get<int>(setting->value())) {
140166
info("'{} has changed to '{}'", name.toStdString(), value);
167+
xpath_add_node(name.toStdString().c_str());
168+
//xml_save();
141169
xml_set_num(name.toStdString().c_str(), value);
142170
}
143171
}
@@ -154,6 +182,8 @@ void setStr(std::vector<std::shared_ptr<Setting>> &settings, QString name, QStri
154182
}
155183
if (value != std::get<QString>(setting->value())) {
156184
info("'{} has changed to '{}'", name.toStdString(), value.toStdString());
185+
xpath_add_node(name.toStdString().c_str());
186+
//xml_save();
157187
xml_set(name.toStdString().c_str(), value.toStdString().c_str());
158188
}
159189
}
@@ -205,6 +235,8 @@ void setBool(std::vector<std::shared_ptr<Setting>> &settings, QString name, QStr
205235
int boolValue = parseBool(value.toStdString().c_str(), -1);
206236
if (boolValue != std::get<int>(setting->value())) {
207237
info("'{} has changed to '{}'", name.toStdString(), value.toStdString());
238+
xpath_add_node(name.toStdString().c_str());
239+
//xml_save();
208240
xml_set(name.toStdString().c_str(), value.toStdString().c_str());
209241
}
210242
}

src/settings.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void initSettings(std::vector<std::shared_ptr<Setting>> &settings)
1515
settings.push_back(std::make_shared<Setting>("/labwc_config/theme/cornerRadius",
1616
LAB_FILE_TYPE_RCXML, LAB_VALUE_TYPE_INT, 8));
1717
settings.push_back(std::make_shared<Setting>("/labwc_config/theme/dropShadows",
18-
LAB_FILE_TYPE_RCXML, LAB_VALUE_TYPE_BOOL, 1));
18+
LAB_FILE_TYPE_RCXML, LAB_VALUE_TYPE_BOOL, 0));
1919
settings.push_back(std::make_shared<Setting>("/labwc_config/theme/icon", LAB_FILE_TYPE_RCXML,
2020
LAB_VALUE_TYPE_STRING, ""));
2121

@@ -47,7 +47,7 @@ Setting::Setting(QString name, enum settingFileType fileType, enum settingValueT
4747
switch (m_valueType) {
4848
case LAB_VALUE_TYPE_STRING: {
4949
QString value = QString(xml_get(m_name.toStdString().c_str()));
50-
if (value != std::get<QString>(m_value)) {
50+
if (!value.isNull() && (value != std::get<QString>(m_value))) {
5151
m_valueOrigin = LAB_VALUE_ORIGIN_USER_OVERRIDE;
5252
m_value = value;
5353
info("[user-override] {}: {}", m_name.toStdString(), value.toStdString());
@@ -56,7 +56,7 @@ Setting::Setting(QString name, enum settingFileType fileType, enum settingValueT
5656
}
5757
case LAB_VALUE_TYPE_INT: {
5858
int value = xml_get_int(m_name.toStdString().c_str());
59-
if (value != std::get<int>(m_value)) {
59+
if (value && (value != std::get<int>(m_value))) {
6060
m_valueOrigin = LAB_VALUE_ORIGIN_USER_OVERRIDE;
6161
m_value = value;
6262
info("[user-override] {}: {}", m_name.toStdString(), value);
@@ -65,7 +65,7 @@ Setting::Setting(QString name, enum settingFileType fileType, enum settingValueT
6565
}
6666
case LAB_VALUE_TYPE_BOOL: {
6767
int value = xml_get_bool_text(m_name.toStdString().c_str());
68-
if (value != std::get<int>(m_value)) {
68+
if (value != -1 && value != std::get<int>(m_value)) {
6969
m_valueOrigin = LAB_VALUE_ORIGIN_USER_OVERRIDE;
7070
m_value = value;
7171
info("[user-override] {}: {}", m_name.toStdString(), value);

src/settings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Setting
3838
Setting(QString name, enum settingFileType fileType, enum settingValueType valueType,
3939
std::variant<int, QString> defaultValue);
4040

41+
4142
private:
4243
QString m_name;
4344
enum settingFileType m_fileType;

src/xml.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ xml_set_num(const char *nodename, double value)
213213
const char *
214214
xml_get(const char *nodename)
215215
{
216+
ctx.value = NULL;
216217
ctx.nodename = nodename;
217218
ctx.mode = XML_MODE_GETTING;
218219
xml_tree_walk(xmlDocGetRootElement(ctx.doc));
@@ -222,6 +223,7 @@ xml_get(const char *nodename)
222223
int
223224
xml_get_int(const char *nodename)
224225
{
226+
ctx.value = NULL;
225227
ctx.nodename = nodename;
226228
ctx.mode = XML_MODE_GETTING;
227229
xml_tree_walk(xmlDocGetRootElement(ctx.doc));

0 commit comments

Comments
 (0)