[libcalamares] Apply coding style to geoip/

main
Adriaan de Groot 6 years ago
parent a2ab91474f
commit ec073ee188

@ -45,16 +45,22 @@ static QString
selectMap( const QVariantMap& m, const QStringList& l, int index ) selectMap( const QVariantMap& m, const QStringList& l, int index )
{ {
if ( index >= l.count() ) if ( index >= l.count() )
{
return QString(); return QString();
}
QString attributeName = l[ index ]; QString attributeName = l[ index ];
if ( index == l.count() - 1 ) if ( index == l.count() - 1 )
{
return CalamaresUtils::getString( m, attributeName ); return CalamaresUtils::getString( m, attributeName );
}
else else
{ {
bool success = false; // bogus bool success = false; // bogus
if ( m.contains( attributeName ) ) if ( m.contains( attributeName ) )
{
return selectMap( CalamaresUtils::getSubMap( m, attributeName, success ), l, index + 1 ); return selectMap( CalamaresUtils::getSubMap( m, attributeName, success ), l, index + 1 );
}
return QString(); return QString();
} }
} }
@ -67,15 +73,15 @@ GeoIPJSON::rawReply( const QByteArray& data )
YAML::Node doc = YAML::Load( data ); YAML::Node doc = YAML::Load( data );
QVariant var = CalamaresUtils::yamlToVariant( doc ); QVariant var = CalamaresUtils::yamlToVariant( doc );
if ( !var.isNull() && if ( !var.isNull() && var.isValid() && var.type() == QVariant::Map )
var.isValid() &&
var.type() == QVariant::Map )
{ {
return selectMap( var.toMap(), m_element.split( '.' ), 0 ); return selectMap( var.toMap(), m_element.split( '.' ), 0 );
} }
else else
{
cWarning() << "Invalid YAML data for GeoIPJSON"; cWarning() << "Invalid YAML data for GeoIPJSON";
} }
}
catch ( YAML::Exception& e ) catch ( YAML::Exception& e )
{ {
CalamaresUtils::explainYamlException( e, data, "GeoIP data" ); CalamaresUtils::explainYamlException( e, data, "GeoIP data" );
@ -90,5 +96,5 @@ GeoIPJSON::processReply( const QByteArray& data )
return splitTZString( rawReply( data ) ); return splitTZString( rawReply( data ) );
} }
} } // namespace GeoIP
} // namespace } // namespace CalamaresUtils

@ -48,6 +48,6 @@ public:
virtual QString rawReply( const QByteArray& ) override; virtual QString rawReply( const QByteArray& ) override;
}; };
} } // namespace GeoIP
} // namespace } // namespace CalamaresUtils
#endif #endif

@ -51,16 +51,20 @@ getElementTexts( const QByteArray& data, const QString& tag )
auto e = tzElements.at( it ).toElement(); auto e = tzElements.at( it ).toElement();
auto e_text = e.text(); auto e_text = e.text();
if ( !e_text.isEmpty() ) if ( !e_text.isEmpty() )
{
elements.append( e_text ); elements.append( e_text );
} }
} }
}
else else
{ {
cWarning() << "GeoIP XML data error:" << domError << "(line" << errorLine << errorColumn << ')'; cWarning() << "GeoIP XML data error:" << domError << "(line" << errorLine << errorColumn << ')';
} }
if ( elements.count() < 1 ) if ( elements.count() < 1 )
{
cWarning() << "GeopIP XML had no non-empty elements" << tag; cWarning() << "GeopIP XML had no non-empty elements" << tag;
}
return elements; return elements;
} }
@ -71,7 +75,9 @@ GeoIPXML::rawReply( const QByteArray& data )
{ {
for ( const auto& e : getElementTexts( data, m_element ) ) for ( const auto& e : getElementTexts( data, m_element ) )
if ( !e.isEmpty() ) if ( !e.isEmpty() )
{
return e; return e;
}
return QString(); return QString();
} }
@ -83,11 +89,13 @@ GeoIPXML::processReply( const QByteArray& data )
{ {
auto tz = splitTZString( e ); auto tz = splitTZString( e );
if ( !tz.first.isEmpty() ) if ( !tz.first.isEmpty() )
{
return tz; return tz;
} }
}
return RegionZonePair(); return RegionZonePair();
} }
} } // namespace GeoIP
} // namespace } // namespace CalamaresUtils

