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

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

sync with github

File size: 7.5 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014-2016, Teo Mrnjavac <teo@kde.org>
4 *   Copyright 2018, Adriaan de Groot <groot.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 "LocaleViewStep.h"
21
22#include "GeoIP.h"
23#include "GeoIPJSON.h"
24#ifdef HAVE_XML
25#include "GeoIPXML.h"
26#endif
27#include "GlobalStorage.h"
28#include "JobQueue.h"
29#include "LocalePage.h"
30
31#include "timezonewidget/localeglobal.h"
32#include "widgets/WaitingWidget.h"
33
34#include "utils/CalamaresUtils.h"
35#include "utils/CalamaresUtilsGui.h"
36#include "utils/Logger.h"
37#include "utils/YamlUtils.h"
38
39#include <QBoxLayout>
40#include <QLabel>
41#include <QNetworkRequest>
42#include <QNetworkReply>
43#include <QtConcurrent/QtConcurrentRun>
44
45#include <yaml-cpp/yaml.h>
46
47
48CALAMARES_PLUGIN_FACTORY_DEFINITION( LocaleViewStepFactory, registerPlugin<LocaleViewStep>(); )
49
50LocaleViewStep::LocaleViewStep( QObject* parent )
51    : Calamares::ViewStep( parent )
52    , m_widget( new QWidget() )
53    , m_actualWidget( new LocalePage() )
54    , m_nextEnabled( false )
55{
56    QBoxLayout* mainLayout = new QHBoxLayout;
57    m_widget->setLayout( mainLayout );
58    CalamaresUtils::unmarginLayout( mainLayout );
59
60    m_waitingWidget = new WaitingWidget( tr( "Loading location data..." ) );
61    mainLayout->addWidget( m_waitingWidget );
62
63    connect( &m_initWatcher, &QFutureWatcher< void >::finished,
64             this, [=]
65    {
66        bool hasInternet = Calamares::JobQueue::instance()->globalStorage()
67                           ->value( "hasInternet" ).toBool();
68        if ( m_geoipUrl.isEmpty() || !hasInternet )
69            setUpPage();
70        else
71            fetchGeoIpTimezone();
72    });
73
74    QFuture< void > initFuture = QtConcurrent::run( [=]
75    {
76        LocaleGlobal::init();
77        if ( m_geoipUrl.isEmpty() )
78            return;
79
80        Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
81
82        // Max 10sec wait for RequirementsChecker to finish, assuming the welcome
83        // module is used.
84        // If welcome is not used, either "hasInternet" should be set by other means,
85        // or the GeoIP feature should be disabled.
86        for ( int i = 0; i < 10; ++i )
87            if ( !gs->contains( "hasInternet" ) )
88                QThread::sleep( 1 );
89    } );
90
91    m_initWatcher.setFuture( initFuture );
92
93    emit nextStatusChanged( m_nextEnabled );
94}
95
96
97LocaleViewStep::~LocaleViewStep()
98{
99    if ( m_widget && m_widget->parent() == nullptr )
100        m_widget->deleteLater();
101}
102
103
104void
105LocaleViewStep::setUpPage()
106{
107    m_actualWidget->init( m_startingTimezone.first,
108                          m_startingTimezone.second,
109                          m_localeGenPath );
110    m_widget->layout()->removeWidget( m_waitingWidget );
111    m_waitingWidget->deleteLater();
112    m_widget->layout()->addWidget( m_actualWidget );
113    m_nextEnabled = true;
114    emit nextStatusChanged( m_nextEnabled );
115}
116
117
118void
119LocaleViewStep::fetchGeoIpTimezone()
120{
121    QString actualUrl( m_geoipUrl );
122    GeoIP *handler = nullptr;
123
124    if ( m_geoipStyle.isEmpty() || m_geoipStyle == "legacy" )
125    {
126        actualUrl.append( "/json/" );
127        handler = new GeoIPJSON( m_geoipSelector );
128    }
129    else if ( m_geoipStyle == "json" )
130    {
131        handler = new GeoIPJSON( m_geoipSelector );
132    }
133#if defined(HAVE_XML)
134    else if ( m_geoipStyle == "xml" )
135    {
136        handler = new GeoIPXML( m_geoipSelector );
137    }
138#endif
139    else
140    {
141        cWarning() << "GeoIP Style" << m_geoipStyle << "is not recognized.";
142        setUpPage();
143        return;
144    }
145    cDebug() << "Fetching GeoIP data from" << actualUrl;
146
147    QNetworkAccessManager *manager = new QNetworkAccessManager( this );
148    connect( manager, &QNetworkAccessManager::finished,
149            [=]( QNetworkReply* reply )
150    {
151        if ( reply->error() == QNetworkReply::NoError )
152        {
153            auto tz = handler->processReply( reply->readAll() );
154            if ( !tz.first.isEmpty() )
155                m_startingTimezone = tz;
156            else
157                cWarning() << "GeoIP lookup at" << reply->url() << "failed.";
158        }
159        delete handler;
160        reply->deleteLater();
161        manager->deleteLater();
162        setUpPage();
163    } );
164
165    QNetworkRequest request;
166    request.setUrl( QUrl::fromUserInput( actualUrl ) );
167    request.setAttribute( QNetworkRequest::FollowRedirectsAttribute, true );
168    manager->get( request );
169}
170
171
172QString
173LocaleViewStep::prettyName() const
174{
175    return tr( "Location" );
176}
177
178
179QString
180LocaleViewStep::prettyStatus() const
181{
182    return m_prettyStatus;
183}
184
185
186QWidget*
187LocaleViewStep::widget()
188{
189    return m_widget;
190}
191
192
193void
194LocaleViewStep::next()
195{
196    emit done();
197}
198
199
200void
201LocaleViewStep::back()
202{}
203
204
205bool
206LocaleViewStep::isNextEnabled() const
207{
208    return m_nextEnabled;
209}
210
211
212bool
213LocaleViewStep::isBackEnabled() const
214{
215    return true;
216}
217
218
219bool
220LocaleViewStep::isAtBeginning() const
221{
222    return true;
223}
224
225
226bool
227LocaleViewStep::isAtEnd() const
228{
229    return true;
230}
231
232
233QList< Calamares::job_ptr >
234LocaleViewStep::jobs() const
235{
236    return m_jobs;
237}
238
239
240void
241LocaleViewStep::onActivate()
242{
243    m_actualWidget->onActivate();
244}
245
246
247void
248LocaleViewStep::onLeave()
249{
250    m_jobs.clear();
251    m_jobs.append( m_actualWidget->createJobs() );
252
253    m_prettyStatus = m_actualWidget->prettyStatus();
254
255    auto map = m_actualWidget->localesMap();
256    QVariantMap vm;
257    for ( auto it = map.constBegin(); it != map.constEnd(); ++it )
258        vm.insert( it.key(), it.value() );
259
260    Calamares::JobQueue::instance()->globalStorage()
261            ->insert( "localeConf", vm );
262}
263
264
265void
266LocaleViewStep::setConfigurationMap( const QVariantMap& configurationMap )
267{
268    if ( configurationMap.contains( "region" ) &&
269         configurationMap.value( "region" ).type() == QVariant::String &&
270         !configurationMap.value( "region" ).toString().isEmpty() &&
271         configurationMap.contains( "zone" ) &&
272         configurationMap.value( "zone" ).type() == QVariant::String &&
273         !configurationMap.value( "zone" ).toString().isEmpty() )
274    {
275        m_startingTimezone = qMakePair( configurationMap.value( "region" ).toString(),
276                                        configurationMap.value( "zone" ).toString() );
277    }
278    else
279    {
280        m_startingTimezone = qMakePair( QStringLiteral( "America" ),
281                                        QStringLiteral( "New_York" ) );
282    }
283
284    if ( configurationMap.contains( "localeGenPath" ) &&
285         configurationMap.value( "localeGenPath" ).type() == QVariant::String &&
286         !configurationMap.value( "localeGenPath" ).toString().isEmpty() )
287    {
288        m_localeGenPath = configurationMap.value( "localeGenPath" ).toString();
289    }
290    else
291    {
292        m_localeGenPath = QStringLiteral( "/etc/locale.gen" );
293    }
294
295    // Optional
296    m_geoipUrl = CalamaresUtils::getString( configurationMap, "geoipUrl" );
297    m_geoipStyle = CalamaresUtils::getString( configurationMap, "geoipStyle" );
298    m_geoipSelector = CalamaresUtils::getString( configurationMap, "geoipSelector" );
299}
Note: See TracBrowser for help on using the repository browser.