Skip to content

Commit 7d67ed2

Browse files
committed
Gracefully handle bad rc.xml
...by showing error message and exiting. Fixes: #124
1 parent 0eeb210 commit 7d67ed2

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

src/maindialog.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <QDebug>
2+
#include <QMessageBox>
23
#include <string>
34
#include <unistd.h>
45
#include "environment.h"
@@ -144,7 +145,15 @@ void MainDialog::activate()
144145

145146
void MainDialog::initConfig(std::string &config_file)
146147
{
147-
xml_init(config_file.data());
148+
bool success = xml_init(config_file.data());
149+
150+
if (!success) {
151+
QMessageBox msgBox;
152+
msgBox.setText(tr("Error loading ") + QString(config_file.data()));
153+
msgBox.setInformativeText(tr("Run labwc-tweaks from a terminal to view error messages"));
154+
msgBox.exec();
155+
exit(EXIT_FAILURE);
156+
}
148157

149158
/* Ensure all relevant nodes exist before we start getting/setting */
150159
xpath_add_node("/labwc_config/theme/cornerRadius");

src/xml.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,13 @@ create_basic_rcxml(const char *filename)
142142
fclose(file);
143143
}
144144

145-
void
145+
bool
146146
xml_init(const char *filename)
147147
{
148148
LIBXML_TEST_VERSION
149149

150+
bool success = true;
151+
150152
if (access(filename, F_OK)) {
151153
create_basic_rcxml(filename);
152154
}
@@ -156,12 +158,15 @@ xml_init(const char *filename)
156158
ctx.doc = xmlReadFile(filename, NULL, XML_PARSE_NOBLANKS);
157159
if (!ctx.doc) {
158160
fprintf(stderr, "warn: xmlReadFile('%s')\n", filename);
161+
success = false;
159162
}
160163
ctx.xpath_ctx_ptr = xmlXPathNewContext(ctx.doc);
161164
if (!ctx.xpath_ctx_ptr) {
162165
fprintf(stderr, "warn: xmlXPathNewContext()\n");
163166
xmlFreeDoc(ctx.doc);
167+
success = false;
164168
}
169+
return success;
165170
}
166171

167172
void

src/xml.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#ifndef __XML_H
33
#define __XML_H
44

5-
void xml_init(const char *filename);
5+
bool xml_init(const char *filename);
66
void xml_save(void);
77
void xml_save_as(const char *filename);
88
void xml_finish(void);

0 commit comments

Comments
 (0)