1 /* === This file is part of Calamares - <https://calamares.io> === 2 * 3 * SPDX-FileCopyrightText: 2018 Adriaan de Groot <groot@kde.org> 4 * SPDX-License-Identifier: GPL-3.0-or-later 5 * 6 * Calamares is Free Software: see the License-Identifier above. 7 * 8 */ 9 10 #include "GeoIPXML.h" 11 12 #include "utils/Logger.h" 13 14 #include <QtXml/QDomDocument> 15 16 namespace CalamaresUtils 17 { 18 namespace GeoIP 19 { 20 GeoIPXML(const QString & element)21GeoIPXML::GeoIPXML( const QString& element ) 22 : Interface( element.isEmpty() ? QStringLiteral( "TimeZone" ) : element ) 23 { 24 } 25 26 static QStringList getElementTexts(const QByteArray & data,const QString & tag)27getElementTexts( const QByteArray& data, const QString& tag ) 28 { 29 QStringList elements; 30 31 QString domError; 32 int errorLine, errorColumn; 33 34 QDomDocument doc; 35 if ( doc.setContent( data, false, &domError, &errorLine, &errorColumn ) ) 36 { 37 const auto tzElements = doc.elementsByTagName( tag ); 38 cDebug() << "GeoIP found" << tzElements.length() << "elements"; 39 for ( int it = 0; it < tzElements.length(); ++it ) 40 { 41 auto e = tzElements.at( it ).toElement(); 42 auto e_text = e.text(); 43 if ( !e_text.isEmpty() ) 44 { 45 elements.append( e_text ); 46 } 47 } 48 } 49 else 50 { 51 cWarning() << "GeoIP XML data error:" << domError << "(line" << errorLine << errorColumn << ')'; 52 } 53 54 if ( elements.count() < 1 ) 55 { 56 cWarning() << "GeopIP XML had no non-empty elements" << tag; 57 } 58 59 return elements; 60 } 61 62 63 QString rawReply(const QByteArray & data)64GeoIPXML::rawReply( const QByteArray& data ) 65 { 66 for ( const auto& e : getElementTexts( data, m_element ) ) 67 { 68 if ( !e.isEmpty() ) 69 { 70 return e; 71 } 72 } 73 74 return QString(); 75 } 76 77 GeoIP::RegionZonePair processReply(const QByteArray & data)78GeoIPXML::processReply( const QByteArray& data ) 79 { 80 for ( const auto& e : getElementTexts( data, m_element ) ) 81 { 82 auto tz = splitTZString( e ); 83 if ( !tz.first.isEmpty() ) 84 { 85 return tz; 86 } 87 } 88 89 return RegionZonePair(); 90 } 91 92 } // namespace GeoIP 93 } // namespace CalamaresUtils 94