diff --git a/src/gpgui/gui/xml/xml_edit.cpp b/src/gpgui/gui/xml/xml_edit.cpp index 84c772e7..caee6c27 100644 --- a/src/gpgui/gui/xml/xml_edit.cpp +++ b/src/gpgui/gui/xml/xml_edit.cpp @@ -18,38 +18,3 @@ */ #include "xml_edit.h" - -#include - -// Look for attribute's node in the document by iterating -// through all nodes and looking for and attribute with same name -QDomNode find_attribute_node(const QDomDocument &doc, const QString &attribute_name) { - QStack elements_to_explore; - const QDomElement top_element = doc.documentElement(); - elements_to_explore.push(top_element); - - while (!elements_to_explore.isEmpty()) { - const QDomElement element = elements_to_explore.pop(); - - QDomNode child = element.firstChild(); - while (!child.isNull()) { - QDomElement child_as_element = child.toElement(); - const bool is_element = !child_as_element.isNull(); - - if (is_element) { - elements_to_explore.push(child_as_element); - } - - // Check node's attributes - const QDomNamedNodeMap attributes = child.attributes(); - const QDomNode attribute_node = attributes.namedItem(attribute_name); - if (!attribute_node.isNull()) { - return attribute_node; - } - - child = child.nextSibling(); - } - } - - return QDomNode(); -} diff --git a/src/gpgui/gui/xml/xml_edit.h b/src/gpgui/gui/xml/xml_edit.h index 5295f455..a251e598 100644 --- a/src/gpgui/gui/xml/xml_edit.h +++ b/src/gpgui/gui/xml/xml_edit.h @@ -46,6 +46,4 @@ bool changed() const;\ bool verify_input(QWidget *parent);\ bool apply(QDomDocument *doc); -QDomNode find_attribute_node(const QDomDocument &doc, const QString &attribute_name); - #endif /* XML_EDIT_H */ diff --git a/src/gpgui/gui/xml/xml_string_edit.cpp b/src/gpgui/gui/xml/xml_string_edit.cpp index 3c30da65..c10aada7 100644 --- a/src/gpgui/gui/xml/xml_string_edit.cpp +++ b/src/gpgui/gui/xml/xml_string_edit.cpp @@ -24,6 +24,19 @@ #include #include +QDomElement get_element_by_tag_name(const QDomDocument &doc, const QString &tag_name) { + const QDomNodeList parents = doc.elementsByTagName(tag_name); + const QDomNode parent_node = parents.at(0); + const QDomElement parent_element = parent_node.toElement(); + + // NOTE: should never happen? as long as xml files are validated on load at least + if (parent_element.isNull()) { + printf("get_element_by_tag_name() failed to find element \"%s\"\n", qPrintable(tag_name)); + } + + return parent_element; +} + XmlStringEdit::XmlStringEdit(const XmlAttribute &attribute_arg) : attribute(attribute_arg) { edit = new QLineEdit(); @@ -36,9 +49,10 @@ XmlStringEdit::XmlStringEdit(const XmlAttribute &attribute_arg) } void XmlStringEdit::load(const QDomDocument &doc) { - const QDomNode attribute_node = find_attribute_node(doc, attribute.name()); + const QDomElement parent_element = get_element_by_tag_name(doc, attribute.parent_name()); + const QString value = parent_element.attribute(attribute.name(), QString()); - original_value = attribute_node.nodeValue(); + original_value = value; edit->blockSignals(true); edit->setText(original_value); @@ -74,9 +88,10 @@ bool XmlStringEdit::apply(QDomDocument *doc) { return true; } - QDomNode attribute_node = find_attribute_node(*doc, attribute.name()); const QString new_value = edit->text(); - attribute_node.setNodeValue(new_value); + + QDomElement parent_element = get_element_by_tag_name(*doc, attribute.parent_name()); + parent_element.setAttribute(attribute.name(), new_value); printf("apply %s: [%s]=>[%s]\n", qPrintable(attribute.name()), qPrintable(original_value), qPrintable(new_value));