2019-04-18 14:27:37 +02:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
2020-05-30 16:15:03 +02:00
|
|
|
*
|
|
|
|
* SPDX-FileCopyrightText: 2019 Camilo Higuita <milo.h@aol.com>
|
|
|
|
* SPDX-FileCopyrightText: 2019-2020 Adriaan de Groot <groot@kde.org>
|
2019-04-18 14:27:37 +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/>.
|
2020-05-30 16:15:03 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
* License-Filename: LICENSE
|
|
|
|
*
|
2019-04-18 14:27:37 +02:00
|
|
|
*/
|
|
|
|
|
2019-05-10 17:46:20 +02:00
|
|
|
#include "LabelModel.h"
|
2019-04-18 14:27:37 +02:00
|
|
|
|
2019-05-10 20:44:54 +02:00
|
|
|
#include "Lookup.h"
|
|
|
|
|
2019-05-10 18:06:10 +02:00
|
|
|
#include "CalamaresVersion.h" // For the list of translations
|
|
|
|
|
2019-05-13 12:23:41 +02:00
|
|
|
namespace CalamaresUtils
|
|
|
|
{
|
|
|
|
namespace Locale
|
2019-05-10 17:46:20 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
LabelModel::LabelModel( const QStringList& locales, QObject* parent )
|
2019-04-19 11:38:43 +02:00
|
|
|
: QAbstractListModel( parent )
|
2019-08-20 11:26:26 +02:00
|
|
|
, m_localeIds( locales )
|
2019-04-18 23:16:02 +02:00
|
|
|
{
|
2019-04-19 09:31:16 +02:00
|
|
|
Q_ASSERT( locales.count() > 0 );
|
2019-04-18 23:16:02 +02:00
|
|
|
m_locales.reserve( locales.count() );
|
|
|
|
|
|
|
|
for ( const auto& l : locales )
|
2019-08-04 22:17:12 +02:00
|
|
|
{
|
2019-12-12 16:41:37 +01:00
|
|
|
m_locales.push_back( new Label( l, Label::LabelFormat::IfNeededWithCountry, this ) );
|
2019-08-04 22:17:12 +02:00
|
|
|
}
|
2019-04-18 23:16:02 +02:00
|
|
|
}
|
|
|
|
|
2019-08-04 22:17:12 +02:00
|
|
|
LabelModel::~LabelModel() {}
|
2019-04-18 23:16:02 +02:00
|
|
|
|
2019-04-18 14:27:37 +02:00
|
|
|
int
|
2019-05-10 17:46:20 +02:00
|
|
|
LabelModel::rowCount( const QModelIndex& ) const
|
2019-04-18 14:27:37 +02:00
|
|
|
{
|
2019-04-19 09:31:16 +02:00
|
|
|
return m_locales.count();
|
2019-04-18 14:27:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
2019-05-10 17:46:20 +02:00
|
|
|
LabelModel::data( const QModelIndex& index, int role ) const
|
2019-04-18 14:27:37 +02:00
|
|
|
{
|
2019-04-19 13:34:25 +02:00
|
|
|
if ( ( role != LabelRole ) && ( role != EnglishLabelRole ) )
|
2019-08-04 22:17:12 +02:00
|
|
|
{
|
2019-04-18 14:27:37 +02:00
|
|
|
return QVariant();
|
2019-08-04 22:17:12 +02:00
|
|
|
}
|
2019-04-18 14:27:37 +02:00
|
|
|
|
|
|
|
if ( !index.isValid() )
|
2019-08-04 22:17:12 +02:00
|
|
|
{
|
2019-04-18 14:27:37 +02:00
|
|
|
return QVariant();
|
2019-08-04 22:17:12 +02:00
|
|
|
}
|
2019-04-18 14:27:37 +02:00
|
|
|
|
|
|
|
const auto& locale = m_locales.at( index.row() );
|
2019-04-19 13:34:25 +02:00
|
|
|
switch ( role )
|
2019-04-18 14:27:37 +02:00
|
|
|
{
|
2019-04-19 13:34:25 +02:00
|
|
|
case LabelRole:
|
2019-12-12 16:41:37 +01:00
|
|
|
return locale->label();
|
2019-04-19 13:34:25 +02:00
|
|
|
case EnglishLabelRole:
|
2019-12-12 16:41:37 +01:00
|
|
|
return locale->englishLabel();
|
2019-04-19 09:39:19 +02:00
|
|
|
default:
|
|
|
|
return QVariant();
|
2019-04-18 14:27:37 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-19 09:31:16 +02:00
|
|
|
|
2020-02-12 13:40:59 +01:00
|
|
|
QHash< int, QByteArray >
|
|
|
|
LabelModel::roleNames() const
|
|
|
|
{
|
|
|
|
return { { LabelRole, "label" }, { EnglishLabelRole, "englishLabel" } };
|
|
|
|
}
|
|
|
|
|
2019-05-10 17:46:20 +02:00
|
|
|
const Label&
|
2019-05-10 17:50:01 +02:00
|
|
|
LabelModel::locale( int row ) const
|
2019-04-19 09:31:16 +02:00
|
|
|
{
|
|
|
|
if ( ( row < 0 ) || ( row >= m_locales.count() ) )
|
|
|
|
{
|
|
|
|
for ( const auto& l : m_locales )
|
2019-12-12 16:41:37 +01:00
|
|
|
if ( l->isEnglish() )
|
2019-08-04 22:17:12 +02:00
|
|
|
{
|
2019-12-12 16:41:37 +01:00
|
|
|
return *l;
|
2019-08-04 22:17:12 +02:00
|
|
|
}
|
2019-12-12 16:41:37 +01:00
|
|
|
return *m_locales[ 0 ];
|
2019-04-19 09:31:16 +02:00
|
|
|
}
|
2019-12-12 16:41:37 +01:00
|
|
|
return *m_locales[ row ];
|
2019-04-19 09:31:16 +02:00
|
|
|
}
|
|
|
|
|
2019-04-19 09:39:19 +02:00
|
|
|
int
|
2019-08-04 22:17:12 +02:00
|
|
|
LabelModel::find( std::function< bool( const Label& ) > predicate ) const
|
2019-04-19 09:31:16 +02:00
|
|
|
{
|
2019-08-04 22:17:12 +02:00
|
|
|
for ( int row = 0; row < m_locales.count(); ++row )
|
2019-04-19 09:31:16 +02:00
|
|
|
{
|
2019-12-12 16:41:37 +01:00
|
|
|
if ( predicate( *m_locales[ row ] ) )
|
2019-08-04 22:17:12 +02:00
|
|
|
{
|
2019-04-19 09:31:16 +02:00
|
|
|
return row;
|
2019-08-04 22:17:12 +02:00
|
|
|
}
|
2019-04-19 09:31:16 +02:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-04-19 09:39:19 +02:00
|
|
|
int
|
2019-08-04 22:17:12 +02:00
|
|
|
LabelModel::find( std::function< bool( const QLocale& ) > predicate ) const
|
2019-04-19 09:31:16 +02:00
|
|
|
{
|
2019-08-04 22:17:12 +02:00
|
|
|
return find( [&]( const Label& l ) { return predicate( l.locale() ); } );
|
2019-04-19 09:39:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-05-10 17:46:20 +02:00
|
|
|
LabelModel::find( const QLocale& locale ) const
|
2019-04-19 09:39:19 +02:00
|
|
|
{
|
2019-08-04 22:17:12 +02:00
|
|
|
return find( [&]( const Label& l ) { return locale == l.locale(); } );
|
2019-04-19 09:31:16 +02:00
|
|
|
}
|
2019-04-19 13:34:25 +02:00
|
|
|
|
2019-05-10 20:44:54 +02:00
|
|
|
int
|
|
|
|
LabelModel::find( const QString& countryCode ) const
|
|
|
|
{
|
|
|
|
if ( countryCode.length() != 2 )
|
2019-08-04 22:17:12 +02:00
|
|
|
{
|
2019-05-10 20:44:54 +02:00
|
|
|
return -1;
|
2019-08-04 22:17:12 +02:00
|
|
|
}
|
2019-05-10 20:44:54 +02:00
|
|
|
|
|
|
|
auto c_l = countryData( countryCode );
|
2019-08-04 22:17:12 +02:00
|
|
|
int r = find( [&]( const Label& l ) { return ( l.language() == c_l.second ) && ( l.country() == c_l.first ); } );
|
2019-05-10 20:44:54 +02:00
|
|
|
if ( r >= 0 )
|
2019-08-04 22:17:12 +02:00
|
|
|
{
|
2019-05-10 20:44:54 +02:00
|
|
|
return r;
|
2019-08-04 22:17:12 +02:00
|
|
|
}
|
|
|
|
return find( [&]( const Label& l ) { return l.language() == c_l.second; } );
|
2019-05-10 20:44:54 +02:00
|
|
|
}
|
|
|
|
|
2019-08-04 22:17:12 +02:00
|
|
|
LabelModel*
|
|
|
|
availableTranslations()
|
2019-05-10 18:06:10 +02:00
|
|
|
{
|
2019-08-20 11:26:26 +02:00
|
|
|
static LabelModel* model = new LabelModel( QStringLiteral( CALAMARES_TRANSLATION_LANGUAGES ).split( ';' ) );
|
2019-05-31 12:38:34 +02:00
|
|
|
return model;
|
2019-05-10 18:06:10 +02:00
|
|
|
}
|
|
|
|
|
2019-08-04 22:17:12 +02:00
|
|
|
} // namespace Locale
|
|
|
|
} // namespace CalamaresUtils
|