calamares/src/modules/welcome/Config.cpp

248 lines
7.3 KiB
C++
Raw Normal View History

/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* Copyright 2019-2020, Adriaan de Groot <groot@kde.org>
*
* 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 "Config.h"
2020-03-25 12:00:41 +01:00
#include "Branding.h"
#include "Settings.h"
2020-03-25 11:41:39 +01:00
#include "utils/Logger.h"
#include "utils/Retranslator.h"
2020-03-25 11:41:39 +01:00
Config::Config( QObject* parent )
: QObject( parent )
, m_requirementsModel( new Calamares::RequirementsModel( this ) )
, m_languages( CalamaresUtils::Locale::availableTranslations() )
{
2020-04-02 21:53:02 +02:00
connect( m_requirementsModel,
&Calamares::RequirementsModel::satisfiedRequirementsChanged,
this,
&Config::setIsNextEnabled );
initLanguages();
CALAMARES_RETRANSLATE_SLOT( &Config::retranslate )
}
void
Config::retranslate()
{
2020-03-24 16:39:29 +01:00
m_genericWelcomeMessage = genericWelcomeMessage().arg( *Calamares::Branding::VersionedName );
emit genericWelcomeMessageChanged( m_genericWelcomeMessage );
if ( !m_requirementsModel->satisfiedRequirements() )
{
QString message;
const bool setup = Calamares::Settings::instance()->isSetupMode();
if ( !m_requirementsModel->satisfiedMandatory() )
{
message = setup ? tr( "This computer does not satisfy the minimum "
"requirements for setting up %1.<br/>"
"Setup cannot continue. "
"<a href=\"#details\">Details...</a>" )
: tr( "This computer does not satisfy the minimum "
"requirements for installing %1.<br/>"
"Installation cannot continue. "
"<a href=\"#details\">Details...</a>" );
}
else
{
message = setup ? tr( "This computer does not satisfy some of the "
"recommended requirements for setting up %1.<br/>"
"Setup can continue, but some features "
"might be disabled." )
: tr( "This computer does not satisfy some of the "
"recommended requirements for installing %1.<br/>"
"Installation can continue, but some features "
"might be disabled." );
}
m_warningMessage = message.arg( *Calamares::Branding::ShortVersionedName );
}
else
{
m_warningMessage = tr( "This program will ask you some questions and "
"set up %2 on your computer." )
.arg( *Calamares::Branding::ProductName );
}
emit warningMessageChanged( m_warningMessage );
}
CalamaresUtils::Locale::LabelModel*
Config::languagesModel() const
{
2020-03-25 11:41:39 +01:00
return m_languages;
}
QString
Config::languageIcon() const
{
2020-03-25 11:41:39 +01:00
return m_languageIcon;
}
void
Config::initLanguages()
{
// Find the best initial translation
QLocale defaultLocale = QLocale( QLocale::system().name() );
cDebug() << "Matching locale" << defaultLocale;
int matchedLocaleIndex = m_languages->find( [&]( const QLocale& x ) {
return x.language() == defaultLocale.language() && x.country() == defaultLocale.country();
} );
if ( matchedLocaleIndex < 0 )
{
cDebug() << Logger::SubEntry << "Matching approximate locale" << defaultLocale.language();
matchedLocaleIndex
2020-03-25 11:41:39 +01:00
= m_languages->find( [&]( const QLocale& x ) { return x.language() == defaultLocale.language(); } );
}
if ( matchedLocaleIndex < 0 )
{
QLocale en_us( QLocale::English, QLocale::UnitedStates );
cDebug() << Logger::SubEntry << "Matching English (US)";
matchedLocaleIndex = m_languages->find( en_us );
// Now, if it matched, because we didn't match the system locale, switch to the one found
if ( matchedLocaleIndex >= 0 )
{
QLocale::setDefault( m_languages->locale( matchedLocaleIndex ).locale() );
}
}
if ( matchedLocaleIndex >= 0 )
{
QString name = m_languages->locale( matchedLocaleIndex ).name();
cDebug() << Logger::SubEntry << "Matched with index" << matchedLocaleIndex << name;
2020-03-25 13:48:10 +01:00
CalamaresUtils::installTranslator( name, Calamares::Branding::instance()->translationsDirectory() );
2020-03-25 11:41:39 +01:00
setLocaleIndex( matchedLocaleIndex );
}
else
{
cWarning() << "No available translation matched" << defaultLocale;
}
}
void
Config::setCountryCode( const QString& countryCode )
{
2020-03-25 11:41:39 +01:00
m_countryCode = countryCode;
setLocaleIndex( CalamaresUtils::Locale::availableTranslations()->find( m_countryCode ) );
2020-03-25 11:41:39 +01:00
emit countryCodeChanged( m_countryCode );
}
void
2020-03-25 11:41:39 +01:00
Config::setLanguageIcon( const QString& languageIcon )
{
2020-03-25 11:41:39 +01:00
m_languageIcon = languageIcon;
}
void
Config::setLocaleIndex( int index )
{
2020-03-25 11:41:39 +01:00
if ( index == m_localeIndex || index > CalamaresUtils::Locale::availableTranslations()->rowCount( QModelIndex() )
|| index < 0 )
{
return;
2020-03-25 11:41:39 +01:00
}
m_localeIndex = index;
const auto& selectedLocale = m_languages->locale( m_localeIndex ).locale();
cDebug() << "Index" << index << "Selected locale" << selectedLocale;
QLocale::setDefault( selectedLocale );
2020-03-25 13:48:10 +01:00
CalamaresUtils::installTranslator( selectedLocale, Calamares::Branding::instance()->translationsDirectory() );
emit localeIndexChanged( m_localeIndex );
}
Calamares::RequirementsModel&
Config::requirementsModel() const
{
return *m_requirementsModel;
}
void
Config::setIsNextEnabled( bool isNextEnabled )
{
m_isNextEnabled = isNextEnabled;
emit isNextEnabledChanged( m_isNextEnabled );
}
2020-03-24 16:39:29 +01:00
void
2020-03-25 11:41:39 +01:00
Config::setDonateUrl( const QString& url )
{
m_donateUrl = url;
2020-03-24 15:26:24 +01:00
emit donateUrlChanged();
}
2020-03-24 16:39:29 +01:00
void
2020-03-25 11:41:39 +01:00
Config::setKnownIssuesUrl( const QString& url )
{
m_knownIssuesUrl = url;
2020-03-24 15:26:24 +01:00
emit knownIssuesUrlChanged();
}
2020-03-24 16:39:29 +01:00
void
2020-03-25 11:41:39 +01:00
Config::setReleaseNotesUrl( const QString& url )
{
2020-03-25 11:41:39 +01:00
m_releaseNotesUrl = url;
emit releaseNotesUrlChanged();
}
2020-03-24 16:39:29 +01:00
void
2020-03-25 11:41:39 +01:00
Config::setSupportUrl( const QString& url )
{
m_supportUrl = url;
2020-03-24 15:26:24 +01:00
emit supportUrlChanged();
}
2020-03-24 16:39:29 +01:00
QString
Config::genericWelcomeMessage() const
2020-03-24 16:39:29 +01:00
{
QString message;
if ( Calamares::Settings::instance()->isSetupMode() )
{
message = Calamares::Branding::instance()->welcomeStyleCalamares()
2020-03-25 11:41:39 +01:00
? tr( "<h1>Welcome to the Calamares setup program for %1.</h1>" )
: tr( "<h1>Welcome to %1 setup.</h1>" );
2020-03-24 16:39:29 +01:00
}
else
{
message = Calamares::Branding::instance()->welcomeStyleCalamares()
2020-03-25 11:41:39 +01:00
? tr( "<h1>Welcome to the Calamares installer for %1.</h1>" )
: tr( "<h1>Welcome to the %1 installer.</h1>" );
2020-03-24 16:39:29 +01:00
}
return message;
}
2020-04-02 21:53:02 +02:00
QString
Config::warningMessage() const
{
return m_warningMessage;
}