2014-10-10 18:18:35 +02:00
|
|
|
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
|
|
|
*
|
2015-05-07 16:10:04 +02:00
|
|
|
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
|
2017-06-12 21:25:05 +02:00
|
|
|
* Copyright 2017, Adriaan de Groot <groot@kde.org>
|
2014-10-10 18:18:35 +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 "Branding.h"
|
|
|
|
|
2014-11-16 04:19:53 +01:00
|
|
|
#include "GlobalStorage.h"
|
2014-10-10 18:18:35 +02:00
|
|
|
#include "utils/CalamaresUtils.h"
|
|
|
|
#include "utils/Logger.h"
|
|
|
|
#include "utils/YamlUtils.h"
|
2014-10-16 16:57:52 +02:00
|
|
|
#include "utils/ImageRegistry.h"
|
2014-10-10 18:18:35 +02:00
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
2014-10-16 16:57:52 +02:00
|
|
|
#include <QPixmap>
|
2014-11-16 04:19:53 +01:00
|
|
|
#include <QVariantMap>
|
2014-10-10 18:18:35 +02:00
|
|
|
|
|
|
|
#include <yaml-cpp/yaml.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace Calamares
|
|
|
|
{
|
|
|
|
|
|
|
|
Branding* Branding::s_instance = nullptr;
|
|
|
|
|
|
|
|
Branding*
|
|
|
|
Branding::instance()
|
|
|
|
{
|
|
|
|
return s_instance;
|
|
|
|
}
|
|
|
|
|
2014-10-16 16:14:48 +02:00
|
|
|
|
2016-09-01 14:21:05 +02:00
|
|
|
const QStringList Branding::s_stringEntryStrings =
|
2014-10-16 16:14:48 +02:00
|
|
|
{
|
|
|
|
"productName",
|
|
|
|
"version",
|
|
|
|
"shortVersion",
|
|
|
|
"versionedName",
|
2014-11-16 05:14:57 +01:00
|
|
|
"shortVersionedName",
|
2014-11-19 16:50:15 +01:00
|
|
|
"shortProductName",
|
2015-04-01 18:03:17 +02:00
|
|
|
"bootloaderEntryName",
|
|
|
|
"productUrl",
|
2015-04-01 19:00:52 +02:00
|
|
|
"supportUrl",
|
|
|
|
"knownIssuesUrl",
|
|
|
|
"releaseNotesUrl"
|
2014-10-16 16:14:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-09-01 14:21:05 +02:00
|
|
|
const QStringList Branding::s_imageEntryStrings =
|
2014-10-16 16:14:48 +02:00
|
|
|
{
|
|
|
|
"productLogo",
|
2015-06-20 00:18:14 +02:00
|
|
|
"productIcon",
|
|
|
|
"productWelcome"
|
2014-10-16 16:14:48 +02:00
|
|
|
};
|
|
|
|
|
2016-09-01 14:21:05 +02:00
|
|
|
const QStringList Branding::s_styleEntryStrings =
|
2015-03-19 16:26:22 +01:00
|
|
|
{
|
|
|
|
"sidebarBackground",
|
|
|
|
"sidebarText",
|
2016-01-07 04:02:56 +01:00
|
|
|
"sidebarTextSelect",
|
|
|
|
"sidebarTextHighlight"
|
2015-03-19 16:26:22 +01:00
|
|
|
};
|
|
|
|
|
2014-10-10 18:18:35 +02:00
|
|
|
|
|
|
|
Branding::Branding( const QString& brandingFilePath,
|
|
|
|
QObject* parent )
|
|
|
|
: QObject( parent )
|
|
|
|
, m_descriptorPath( brandingFilePath )
|
|
|
|
{
|
|
|
|
cDebug() << "Using Calamares branding file at" << brandingFilePath;
|
|
|
|
QFile file( brandingFilePath );
|
|
|
|
if ( file.exists() && file.open( QFile::ReadOnly | QFile::Text ) )
|
|
|
|
{
|
|
|
|
QByteArray ba = file.readAll();
|
|
|
|
|
2014-10-16 16:14:48 +02:00
|
|
|
QFileInfo fi ( m_descriptorPath );
|
|
|
|
QDir componentDir = fi.absoluteDir();
|
|
|
|
if ( !componentDir.exists() )
|
|
|
|
bail( "Bad component directory path." );
|
|
|
|
|
2014-10-10 18:18:35 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
YAML::Node doc = YAML::Load( ba.constData() );
|
|
|
|
Q_ASSERT( doc.IsMap() );
|
|
|
|
|
2014-10-16 16:14:48 +02:00
|
|
|
m_componentName = QString::fromStdString( doc[ "componentName" ]
|
2014-10-10 18:18:35 +02:00
|
|
|
.as< std::string >() );
|
|
|
|
if ( m_componentName != QFileInfo( m_descriptorPath ).absoluteDir().dirName() )
|
2014-10-16 16:14:48 +02:00
|
|
|
bail( "The branding component name should match the name of the "
|
|
|
|
"component directory." );
|
|
|
|
|
|
|
|
if ( !doc[ "strings" ].IsMap() )
|
|
|
|
bail( "Syntax error in strings map." );
|
|
|
|
|
2017-06-12 21:25:05 +02:00
|
|
|
m_welcomeStyleCalamares = doc[ "welcomeStyleCalamares" ].as< bool >( false );
|
|
|
|
|
2014-10-16 16:14:48 +02:00
|
|
|
QVariantMap strings =
|
|
|
|
CalamaresUtils::yamlMapToVariant( doc[ "strings" ] ).toMap();
|
|
|
|
m_strings.clear();
|
|
|
|
for ( auto it = strings.constBegin(); it != strings.constEnd(); ++it )
|
|
|
|
m_strings.insert( it.key(), it.value().toString() );
|
|
|
|
|
|
|
|
if ( !doc[ "images" ].IsMap() )
|
|
|
|
bail( "Syntax error in images map." );
|
|
|
|
|
|
|
|
QVariantMap images =
|
|
|
|
CalamaresUtils::yamlMapToVariant( doc[ "images" ] ).toMap();
|
|
|
|
m_images.clear();
|
|
|
|
for ( auto it = images.constBegin(); it != images.constEnd(); ++it )
|
|
|
|
{
|
|
|
|
QString pathString = it.value().toString();
|
|
|
|
QFileInfo imageFi( componentDir.absoluteFilePath( pathString ) );
|
|
|
|
if ( !imageFi.exists() )
|
|
|
|
bail( QString( "Image file %1 does not exist." )
|
|
|
|
.arg( imageFi.absoluteFilePath() ) );
|
|
|
|
|
|
|
|
m_images.insert( it.key(), imageFi.absoluteFilePath() );
|
|
|
|
}
|
|
|
|
|
2015-01-23 14:02:40 +01:00
|
|
|
if ( doc[ "slideshow" ].IsSequence() )
|
2014-10-10 18:18:35 +02:00
|
|
|
{
|
2015-01-23 14:02:40 +01:00
|
|
|
QStringList slideShowPictures;
|
|
|
|
doc[ "slideshow" ] >> slideShowPictures;
|
|
|
|
for ( int i = 0; i < slideShowPictures.count(); ++i )
|
|
|
|
{
|
|
|
|
QString pathString = slideShowPictures[ i ];
|
|
|
|
QFileInfo imageFi( componentDir.absoluteFilePath( pathString ) );
|
|
|
|
if ( !imageFi.exists() )
|
|
|
|
bail( QString( "Slideshow file %1 does not exist." )
|
|
|
|
.arg( imageFi.absoluteFilePath() ) );
|
|
|
|
|
|
|
|
slideShowPictures[ i ] = imageFi.absoluteFilePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
//FIXME: implement a GenericSlideShow.qml that uses these slideShowPictures
|
2014-10-10 18:18:35 +02:00
|
|
|
}
|
2015-01-23 14:02:40 +01:00
|
|
|
else if ( doc[ "slideshow" ].IsScalar() )
|
|
|
|
{
|
|
|
|
QString slideshowPath = QString::fromStdString( doc[ "slideshow" ]
|
|
|
|
.as< std::string >() );
|
|
|
|
QFileInfo slideshowFi( componentDir.absoluteFilePath( slideshowPath ) );
|
|
|
|
if ( !slideshowFi.exists() ||
|
|
|
|
!slideshowFi.fileName().toLower().endsWith( ".qml" ) )
|
|
|
|
bail( QString( "Slideshow file %1 does not exist or is not a valid QML file." )
|
|
|
|
.arg( slideshowFi.absoluteFilePath() ) );
|
|
|
|
m_slideshowPath = slideshowFi.absoluteFilePath();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
bail( "Syntax error in slideshow sequence." );
|
2015-03-19 16:26:22 +01:00
|
|
|
|
|
|
|
if ( !doc[ "style" ].IsMap() )
|
|
|
|
bail( "Syntax error in style map." );
|
|
|
|
|
|
|
|
QVariantMap style =
|
|
|
|
CalamaresUtils::yamlMapToVariant( doc[ "style" ] ).toMap();
|
|
|
|
m_style.clear();
|
|
|
|
for ( auto it = style.constBegin(); it != style.constEnd(); ++it )
|
|
|
|
m_style.insert( it.key(), it.value().toString() );
|
2015-01-23 14:02:40 +01:00
|
|
|
|
2014-10-10 18:18:35 +02:00
|
|
|
}
|
|
|
|
catch ( YAML::Exception& e )
|
|
|
|
{
|
|
|
|
cDebug() << "WARNING: YAML parser error " << e.what();
|
|
|
|
}
|
2015-05-07 16:10:04 +02:00
|
|
|
|
|
|
|
QDir translationsDir( componentDir.filePath( "lang" ) );
|
|
|
|
if ( !translationsDir.exists() )
|
|
|
|
cDebug() << "WARNING: the selected branding component does not ship translations.";
|
|
|
|
m_translationsPathPrefix = translationsDir.absolutePath();
|
|
|
|
m_translationsPathPrefix.append( QString( "%1calamares-%2" )
|
|
|
|
.arg( QDir::separator() )
|
|
|
|
.arg( m_componentName ) );
|
2014-10-10 18:18:35 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cDebug() << "WARNING: Cannot read " << file.fileName();
|
|
|
|
}
|
|
|
|
|
|
|
|
s_instance = this;
|
|
|
|
cDebug() << "Loaded branding component" << m_componentName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString
|
|
|
|
Branding::descriptorPath() const
|
|
|
|
{
|
|
|
|
return m_descriptorPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString
|
|
|
|
Branding::componentName() const
|
|
|
|
{
|
|
|
|
return m_componentName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString
|
|
|
|
Branding::componentDirectory() const
|
|
|
|
{
|
|
|
|
QFileInfo fi ( m_descriptorPath );
|
|
|
|
return fi.absoluteDir().absolutePath();
|
|
|
|
}
|
|
|
|
|
2014-10-16 16:14:48 +02:00
|
|
|
|
2015-05-07 16:10:04 +02:00
|
|
|
QString
|
|
|
|
Branding::translationsPathPrefix() const
|
|
|
|
{
|
|
|
|
return m_translationsPathPrefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-16 16:14:48 +02:00
|
|
|
QString
|
|
|
|
Branding::string( Branding::StringEntry stringEntry ) const
|
|
|
|
{
|
|
|
|
return m_strings.value( s_stringEntryStrings.value( stringEntry ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-19 17:58:18 +01:00
|
|
|
QString
|
|
|
|
Branding::styleString( Branding::StyleEntry styleEntry ) const
|
|
|
|
{
|
|
|
|
return m_style.value( s_styleEntryStrings.value( styleEntry ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-16 16:14:48 +02:00
|
|
|
QString
|
2014-10-16 16:57:52 +02:00
|
|
|
Branding::imagePath( Branding::ImageEntry imageEntry ) const
|
2014-10-16 16:14:48 +02:00
|
|
|
{
|
|
|
|
return m_images.value( s_imageEntryStrings.value( imageEntry ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-16 16:57:52 +02:00
|
|
|
QPixmap
|
|
|
|
Branding::image( Branding::ImageEntry imageEntry, const QSize& size ) const
|
|
|
|
{
|
|
|
|
QPixmap pixmap =
|
|
|
|
ImageRegistry::instance()->pixmap( imagePath( imageEntry ), size );
|
|
|
|
|
|
|
|
if ( pixmap.isNull() )
|
|
|
|
{
|
|
|
|
Q_ASSERT( false );
|
|
|
|
return QPixmap();
|
|
|
|
}
|
|
|
|
return pixmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-23 14:02:40 +01:00
|
|
|
QString
|
|
|
|
Branding::slideshowPath() const
|
2014-10-16 16:14:48 +02:00
|
|
|
{
|
2015-01-23 14:02:40 +01:00
|
|
|
return m_slideshowPath;
|
2014-10-16 16:14:48 +02:00
|
|
|
}
|
|
|
|
|
2017-06-12 21:25:05 +02:00
|
|
|
bool
|
|
|
|
Branding::welcomeStyleCalamares() const
|
|
|
|
{
|
|
|
|
return m_welcomeStyleCalamares;
|
|
|
|
}
|
2014-10-16 16:14:48 +02:00
|
|
|
|
2014-11-16 04:19:53 +01:00
|
|
|
void
|
|
|
|
Branding::setGlobals( GlobalStorage* globalStorage ) const
|
|
|
|
{
|
|
|
|
QVariantMap brandingMap;
|
2016-09-01 14:21:05 +02:00
|
|
|
for ( const QString& key : s_stringEntryStrings )
|
2014-11-16 05:14:57 +01:00
|
|
|
brandingMap.insert( key, m_strings.value( key ) );
|
2014-11-16 04:19:53 +01:00
|
|
|
globalStorage->insert( "branding", brandingMap );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-16 16:14:48 +02:00
|
|
|
void
|
|
|
|
Branding::bail( const QString& message )
|
|
|
|
{
|
|
|
|
cLog() << "FATAL ERROR in"
|
|
|
|
<< m_descriptorPath
|
|
|
|
<< "\n" + message;
|
|
|
|
::exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
2014-10-10 18:18:35 +02:00
|
|
|
}
|