2014-10-10 18:18:35 +02:00
|
|
|
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
|
|
|
*
|
|
|
|
* Copyright 2014, Teo Mrnjavac <teo@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 "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
|
|
|
|
|
|
|
QStringList Branding::s_stringEntryStrings =
|
|
|
|
{
|
|
|
|
"productName",
|
|
|
|
"version",
|
|
|
|
"shortVersion",
|
|
|
|
"versionedName",
|
2014-11-16 05:14:57 +01:00
|
|
|
"shortVersionedName",
|
|
|
|
"shortProductName"
|
2014-10-16 16:14:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
QStringList Branding::s_imageEntryStrings =
|
|
|
|
{
|
|
|
|
"productLogo",
|
|
|
|
"productIcon"
|
|
|
|
};
|
|
|
|
|
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." );
|
|
|
|
|
|
|
|
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() );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !doc[ "slideshow" ].IsSequence() )
|
|
|
|
bail( "Syntax error in slideshow sequence." );
|
|
|
|
|
|
|
|
doc[ "slideshow" ] >> m_slideshow;
|
|
|
|
for ( int i = 0; i < m_slideshow.count(); ++i )
|
2014-10-10 18:18:35 +02:00
|
|
|
{
|
2014-10-16 16:14:48 +02:00
|
|
|
QString pathString = m_slideshow[ i ];
|
|
|
|
QFileInfo imageFi( componentDir.absoluteFilePath( pathString ) );
|
|
|
|
if ( !imageFi.exists() )
|
|
|
|
bail( QString( "Slideshow file %1 does not exist." )
|
|
|
|
.arg( imageFi.absoluteFilePath() ) );
|
|
|
|
|
|
|
|
m_slideshow[ i ] = imageFi.absoluteFilePath();
|
2014-10-10 18:18:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch ( YAML::Exception& e )
|
|
|
|
{
|
|
|
|
cDebug() << "WARNING: YAML parser error " << e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
QString
|
|
|
|
Branding::string( Branding::StringEntry stringEntry ) const
|
|
|
|
{
|
|
|
|
return m_strings.value( s_stringEntryStrings.value( stringEntry ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-16 16:14:48 +02:00
|
|
|
QStringList
|
2014-10-16 16:57:52 +02:00
|
|
|
Branding::slideshowPaths() const
|
2014-10-16 16:14:48 +02:00
|
|
|
{
|
|
|
|
return m_slideshow;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-16 04:19:53 +01:00
|
|
|
void
|
|
|
|
Branding::setGlobals( GlobalStorage* globalStorage ) const
|
|
|
|
{
|
|
|
|
QVariantMap brandingMap;
|
2014-11-16 05:14:57 +01:00
|
|
|
foreach ( const QString& key, s_stringEntryStrings )
|
|
|
|
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
|
|
|
}
|