diff --git a/src/libcalamares/locale/TimeZone.cpp b/src/libcalamares/locale/TimeZone.cpp index 420344ed7..75d1e8e38 100644 --- a/src/libcalamares/locale/TimeZone.cpp +++ b/src/libcalamares/locale/TimeZone.cpp @@ -18,8 +18,14 @@ #include "TimeZone.h" +#include +#include +#include + #include +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 diff --git a/src/libcalamares/locale/TimeZone.h b/src/libcalamares/locale/TimeZone.h index 8bba1c45d..c08aa6499 100644 --- a/src/libcalamares/locale/TimeZone.h +++ b/src/libcalamares/locale/TimeZone.h @@ -19,6 +19,9 @@ #ifndef LOCALE_TIMEZONE_H #define LOCALE_TIMEZONE_H +#include "DllMacro.h" + +#include #include #include @@ -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