StelLocationMgr.cpp   StelLocationMgr.cpp 
skipping to change at line 43 skipping to change at line 43
#include <QNetworkReply> #include <QNetworkReply>
#include <QUrl> #include <QUrl>
#include <QUrlQuery> #include <QUrlQuery>
#include <QSettings> #include <QSettings>
StelLocationMgr::StelLocationMgr() StelLocationMgr::StelLocationMgr()
{ {
QSettings* conf = StelApp::getInstance().getSettings(); QSettings* conf = StelApp::getInstance().getSettings();
// The line below allows to re-generate the location file, you still need to gunzip it manually afterward. // The line below allows to re-generate the location file, you still need to gunzip it manually afterward.
// generateBinaryLocationFile("data/base_locations.txt", false, "dat if (conf->value("devel/convert_locations_list", false).toBool())
a/base_locations.bin"); generateBinaryLocationFile("data/base_locations.txt", false,
"data/base_locations.bin");
locations = loadCitiesBin("data/base_locations.bin.gz"); locations = loadCitiesBin("data/base_locations.bin.gz");
locations.unite(loadCities("data/user_locations.txt", true)); locations.unite(loadCities("data/user_locations.txt", true));
// Init to Paris France because it's the center of the world. // Init to Paris France because it's the center of the world.
lastResortLocation = locationForString(conf->value("init_location/la st_location", "Paris, France").toString()); lastResortLocation = locationForString(conf->value("init_location/la st_location", "Paris, France").toString());
} }
StelLocationMgr::StelLocationMgr(const LocationList &locations) StelLocationMgr::StelLocationMgr(const LocationList &locations)
{ {
skipping to change at line 73 skipping to change at line 74
for(LocationList::const_iterator it = locations.constBegin();it!=loc ations.constEnd();++it) for(LocationList::const_iterator it = locations.constBegin();it!=loc ations.constEnd();++it)
{ {
this->locations.insert(it->getID(),*it); this->locations.insert(it->getID(),*it);
} }
emit locationListChanged(); emit locationListChanged();
} }
void StelLocationMgr::generateBinaryLocationFile(const QString& fileName, b ool isUserLocation, const QString& binFilePath) const void StelLocationMgr::generateBinaryLocationFile(const QString& fileName, b ool isUserLocation, const QString& binFilePath) const
{ {
qWarning() << "Generating a locations list...";
const QMap<QString, StelLocation>& cities = loadCities(fileName, isU serLocation); const QMap<QString, StelLocation>& cities = loadCities(fileName, isU serLocation);
QFile binfile(binFilePath); QFile binfile(StelFileMgr::findFile(binFilePath));
if(binfile.open(QIODevice::WriteOnly)) if(binfile.open(QIODevice::WriteOnly))
{ {
QDataStream out(&binfile); QDataStream out(&binfile);
out.setVersion(QDataStream::Qt_4_6); out.setVersion(QDataStream::Qt_5_2);
out << cities; out << cities;
binfile.close(); binfile.close();
} }
} }
LocationMap StelLocationMgr::loadCitiesBin(const QString& fileName) LocationMap StelLocationMgr::loadCitiesBin(const QString& fileName)
{ {
QMap<QString, StelLocation> res; QMap<QString, StelLocation> res;
QString cityDataPath = StelFileMgr::findFile(fileName); QString cityDataPath = StelFileMgr::findFile(fileName);
if (cityDataPath.isEmpty()) if (cityDataPath.isEmpty())
skipping to change at line 101 skipping to change at line 103
QFile sourcefile(cityDataPath); QFile sourcefile(cityDataPath);
if (!sourcefile.open(QIODevice::ReadOnly)) if (!sourcefile.open(QIODevice::ReadOnly))
{ {
qWarning() << "ERROR: Could not open location data file: " < < QDir::toNativeSeparators(cityDataPath); qWarning() << "ERROR: Could not open location data file: " < < QDir::toNativeSeparators(cityDataPath);
return res; return res;
} }
if (fileName.endsWith(".gz")) if (fileName.endsWith(".gz"))
{ {
QDataStream in(StelUtils::uncompress(sourcefile.readAll())); QDataStream in(StelUtils::uncompress(sourcefile.readAll()));
in.setVersion(QDataStream::Qt_4_6); in.setVersion(QDataStream::Qt_5_2);
in >> res; in >> res;
return res; return res;
} }
else else
{ {
QDataStream in(&sourcefile); QDataStream in(&sourcefile);
in.setVersion(QDataStream::Qt_4_6); in.setVersion(QDataStream::Qt_5_2);
in >> res; in >> res;
return res; return res;
} }
} }
LocationMap StelLocationMgr::loadCities(const QString& fileName, bool isUse rLocation) LocationMap StelLocationMgr::loadCities(const QString& fileName, bool isUse rLocation)
{ {
// Load the cities from data file // Load the cities from data file
QMap<QString, StelLocation> locations; QMap<QString, StelLocation> locations;
QString cityDataPath = StelFileMgr::findFile(fileName); QString cityDataPath = StelFileMgr::findFile(fileName);
skipping to change at line 357 skipping to change at line 359
sourcefile.close(); sourcefile.close();
return true; return true;
} }
// lookup location from IP address. // lookup location from IP address.
void StelLocationMgr::locationFromIP() void StelLocationMgr::locationFromIP()
{ {
QNetworkRequest req( QUrl( QString("http://freegeoip.net/json/") ) ) ; QNetworkRequest req( QUrl( QString("http://freegeoip.net/json/") ) ) ;
req.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetwor kRequest::PreferCache); req.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetwor kRequest::PreferCache);
req.setRawHeader("User-Agent", StelUtils::getApplicationName().toLat in1()); req.setRawHeader("User-Agent", StelUtils::getUserAgentString().toLat in1());
QNetworkReply* networkReply=StelApp::getInstance().getNetworkAccessM anager()->get(req); QNetworkReply* networkReply=StelApp::getInstance().getNetworkAccessM anager()->get(req);
connect(networkReply, SIGNAL(finished()), this, SLOT(changeLocationF romNetworkLookup())); connect(networkReply, SIGNAL(finished()), this, SLOT(changeLocationF romNetworkLookup()));
} }
// slot that receives IP-based location data from the network. // slot that receives IP-based location data from the network.
void StelLocationMgr::changeLocationFromNetworkLookup() void StelLocationMgr::changeLocationFromNetworkLookup()
{ {
StelLocation location; StelLocation location;
StelCore *core=StelApp::getInstance().getCore(); StelCore *core=StelApp::getInstance().getCore();
QNetworkReply* networkReply = qobject_cast<QNetworkReply*>(sender()) ; QNetworkReply* networkReply = qobject_cast<QNetworkReply*>(sender()) ;
skipping to change at line 381 skipping to change at line 383
//success //success
QVariantMap locMap = StelJsonParser::parse(networkReply->rea dAll()).toMap(); QVariantMap locMap = StelJsonParser::parse(networkReply->rea dAll()).toMap();
QString ipRegion = locMap.value("region_name").toString(); QString ipRegion = locMap.value("region_name").toString();
QString ipCity = locMap.value("city").toString(); QString ipCity = locMap.value("city").toString();
QString ipCountry = locMap.value("country_name").toString(); // NOTE: Got a short name of country QString ipCountry = locMap.value("country_name").toString(); // NOTE: Got a short name of country
QString ipCountryCode = locMap.value("country_code").toStrin g(); QString ipCountryCode = locMap.value("country_code").toStrin g();
QString ipTimeZone = locMap.value("time_zone").toString(); QString ipTimeZone = locMap.value("time_zone").toString();
float latitude=locMap.value("latitude").toFloat(); float latitude=locMap.value("latitude").toFloat();
float longitude=locMap.value("longitude").toFloat(); float longitude=locMap.value("longitude").toFloat();
qDebug() << "Got location" << QString("%1, %2, %3 (%4, %5)") .arg(ipCity).arg(ipRegion).arg(ipCountry).arg(latitude).arg(longitude) << " for IP" << locMap.value("ip").toString(); qDebug() << "Got location" << QString("%1, %2, %3 (%4, %5; % 6)").arg(ipCity).arg(ipRegion).arg(ipCountry).arg(latitude).arg(longitude). arg(ipTimeZone) << "for IP" << locMap.value("ip").toString();
QString locLine= // we re-pack into a new line that will be parsed back by StelLocation... QString locLine= // we re-pack into a new line that will be parsed back by StelLocation...
QString("%1\t%2\t%3\tX\t0\t%4\t%5\t0\t\t%6") QString("%1\t%2\t%3\tX\t0\t%4\t%5\t0\t\t%6")
.arg(ipCity.isEmpty() ? QString("%1, %2").ar g(latitude).arg(longitude) : ipCity) .arg(ipCity.isEmpty() ? QString("%1, %2").ar g(latitude).arg(longitude) : ipCity)
.arg(ipRegion.isEmpty() ? "IPregion" : ipRe gion) .arg(ipRegion.isEmpty() ? "IPregion" : ipRe gion)
.arg(ipCountryCode.isEmpty() ? "" : ipCountr yCode.toLower()) .arg(ipCountryCode.isEmpty() ? "" : ipCountr yCode.toLower())
.arg(latitude<0 ? QString("%1S").arg(-latitu de, 0, 'f', 6) : QString("%1N").arg(latitude, 0, 'f', 6)) .arg(latitude<0 ? QString("%1S").arg(-latitu de, 0, 'f', 6) : QString("%1N").arg(latitude, 0, 'f', 6))
.arg(longitude<0 ? QString("%1W").arg(-longi tude, 0, 'f', 6) : QString("%1E").arg(longitude, 0, 'f', 6)) .arg(longitude<0 ? QString("%1W").arg(-longi tude, 0, 'f', 6) : QString("%1E").arg(longitude, 0, 'f', 6))
.arg(ipTimeZone.isEmpty() ? "" : ipTimeZone) ; .arg(ipTimeZone.isEmpty() ? "" : ipTimeZone) ;
location=StelLocation::createFromLine(locLine); // in lack o f a regular constructor ;-) location=StelLocation::createFromLine(locLine); // in lack o f a regular constructor ;-)
core->setCurrentTimeZone(ipTimeZone.isEmpty() ? "LMST" : ipT imeZone);
core->moveObserverTo(location, 0.0f, 0.0f); core->moveObserverTo(location, 0.0f, 0.0f);
QSettings* conf = StelApp::getInstance().getSettings(); QSettings* conf = StelApp::getInstance().getSettings();
conf->setValue("init_location/last_location", QString("%1,%2 ").arg(latitude).arg(longitude)); conf->setValue("init_location/last_location", QString("%1,%2 ").arg(latitude).arg(longitude));
} }
else else
{ {
qDebug() << "Failure getting IP-based location: \n\t" <<netw orkReply->errorString(); qDebug() << "Failure getting IP-based location: \n\t" <<netw orkReply->errorString();
// If there is a problem, this must not change to some other location! // If there is a problem, this must not change to some other location!
//core->moveObserverTo(lastResortLocation, 0.0f, 0.0f); //core->moveObserverTo(lastResortLocation, 0.0f, 0.0f);
} }
 End of changes. 9 change blocks. 
8 lines changed or deleted 11 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/