@ -48,6 +48,6 @@ public:
virtual QString rawReply( const QByteArray& ) override; virtual QString rawReply( const QByteArray& ) override;
}; };
} } // namespace GeoIP
} // namespace } // namespace CalamaresUtils
#endif #endif

@ -28,8 +28,8 @@
#include "utils/Variant.h" #include "utils/Variant.h"
#include <QEventLoop> #include <QEventLoop>
#include <QNetworkRequest>
#include <QNetworkReply> #include <QNetworkReply>
#include <QNetworkRequest>
#include <memory> #include <memory>
@ -85,9 +85,7 @@ Handler::Handler( const QString& implementation, const QString& url, const QStri
#endif #endif
} }
Handler::~Handler() Handler::~Handler() {}
{
}
static QByteArray static QByteArray
synchronous_get( const QString& urlstring ) synchronous_get( const QString& urlstring )
@ -129,7 +127,9 @@ do_query( Handler::Type type, const QString& url, const QString& selector )
{ {
const auto interface = create_interface( type, selector ); const auto interface = create_interface( type, selector );
if ( !interface ) if ( !interface )
{
return RegionZonePair(); return RegionZonePair();
}
return interface->processReply( synchronous_get( url ) ); return interface->processReply( synchronous_get( url ) );
} }
@ -139,7 +139,9 @@ do_raw_query( Handler::Type type, const QString& url, const QString& selector )
{ {
const auto interface = create_interface( type, selector ); const auto interface = create_interface( type, selector );
if ( !interface ) if ( !interface )
{
return QString(); return QString();
}
return interface->rawReply( synchronous_get( url ) ); return interface->rawReply( synchronous_get( url ) );
} }
@ -148,7 +150,9 @@ RegionZonePair
Handler::get() const Handler::get() const
{ {
if ( !isValid() ) if ( !isValid() )
{
return RegionZonePair(); return RegionZonePair();
}
return do_query( m_type, m_url, m_selector ); return do_query( m_type, m_url, m_selector );
} }
@ -160,17 +164,16 @@ Handler::query() const
QString url = m_url; QString url = m_url;
QString selector = m_selector; QString selector = m_selector;
return QtConcurrent::run( [=] return QtConcurrent::run( [=] { return do_query( type, url, selector ); } );
{
return do_query( type, url, selector );
} );
} }
QString QString
Handler::getRaw() const Handler::getRaw() const
{ {
if ( !isValid() ) if ( !isValid() )
{
return QString(); return QString();
}
return do_raw_query( m_type, m_url, m_selector ); return do_raw_query( m_type, m_url, m_selector );
} }
@ -182,11 +185,8 @@ Handler::queryRaw() const
QString url = m_url; QString url = m_url;
QString selector = m_selector; QString selector = m_selector;
return QtConcurrent::run( [=] return QtConcurrent::run( [=] { return do_raw_query( type, url, selector ); } );
{
return do_raw_query( type, url, selector );
} );
} }
} } // namespace GeoIP
} // namespace } // namespace CalamaresUtils

@ -21,9 +21,9 @@
#include "Interface.h" #include "Interface.h"
#include <QtConcurrent/QtConcurrentRun>
#include <QString> #include <QString>
#include <QVariantMap> #include <QVariantMap>
#include <QtConcurrent/QtConcurrentRun>
namespace CalamaresUtils namespace CalamaresUtils
{ {
@ -89,7 +89,6 @@ private:
const QString m_selector; const QString m_selector;
}; };
} } // namespace GeoIP
} // namespace } // namespace CalamaresUtils
#endif #endif

