2014-07-02 17:59:24 +02:00
|
|
|
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
|
|
|
*
|
2016-08-18 15:38:41 +02:00
|
|
|
* Copyright 2014-2016, Teo Mrnjavac <teo@kde.org>
|
2014-07-02 17:59:24 +02:00
|
|
|
*
|
|
|
|
* Calamares is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Calamares is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "LocaleViewStep.h"
|
|
|
|
|
|
|
|
#include "LocalePage.h"
|
|
|
|
#include "timezonewidget/localeglobal.h"
|
2014-09-03 18:01:39 +02:00
|
|
|
#include "widgets/WaitingWidget.h"
|
2014-11-25 17:43:12 +01:00
|
|
|
#include "JobQueue.h"
|
|
|
|
#include "GlobalStorage.h"
|
2014-07-02 17:59:24 +02:00
|
|
|
|
2014-07-03 13:13:38 +02:00
|
|
|
#include "utils/CalamaresUtilsGui.h"
|
2014-11-26 17:46:54 +01:00
|
|
|
#include "utils/Logger.h"
|
2016-08-18 15:38:41 +02:00
|
|
|
#include "utils/YamlUtils.h"
|
2014-07-03 13:13:38 +02:00
|
|
|
|
|
|
|
#include <QBoxLayout>
|
|
|
|
#include <QLabel>
|
2016-08-18 15:38:41 +02:00
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QNetworkReply>
|
2014-07-03 13:13:38 +02:00
|
|
|
#include <QtConcurrent/QtConcurrentRun>
|
|
|
|
|
2016-08-18 15:38:41 +02:00
|
|
|
#include <yaml-cpp/yaml.h>
|
|
|
|
|
2015-09-09 18:52:47 +02:00
|
|
|
|
|
|
|
CALAMARES_PLUGIN_FACTORY_DEFINITION( LocaleViewStepFactory, registerPlugin<LocaleViewStep>(); )
|
|
|
|
|
2014-07-02 17:59:24 +02:00
|
|
|
LocaleViewStep::LocaleViewStep( QObject* parent )
|
|
|
|
: Calamares::ViewStep( parent )
|
2014-07-03 13:13:38 +02:00
|
|
|
, m_widget( new QWidget() )
|
|
|
|
, m_actualWidget( new LocalePage() )
|
2014-07-03 13:19:32 +02:00
|
|
|
, m_nextEnabled( false )
|
2014-07-02 17:59:24 +02:00
|
|
|
{
|
2014-07-03 13:13:38 +02:00
|
|
|
QBoxLayout* mainLayout = new QHBoxLayout;
|
|
|
|
m_widget->setLayout( mainLayout );
|
|
|
|
CalamaresUtils::unmarginLayout( mainLayout );
|
|
|
|
|
2016-08-18 15:38:41 +02:00
|
|
|
m_waitingWidget = new WaitingWidget( tr( "Loading location data..." ) );
|
|
|
|
mainLayout->addWidget( m_waitingWidget );
|
2014-07-03 13:13:38 +02:00
|
|
|
|
|
|
|
connect( &m_initWatcher, &QFutureWatcher< void >::finished,
|
2016-08-18 15:38:41 +02:00
|
|
|
this, [=]
|
2014-07-03 13:13:38 +02:00
|
|
|
{
|
2016-08-18 15:38:41 +02:00
|
|
|
bool hasInternet = Calamares::JobQueue::instance()->globalStorage()
|
|
|
|
->value( "hasInternet" ).toBool();
|
|
|
|
if ( m_geoipUrl.isEmpty() || !hasInternet )
|
|
|
|
setUpPage();
|
|
|
|
else
|
|
|
|
fetchGeoIpTimezone();
|
2014-07-03 13:13:38 +02:00
|
|
|
});
|
|
|
|
|
2016-08-18 15:38:41 +02:00
|
|
|
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 );
|
|
|
|
} );
|
|
|
|
|
2014-07-03 13:13:38 +02:00
|
|
|
m_initWatcher.setFuture( initFuture );
|
|
|
|
|
2014-07-03 13:19:32 +02:00
|
|
|
emit nextStatusChanged( m_nextEnabled );
|
2014-07-02 17:59:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LocaleViewStep::~LocaleViewStep()
|
|
|
|
{
|
|
|
|
if ( m_widget && m_widget->parent() == nullptr )
|
|
|
|
m_widget->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-18 15:38:41 +02:00
|
|
|
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 )
|
|
|
|
{
|
2017-09-06 13:51:22 +02:00
|
|
|
QByteArray data = reply->readAll();
|
2016-08-18 15:38:41 +02:00
|
|
|
|
2017-09-06 13:51:22 +02:00
|
|
|
try
|
2016-08-18 15:38:41 +02:00
|
|
|
{
|
2017-09-06 13:51:22 +02:00
|
|
|
YAML::Node doc = YAML::Load( reply->readAll() );
|
|
|
|
|
|
|
|
QVariant var = CalamaresUtils::yamlToVariant( doc );
|
|
|
|
if ( !var.isNull() &&
|
|
|
|
var.isValid() &&
|
|
|
|
var.type() == QVariant::Map )
|
2016-08-18 15:38:41 +02:00
|
|
|
{
|
2017-09-06 13:51:22 +02:00
|
|
|
QVariantMap map = var.toMap();
|
|
|
|
if ( map.contains( "time_zone" ) &&
|
|
|
|
!map.value( "time_zone" ).toString().isEmpty() )
|
2016-08-18 15:38:41 +02:00
|
|
|
{
|
2017-09-06 13:51:22 +02:00
|
|
|
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 );
|
|
|
|
}
|
2016-08-18 15:38:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-06 13:51:22 +02:00
|
|
|
catch ( YAML::Exception& e )
|
|
|
|
{
|
|
|
|
CalamaresUtils::explainYamlException( e, data, "GeoIP data");
|
|
|
|
}
|
2016-08-18 15:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-02 17:59:24 +02:00
|
|
|
QString
|
|
|
|
LocaleViewStep::prettyName() const
|
|
|
|
{
|
|
|
|
return tr( "Location" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-08 18:25:54 +02:00
|
|
|
QString
|
|
|
|
LocaleViewStep::prettyStatus() const
|
|
|
|
{
|
|
|
|
return m_prettyStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-02 17:59:24 +02:00
|
|
|
QWidget*
|
|
|
|
LocaleViewStep::widget()
|
|
|
|
{
|
|
|
|
return m_widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
LocaleViewStep::next()
|
|
|
|
{
|
|
|
|
emit done();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
LocaleViewStep::back()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
LocaleViewStep::isNextEnabled() const
|
|
|
|
{
|
2014-07-03 13:19:32 +02:00
|
|
|
return m_nextEnabled;
|
2014-07-02 17:59:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-29 20:24:09 +01:00
|
|
|
bool
|
|
|
|
LocaleViewStep::isBackEnabled() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-02 17:59:24 +02:00
|
|
|
bool
|
|
|
|
LocaleViewStep::isAtBeginning() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
LocaleViewStep::isAtEnd() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2014-07-08 14:02:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
QList< Calamares::job_ptr >
|
|
|
|
LocaleViewStep::jobs() const
|
|
|
|
{
|
2014-08-01 16:29:19 +02:00
|
|
|
return m_jobs;
|
2014-07-08 14:02:21 +02:00
|
|
|
}
|
2014-07-08 18:25:54 +02:00
|
|
|
|
|
|
|
|
2014-11-26 18:52:44 +01:00
|
|
|
void
|
|
|
|
LocaleViewStep::onActivate()
|
|
|
|
{
|
|
|
|
m_actualWidget->onActivate();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-08 18:25:54 +02:00
|
|
|
void
|
|
|
|
LocaleViewStep::onLeave()
|
|
|
|
{
|
2014-08-01 16:29:19 +02:00
|
|
|
m_jobs.clear();
|
|
|
|
m_jobs.append( m_actualWidget->createJobs() );
|
|
|
|
|
2014-07-08 18:25:54 +02:00
|
|
|
m_prettyStatus = m_actualWidget->prettyStatus();
|
2014-11-26 18:47:46 +01:00
|
|
|
|
2016-08-10 11:47:24 +02:00
|
|
|
auto map = m_actualWidget->localesMap();
|
|
|
|
QVariantMap vm;
|
|
|
|
for ( auto it = map.constBegin(); it != map.constEnd(); ++it )
|
|
|
|
vm.insert( it.key(), it.value() );
|
|
|
|
|
|
|
|
Calamares::JobQueue::instance()->globalStorage()
|
|
|
|
->insert( "localeConf", vm );
|
2014-07-08 18:25:54 +02:00
|
|
|
}
|
2014-07-15 11:35:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
LocaleViewStep::setConfigurationMap( const QVariantMap& configurationMap )
|
|
|
|
{
|
|
|
|
if ( configurationMap.contains( "region" ) &&
|
|
|
|
configurationMap.value( "region" ).type() == QVariant::String &&
|
|
|
|
!configurationMap.value( "region" ).toString().isEmpty() &&
|
|
|
|
configurationMap.contains( "zone" ) &&
|
|
|
|
configurationMap.value( "zone" ).type() == QVariant::String &&
|
|
|
|
!configurationMap.value( "zone" ).toString().isEmpty() )
|
|
|
|
{
|
|
|
|
m_startingTimezone = qMakePair( configurationMap.value( "region" ).toString(),
|
|
|
|
configurationMap.value( "zone" ).toString() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_startingTimezone = qMakePair( QStringLiteral( "Europe" ),
|
|
|
|
QStringLiteral( "Berlin" ) );
|
|
|
|
}
|
2014-11-25 17:43:12 +01:00
|
|
|
|
|
|
|
if ( configurationMap.contains( "localeGenPath" ) &&
|
|
|
|
configurationMap.value( "localeGenPath" ).type() == QVariant::String &&
|
|
|
|
!configurationMap.value( "localeGenPath" ).toString().isEmpty() )
|
|
|
|
{
|
|
|
|
m_localeGenPath = configurationMap.value( "localeGenPath" ).toString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_localeGenPath = QStringLiteral( "/etc/locale.gen" );
|
|
|
|
}
|
2016-08-18 15:38:41 +02:00
|
|
|
|
|
|
|
// Optional
|
|
|
|
if ( configurationMap.contains( "geoipUrl" ) &&
|
|
|
|
configurationMap.value( "geoipUrl" ).type() == QVariant::String &&
|
|
|
|
!configurationMap.value( "geoipUrl" ).toString().isEmpty() )
|
|
|
|
{
|
|
|
|
m_geoipUrl = configurationMap.value( "geoipUrl" ).toString();
|
|
|
|
}
|
2014-07-15 11:35:05 +02:00
|
|
|
}
|