@ -20,8 +20,13 @@
# include "Branding.h"
# include "Settings.h"
# include "geoip/Handler.h"
# include "locale/Lookup.h"
# include "utils/Logger.h"
# include "utils/Retranslator.h"
# include "utils/Variant.h"
# include <QFutureWatcher>
Config : : Config ( QObject * parent )
: QObject ( parent )
@ -103,7 +108,7 @@ Config::initLanguages()
QLocale defaultLocale = QLocale ( QLocale : : system ( ) . name ( ) ) ;
cDebug ( ) < < " Matching locale " < < defaultLocale ;
int matchedLocaleIndex = m_languages - > find ( [ & ] ( const QLocale & x ) {
int matchedLocaleIndex = m_languages - > find ( [ & ] ( const QLocale & x ) {
return x . language ( ) = = defaultLocale . language ( ) & & x . country ( ) = = defaultLocale . country ( ) ;
} ) ;
@ -112,7 +117,7 @@ Config::initLanguages()
cDebug ( ) < < Logger : : SubEntry < < " Matching approximate locale " < < defaultLocale . language ( ) ;
matchedLocaleIndex
= m_languages - > find ( [ & ] ( const QLocale & x ) { return x . language ( ) = = defaultLocale . language ( ) ; } ) ;
= m_languages - > find ( [ & ] ( const QLocale & x ) { return x . language ( ) = = defaultLocale . language ( ) ; } ) ;
}
if ( matchedLocaleIndex < 0 )
@ -245,3 +250,136 @@ Config::warningMessage() const
{
return m_warningMessage ;
}
/** @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 ( ) ? ( Calamares : : Branding : : instance ( ) - > string ( e ) ) : QString ( ) ;
}
if ( v . type ( ) = = QVariant : : String )
{
return v . toString ( ) ;
}
return QString ( ) ;
}
static inline void
setLanguageIcon ( Config * c , const QVariantMap & configurationMap )
{
QString language = CalamaresUtils : : getString ( configurationMap , " languageIcon " ) ;
if ( ! language . isEmpty ( ) )
{
auto icon = Calamares : : Branding : : instance ( ) - > image ( language , QSize ( 48 , 48 ) ) ;
if ( ! icon . isNull ( ) )
{
c - > setLanguageIcon ( language ) ;
}
}
}
static inline void
logGeoIPHandler ( CalamaresUtils : : GeoIP : : Handler * handler )
{
if ( handler )
{
cDebug ( ) < < Logger : : SubEntry < < " Obtained from " < < handler - > url ( ) < < " ( "
< < static_cast < int > ( handler - > type ( ) ) < < handler - > selector ( ) < < ' ) ' ;
}
}
static void
setCountry ( Config * config , const QString & countryCode , CalamaresUtils : : GeoIP : : Handler * handler )
{
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 ) & & config )
{
config - > setCountryCode ( countryCode ) ;
}
}
}
static inline void
setGeoIP ( Config * c , const QVariantMap & configurationMap )
{
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 ( ) ;
QObject : : connect ( future , & FWString : : finished , [ config = c , f = future , h = handler ] ( ) {
QString countryResult = f - > future ( ) . result ( ) ;
cDebug ( ) < < " GeoIP result for welcome= " < < countryResult ;
: : setCountry ( config , countryResult , h ) ;
f - > deleteLater ( ) ;
delete h ;
} ) ;
future - > setFuture ( handler - > queryRaw ( ) ) ;
}
else
{
// Would not produce useful country code anyway.
delete handler ;
}
}
}
void
Config : : setConfigurationMap ( const QVariantMap & configurationMap )
{
using Calamares : : Branding ;
setSupportUrl ( jobOrBrandingSetting ( Branding : : SupportUrl , configurationMap , " showSupportUrl " ) ) ;
setKnownIssuesUrl ( jobOrBrandingSetting ( Branding : : KnownIssuesUrl , configurationMap , " showKnownIssuesUrl " ) ) ;
setReleaseNotesUrl ( jobOrBrandingSetting ( Branding : : ReleaseNotesUrl , configurationMap , " showReleaseNotesUrl " ) ) ;
setDonateUrl ( jobOrBrandingSetting ( Branding : : DonateUrl , configurationMap , " showDonateUrl " ) ) ;
: : setLanguageIcon ( this , configurationMap ) ;
: : setGeoIP ( this , configurationMap ) ;
}