2019-12-14 12:48:09 +01:00
/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* Copyright 2014 - 2015 , Teo Mrnjavac < teo @ kde . org >
2020-01-09 15:11:36 +01:00
* Copyright 2018 , 2020 Adriaan de Groot < groot @ kde . org >
2019-12-14 12:48:09 +01: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 "WelcomeQmlViewStep.h"
# include "checker/GeneralRequirements.h"
# include "geoip/Handler.h"
# include "locale/LabelModel.h"
# include "locale/Lookup.h"
# include "utils/Logger.h"
# include "utils/Variant.h"
2020-03-10 23:56:09 +01:00
# include "utils/Dirs.h"
2019-12-14 12:48:09 +01:00
# include "Branding.h"
# include "modulesystem/ModuleManager.h"
2020-03-10 23:56:09 +01:00
# include <QQmlEngine>
# include "utils/Yaml.h"
2019-12-14 12:48:09 +01:00
# include <QFutureWatcher>
# include <QPixmap>
# include <QVariant>
CALAMARES_PLUGIN_FACTORY_DEFINITION ( WelcomeQmlViewStepFactory , registerPlugin < WelcomeQmlViewStep > ( ) ; )
WelcomeQmlViewStep : : WelcomeQmlViewStep ( QObject * parent )
2020-03-11 15:57:08 +01:00
: Calamares : : QmlViewStep ( parent )
2020-03-24 15:26:24 +01:00
, m_config ( new Config ( this ) ) // the qml singleton takes ownership and deletes it
2020-03-10 23:56:09 +01:00
// , m_nextEnabled( false )
2019-12-14 12:48:09 +01:00
, m_requirementsChecker ( new GeneralRequirements ( this ) )
2020-03-10 23:56:09 +01:00
2019-12-14 12:48:09 +01:00
{
2020-03-10 23:56:09 +01:00
// connect( m_config,
// &Config::isNextEnabledChanged,
// this,
// &WelcomeQmlViewStep::nextStatusChanged );
// emit nextStatusChanged(true);
2019-12-14 12:48:09 +01:00
}
QString
WelcomeQmlViewStep : : prettyName ( ) const
{
2020-03-10 23:56:09 +01:00
return tr ( " Welcome " ) ;
2019-12-14 12:48:09 +01:00
}
2020-03-10 23:56:09 +01:00
bool
WelcomeQmlViewStep : : isNextEnabled ( ) const
{
// TODO: should return true
// return m_config->property("isNextEnabled").toBool();
return true ;
}
bool
WelcomeQmlViewStep : : isBackEnabled ( ) const
{
// TODO: should return true (it's weird that you are not allowed to have welcome *after* anything
return false ;
}
bool
WelcomeQmlViewStep : : isAtBeginning ( ) const
{
// TODO: adjust to "pages" in the QML
return true ;
}
bool
WelcomeQmlViewStep : : isAtEnd ( ) const
{
// TODO: adjust to "pages" in the QML
return true ;
}
Calamares : : JobList
WelcomeQmlViewStep : : jobs ( ) const
2019-12-14 12:48:09 +01:00
{
2020-03-10 23:56:09 +01:00
return Calamares : : JobList ( ) ;
2019-12-14 12:48:09 +01:00
}
2020-03-24 15:26:24 +01:00
/** @brief Look up a URL for a button
*
* Looks up @ p key in @ p map ; if it is a * boolean * value , then
* assume an old - style configuration , and fetch the string from
* the branding settings @ p e . If it is a string , not a boolean ,
* use it as - is . If not found , or a weird type , returns empty .
*
* This allows switching the showKnownIssuesUrl and similar settings
* in welcome . conf from a boolean ( deferring to branding ) to an
* actual string for immediate use . Empty strings , as well as
* " false " as a setting , will hide the buttons as before .
*/
static QString
jobOrBrandingSetting ( Calamares : : Branding : : StringEntry e , const QVariantMap & map , const QString & key )
{
if ( ! map . contains ( key ) )
{
return QString ( ) ;
}
auto v = map . value ( key ) ;
if ( v . type ( ) = = QVariant : : Bool )
{
return v . toBool ( ) ? ( * e ) : QString ( ) ;
}
if ( v . type ( ) = = QVariant : : String )
{
return v . toString ( ) ;
}
return QString ( ) ;
}
2019-12-14 12:48:09 +01:00
void
WelcomeQmlViewStep : : setConfigurationMap ( const QVariantMap & configurationMap )
{
2020-03-10 23:56:09 +01:00
using Calamares : : Branding ;
2020-03-24 15:26:24 +01:00
m_config - > setSupportUrl ( jobOrBrandingSetting ( Branding : : SupportUrl , configurationMap , " showSupportUrl " ) ) ;
m_config - > setKnownIssuesUrl ( jobOrBrandingSetting ( Branding : : KnownIssuesUrl , configurationMap , " showKnownIssuesUrl " ) ) ;
m_config - > setReleaseNotesUrl ( jobOrBrandingSetting ( Branding : : ReleaseNotesUrl , configurationMap , " showReleaseNotesUrl " ) ) ;
m_config - > setDonateUrl ( CalamaresUtils : : getString ( configurationMap , " showDonateUrl " ) ) ;
2020-03-10 23:56:09 +01:00
2020-04-02 20:16:05 +02:00
// TODO: expand Config class and set the remaining fields // with the configurationMap all those properties can be accessed without having to declare a property, get and setter for each
2020-03-10 23:56:09 +01:00
// TODO: figure out how the requirements (held by ModuleManager) should be accessible
2020-04-02 20:16:05 +02:00
// to QML as a model. //will be model as a qvariantmap containing a alert level and the message string
2020-03-10 23:56:09 +01:00
if ( configurationMap . contains ( " requirements " )
& & configurationMap . value ( " requirements " ) . type ( ) = = QVariant : : Map )
{
m_requirementsChecker - > setConfigurationMap ( configurationMap . value ( " requirements " ) . toMap ( ) ) ;
m_config - > requirementsModel ( ) . setRequirementsList ( checkRequirements ( ) ) ;
}
else
cWarning ( ) < < " no valid requirements map found in welcome "
" module configuration. " ;
bool ok = false ;
QVariantMap geoip = CalamaresUtils : : getSubMap ( configurationMap , " geoip " , ok ) ;
if ( ok )
{
using FWString = QFutureWatcher < QString > ;
auto * handler = new CalamaresUtils : : GeoIP : : Handler ( CalamaresUtils : : getString ( geoip , " style " ) ,
CalamaresUtils : : getString ( geoip , " url " ) ,
CalamaresUtils : : getString ( geoip , " selector " ) ) ;
if ( handler - > type ( ) ! = CalamaresUtils : : GeoIP : : Handler : : Type : : None )
{
auto * future = new FWString ( ) ;
connect ( future , & FWString : : finished , [ view = this , f = future , h = handler ] ( ) {
QString countryResult = f - > future ( ) . result ( ) ;
cDebug ( ) < < " GeoIP result for welcome= " < < countryResult ;
view - > setCountry ( countryResult , h ) ;
f - > deleteLater ( ) ;
delete h ;
} ) ;
future - > setFuture ( handler - > queryRaw ( ) ) ;
}
else
{
// Would not produce useful country code anyway.
delete handler ;
}
}
QString language = CalamaresUtils : : getString ( configurationMap , " languageIcon " ) ;
if ( ! language . isEmpty ( ) )
{
auto icon = Calamares : : Branding : : instance ( ) - > image ( language , QSize ( 48 , 48 ) ) ;
if ( ! icon . isNull ( ) )
{
m_config - > setLanguageIcon ( language ) ;
}
}
Calamares : : QmlViewStep : : setConfigurationMap ( configurationMap ) ; // call parent implementation last
2020-03-18 11:02:35 +01:00
setContextProperty ( " Welcome " , m_config ) ;
2019-12-14 12:48:09 +01:00
}
Calamares : : RequirementsList
WelcomeQmlViewStep : : checkRequirements ( )
{
2020-03-10 23:56:09 +01:00
return m_requirementsChecker - > checkRequirements ( ) ;
}
2020-03-11 16:49:07 +01:00
QObject *
WelcomeQmlViewStep : : getConfig ( )
2020-03-10 23:56:09 +01:00
{
return m_config ;
2019-12-14 12:48:09 +01:00
}
static inline void
logGeoIPHandler ( CalamaresUtils : : GeoIP : : Handler * handler )
{
2020-03-10 23:56:09 +01:00
if ( handler )
{
cDebug ( ) < < Logger : : SubEntry < < " Obtained from " < < handler - > url ( ) < < " ( "
< < static_cast < int > ( handler - > type ( ) ) < < handler - > selector ( ) < < ' ) ' ;
}
2019-12-14 12:48:09 +01:00
}
void
WelcomeQmlViewStep : : setCountry ( const QString & countryCode , CalamaresUtils : : GeoIP : : Handler * handler )
{
2020-03-10 23:56:09 +01:00
if ( countryCode . length ( ) ! = 2 )
{
cDebug ( ) < < " Unusable country code " < < countryCode ;
logGeoIPHandler ( handler ) ;
return ;
}
auto c_l = CalamaresUtils : : Locale : : countryData ( countryCode ) ;
if ( c_l . first = = QLocale : : Country : : AnyCountry )
{
cDebug ( ) < < " Unusable country code " < < countryCode ;
logGeoIPHandler ( handler ) ;
return ;
}
else
{
int r = CalamaresUtils : : Locale : : availableTranslations ( ) - > find ( countryCode ) ;
if ( r < 0 )
{
cDebug ( ) < < " Unusable country code " < < countryCode < < " (no suitable translation) " ;
}
if ( ( r > = 0 ) )
{
// TODO: update Config to point to selected language
m_config - > setCountryCode ( countryCode ) ;
}
}
2020-02-12 17:51:10 +01:00
}