Add support for freegeoip.net in locale module.
This is disabled by default. To enable, provide a geoipUrl setting in locale.conf. Relies on the RequirementsChecker output, in the welcome module.
This commit is contained in:
parent
cd1268cb63
commit
3146d2093e
@ -1,6 +1,6 @@
|
|||||||
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
||||||
*
|
*
|
||||||
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
|
* Copyright 2014-2016, Teo Mrnjavac <teo@kde.org>
|
||||||
*
|
*
|
||||||
* Calamares is free software: you can redistribute it and/or modify
|
* Calamares is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@ -26,11 +26,16 @@
|
|||||||
|
|
||||||
#include "utils/CalamaresUtilsGui.h"
|
#include "utils/CalamaresUtilsGui.h"
|
||||||
#include "utils/Logger.h"
|
#include "utils/Logger.h"
|
||||||
|
#include "utils/YamlUtils.h"
|
||||||
|
|
||||||
#include <QBoxLayout>
|
#include <QBoxLayout>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
|
#include <QNetworkRequest>
|
||||||
|
#include <QNetworkReply>
|
||||||
#include <QtConcurrent/QtConcurrentRun>
|
#include <QtConcurrent/QtConcurrentRun>
|
||||||
|
|
||||||
|
#include <yaml-cpp/yaml.h>
|
||||||
|
|
||||||
|
|
||||||
CALAMARES_PLUGIN_FACTORY_DEFINITION( LocaleViewStepFactory, registerPlugin<LocaleViewStep>(); )
|
CALAMARES_PLUGIN_FACTORY_DEFINITION( LocaleViewStepFactory, registerPlugin<LocaleViewStep>(); )
|
||||||
|
|
||||||
@ -44,25 +49,37 @@ LocaleViewStep::LocaleViewStep( QObject* parent )
|
|||||||
m_widget->setLayout( mainLayout );
|
m_widget->setLayout( mainLayout );
|
||||||
CalamaresUtils::unmarginLayout( mainLayout );
|
CalamaresUtils::unmarginLayout( mainLayout );
|
||||||
|
|
||||||
WaitingWidget* waitingWidget =
|
m_waitingWidget = new WaitingWidget( tr( "Loading location data..." ) );
|
||||||
new WaitingWidget( tr( "Loading location data..." ) );
|
mainLayout->addWidget( m_waitingWidget );
|
||||||
|
|
||||||
mainLayout->addWidget( waitingWidget );
|
|
||||||
|
|
||||||
connect( &m_initWatcher, &QFutureWatcher< void >::finished,
|
connect( &m_initWatcher, &QFutureWatcher< void >::finished,
|
||||||
[=]
|
this, [=]
|
||||||
{
|
{
|
||||||
m_actualWidget->init( m_startingTimezone.first,
|
bool hasInternet = Calamares::JobQueue::instance()->globalStorage()
|
||||||
m_startingTimezone.second,
|
->value( "hasInternet" ).toBool();
|
||||||
m_localeGenPath );
|
if ( m_geoipUrl.isEmpty() || !hasInternet )
|
||||||
m_widget->layout()->removeWidget( waitingWidget );
|
setUpPage();
|
||||||
waitingWidget->deleteLater();
|
else
|
||||||
m_widget->layout()->addWidget( m_actualWidget );
|
fetchGeoIpTimezone();
|
||||||
m_nextEnabled = true;
|
|
||||||
emit nextStatusChanged( m_nextEnabled );
|
|
||||||
});
|
});
|
||||||
|
|
||||||
QFuture< void > initFuture = QtConcurrent::run( LocaleGlobal::init );
|
QFuture< void > initFuture = QtConcurrent::run( [=]
|
||||||
|
{
|
||||||
|
LocaleGlobal::init();
|
||||||
|
if ( m_geoipUrl.isEmpty() )
|
||||||
|
return;
|
||||||
|
|
||||||
|
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
|
||||||
|
|
||||||
|
// Max 10sec wait for RequirementsChecker to finish, assuming the welcome
|
||||||
|
// module is used.
|
||||||
|
// If welcome is not used, either "hasInternet" should be set by other means,
|
||||||
|
// or the GeoIP feature should be disabled.
|
||||||
|
for ( int i = 0; i < 10; ++i )
|
||||||
|
if ( !gs->contains( "hasInternet" ) )
|
||||||
|
QThread::sleep( 1 );
|
||||||
|
} );
|
||||||
|
|
||||||
m_initWatcher.setFuture( initFuture );
|
m_initWatcher.setFuture( initFuture );
|
||||||
|
|
||||||
emit nextStatusChanged( m_nextEnabled );
|
emit nextStatusChanged( m_nextEnabled );
|
||||||
@ -76,6 +93,67 @@ LocaleViewStep::~LocaleViewStep()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
LocaleViewStep::setUpPage()
|
||||||
|
{
|
||||||
|
m_actualWidget->init( m_startingTimezone.first,
|
||||||
|
m_startingTimezone.second,
|
||||||
|
m_localeGenPath );
|
||||||
|
m_widget->layout()->removeWidget( m_waitingWidget );
|
||||||
|
m_waitingWidget->deleteLater();
|
||||||
|
m_widget->layout()->addWidget( m_actualWidget );
|
||||||
|
m_nextEnabled = true;
|
||||||
|
emit nextStatusChanged( m_nextEnabled );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
LocaleViewStep::fetchGeoIpTimezone()
|
||||||
|
{
|
||||||
|
QNetworkAccessManager *manager = new QNetworkAccessManager( this );
|
||||||
|
connect( manager, &QNetworkAccessManager::finished,
|
||||||
|
[=]( QNetworkReply* reply )
|
||||||
|
{
|
||||||
|
if ( reply->error() == QNetworkReply::NoError )
|
||||||
|
{
|
||||||
|
YAML::Node doc = YAML::Load( reply->readAll() );
|
||||||
|
|
||||||
|
QVariant var = CalamaresUtils::yamlToVariant( doc );
|
||||||
|
if ( !var.isNull() &&
|
||||||
|
var.isValid() &&
|
||||||
|
var.type() == QVariant::Map )
|
||||||
|
{
|
||||||
|
QVariantMap map = var.toMap();
|
||||||
|
if ( map.contains( "time_zone" ) &&
|
||||||
|
!map.value( "time_zone" ).toString().isEmpty() )
|
||||||
|
{
|
||||||
|
QString timezoneString = map.value( "time_zone" ).toString();
|
||||||
|
QStringList timezone = timezoneString.split( '/', QString::SkipEmptyParts );
|
||||||
|
if ( timezone.size() >= 2 )
|
||||||
|
{
|
||||||
|
cDebug() << "GeoIP reporting" << timezoneString;
|
||||||
|
QString region = timezone.takeFirst();
|
||||||
|
QString zone = timezone.join( '/' );
|
||||||
|
m_startingTimezone = qMakePair( region, zone );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
reply->deleteLater();
|
||||||
|
manager->deleteLater();
|
||||||
|
setUpPage();
|
||||||
|
} );
|
||||||
|
|
||||||
|
QNetworkRequest request;
|
||||||
|
QString requestUrl = QString( "%1/json" )
|
||||||
|
.arg( QUrl::fromUserInput( m_geoipUrl ).toString() );
|
||||||
|
request.setUrl( QUrl( requestUrl ) );
|
||||||
|
request.setAttribute( QNetworkRequest::FollowRedirectsAttribute, true );
|
||||||
|
manager->get( request );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
QString
|
QString
|
||||||
LocaleViewStep::prettyName() const
|
LocaleViewStep::prettyName() const
|
||||||
{
|
{
|
||||||
@ -198,4 +276,12 @@ LocaleViewStep::setConfigurationMap( const QVariantMap& configurationMap )
|
|||||||
{
|
{
|
||||||
m_localeGenPath = QStringLiteral( "/etc/locale.gen" );
|
m_localeGenPath = QStringLiteral( "/etc/locale.gen" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Optional
|
||||||
|
if ( configurationMap.contains( "geoipUrl" ) &&
|
||||||
|
configurationMap.value( "geoipUrl" ).type() == QVariant::String &&
|
||||||
|
!configurationMap.value( "geoipUrl" ).toString().isEmpty() )
|
||||||
|
{
|
||||||
|
m_geoipUrl = configurationMap.value( "geoipUrl" ).toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
||||||
*
|
*
|
||||||
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
|
* Copyright 2014-2016, Teo Mrnjavac <teo@kde.org>
|
||||||
*
|
*
|
||||||
* Calamares is free software: you can redistribute it and/or modify
|
* Calamares is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@ -29,6 +29,7 @@
|
|||||||
#include <QFutureWatcher>
|
#include <QFutureWatcher>
|
||||||
|
|
||||||
class LocalePage;
|
class LocalePage;
|
||||||
|
class WaitingWidget;
|
||||||
|
|
||||||
class PLUGINDLLEXPORT LocaleViewStep : public Calamares::ViewStep
|
class PLUGINDLLEXPORT LocaleViewStep : public Calamares::ViewStep
|
||||||
{
|
{
|
||||||
@ -59,9 +60,14 @@ public:
|
|||||||
|
|
||||||
void setConfigurationMap( const QVariantMap& configurationMap ) override;
|
void setConfigurationMap( const QVariantMap& configurationMap ) override;
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void setUpPage();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void fetchGeoIpTimezone();
|
||||||
QWidget* m_widget;
|
QWidget* m_widget;
|
||||||
QFutureWatcher< void > m_initWatcher;
|
QFutureWatcher< void > m_initWatcher;
|
||||||
|
WaitingWidget* m_waitingWidget;
|
||||||
|
|
||||||
LocalePage* m_actualWidget;
|
LocalePage* m_actualWidget;
|
||||||
bool m_nextEnabled;
|
bool m_nextEnabled;
|
||||||
@ -69,6 +75,7 @@ private:
|
|||||||
|
|
||||||
QPair< QString, QString > m_startingTimezone;
|
QPair< QString, QString > m_startingTimezone;
|
||||||
QString m_localeGenPath;
|
QString m_localeGenPath;
|
||||||
|
QString m_geoipUrl;
|
||||||
|
|
||||||
QList< Calamares::job_ptr > m_jobs;
|
QList< Calamares::job_ptr > m_jobs;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user