@ -30,9 +30,7 @@ Interface::Interface(const QString& e)
{ {
} }
Interface::~Interface() Interface::~Interface() {}
{
}
RegionZonePair RegionZonePair
splitTZString( const QString& tz ) splitTZString( const QString& tz )
@ -53,5 +51,5 @@ splitTZString( const QString& tz )
return RegionZonePair( QString(), QString() ); return RegionZonePair( QString(), QString() );
} }
} } // namespace GeoIP
} // namespace } // namespace CalamaresUtils

@ -42,11 +42,20 @@ class DLLEXPORT RegionZonePair : public QPair<QString, QString>
{ {
public: public:
/** @brief Construct from an existing pair. */ /** @brief Construct from an existing pair. */
explicit RegionZonePair( const QPair& p ) : QPair(p) { } explicit RegionZonePair( const QPair& p )
: QPair( p )
{
}
/** @brief Construct from two strings, like qMakePair(). */ /** @brief Construct from two strings, like qMakePair(). */
RegionZonePair( const QString& region, const QString& zone ) : QPair( region, zone ) { } RegionZonePair( const QString& region, const QString& zone )
: QPair( region, zone )
{
}
/** @brief An invalid zone pair (empty strings). */ /** @brief An invalid zone pair (empty strings). */
RegionZonePair() : QPair( QString(), QString() ) { } RegionZonePair()
: QPair( QString(), QString() )
{
}
bool isValid() const { return !first.isEmpty(); } bool isValid() const { return !first.isEmpty(); }
}; };
@ -60,8 +69,7 @@ public:
* pair of empty QStrings if it can't. (e.g. America/North Dakota/Beulah * pair of empty QStrings if it can't. (e.g. America/North Dakota/Beulah
* will return "America", "North_Dakota/Beulah"). * will return "America", "North_Dakota/Beulah").
*/ */
DLLEXPORT RegionZonePair DLLEXPORT RegionZonePair splitTZString( const QString& s );
splitTZString( const QString& s );
/** /**
* @brief Interface for GeoIP retrievers. * @brief Interface for GeoIP retrievers.
@ -95,6 +103,6 @@ protected:
QString m_element; // string for selecting from data QString m_element; // string for selecting from data
}; };
} } // namespace GeoIP
} // namespace } // namespace CalamaresUtils
#endif #endif

@ -30,7 +30,8 @@
using std::cerr; using std::cerr;
using namespace CalamaresUtils::GeoIP; using namespace CalamaresUtils::GeoIP;
int main(int argc, char** argv) int
main( int argc, char** argv )
{ {
if ( argc != 2 ) if ( argc != 2 )
{ {
@ -40,10 +41,14 @@ int main(int argc, char** argv)
Interface* handler = nullptr; Interface* handler = nullptr;
if ( QStringLiteral( "json" ) == argv[ 1 ] ) if ( QStringLiteral( "json" ) == argv[ 1 ] )
{
handler = new GeoIPJSON; handler = new GeoIPJSON;
}
#ifdef QT_XML_LIB #ifdef QT_XML_LIB
else if ( QStringLiteral( "xml" ) == argv[ 1 ] ) else if ( QStringLiteral( "xml" ) == argv[ 1 ] )
{
handler = new GeoIPXML; handler = new GeoIPXML;
}
#endif #endif
if ( !handler ) if ( !handler )
@ -53,7 +58,8 @@ int main(int argc, char** argv)
} }
QByteArray ba; QByteArray ba;
while( !std::cin.eof() ) { while ( !std::cin.eof() )
{
char arr[ 1024 ]; char arr[ 1024 ];
std::cin.read( arr, sizeof( arr ) ); std::cin.read( arr, sizeof( arr ) );
int s = static_cast< int >( std::cin.gcount() ); int s = static_cast< int >( std::cin.gcount() );
@ -67,7 +73,8 @@ int main(int argc, char** argv)
} }
else else
{ {
std::cout << "TimeZone Region=" << tz.first.toLatin1().constData() << "\nTimeZone Zone=" << tz.second.toLatin1().constData() << '\n'; std::cout << "TimeZone Region=" << tz.first.toLatin1().constData()
<< "\nTimeZone Zone=" << tz.second.toLatin1().constData() << '\n';
} }
return 0; return 0;

Loading…
Cancel
Save