[libcalamares] Deal with deprecations in XML QDomDocument

Introduce some Calamares helpers in the compat/ headers
space to help Xml-wrangling (i18n support tools, GeoIP)
avoid compiler warnings about deprecated API.

It's cleaner this way anyway, with a nice value returned
from setting a document's contents, rather than a bunch
of pointer arguments.
This commit is contained in:
Adriaan de Groot 2024-12-02 15:01:31 +01:00
parent 9106dd9337
commit 03c5e366ed
4 changed files with 57 additions and 9 deletions

View File

@ -19,6 +19,9 @@ find_package(${qtname} COMPONENTS Xml)
if(TARGET ${qtname}::Xml) if(TARGET ${qtname}::Xml)
add_executable(txload txload.cpp) add_executable(txload txload.cpp)
target_link_libraries(txload ${qtname}::Xml) 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() endif()
install_calamares_gettext_translations(python install_calamares_gettext_translations(python

View File

@ -18,6 +18,8 @@
* differences in translation are. * differences in translation are.
*/ */
#include "compat/Xml.h"
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug> #include <QDebug>
#include <QFile> #include <QFile>
@ -39,8 +41,6 @@ bool
load_file( const char* filename, QDomDocument& doc ) load_file( const char* filename, QDomDocument& doc )
{ {
QFile file( filename ); QFile file( filename );
QString err;
int err_line, err_column;
if ( !file.open( QIODevice::ReadOnly ) ) if ( !file.open( QIODevice::ReadOnly ) )
{ {
qDebug() << "Could not open" << filename; qDebug() << "Could not open" << filename;
@ -49,9 +49,10 @@ load_file( const char* filename, QDomDocument& doc )
QByteArray ba( file.read( 1024 * 1024 ) ); QByteArray ba( file.read( 1024 * 1024 ) );
qDebug() << "Read" << ba.length() << "bytes from" << filename; 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(); file.close();
return false; return false;
} }
@ -177,10 +178,12 @@ merge_into( QDomDocument& originDocument, QDomDocument& alternateDocument )
{ {
QDomElement e = n.toElement(); QDomElement e = n.toElement();
if ( e.tagName() == "context" ) if ( e.tagName() == "context" )
{
if ( !merge_into( originDocument, e ) ) if ( !merge_into( originDocument, e ) )
{ {
return false; return false;
} }
}
} }
n = n.nextSibling(); n = n.nextSibling();
} }

View File

@ -0,0 +1,42 @@
/* === This file is part of Calamares - <https://calamares.io> ===
*
* SPDX-FileCopyrightText: 2024 Adriaan de Groot <groot@kde.org>
* 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 <QDomDocument>
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

View File

@ -9,6 +9,7 @@
#include "GeoIPXML.h" #include "GeoIPXML.h"
#include "compat/Xml.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include <QtXml/QDomDocument> #include <QtXml/QDomDocument>
@ -28,11 +29,9 @@ getElementTexts( const QByteArray& data, const QString& tag )
{ {
QStringList elements; QStringList elements;
QString domError;
int errorLine, errorColumn;
QDomDocument doc; 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 ); const auto tzElements = doc.elementsByTagName( tag );
cDebug() << "GeoIP found" << tzElements.length() << "elements"; cDebug() << "GeoIP found" << tzElements.length() << "elements";
@ -48,7 +47,8 @@ getElementTexts( const QByteArray& data, const QString& tag )
} }
else 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 ) if ( elements.count() < 1 )