[libcalamares] Refactor timezone loading

- load from a TextStream. This is prep-work for alternate TZ data
  sources.
main
Adriaan de Groot 5 years ago
parent 0948963d86
commit 00626fd96c

@ -110,81 +110,87 @@ RegionData::tr() const
} }
static void static void
loadTZData( RegionVector& regions, ZoneVector& zones ) loadTZData( RegionVector& regions, ZoneVector& zones, QTextStream& in )
{ {
QFile file( TZ_DATA_FILE ); while ( !in.atEnd() )
if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
{ {
QTextStream in( &file ); QString line = in.readLine().trimmed().split( '#', SplitKeepEmptyParts ).first().trimmed();
while ( !in.atEnd() ) if ( line.isEmpty() )
{ {
QString line = in.readLine().trimmed().split( '#', SplitKeepEmptyParts ).first().trimmed(); continue;
if ( line.isEmpty() ) }
{
continue;
}
QStringList list = line.split( QRegExp( "[\t ]" ), SplitSkipEmptyParts ); QStringList list = line.split( QRegExp( "[\t ]" ), SplitSkipEmptyParts );
if ( list.size() < 3 ) if ( list.size() < 3 )
{ {
continue; continue;
} }
QStringList timezoneParts = list.at( 2 ).split( '/', SplitSkipEmptyParts ); QStringList timezoneParts = list.at( 2 ).split( '/', SplitSkipEmptyParts );
if ( timezoneParts.size() < 2 ) if ( timezoneParts.size() < 2 )
{ {
continue; continue;
} }
QString region = timezoneParts.first().trimmed(); QString region = timezoneParts.first().trimmed();
if ( region.isEmpty() ) if ( region.isEmpty() )
{ {
continue; continue;
} }
QString countryCode = list.at( 0 ).trimmed(); QString countryCode = list.at( 0 ).trimmed();
if ( countryCode.size() != 2 ) if ( countryCode.size() != 2 )
{ {
continue; continue;
} }
timezoneParts.removeFirst(); timezoneParts.removeFirst();
QString zone = timezoneParts.join( '/' ); QString zone = timezoneParts.join( '/' );
if ( zone.length() < 2 ) if ( zone.length() < 2 )
{ {
continue; continue;
} }
QString position = list.at( 1 ); QString position = list.at( 1 );
int cooSplitPos = position.indexOf( QRegExp( "[-+]" ), 1 ); int cooSplitPos = position.indexOf( QRegExp( "[-+]" ), 1 );
double latitude; double latitude;
double longitude; double longitude;
if ( cooSplitPos > 0 ) if ( cooSplitPos > 0 )
{ {
latitude = getRightGeoLocation( position.mid( 0, cooSplitPos ) ); latitude = getRightGeoLocation( position.mid( 0, cooSplitPos ) );
longitude = getRightGeoLocation( position.mid( cooSplitPos ) ); longitude = getRightGeoLocation( position.mid( cooSplitPos ) );
} }
else else
{ {
continue; continue;
} }
// Now we have region, zone, country, lat and longitude // Now we have region, zone, country, lat and longitude
const RegionData* existingRegion = nullptr; const RegionData* existingRegion = nullptr;
for ( const auto* p : regions ) for ( const auto* p : regions )
{ {
if ( p->key() == region ) if ( p->key() == region )
{
existingRegion = p;
break;
}
}
if ( !existingRegion )
{ {
regions.append( new RegionData( region ) ); existingRegion = p;
break;
} }
zones.append( new TimeZoneData( region, zone, countryCode, latitude, longitude ) );
} }
if ( !existingRegion )
{
regions.append( new RegionData( region ) );
}
zones.append( new TimeZoneData( region, zone, countryCode, latitude, longitude ) );
}
}
static void
loadTZData( RegionVector& regions, ZoneVector& zones )
{
QFile file( TZ_DATA_FILE );
if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
QTextStream in( &file );
loadTZData( regions, zones, in );
} }
} }

Loading…
Cancel
Save