StelLocationMgr.cpp   StelLocationMgr.cpp 
skipping to change at line 22 skipping to change at line 22
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, U SA. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, U SA.
*/ */
#include "StelApp.hpp" #include "StelApp.hpp"
#include "StelFileMgr.hpp" #include "StelFileMgr.hpp"
#include "StelLocationMgr.hpp" #include "StelLocationMgr.hpp"
#include "kfilterdev.h"
#include <QStringListModel> #include <QStringListModel>
#include <QDebug> #include <QDebug>
#include <QFile> #include <QFile>
StelLocationMgr::StelLocationMgr() StelLocationMgr::StelLocationMgr()
{ {
loadCities("data/base_locations.txt", false); // The line below allows to re-generate the location file, you still
loadCities("data/user_locations.txt", true); need to gunzip manually it afterward.
// generateBinaryLocationFile("data/base_locations.txt", false, "dat
a/base_locations.bin");
locations = loadCitiesBin("data/base_locations.bin.gz");
locations.unite(loadCities("data/user_locations.txt", true));
modelAllLocation = new QStringListModel(this); modelAllLocation = new QStringListModel(this);
modelAllLocation->setStringList(locations.keys()); modelAllLocation->setStringList(locations.keys());
} }
void StelLocationMgr::loadCities(const QString& fileName, bool isUserLocati void StelLocationMgr::generateBinaryLocationFile(const QString& fileName, b
on) ool isUserLocation, const QString& binFilePath) const
{
const QMap<QString, StelLocation>& cities = loadCities(fileName, isU
serLocation);
QFile binfile(binFilePath);
binfile.open(QIODevice::WriteOnly);
QDataStream out(&binfile);
out.setVersion(QDataStream::Qt_4_6);
out << cities;
binfile.close();
}
QMap<QString, StelLocation> StelLocationMgr::loadCitiesBin(const QString& f
ileName) const
{
QMap<QString, StelLocation> res;
QString cityDataPath;
try
{
cityDataPath = StelFileMgr::findFile(fileName);
}
catch (std::runtime_error& e)
{
return res;
}
QFile sourcefile(cityDataPath);
if (!sourcefile.open(QIODevice::ReadOnly))
{
qWarning() << "ERROR: Could not open location data file: " <
< cityDataPath;
return res;
}
if (fileName.endsWith(".gz"))
{
QIODevice* d = KFilterDev::device(&sourcefile, "application/
x-gzip", false);
d->open(QIODevice::ReadOnly);
QDataStream in(d);
in.setVersion(QDataStream::Qt_4_6);
in >> res;
d->close();
delete d;
return res;
}
else
{
QDataStream in(&sourcefile);
in.setVersion(QDataStream::Qt_4_6);
in >> res;
return res;
}
}
QMap<QString, StelLocation> StelLocationMgr::loadCities(const QString& file
Name, bool isUserLocation) const
{ {
// Load the cities from data file // Load the cities from data file
QMap<QString, StelLocation> locations;
QString cityDataPath; QString cityDataPath;
try try
{ {
cityDataPath = StelFileMgr::findFile(fileName); cityDataPath = StelFileMgr::findFile(fileName);
} }
catch (std::runtime_error& e) catch (std::runtime_error& e)
{ {
// Note it is quite normal to nor have a user locations file (e.g. first run) // Note it is quite normal to nor have a user locations file (e.g. first run)
if (!isUserLocation) if (!isUserLocation)
qWarning() << "WARNING: Failed to locate location da ta file: " << fileName << e.what(); qWarning() << "WARNING: Failed to locate location da ta file: " << fileName << e.what();
return locations;
return;
} }
QFile sourcefile(cityDataPath); QFile sourcefile(cityDataPath);
if (!sourcefile.open(QIODevice::ReadOnly | QIODevice::Text)) if (!sourcefile.open(QIODevice::ReadOnly | QIODevice::Text))
{ {
qWarning() << "ERROR: Could not open location data file: " < < cityDataPath; qWarning() << "ERROR: Could not open location data file: " < < cityDataPath;
return; return locations;
} }
// Read the data serialized from the file. // Read the data serialized from the file.
// Code below borrowed from Marble (http://edu.kde.org/marble/) // Code below borrowed from Marble (http://edu.kde.org/marble/)
QTextStream sourcestream(&sourcefile); QTextStream sourcestream(&sourcefile);
sourcestream.setCodec("UTF-8"); sourcestream.setCodec("UTF-8");
StelLocation loc; StelLocation loc;
while (!sourcestream.atEnd()) while (!sourcestream.atEnd())
{ {
const QString& rawline=sourcestream.readLine(); const QString& rawline=sourcestream.readLine();
skipping to change at line 82 skipping to change at line 137
const QString& locId = loc.getID(); const QString& locId = loc.getID();
if (locations.contains(locId)) if (locations.contains(locId))
{ {
// Add the state in the name of the existing one and the new one to differentiate // Add the state in the name of the existing one and the new one to differentiate
StelLocation loc2 = locations[locId]; StelLocation loc2 = locations[locId];
if (!loc2.state.isEmpty()) if (!loc2.state.isEmpty())
loc2.name += " ("+loc2.state+")"; loc2.name += " ("+loc2.state+")";
// remove and re-add the fixed version // remove and re-add the fixed version
locations.remove(locId); locations.remove(locId);
locations[loc2.getID()] = loc2; locations.insert(loc2.getID(), loc2);
if (!loc.state.isEmpty()) if (!loc.state.isEmpty())
loc.name += " ("+loc.state+")"; loc.name += " ("+loc.state+")";
locations[locId] = loc; locations.insert(loc.getID(), loc);
} }
else else
{ {
locations.insert(locId, loc); locations.insert(locId, loc);
} }
} }
sourcefile.close(); sourcefile.close();
return locations;
} }
StelLocationMgr::~StelLocationMgr() StelLocationMgr::~StelLocationMgr()
{ {
} }
const StelLocation StelLocationMgr::locationForSmallString(const QString& s ) const const StelLocation StelLocationMgr::locationForSmallString(const QString& s ) const
{ {
QMap<QString, StelLocation>::const_iterator iter = locations.find(s) ; QMap<QString, StelLocation>::const_iterator iter = locations.find(s) ;
if (iter==locations.end()) if (iter==locations.end())
skipping to change at line 225 skipping to change at line 281
QFile sourcefile(cityDataPath); QFile sourcefile(cityDataPath);
if (!sourcefile.open(QIODevice::WriteOnly | QIODevice::Text)) if (!sourcefile.open(QIODevice::WriteOnly | QIODevice::Text))
{ {
qWarning() << "ERROR: Could not open location data file: " < < cityDataPath; qWarning() << "ERROR: Could not open location data file: " < < cityDataPath;
return false; return false;
} }
QTextStream outstream(&sourcefile); QTextStream outstream(&sourcefile);
outstream.setCodec("UTF-8"); outstream.setCodec("UTF-8");
for (QMap<QString, StelLocation>::const_iterator iter=locations.begi n();iter!=locations.end();++iter) for (QMap<QString, StelLocation>::ConstIterator iter=locations.const Begin();iter!=locations.constEnd();++iter)
{ {
if (iter.value().isUserLocation) if (iter.value().isUserLocation)
{ {
outstream << iter.value().serializeToLine() << '\n'; outstream << iter.value().serializeToLine() << '\n';
} }
} }
sourcefile.close(); sourcefile.close();
return true; return true;
} }
 End of changes. 10 change blocks. 
10 lines changed or deleted 73 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/