[libcalamares] Add a model of timezone regions

main
Adriaan de Groot 5 years ago
parent ddc2f60768
commit b2c2255f6a

@ -18,8 +18,14 @@
#include "TimeZone.h"
#include <QFile>
#include <QStringList>
#include <QTextStream>
#include <cstring>
static const char TZ_DATA_FILE[] = "/usr/share/zoneinfo/zone.tab";
namespace CalamaresUtils
{
namespace Locale
@ -27,6 +33,8 @@ namespace Locale
CStringPair::CStringPair( CStringPair&& t )
: m_human( nullptr )
, m_key( nullptr )
{
// My pointers are initialized to nullptr
std::swap( m_human, t.m_human );
@ -94,5 +102,83 @@ TZZone::tr() const
return QObject::tr( m_human, "tz_names" );
}
TZRegionModel::TZRegionModel()
{
QFile file( TZ_DATA_FILE );
if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
return;
}
QStringList regions;
QTextStream in( &file );
while ( !in.atEnd() )
{
QString line = in.readLine().trimmed().split( '#', QString::KeepEmptyParts ).first().trimmed();
if ( line.isEmpty() )
{
continue;
}
QStringList list = line.split( QRegExp( "[\t ]" ), QString::SkipEmptyParts );
if ( list.size() < 3 )
{
continue;
}
QStringList timezoneParts = list.at( 2 ).split( '/', QString::SkipEmptyParts );
if ( timezoneParts.size() < 2 )
{
continue;
}
QString region = timezoneParts.first().trimmed();
;
if ( region.isEmpty() )
{
continue;
}
if ( !regions.contains( region ) )
{
regions.append( region );
}
}
m_regions.reserve( regions.length() );
for ( int i = 0; i < regions.length(); ++i )
{
m_regions.append( TZRegion( regions[ i ].toUtf8().data() ) );
}
}
TZRegionModel::~TZRegionModel() {}
int
TZRegionModel::rowCount( const QModelIndex& parent ) const
{
return m_regions.count();
}
QVariant
TZRegionModel::data( const QModelIndex& index, int role ) const
{
if ( role != LabelRole )
{
return QVariant();
}
if ( !index.isValid() )
{
return QVariant();
}
const TZRegion& region = m_regions.at( index.row() );
return region.tr();
}
} // namespace Locale
} // namespace CalamaresUtils

@ -19,6 +19,9 @@
#ifndef LOCALE_TIMEZONE_H
#define LOCALE_TIMEZONE_H
#include "DllMacro.h"
#include <QAbstractListModel>
#include <QObject>
#include <QString>
@ -72,6 +75,26 @@ public:
QString tr() const override;
};
class DLLEXPORT TZRegionModel : public QAbstractListModel
{
public:
enum
{
LabelRole = Qt::DisplayRole
};
/// @brief Create from the zone.tab file
TZRegionModel();
virtual ~TZRegionModel() override;
int rowCount( const QModelIndex& parent ) const override;
QVariant data( const QModelIndex& index, int role ) const override;
private:
QVector< TZRegion > m_regions;
};
} // namespace Locale
} // namespace CalamaresUtils

Loading…
Cancel
Save