diff --git a/lang/CMakeLists.txt b/lang/CMakeLists.txt index efd3182f3..3c1b2b9c4 100644 --- a/lang/CMakeLists.txt +++ b/lang/CMakeLists.txt @@ -19,6 +19,9 @@ find_package(${qtname} COMPONENTS Xml) if(TARGET ${qtname}::Xml) add_executable(txload txload.cpp) target_link_libraries(txload ${qtname}::Xml) + # Special-case, needs compatibility-header for XML-handling, + # but doesn't want all of libcalamares. + target_include_directories(txload PRIVATE ${CMAKE_SOURCE_DIR}/src/libcalamares) endif() install_calamares_gettext_translations(python diff --git a/lang/txload.cpp b/lang/txload.cpp index b7d14c8bf..267ec10a9 100644 --- a/lang/txload.cpp +++ b/lang/txload.cpp @@ -18,6 +18,8 @@ * differences in translation are. */ +#include "compat/Xml.h" + #include #include #include @@ -39,8 +41,6 @@ bool load_file( const char* filename, QDomDocument& doc ) { QFile file( filename ); - QString err; - int err_line, err_column; if ( !file.open( QIODevice::ReadOnly ) ) { qDebug() << "Could not open" << filename; @@ -49,9 +49,10 @@ load_file( const char* filename, QDomDocument& doc ) QByteArray ba( file.read( 1024 * 1024 ) ); qDebug() << "Read" << ba.length() << "bytes from" << filename; - if ( !doc.setContent( ba, &err, &err_line, &err_column ) ) + auto p = Calamares::setXmlContent( doc, ba ); + if ( !p.errorMessage.isEmpty() ) { - qDebug() << "Could not read" << filename << ':' << err_line << ':' << err_column << ' ' << err; + qDebug() << "Could not read" << filename << ':' << p.errorLine << ':' << p.errorColumn << ' ' << p.errorMessage; file.close(); return false; } @@ -177,10 +178,12 @@ merge_into( QDomDocument& originDocument, QDomDocument& alternateDocument ) { QDomElement e = n.toElement(); if ( e.tagName() == "context" ) + { if ( !merge_into( originDocument, e ) ) { return false; } + } } n = n.nextSibling(); } diff --git a/src/libcalamares/compat/Xml.h b/src/libcalamares/compat/Xml.h new file mode 100644 index 000000000..26be358a3 --- /dev/null +++ b/src/libcalamares/compat/Xml.h @@ -0,0 +1,42 @@ +/* === This file is part of Calamares - === + * + * SPDX-FileCopyrightText: 2024 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Calamares is Free Software: see the License-Identifier above. + * + * + */ +#ifndef CALAMARES_COMPAT_XML_H +#define CALAMARES_COMPAT_XML_H + +#include + +namespace Calamares +{ +#if QT_VERSION < QT_VERSION_CHECK( 6, 6, 0 ) +struct ParseResult +{ + QString errorMessage; + int errorLine = -1; + int errorColumn = -1; +} + +[[nodiscard]] inline ParseResult +setXmlContent( QDomDocument& doc, const QByteArray& ba ) +{ + ParseResult p; + const bool r = doc.setContent( ba, &p.errorMessage, &p.errorLine, &p.errorColumn ); + return r ? ParseResult {} : p; +} +#else +[[nodiscard]] inline QDomDocument::ParseResult +setXmlContent( QDomDocument& doc, const QByteArray& ba ) +{ + return doc.setContent( ba ); +} +#endif + +} // namespace Calamares + +#endif diff --git a/src/libcalamares/geoip/GeoIPXML.cpp b/src/libcalamares/geoip/GeoIPXML.cpp index 569d04772..f779abce1 100644 --- a/src/libcalamares/geoip/GeoIPXML.cpp +++ b/src/libcalamares/geoip/GeoIPXML.cpp @@ -9,6 +9,7 @@ #include "GeoIPXML.h" +#include "compat/Xml.h" #include "utils/Logger.h" #include @@ -28,11 +29,9 @@ getElementTexts( const QByteArray& data, const QString& tag ) { QStringList elements; - QString domError; - int errorLine, errorColumn; - QDomDocument doc; - if ( doc.setContent( data, false, &domError, &errorLine, &errorColumn ) ) + const auto p = Calamares::setXmlContent( doc, data ); + if ( p.errorMessage.isEmpty() ) { const auto tzElements = doc.elementsByTagName( tag ); cDebug() << "GeoIP found" << tzElements.length() << "elements"; @@ -48,7 +47,8 @@ getElementTexts( const QByteArray& data, const QString& tag ) } else { - cWarning() << "GeoIP XML data error:" << domError << "(line" << errorLine << errorColumn << ')'; + cWarning() << "GeoIP XML data error:" << p.errorMessage << "(line" << p.errorLine << ':' << p.errorColumn + << ')'; } if ( elements.count() < 1 )