Quasars.cpp   Quasars.cpp 
skipping to change at line 152 skipping to change at line 152
{ {
upgradeConfigIni(); upgradeConfigIni();
try try
{ {
StelFileMgr::makeSureDirExistsAndIsWritable(StelFileMgr::get UserDir()+"/modules/Quasars"); StelFileMgr::makeSureDirExistsAndIsWritable(StelFileMgr::get UserDir()+"/modules/Quasars");
// If no settings in the main config file, create with defau lts // If no settings in the main config file, create with defau lts
if (!conf->childGroups().contains("Quasars")) if (!conf->childGroups().contains("Quasars"))
{ {
qDebug() << "Quasars: no Quasars section exists in m ain config file - creating with defaults"; qDebug() << "[Quasars] No Quasars section exists in main config file - creating with defaults";
restoreDefaultConfigIni(); restoreDefaultConfigIni();
} }
// populate settings from main config file. // populate settings from main config file.
readSettingsFromConfig(); readSettingsFromConfig();
catalogJsonPath = StelFileMgr::findFile("modules/Quasars", ( StelFileMgr::Flags)(StelFileMgr::Directory|StelFileMgr::Writable)) + "/quas ars.json"; catalogJsonPath = StelFileMgr::findFile("modules/Quasars", ( StelFileMgr::Flags)(StelFileMgr::Directory|StelFileMgr::Writable)) + "/quas ars.json";
if (catalogJsonPath.isEmpty()) if (catalogJsonPath.isEmpty())
return; return;
skipping to change at line 179 skipping to change at line 179
GlowIcon = new QPixmap(":/graphicGui/glow32x32.png"); GlowIcon = new QPixmap(":/graphicGui/glow32x32.png");
OnIcon = new QPixmap(":/Quasars/btQuasars-on.png"); OnIcon = new QPixmap(":/Quasars/btQuasars-on.png");
OffIcon = new QPixmap(":/Quasars/btQuasars-off.png"); OffIcon = new QPixmap(":/Quasars/btQuasars-off.png");
setFlagShowQuasars(getEnableAtStartup()); setFlagShowQuasars(getEnableAtStartup());
setFlagShowQuasarsButton(flagShowQuasarsButton); setFlagShowQuasarsButton(flagShowQuasarsButton);
} }
catch (std::runtime_error &e) catch (std::runtime_error &e)
{ {
qWarning() << "Quasars: init error:" << e.what(); qWarning() << "[Quasars] init error:" << e.what();
return; return;
} }
// A timer for hiding alert messages // A timer for hiding alert messages
messageTimer = new QTimer(this); messageTimer = new QTimer(this);
messageTimer->setSingleShot(true); // recurring check for update messageTimer->setSingleShot(true); // recurring check for update
messageTimer->setInterval(9000); // 6 seconds should be enough time messageTimer->setInterval(9000); // 6 seconds should be enough time
messageTimer->stop(); messageTimer->stop();
connect(messageTimer, SIGNAL(timeout()), this, SLOT(messageTimeout() )); connect(messageTimer, SIGNAL(timeout()), this, SLOT(messageTimeout() ));
// If the json file does not already exist, create it from the resou rce in the Qt resource // If the json file does not already exist, create it from the resou rce in the Qt resource
if(QFileInfo(catalogJsonPath).exists()) if(QFileInfo(catalogJsonPath).exists())
{ {
if (!checkJsonFileFormat() || getJsonFileFormatVersion()<CAT ALOG_FORMAT_VERSION) if (!checkJsonFileFormat() || getJsonFileFormatVersion()<CAT ALOG_FORMAT_VERSION)
{ {
restoreDefaultJsonFile(); restoreDefaultJsonFile();
} }
} }
else else
{ {
qDebug() << "Quasars: quasars.json does not exist - copying default file to" << QDir::toNativeSeparators(catalogJsonPath); qDebug() << "[Quasars] quasars.json does not exist - copying default file to" << QDir::toNativeSeparators(catalogJsonPath);
restoreDefaultJsonFile(); restoreDefaultJsonFile();
} }
qDebug() << "Quasars: loading catalog file:" << QDir::toNativeSepara tors(catalogJsonPath); qDebug() << "[Quasars] Loading catalog file:" << QDir::toNativeSepar ators(catalogJsonPath);
readJsonFile(); readJsonFile();
// Set up download manager and the update schedule // Set up download manager and the update schedule
downloadMgr = new QNetworkAccessManager(this); downloadMgr = new QNetworkAccessManager(this);
connect(downloadMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(up dateDownloadComplete(QNetworkReply*))); connect(downloadMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(up dateDownloadComplete(QNetworkReply*)));
updateState = CompleteNoUpdates; updateState = CompleteNoUpdates;
updateTimer = new QTimer(this); updateTimer = new QTimer(this);
updateTimer->setSingleShot(false); // recurring check for update updateTimer->setSingleShot(false); // recurring check for update
updateTimer->setInterval(13000); // check once every 13 seconds to see if it is time for an update updateTimer->setInterval(13000); // check once every 13 seconds to see if it is time for an update
skipping to change at line 325 skipping to change at line 325
foreach(const QuasarP& quasar, QSO) foreach(const QuasarP& quasar, QSO)
{ {
if (quasar->getNameI18n().toUpper() == nameI18n.toUpper()) if (quasar->getNameI18n().toUpper() == nameI18n.toUpper())
return qSharedPointerCast<StelObject>(quasar); return qSharedPointerCast<StelObject>(quasar);
} }
return NULL; return NULL;
} }
QStringList Quasars::listMatchingObjectsI18n(const QString& objPrefix, int maxNbItem, bool useStartOfWords) const QStringList Quasars::listMatchingObjects(const QString& objPrefix, int maxN bItem, bool useStartOfWords, bool inEnglish) const
{ {
QStringList result; QStringList result;
if (!flagShowQuasars) if (flagShowQuasars)
return result;
if (maxNbItem==0)
return result;
QString qson;
bool find;
foreach(const QuasarP& quasar, QSO)
{
qson = quasar->getNameI18n();
find = false;
if (useStartOfWords)
{
if (qson.toUpper().left(objPrefix.length()) == objPr
efix.toUpper())
find = true;
}
else
{
if (qson.contains(objPrefix, Qt::CaseInsensitive))
find = true;
}
if (find)
{
result << qson;
}
}
result.sort();
if (result.size()>maxNbItem)
result.erase(result.begin()+maxNbItem, result.end());
return result;
}
QStringList Quasars::listMatchingObjects(const QString& objPrefix, int maxN
bItem, bool useStartOfWords) const
{
QStringList result;
if (!flagShowQuasars)
return result;
if (maxNbItem==0)
return result;
QString qson;
bool find;
foreach(const QuasarP& quasar, QSO)
{ {
qson = quasar->getEnglishName(); result = StelObjectModule::listMatchingObjects(objPrefix, ma
find = false; xNbItem, useStartOfWords, inEnglish);
if (useStartOfWords)
{
if (qson.toUpper().left(objPrefix.length()) == objPr
efix.toUpper())
find = true;
}
else
{
if (qson.contains(objPrefix, Qt::CaseInsensitive))
find = true;
}
if (find)
{
result << qson;
}
} }
result.sort();
if (result.size()>maxNbItem)
result.erase(result.begin()+maxNbItem, result.end());
return result; return result;
} }
QStringList Quasars::listAllObjects(bool inEnglish) const QStringList Quasars::listAllObjects(bool inEnglish) const
{ {
QStringList result; QStringList result;
if (!flagShowQuasars) if (!flagShowQuasars)
return result; return result;
if (inEnglish) if (inEnglish)
skipping to change at line 435 skipping to change at line 369
Replace the JSON file with the default from the compiled-in resource Replace the JSON file with the default from the compiled-in resource
*/ */
void Quasars::restoreDefaultJsonFile(void) void Quasars::restoreDefaultJsonFile(void)
{ {
if (QFileInfo(catalogJsonPath).exists()) if (QFileInfo(catalogJsonPath).exists())
backupJsonFile(true); backupJsonFile(true);
QFile src(":/Quasars/quasars.json"); QFile src(":/Quasars/quasars.json");
if (!src.copy(catalogJsonPath)) if (!src.copy(catalogJsonPath))
{ {
qWarning() << "Quasars: cannot copy json resource to" + QDir ::toNativeSeparators(catalogJsonPath); qWarning() << "[Quasars] Cannot copy json resource to" + QDi r::toNativeSeparators(catalogJsonPath);
} }
else else
{ {
qDebug() << "Quasars: copied default quasars.json to" << QDi r::toNativeSeparators(catalogJsonPath); qDebug() << "[Quasars] Copied default quasars.json to" << QD ir::toNativeSeparators(catalogJsonPath);
// The resource is read only, and the new file inherits this ... make sure the new file // The resource is read only, and the new file inherits this ... make sure the new file
// is writable by the Stellarium process so that updates can be done. // is writable by the Stellarium process so that updates can be done.
QFile dest(catalogJsonPath); QFile dest(catalogJsonPath);
dest.setPermissions(dest.permissions() | QFile::WriteOwner); dest.setPermissions(dest.permissions() | QFile::WriteOwner);
// Make sure that in the case where an online update has pre viously been done, but // Make sure that in the case where an online update has pre viously been done, but
// the json file has been manually removed, that an update i s schreduled in a timely // the json file has been manually removed, that an update i s schreduled in a timely
// manner // manner
conf->remove("Quasars/last_update"); conf->remove("Quasars/last_update");
lastUpdate = QDateTime::fromString("2012-05-24T12:00:00", Qt ::ISODate); lastUpdate = QDateTime::fromString("2012-05-24T12:00:00", Qt ::ISODate);
skipping to change at line 462 skipping to change at line 396
} }
/* /*
Creates a backup of the quasars.json file called quasars.json.old Creates a backup of the quasars.json file called quasars.json.old
*/ */
bool Quasars::backupJsonFile(bool deleteOriginal) bool Quasars::backupJsonFile(bool deleteOriginal)
{ {
QFile old(catalogJsonPath); QFile old(catalogJsonPath);
if (!old.exists()) if (!old.exists())
{ {
qWarning() << "Quasars: no file to backup"; qWarning() << "[Quasars] No file to backup";
return false; return false;
} }
QString backupPath = catalogJsonPath + ".old"; QString backupPath = catalogJsonPath + ".old";
if (QFileInfo(backupPath).exists()) if (QFileInfo(backupPath).exists())
QFile(backupPath).remove(); QFile(backupPath).remove();
if (old.copy(backupPath)) if (old.copy(backupPath))
{ {
if (deleteOriginal) if (deleteOriginal)
{ {
if (!old.remove()) if (!old.remove())
{ {
qWarning() << "Quasars: WARNING - could not remove old quasars.json file"; qWarning() << "[Quasars] WARNING - could not remove old quasars.json file";
return false; return false;
} }
} }
} }
else else
{ {
qWarning() << "Quasars: WARNING - failed to copy quasars.jso n to quasars.json.old"; qWarning() << "[Quasars] WARNING - failed to copy quasars.js on to quasars.json.old";
return false; return false;
} }
return true; return true;
} }
/* /*
Read the JSON file and create list of quasars. Read the JSON file and create list of quasars.
*/ */
void Quasars::readJsonFile(void) void Quasars::readJsonFile(void)
skipping to change at line 509 skipping to change at line 443
Parse JSON file and load quasarss to map Parse JSON file and load quasarss to map
*/ */
QVariantMap Quasars::loadQSOMap(QString path) QVariantMap Quasars::loadQSOMap(QString path)
{ {
if (path.isEmpty()) if (path.isEmpty())
path = catalogJsonPath; path = catalogJsonPath;
QVariantMap map; QVariantMap map;
QFile jsonFile(path); QFile jsonFile(path);
if (!jsonFile.open(QIODevice::ReadOnly)) if (!jsonFile.open(QIODevice::ReadOnly))
qWarning() << "Quasars: cannot open" << QDir::toNativeSepara tors(path); qWarning() << "[Quasars] Cannot open" << QDir::toNativeSepar ators(path);
else else
{ {
map = StelJsonParser::parse(jsonFile.readAll()).toMap(); map = StelJsonParser::parse(jsonFile.readAll()).toMap();
jsonFile.close(); jsonFile.close();
} }
return map; return map;
} }
/* /*
Set items for list of struct from data map Set items for list of struct from data map
skipping to change at line 546 skipping to change at line 480
} }
} }
int Quasars::getJsonFileFormatVersion(void) int Quasars::getJsonFileFormatVersion(void)
{ {
int jsonVersion = -1; int jsonVersion = -1;
QFile catalogJsonFile(catalogJsonPath); QFile catalogJsonFile(catalogJsonPath);
if (!catalogJsonFile.open(QIODevice::ReadOnly)) if (!catalogJsonFile.open(QIODevice::ReadOnly))
{ {
qWarning() << "Quasars: cannot open" << QDir::toNativeSepara tors(catalogJsonPath); qWarning() << "[Quasars] Cannot open" << QDir::toNativeSepar ators(catalogJsonPath);
return jsonVersion; return jsonVersion;
} }
QVariantMap map; QVariantMap map;
map = StelJsonParser::parse(&catalogJsonFile).toMap(); map = StelJsonParser::parse(&catalogJsonFile).toMap();
if (map.contains("version")) if (map.contains("version"))
{ {
jsonVersion = map.value("version").toInt(); jsonVersion = map.value("version").toInt();
} }
catalogJsonFile.close(); catalogJsonFile.close();
qDebug() << "Quasars: version of the format of the catalog:" << json Version; qDebug() << "[Quasars] Version of the format of the catalog:" << jso nVersion;
return jsonVersion; return jsonVersion;
} }
bool Quasars::checkJsonFileFormat() bool Quasars::checkJsonFileFormat()
{ {
QFile catalogJsonFile(catalogJsonPath); QFile catalogJsonFile(catalogJsonPath);
if (!catalogJsonFile.open(QIODevice::ReadOnly)) if (!catalogJsonFile.open(QIODevice::ReadOnly))
{ {
qWarning() << "Quasars: cannot open" << QDir::toNativeSepara tors(catalogJsonPath); qWarning() << "[Quasars] Cannot open" << QDir::toNativeSepar ators(catalogJsonPath);
return false; return false;
} }
QVariantMap map; QVariantMap map;
try try
{ {
map = StelJsonParser::parse(&catalogJsonFile).toMap(); map = StelJsonParser::parse(&catalogJsonFile).toMap();
catalogJsonFile.close(); catalogJsonFile.close();
} }
catch (std::runtime_error& e) catch (std::runtime_error& e)
{ {
qDebug() << "Quasars: file format is wrong! Error:" << e.wha t(); qDebug() << "[Quasars] File format is wrong! Error:" << e.wh at();
return false; return false;
} }
return true; return true;
} }
QuasarP Quasars::getByID(const QString& id) QuasarP Quasars::getByID(const QString& id)
{ {
foreach(const QuasarP& quasar, QSO) foreach(const QuasarP& quasar, QSO)
{ {
skipping to change at line 637 skipping to change at line 571
void Quasars::readSettingsFromConfig(void) void Quasars::readSettingsFromConfig(void)
{ {
conf->beginGroup("Quasars"); conf->beginGroup("Quasars");
updateUrl = conf->value("url", "http://stellarium.org/json/quasars.j son").toString(); updateUrl = conf->value("url", "http://stellarium.org/json/quasars.j son").toString();
updateFrequencyDays = conf->value("update_frequency_days", 100).toIn t(); updateFrequencyDays = conf->value("update_frequency_days", 100).toIn t();
lastUpdate = QDateTime::fromString(conf->value("last_update", "2012- 05-24T12:00:00").toString(), Qt::ISODate); lastUpdate = QDateTime::fromString(conf->value("last_update", "2012- 05-24T12:00:00").toString(), Qt::ISODate);
updatesEnabled = conf->value("updates_enabled", true).toBool(); updatesEnabled = conf->value("updates_enabled", true).toBool();
setDisplayMode(conf->value("distribution_enabled", false).toBool()); setDisplayMode(conf->value("distribution_enabled", false).toBool());
setMarkerColor(conf->value("marker_color", "1.0,0.5,0.4").toString() ); setMarkerColor(StelUtils::strToVec3f(conf->value("marker_color", "1. 0,0.5,0.4").toString()));
enableAtStartup = conf->value("enable_at_startup", false).toBool(); enableAtStartup = conf->value("enable_at_startup", false).toBool();
flagShowQuasarsButton = conf->value("flag_show_quasars_button", true ).toBool(); flagShowQuasarsButton = conf->value("flag_show_quasars_button", true ).toBool();
conf->endGroup(); conf->endGroup();
} }
void Quasars::saveSettingsToConfig(void) void Quasars::saveSettingsToConfig(void)
{ {
conf->beginGroup("Quasars"); conf->beginGroup("Quasars");
conf->setValue("url", updateUrl); conf->setValue("url", updateUrl);
conf->setValue("update_frequency_days", updateFrequencyDays); conf->setValue("update_frequency_days", updateFrequencyDays);
conf->setValue("updates_enabled", updatesEnabled ); conf->setValue("updates_enabled", updatesEnabled );
conf->setValue("distribution_enabled", getDisplayMode()); conf->setValue("distribution_enabled", getDisplayMode());
conf->setValue("enable_at_startup", enableAtStartup); conf->setValue("enable_at_startup", enableAtStartup);
conf->setValue("flag_show_quasars_button", flagShowQuasarsButton); conf->setValue("flag_show_quasars_button", flagShowQuasarsButton);
conf->setValue("marker_color", getMarkerColor()); conf->setValue("marker_color", StelUtils::vec3fToStr(getMarkerColor( )));
conf->endGroup(); conf->endGroup();
} }
int Quasars::getSecondsToUpdate(void) int Quasars::getSecondsToUpdate(void)
{ {
QDateTime nextUpdate = lastUpdate.addSecs(updateFrequencyDays * 3600 * 24); QDateTime nextUpdate = lastUpdate.addSecs(updateFrequencyDays * 3600 * 24);
return QDateTime::currentDateTime().secsTo(nextUpdate); return QDateTime::currentDateTime().secsTo(nextUpdate);
} }
void Quasars::checkForUpdate(void) void Quasars::checkForUpdate(void)
{ {
if (updatesEnabled && lastUpdate.addSecs(updateFrequencyDays * 3600 * 24) <= QDateTime::currentDateTime()) if (updatesEnabled && lastUpdate.addSecs(updateFrequencyDays * 3600 * 24) <= QDateTime::currentDateTime())
updateJSON(); updateJSON();
} }
void Quasars::updateJSON(void) void Quasars::updateJSON(void)
{ {
if (updateState==Quasars::Updating) if (updateState==Quasars::Updating)
{ {
qWarning() << "Quasars: already updating... will not start again current update is complete."; qWarning() << "[Quasars] Already updating... will not start again current update is complete.";
return; return;
} }
else else
{ {
qDebug() << "Quasars: starting update..."; qDebug() << "[Quasars] Starting update...";
} }
lastUpdate = QDateTime::currentDateTime(); lastUpdate = QDateTime::currentDateTime();
conf->setValue("Quasars/last_update", lastUpdate.toString(Qt::ISODat e)); conf->setValue("Quasars/last_update", lastUpdate.toString(Qt::ISODat e));
updateState = Quasars::Updating; updateState = Quasars::Updating;
emit(updateStateChanged(updateState)); emit(updateStateChanged(updateState));
if (progressBar==NULL) if (progressBar==NULL)
progressBar = StelApp::getInstance().addProgressBar(); progressBar = StelApp::getInstance().addProgressBar();
skipping to change at line 711 skipping to change at line 645
updateState = Quasars::CompleteUpdates; updateState = Quasars::CompleteUpdates;
emit(updateStateChanged(updateState)); emit(updateStateChanged(updateState));
emit(jsonUpdateComplete()); emit(jsonUpdateComplete());
} }
void Quasars::updateDownloadComplete(QNetworkReply* reply) void Quasars::updateDownloadComplete(QNetworkReply* reply)
{ {
// check the download worked, and save the data to file if this is t he case. // check the download worked, and save the data to file if this is t he case.
if (reply->error() != QNetworkReply::NoError) if (reply->error() != QNetworkReply::NoError)
{ {
qWarning() << "Quasars: FAILED to download" << reply->url() << " Error: " << reply->errorString(); qWarning() << "[Quasars] FAILED to download" << reply->url() << " Error: " << reply->errorString();
} }
else else
{ {
// download completed successfully. // download completed successfully.
QString jsonFilePath = StelFileMgr::findFile("modules/Quasar s", StelFileMgr::Flags(StelFileMgr::Writable|StelFileMgr::Directory)) + "/q uasars.json"; QString jsonFilePath = StelFileMgr::findFile("modules/Quasar s", StelFileMgr::Flags(StelFileMgr::Writable|StelFileMgr::Directory)) + "/q uasars.json";
if (jsonFilePath.isEmpty()) if (jsonFilePath.isEmpty())
{ {
qWarning() << "Quasars: cannot write JSON data to fi le:" << QDir::toNativeSeparators(jsonFilePath); qWarning() << "[Quasars] Cannot write JSON data to f ile:" << QDir::toNativeSeparators(jsonFilePath);
return; return;
} }
QFile jsonFile(jsonFilePath); QFile jsonFile(jsonFilePath);
if (jsonFile.exists()) if (jsonFile.exists())
jsonFile.remove(); jsonFile.remove();
if(jsonFile.open(QIODevice::WriteOnly | QIODevice::Text)) if(jsonFile.open(QIODevice::WriteOnly | QIODevice::Text))
{ {
jsonFile.write(reply->readAll()); jsonFile.write(reply->readAll());
jsonFile.close(); jsonFile.close();
skipping to change at line 796 skipping to change at line 730
bool Quasars::getDisplayMode() bool Quasars::getDisplayMode()
{ {
return Quasar::distributionMode; return Quasar::distributionMode;
} }
void Quasars::setDisplayMode(bool b) void Quasars::setDisplayMode(bool b)
{ {
Quasar::distributionMode=b; Quasar::distributionMode=b;
} }
QString Quasars::getMarkerColor() Vec3f Quasars::getMarkerColor()
{
return Quasar::markerColor;
}
void Quasars::setMarkerColor(const Vec3f &c)
{ {
Vec3f c = Quasar::markerColor; Quasar::markerColor = c;
return QString("%1,%2,%3").arg(c[0]).arg(c[1]).arg(c[2]);
} }
void Quasars::setMarkerColor(QString c) void Quasars::reloadCatalog(void)
{ {
Quasar::markerColor = StelUtils::strToVec3f(c); bool hasSelection = false;
StelObjectMgr* objMgr = GETSTELMODULE(StelObjectMgr);
// Whether any quasar are selected? Save the current selection...
const QList<StelObjectP> selectedObject = objMgr->getSelectedObject(
"Quasar");
if (!selectedObject.isEmpty())
{
// ... unselect current quasar.
hasSelection = true;
objMgr->unSelect();
}
readJsonFile();
if (hasSelection)
{
// Restore selection...
objMgr->setSelectedObject(selectedObject);
}
}
void Quasars::setFlagShowQuasars(bool b)
{
if (b!=flagShowQuasars)
{
flagShowQuasars=b;
emit flagQuasarsVisibilityChanged(b);
}
} }
 End of changes. 28 change blocks. 
97 lines changed or deleted 60 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/