LocationMgr.cpp   StelLocationMgr.cpp 
skipping to change at line 21 skipping to change at line 21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* 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 "LocationMgr.hpp" #include "StelLocationMgr.hpp"
#include <QStringListModel> #include <QStringListModel>
#include <QDebug> #include <QDebug>
#include <QFile> #include <QFile>
LocationMgr::LocationMgr() StelLocationMgr::StelLocationMgr()
{ {
loadCities("data/base_locations.txt", false); loadCities("data/base_locations.txt", false);
loadCities("data/user_locations.txt", true); loadCities("data/user_locations.txt", true);
modelAllLocation = new QStringListModel(this); modelAllLocation = new QStringListModel(this);
modelAllLocation->setStringList(locations.keys()); modelAllLocation->setStringList(locations.keys());
} }
void LocationMgr::loadCities(const QString& fileName, bool isUserLocation) void StelLocationMgr::loadCities(const QString& fileName, bool isUserLocati on)
{ {
// Load the cities from data file // Load the cities from data file
QString cityDataPath; QString cityDataPath;
try try
{ {
cityDataPath = StelApp::getInstance().getFileMgr().findFile( fileName); cityDataPath = StelApp::getInstance().getFileMgr().findFile( fileName);
} }
catch (std::runtime_error& e) catch (std::runtime_error& e)
{ {
qWarning() << "ERROR: Failed to locate location data file: " // Note it is quite normal to nor have a user locations file
<< fileName << e.what(); (e.g. first run)
if (!isUserLocation)
qWarning() << "WARNING: Failed to locate location da
ta file: " << fileName << e.what();
return; 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;
} }
// 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");
while (!sourcestream.atEnd()) while (!sourcestream.atEnd())
{ {
const QString& rawline=sourcestream.readLine(); const QString& rawline=sourcestream.readLine();
if (rawline.isEmpty() || rawline.startsWith('#')) if (rawline.isEmpty() || rawline.startsWith('#'))
continue; continue;
Location loc = Location::createFromLine(rawline); StelLocation loc = StelLocation::createFromLine(rawline);
loc.isUserLocation = isUserLocation; loc.isUserLocation = isUserLocation;
if (locations.contains(loc.getID())) if (locations.contains(loc.getID()))
{ {
// 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
Location loc2 = locations[loc.getID()]; StelLocation loc2 = locations[loc.getID()];
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(loc.getID()); locations.remove(loc.getID());
locations[loc2.getID()] = loc2; locations[loc2.getID()] = loc2;
if (!loc.state.isEmpty()) if (!loc.state.isEmpty())
loc.name += " ("+loc.state+")"; loc.name += " ("+loc.state+")";
locations[loc.getID()] = loc; locations[loc.getID()] = loc;
} }
else else
{ {
locations[loc.getID()] = loc; locations[loc.getID()] = loc;
} }
} }
sourcefile.close(); sourcefile.close();
} }
LocationMgr::~LocationMgr() StelLocationMgr::~StelLocationMgr()
{ {
} }
const Location LocationMgr::locationForSmallString(const QString& s) const const StelLocation StelLocationMgr::locationForSmallString(const QString& s ) const
{ {
QMap<QString, Location>::const_iterator iter = locations.find(s); QMap<QString, StelLocation>::const_iterator iter = locations.find(s) ;
if (iter==locations.end()) if (iter==locations.end())
{ {
qWarning() << "Warning: location " << s << "is unknown, use Paris as default location."; qWarning() << "Warning: location" << s << "is unknown";
return locations["Paris, France"]; return locations["Paris, France"];
} }
else else
{ {
return locations.value(s); return locations.value(s);
} }
} }
// Get whether a location can be permanently added to the list of user loca tions // Get whether a location can be permanently added to the list of user loca tions
bool LocationMgr::canSaveUserLocation(const Location& loc) const bool StelLocationMgr::canSaveUserLocation(const StelLocation& loc) const
{ {
return locations.find(loc.getID())==locations.end(); return locations.find(loc.getID())==locations.end();
} }
// Add permanently a location to the list of user locations // Add permanently a location to the list of user locations
bool LocationMgr::saveUserLocation(const Location& loc) bool StelLocationMgr::saveUserLocation(const StelLocation& loc)
{ {
if (!canSaveUserLocation(loc)) if (!canSaveUserLocation(loc))
return false; return false;
// Add in the program // Add in the program
locations[loc.getID()]=loc; locations[loc.getID()]=loc;
// Append in the Qt model // Append in the Qt model
modelAllLocation->setStringList(locations.keys()); modelAllLocation->setStringList(locations.keys());
// Append to the user location file // Append to the user location file
QString cityDataPath; QString cityDataPath;
try try
{ {
cityDataPath = StelApp::getInstance().getFileMgr().findFile( "data/user_locations.txt", StelFileMgr::Writable); cityDataPath = StelApp::getInstance().getFileMgr().findFile( "data/user_locations.txt", StelFileMgr::Flags(StelFileMgr::Writable|StelFil eMgr::File));
} }
catch (std::runtime_error& e) catch (std::runtime_error& e)
{ {
if (!StelFileMgr::exists(StelApp::getInstance().getFileMgr() .getUserDir()+"/data")) if (!StelFileMgr::exists(StelApp::getInstance().getFileMgr() .getUserDir()+"/data"))
{ {
if (!StelFileMgr::mkDir(StelApp::getInstance().getFi leMgr().getUserDir()+"/data")) if (!StelFileMgr::mkDir(StelApp::getInstance().getFi leMgr().getUserDir()+"/data"))
{ {
qWarning() << "ERROR - cannot create non-exi stent data directory" << StelApp::getInstance().getFileMgr().getUserDir()+" /data"; qWarning() << "ERROR - cannot create non-exi stent data directory" << StelApp::getInstance().getFileMgr().getUserDir()+" /data";
qWarning() << "Location cannot be saved"; qWarning() << "Location cannot be saved";
return false; return false;
skipping to change at line 166 skipping to change at line 169
QTextStream outstream(&sourcefile); QTextStream outstream(&sourcefile);
outstream.setCodec("UTF-8"); outstream.setCodec("UTF-8");
outstream << loc.serializeToLine() << '\n'; outstream << loc.serializeToLine() << '\n';
sourcefile.close(); sourcefile.close();
return true; return true;
} }
// Get whether a location can be deleted from the list of user locations // Get whether a location can be deleted from the list of user locations
// If the location comes from the base read only list, it cannot be deleted // If the location comes from the base read only list, it cannot be deleted
bool LocationMgr::canDeleteUserLocation(const QString& id) const bool StelLocationMgr::canDeleteUserLocation(const QString& id) const
{ {
QMap<QString, Location>::const_iterator iter=locations.find(id); QMap<QString, StelLocation>::const_iterator iter=locations.find(id);
// If it's not known at all there is a problem // If it's not known at all there is a problem
if (iter==locations.end()) if (iter==locations.end())
return false; return false;
return iter.value().isUserLocation; return iter.value().isUserLocation;
} }
// Delete permanently the given location from the list of user locations // Delete permanently the given location from the list of user locations
// If the location comes from the base read only list, it cannot be deleted and false is returned // If the location comes from the base read only list, it cannot be deleted and false is returned
bool LocationMgr::deleteUserLocation(const QString& id) bool StelLocationMgr::deleteUserLocation(const QString& id)
{ {
if (!canDeleteUserLocation(id)) if (!canDeleteUserLocation(id))
return false; return false;
locations.remove(id); locations.remove(id);
// Remove in the Qt model file // Remove in the Qt model file
modelAllLocation->setStringList(locations.keys()); modelAllLocation->setStringList(locations.keys());
// Resave the whole remaining user locations file // Resave the whole remaining user locations file
QString cityDataPath; QString cityDataPath;
skipping to change at line 220 skipping to change at line 223
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, Location>::const_iterator iter=locations.begin(); iter!=locations.end();++iter) for (QMap<QString, StelLocation>::const_iterator iter=locations.begi n();iter!=locations.end();++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. 17 change blocks. 
18 lines changed or deleted 22 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/