1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
/***************************************************************************
**
** Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/
#include "mlocationdatabase.h"
#include <QFile>
#include <QTextStream>
#include <QDomDocument>
#include <QStringMatcher>
#include <QStringList>
#include <QDebug>
#ifdef HAVE_ICU
#include <unicode/timezone.h>
#endif
namespace ML10N {
static const QString path = "/usr/share/meegotouch/locationdatabase/";
static const QString zoneAliasFile = "/usr/share/tzdata-timed/zone.alias";
static const QString zoneAliasFileFallback = ":/zone.alias.fallback";
class MLocationDatabasePrivate
{
public:
MLocationDatabasePrivate();
bool loadCountries();
bool loadCities();
bool loadTimeZoneData();
bool loadCapitals();
QString canonicalizeTimeZoneId(QString timeZoneId);
QHash<QString, MCity> cities;
QHash<QString, MCountry> countries;
QHash<QString, QString> canonicalTimeZoneIds;
QHash<QString, QString> capitals;
};
MLocationDatabasePrivate::MLocationDatabasePrivate()
{
}
bool MLocationDatabasePrivate::loadCountries()
{
QFile file( path + "countries.xml" );
if (!file.open(QIODevice::ReadOnly))
{
qDebug( "loadCountries file open failed" );
return false;
}
QDomDocument doc;
if (!doc.setContent(&file))
{
qDebug( "loadCountries setContent failed" );
file.close();
return false;
}
file.close();
QDomElement docElem = doc.documentElement();
QDomNode n = docElem.firstChild();
while(!n.isNull())
{
QDomElement e = n.toElement(); // try to convert the node to an element.
if ( e.isNull() )
{
continue;
}
if ( e.tagName() != "country" )
{
continue;
}
// now we found a country key
MCountry country;
QDomElement tmpEl;
tmpEl = e.elementsByTagName( "key" ).at( 0 ).toElement();
country.setKey( tmpEl.text() );
tmpEl = e.elementsByTagName( "englishname" ).at( 0 ).toElement();
country.setEnglishName( tmpEl.text() );
tmpEl = e.elementsByTagName( "localname" ).at( 0 ).toElement();
country.setLocalName( tmpEl.text() );
tmpEl = e.elementsByTagName( "countrycode" ).at( 0 ).toElement();
country.setCountryCode( tmpEl.text() );
countries[ country.key() ] = country;
// QTextStream s( stdout );
// s.setCodec( "UTF-8" );
// s << "country: key: " << country.key()
// << "\nen: " << country.englishName()
// << "\nloc: " << country.localName() << "\n";
n = n.nextSibling();
}
return true;
}
bool MLocationDatabasePrivate::loadCities()
{
QFile file( path + "cities.xml" );
if (!file.open(QIODevice::ReadOnly))
{
qDebug( "loadCities file open failed" );
return false;
}
QDomDocument doc;
if (!doc.setContent(&file))
{
qDebug( "loadCities setContent failed" );
file.close();
return false;
}
file.close();
QDomElement docElem = doc.documentElement();
QDomNode n = docElem.firstChild();
while(!n.isNull())
{
QDomElement e = n.toElement(); // try to convert the node to an element.
if ( e.isNull() )
{
continue;
}
if ( e.tagName() != "city" )
{
continue;
}
// now we found a country key
MCity city;
QDomElement tmpEl;
tmpEl = e.elementsByTagName( "key" ).at( 0 ).toElement();
city.setKey( tmpEl.text() );
tmpEl = e.elementsByTagName( "englishname" ).at( 0 ).toElement();
city.setEnglishName( tmpEl.text() );
tmpEl = e.elementsByTagName( "localname" ).at( 0 ).toElement();
city.setLocalName( tmpEl.text() );
tmpEl = e.elementsByTagName( "timezone" ).at( 0 ).toElement();
QString timeZoneId = canonicalizeTimeZoneId(tmpEl.text());
if(timeZoneId.isEmpty()) {
qWarning() << __PRETTY_FUNCTION__ << "Time zone id"
<< tmpEl.text() << "cannot be canonicalized. Using it as it is.";
timeZoneId = tmpEl.text();
}
else if(timeZoneId != tmpEl.text()) {
qWarning() << __PRETTY_FUNCTION__ << "Time zone id"
<< tmpEl.text() << "canonicalized to" << timeZoneId;
}
city.setTimeZone (timeZoneId);
tmpEl = e.elementsByTagName( "countrykey" ).at( 0 ).toElement();
QString countryKey = tmpEl.text();
if ( countries.contains( countryKey ) )
{
city.setCountry( countries[ countryKey ] );
}
tmpEl = e.elementsByTagName( "latitude" ).at( 0 ).toElement();
QString latString = tmpEl.text();
city.setLatitude( latString.toDouble() );
tmpEl = e.elementsByTagName( "longitude" ).at( 0 ).toElement();
QString lonString = tmpEl.text();
city.setLongitude( lonString.toDouble() );
cities[ city.key() ] = city;
n = n.nextSibling();
}
return true;
}
bool MLocationDatabasePrivate::loadTimeZoneData()
{
QFile file;
file.setFileName(zoneAliasFile);
if(!file.open(QIODevice::ReadOnly)) {
qWarning() << "file" << zoneAliasFile << "is missing."
<< "Using fallback, aliases might have problems.";
file.setFileName(zoneAliasFileFallback);
if(!file.open(QIODevice::ReadOnly)) {
qWarning() << "fallback cannot be opened either.";
return false;
}
}
QTextStream stream(&file);
QString line;
do {
line = stream.readLine();
QStringList timeZoneIds(line.split(QLatin1Char(' '),
QString::SkipEmptyParts));
if(!timeZoneIds.isEmpty()) {
QString canonicalTimeZoneId = timeZoneIds.first();
foreach(const QString &alias, timeZoneIds) {
canonicalTimeZoneIds[alias] = canonicalTimeZoneId;
}
}
} while (!line.isNull());
file.close();
return true;
}
QString MLocationDatabasePrivate::canonicalizeTimeZoneId(QString timeZoneId)
{
// returns an empty string if the hash does not contain the key:
return canonicalTimeZoneIds[timeZoneId];
}
bool MLocationDatabasePrivate::loadCapitals()
{
// This information should be read from the database,
// but I have no time to add the information for
// all the capitals to the database now. Therefore
// I hardcode a few capitals here which can make
// a difference if two cities per Olson ID are displayed:
capitals[QLatin1String("qtn_clk_city_aus_canberra")]
= QLatin1String("qtn_clk_country_australia");
capitals[QLatin1String("qtn_clk_city_bra_brasilia")]
= QLatin1String("qtn_clk_country_brazil");
capitals[QLatin1String("qtn_clk_city_china_beijing")]
= QLatin1String("qtn_clk_country_china");
capitals[QLatin1String("qtn_clk_city_india_newdel")]
= QLatin1String("qtn_clk_country_india");
capitals[QLatin1String("qtn_clk_city_jp_tokyo")]
= QLatin1String("qtn_clk_country_japan");
capitals[QLatin1String("qtn_clk_city_switz_bern")]
= QLatin1String("qtn_clk_country_switzerland");
capitals[QLatin1String("qtn_clk_city_usa_washington")]
= QLatin1String("qtn_clk_country_usa");
capitals[QLatin1String("qtn_clk_city_israel_jerusalem")]
= QLatin1String("qtn_clk_country_israel");
return true;
}
MLocationDatabase::MLocationDatabase()
: d_ptr( new MLocationDatabasePrivate )
{
if ( ! d_ptr->loadTimeZoneData() )
{
qWarning( "loading of time zone data failed." );
}
if ( ! d_ptr->loadCountries() )
{
qWarning( "loading of country list failed." );
}
if ( ! d_ptr->loadCities() )
{
qWarning( "loading of city list failed." );
}
if ( ! d_ptr->loadCapitals() )
{
qWarning( "loading of city list failed." );
}
}
MLocationDatabase::~MLocationDatabase()
{
delete d_ptr;
}
QList<MCountry> MLocationDatabase::countries()
{
Q_D(MLocationDatabase);
QList<MCountry> list;
foreach( const MCountry& country, d->countries )
{
list.append( country );
}
return list;
}
QList<MCity> MLocationDatabase::cities()
{
Q_D(MLocationDatabase);
QList<MCity> list;
foreach( const MCity& city, d->cities )
{
list.append( city );
}
return list;
}
QList<MCity> MLocationDatabase::citiesInCountry( const QString& countryKey )
{
Q_D(MLocationDatabase);
QList<MCity> list;
foreach( const MCity& city, d->cities )
{
if ( city.country().key() == countryKey )
{
list.append( city );
}
}
return list;
}
static QString removeAccents(const QString &str)
{
QString result;
for(int i = 0; i < str.size(); ++i) {
QString decomposition = str[i].decomposition();
if(decomposition == "")
result += str[i];
else
for(int j = 0; j < decomposition.size(); ++j)
if(!decomposition[j].isMark())
result += decomposition[j];
}
return result;
}
QList<MCity> MLocationDatabase::citiesInTimeZone(const QString& timeZoneId)
{
Q_D(MLocationDatabase);
QList<MCity> list;
QString canonicalTimeZoneId = d->canonicalizeTimeZoneId(timeZoneId);
if(canonicalTimeZoneId.isEmpty())
return list;
// Cut out last section of timezone id, for example cut out
// “Tell_City” out of “America/Indiana/Tell_City” In case of
// canonical time zone ids, the part after the last / seems to be
// a city in most cases, although there are exceptions (For
// example “Indian/Mahe” is a canonical id but “Mahe” is an
// island, not a city. There are many non-canonical time zone ids
// which do not have a city name in the last part, for example
// “US/Pacific”.
QString canonicalCity = canonicalTimeZoneId.section('/', -1);
canonicalCity.replace('_', ' ');
QList<MCity> olsonCities;
QList<MCity> capitalCities;
foreach(const MCity& city, d->cities) {
// city.timeZone is already canonical
if (city.timeZone() == canonicalTimeZoneId) {
if(removeAccents(city.englishName()).contains(canonicalCity))
olsonCities.append(city);
else if (!d->capitals[city.key()].isEmpty())
capitalCities.append(city);
else
list.append(city);
}
}
foreach(const MCity &olsonCity, olsonCities)
list.prepend(olsonCity);
foreach(const MCity &capitalCity, capitalCities)
list.prepend(capitalCity);
return list;
}
QList<MCity> MLocationDatabase::matchingCities(const QString& searchString)
{
Q_D(MLocationDatabase);
QList<MCity> list;
QStringMatcher *matcher = new QStringMatcher(searchString, Qt::CaseInsensitive);
foreach (const MCity &city, d->cities) {
if (matcher->indexIn(city.englishName()) != -1
|| matcher->indexIn(city.localName()) != -1)
{
list.append( city );
}
}
delete matcher;
return list;
}
MCity MLocationDatabase::nearestCity(qreal latitude, qreal longitude)
{
Q_D(MLocationDatabase);
MCity bestCity;
qreal bestDistance = 1000000;
QList<MCity> list;
foreach(const MCity &city, d->cities) {
qreal distance =
(latitude - city.latitude()) * (latitude - city.latitude())
+ (longitude - city.longitude()) * (longitude - city.longitude());
if ( distance < bestDistance ) {
bestDistance = distance;
bestCity = city;
}
}
return bestCity;
}
}
|