[libcalamares] Refactor timezone loading
- load from a TextStream. This is prep-work for alternate TZ data sources.
This commit is contained in:
parent
0948963d86
commit
00626fd96c
@ -109,6 +109,80 @@ RegionData::tr() const
|
||||
return QObject::tr( m_human, "tz_regions" );
|
||||
}
|
||||
|
||||
static void
|
||||
loadTZData( RegionVector& regions, ZoneVector& zones, QTextStream& in )
|
||||
{
|
||||
while ( !in.atEnd() )
|
||||
{
|
||||
QString line = in.readLine().trimmed().split( '#', SplitKeepEmptyParts ).first().trimmed();
|
||||
if ( line.isEmpty() )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
QStringList list = line.split( QRegExp( "[\t ]" ), SplitSkipEmptyParts );
|
||||
if ( list.size() < 3 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
QStringList timezoneParts = list.at( 2 ).split( '/', SplitSkipEmptyParts );
|
||||
if ( timezoneParts.size() < 2 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
QString region = timezoneParts.first().trimmed();
|
||||
if ( region.isEmpty() )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
QString countryCode = list.at( 0 ).trimmed();
|
||||
if ( countryCode.size() != 2 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
timezoneParts.removeFirst();
|
||||
QString zone = timezoneParts.join( '/' );
|
||||
if ( zone.length() < 2 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
QString position = list.at( 1 );
|
||||
int cooSplitPos = position.indexOf( QRegExp( "[-+]" ), 1 );
|
||||
double latitude;
|
||||
double longitude;
|
||||
if ( cooSplitPos > 0 )
|
||||
{
|
||||
latitude = getRightGeoLocation( position.mid( 0, cooSplitPos ) );
|
||||
longitude = getRightGeoLocation( position.mid( cooSplitPos ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Now we have region, zone, country, lat and longitude
|
||||
const RegionData* existingRegion = nullptr;
|
||||
for ( const auto* p : regions )
|
||||
{
|
||||
if ( p->key() == region )
|
||||
{
|
||||
existingRegion = p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( !existingRegion )
|
||||
{
|
||||
regions.append( new RegionData( region ) );
|
||||
}
|
||||
zones.append( new TimeZoneData( region, zone, countryCode, latitude, longitude ) );
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
loadTZData( RegionVector& regions, ZoneVector& zones )
|
||||
{
|
||||
@ -116,75 +190,7 @@ loadTZData( RegionVector& regions, ZoneVector& zones )
|
||||
if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
|
||||
{
|
||||
QTextStream in( &file );
|
||||
while ( !in.atEnd() )
|
||||
{
|
||||
QString line = in.readLine().trimmed().split( '#', SplitKeepEmptyParts ).first().trimmed();
|
||||
if ( line.isEmpty() )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
QStringList list = line.split( QRegExp( "[\t ]" ), SplitSkipEmptyParts );
|
||||
if ( list.size() < 3 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
QStringList timezoneParts = list.at( 2 ).split( '/', SplitSkipEmptyParts );
|
||||
if ( timezoneParts.size() < 2 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
QString region = timezoneParts.first().trimmed();
|
||||
if ( region.isEmpty() )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
QString countryCode = list.at( 0 ).trimmed();
|
||||
if ( countryCode.size() != 2 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
timezoneParts.removeFirst();
|
||||
QString zone = timezoneParts.join( '/' );
|
||||
if ( zone.length() < 2 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
QString position = list.at( 1 );
|
||||
int cooSplitPos = position.indexOf( QRegExp( "[-+]" ), 1 );
|
||||
double latitude;
|
||||
double longitude;
|
||||
if ( cooSplitPos > 0 )
|
||||
{
|
||||
latitude = getRightGeoLocation( position.mid( 0, cooSplitPos ) );
|
||||
longitude = getRightGeoLocation( position.mid( cooSplitPos ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Now we have region, zone, country, lat and longitude
|
||||
const RegionData* existingRegion = nullptr;
|
||||
for ( const auto* p : regions )
|
||||
{
|
||||
if ( p->key() == region )
|
||||
{
|
||||
existingRegion = p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( !existingRegion )
|
||||
{
|
||||
regions.append( new RegionData( region ) );
|
||||
}
|
||||
zones.append( new TimeZoneData( region, zone, countryCode, latitude, longitude ) );
|
||||
}
|
||||
loadTZData( regions, zones, in );
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user