source: calamares/trunk/fuentes/src/modules/locale/GeoIPJSON.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 17 months ago

sync with github

File size: 2.2 KB
Line 
1/* === This file is part of Calamares - <http://github.com/calamares> ===
2 *
3 *   Copyright 2014-2016, Teo Mrnjavac <teo@kde.org>
4 *   Copyright 2018, Adriaan de Groot <groot@kde.org>
5 *
6 *   Calamares is free software: you can redistribute it and/or modify
7 *   it under the terms of the GNU General Public License as published by
8 *   the Free Software Foundation, either version 3 of the License, or
9 *   (at your option) any later version.
10 *
11 *   Calamares is distributed in the hope that it will be useful,
12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *   GNU General Public License for more details.
15 *
16 *   You should have received a copy of the GNU General Public License
17 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "GeoIPJSON.h"
21
22#include "utils/CalamaresUtils.h"
23#include "utils/Logger.h"
24#include "utils/YamlUtils.h"
25
26#include <QByteArray>
27
28#include <yaml-cpp/yaml.h>
29
30GeoIPJSON::GeoIPJSON(const QString& attribute)
31    : GeoIP( attribute.isEmpty() ? QLatin1String( "time_zone" ) : attribute )
32{
33}
34
35static QString
36selectMap( const QVariantMap& m, const QStringList& l, int index)
37{
38    if ( index >= l.count() )
39        return QString();
40
41    QString attributeName = l[index];
42    if ( index == l.count() - 1 )
43        return CalamaresUtils::getString( m, attributeName );
44    else
45    {
46        bool success = false;  // bogus
47        if ( m.contains( attributeName ) )
48            return selectMap( CalamaresUtils::getSubMap( m, attributeName, success ), l, index+1 );
49        return QString();
50    }
51}
52
53GeoIP::RegionZonePair
54GeoIPJSON::processReply( const QByteArray& data )
55{
56    try
57    {
58        YAML::Node doc = YAML::Load( data );
59
60        QVariant var = CalamaresUtils::yamlToVariant( doc );
61        if ( !var.isNull() &&
62            var.isValid() &&
63            var.type() == QVariant::Map )
64        {
65            return splitTZString( selectMap( var.toMap(), m_element.split('.'), 0 ) );
66        }
67        else
68            cWarning() << "Invalid YAML data for GeoIPJSON";
69    }
70    catch ( YAML::Exception& e )
71    {
72        CalamaresUtils::explainYamlException( e, data, "GeoIP data");
73    }
74
75    return qMakePair( QString(), QString() );
76}
Note: See TracBrowser for help on using the repository browser.