Quasars.cpp   Quasars.cpp 
skipping to change at line 24 skipping to change at line 24
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA. * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*/ */
#include "StelProjector.hpp" #include "StelProjector.hpp"
#include "StelPainter.hpp" #include "StelPainter.hpp"
#include "StelApp.hpp" #include "StelApp.hpp"
#include "StelCore.hpp" #include "StelCore.hpp"
#include "StelGui.hpp" #include "StelGui.hpp"
#include "StelGuiItems.hpp"
#include "StelLocaleMgr.hpp" #include "StelLocaleMgr.hpp"
#include "StelModuleMgr.hpp" #include "StelModuleMgr.hpp"
#include "StelObjectMgr.hpp" #include "StelObjectMgr.hpp"
#include "StelTextureMgr.hpp" #include "StelTextureMgr.hpp"
#include "StelJsonParser.hpp" #include "StelJsonParser.hpp"
#include "StelFileMgr.hpp" #include "StelFileMgr.hpp"
#include "StelUtils.hpp" #include "StelUtils.hpp"
#include "StelTranslator.hpp" #include "StelTranslator.hpp"
#include "Quasars.hpp" #include "LabelMgr.hpp"
#include "Quasar.hpp" #include "Quasar.hpp"
#include "Quasars.hpp"
#include "QuasarsDialog.hpp"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QKeyEvent>
#include <QAction>
#include <QProgressBar>
#include <QDebug> #include <QDebug>
#include <QFileInfo> #include <QFileInfo>
#include <QFile> #include <QFile>
#include <QTimer>
#include <QVariantMap> #include <QVariantMap>
#include <QVariant> #include <QVariant>
#include <QList> #include <QList>
#include <QSharedPointer> #include <QSharedPointer>
#include <QStringList> #include <QStringList>
#define CATALOG_FORMAT_VERSION 1 /* Version of format of catalog */
/* /*
This method is the one called automatically by the StelModuleMgr just This method is the one called automatically by the StelModuleMgr just
after loading the dynamic library after loading the dynamic library
*/ */
StelModule* QuasarsStelPluginInterface::getStelModule() const StelModule* QuasarsStelPluginInterface::getStelModule() const
{ {
return new Quasars(); return new Quasars();
} }
StelPluginInfo QuasarsStelPluginInterface::getPluginInfo() const StelPluginInfo QuasarsStelPluginInterface::getPluginInfo() const
skipping to change at line 72 skipping to change at line 83
info.description = N_("A plugin that shows some quasars brighter tha n 16 visual magnitude. A catalogue of quasars compiled from 'Quasars and Ac tive Galactic Nuclei' (13th Ed.) (Veron+ 2010) =2010A&A...518A..10V"); info.description = N_("A plugin that shows some quasars brighter tha n 16 visual magnitude. A catalogue of quasars compiled from 'Quasars and Ac tive Galactic Nuclei' (13th Ed.) (Veron+ 2010) =2010A&A...518A..10V");
return info; return info;
} }
Q_EXPORT_PLUGIN2(Quasars, QuasarsStelPluginInterface) Q_EXPORT_PLUGIN2(Quasars, QuasarsStelPluginInterface)
/* /*
Constructor Constructor
*/ */
Quasars::Quasars() Quasars::Quasars()
: progressBar(NULL)
{ {
setObjectName("Quasars"); setObjectName("Quasars");
font.setPixelSize(StelApp::getInstance().getSettings()->value("gui/b configDialog = new QuasarsDialog();
ase_font_size", 13).toInt()); conf = StelApp::getInstance().getSettings();
font.setPixelSize(conf->value("gui/base_font_size", 13).toInt());
} }
/* /*
Destructor Destructor
*/ */
Quasars::~Quasars() Quasars::~Quasars()
{ {
// delete configDialog;
} }
void Quasars::deinit() void Quasars::deinit()
{ {
QSO.clear();
Quasar::markerTexture.clear();
texPointer.clear(); texPointer.clear();
} }
/* /*
Reimplementation of the getCallOrder method Reimplementation of the getCallOrder method
*/ */
double Quasars::getCallOrder(StelModuleActionName actionName) const double Quasars::getCallOrder(StelModuleActionName actionName) const
{ {
if (actionName==StelModule::ActionDraw) if (actionName==StelModule::ActionDraw)
return StelApp::getInstance().getModuleMgr().getModule("Cons tellationMgr")->getCallOrder(actionName)+10.; return StelApp::getInstance().getModuleMgr().getModule("Cons tellationMgr")->getCallOrder(actionName)+10.;
skipping to change at line 109 skipping to change at line 125
/* /*
Init our module Init our module
*/ */
void Quasars::init() void Quasars::init()
{ {
try try
{ {
StelFileMgr::makeSureDirExistsAndIsWritable(StelFileMgr::get UserDir()+"/modules/Quasars"); StelFileMgr::makeSureDirExistsAndIsWritable(StelFileMgr::get UserDir()+"/modules/Quasars");
catalogJsonPath = StelFileMgr::findFile("modules/Quasars", ( // If no settings in the main config file, create with defau
StelFileMgr::Flags)(StelFileMgr::Directory|StelFileMgr::Writable)) + "/cata lts
log.json"; if (!conf->childGroups().contains("Quasars"))
{
qDebug() << "Quasars::init no Quasars section exists
in main config file - creating with defaults";
restoreDefaultConfigIni();
}
// populate settings from main config file.
readSettingsFromConfig();
catalogJsonPath = StelFileMgr::findFile("modules/Quasars", (
StelFileMgr::Flags)(StelFileMgr::Directory|StelFileMgr::Writable)) + "/quas
ars.json";
texPointer = StelApp::getInstance().getTextureManager().crea teTexture("textures/pointeur2.png"); texPointer = StelApp::getInstance().getTextureManager().crea teTexture("textures/pointeur2.png");
Quasar::markerTexture = StelApp::getInstance().getTextureMan
ager().createTexture(":/Quasars/quasar.png");
// key bindings and other actions
// TRANSLATORS: Title of a group of key bindings in the Help
window
QString groupName = N_("Plugin Key Bindings");
StelGui* gui = dynamic_cast<StelGui*>(StelApp::getInstance()
.getGui());
gui->addGuiActions("actionShow_Quasars_ConfigDialog", N_("Qu
asars configuration window"), "", groupName, true);
connect(gui->getGuiActions("actionShow_Quasars_ConfigDialog"
), SIGNAL(toggled(bool)), configDialog, SLOT(setVisible(bool)));
connect(configDialog, SIGNAL(visibleChanged(bool)), gui->get
GuiActions("actionShow_Quasars_ConfigDialog"), SLOT(setChecked(bool)));
} }
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
messageTimer = new QTimer(this);
messageTimer->setSingleShot(true); // recurring check for update
messageTimer->setInterval(9000); // 6 seconds should be enough
time
messageTimer->stop();
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 (getJsonFileVersion() != QUASARS_PLUGIN_VERSION) if (getJsonFileVersion() < CATALOG_FORMAT_VERSION)
{ {
restoreDefaultJsonFile(); restoreDefaultJsonFile();
} }
} }
else else
{ {
qDebug() << "Quasars::init catalog.json does not exist - cop ying default file to " << catalogJsonPath; qDebug() << "Quasars::init catalog.json does not exist - cop ying default file to " << catalogJsonPath;
restoreDefaultJsonFile(); restoreDefaultJsonFile();
} }
qDebug() << "Quasars::init using catalog.json file: " << catalogJson Path; qDebug() << "Quasars::init using catalog.json file: " << catalogJson Path;
readJsonFile(); readJsonFile();
// Set up download manager and the update schedule
downloadMgr = new QNetworkAccessManager(this);
connect(downloadMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(up
dateDownloadComplete(QNetworkReply*)));
updateState = CompleteNoUpdates;
updateTimer = new QTimer(this);
updateTimer->setSingleShot(false); // recurring check for update
updateTimer->setInterval(13000); // check once every 13 seconds
to see if it is time for an update
connect(updateTimer, SIGNAL(timeout()), this, SLOT(checkForUpdate())
);
updateTimer->start();
GETSTELMODULE(StelObjectMgr)->registerStelObjectMgr(this); GETSTELMODULE(StelObjectMgr)->registerStelObjectMgr(this);
} }
/* /*
Draw our module. This should print name of first QSO in the main window Draw our module. This should print name of first QSO in the main window
*/ */
void Quasars::draw(StelCore* core) void Quasars::draw(StelCore* core)
{ {
StelProjectorP prj = core->getProjection(StelCore::FrameJ2000); StelProjectorP prj = core->getProjection(StelCore::FrameJ2000);
StelPainter painter(prj); StelPainter painter(prj);
skipping to change at line 264 skipping to change at line 317
} }
/* /*
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/catalog.json"); QFile src(":/Quasars/quasars.json");
if (!src.copy(catalogJsonPath)) if (!src.copy(catalogJsonPath))
{ {
qWarning() << "Quasars::restoreDefaultJsonFile cannot copy j son resource to " + catalogJsonPath; qWarning() << "Quasars::restoreDefaultJsonFile cannot copy j son resource to " + catalogJsonPath;
} }
else else
{ {
qDebug() << "Quasars::init copied default catalog.json to " << catalogJsonPath; qDebug() << "Quasars::init copied default catalog.json to " << 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
// the json file has been manually removed, that an update i
s schreduled in a timely
// manner
conf->remove("Quasars/last_update");
lastUpdate = QDateTime::fromString("2012-05-24T12:00:00", Qt
::ISODate);
} }
} }
/* /*
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())
skipping to change at line 301 skipping to change at line 361
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::backupJsonFile WARNI NG - could not remove old catalog.json file"; qWarning() << "Quasars::backupJsonFile WARNI NG - could not remove old quasars.json file";
return false; return false;
} }
} }
} }
else else
{ {
qWarning() << "Quasars::backupJsonFile WARNING - failed to c opy catalog.json to catalog.json.old"; qWarning() << "Quasars::backupJsonFile WARNING - failed to c opy quasars.json 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 361 skipping to change at line 421
QVariantMap qsoData = qsoMap.value(qsoKey).toMap(); QVariantMap qsoData = qsoMap.value(qsoKey).toMap();
qsoData["designation"] = qsoKey; qsoData["designation"] = qsoKey;
QuasarP quasar(new Quasar(qsoData)); QuasarP quasar(new Quasar(qsoData));
if (quasar->initialized) if (quasar->initialized)
QSO.append(quasar); QSO.append(quasar);
} }
} }
const QString Quasars::getJsonFileVersion(void) int Quasars::getJsonFileVersion(void)
{ {
QString jsonVersion("unknown"); int jsonVersion = -1;
QFile catalogJsonFile(catalogJsonPath); QFile catalogJsonFile(catalogJsonPath);
if (!catalogJsonFile.open(QIODevice::ReadOnly)) if (!catalogJsonFile.open(QIODevice::ReadOnly))
{ {
qWarning() << "Quasars::init cannot open " << catalogJsonPat h; qWarning() << "Quasars::init cannot open " << catalogJsonPat h;
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"))
{ {
QString creator = map.value("version").toString(); jsonVersion = map.value("version").toInt();
QRegExp vRx(".*(\\d+\\.\\d+\\.\\d+).*");
if (vRx.exactMatch(creator))
{
jsonVersion = vRx.capturedTexts().at(1);
}
} }
catalogJsonFile.close(); catalogJsonFile.close();
qDebug() << "Quasars::getJsonFileVersion() version from file:" << js onVersion; qDebug() << "Quasars::getJsonFileVersion() version from file:" << js onVersion;
return jsonVersion; return jsonVersion;
} }
QuasarP Quasars::getByID(const QString& id) QuasarP Quasars::getByID(const QString& id)
{ {
foreach(const QuasarP& quasar, QSO) foreach(const QuasarP& quasar, QSO)
{ {
if (quasar->initialized && quasar->designation == id) if (quasar->initialized && quasar->designation == id)
return quasar; return quasar;
} }
return QuasarP(); return QuasarP();
} }
bool Quasars::configureGui(bool show)
{
if (show)
{
StelGui* gui = dynamic_cast<StelGui*>(StelApp::getInstance()
.getGui());
gui->getGuiActions("actionShow_Quasars_ConfigDialog")->setCh
ecked(true);
}
return true;
}
void Quasars::restoreDefaults(void)
{
restoreDefaultConfigIni();
restoreDefaultJsonFile();
readJsonFile();
readSettingsFromConfig();
}
void Quasars::restoreDefaultConfigIni(void)
{
conf->beginGroup("Quasars");
// delete all existing Quasars settings...
conf->remove("");
conf->setValue("distribution_enabled", false);
conf->setValue("updates_enabled", true);
conf->setValue("url", "http://stellarium.org/json/quasars.json");
conf->setValue("update_frequency_days", 100);
conf->endGroup();
}
void Quasars::readSettingsFromConfig(void)
{
conf->beginGroup("Quasars");
updateUrl = conf->value("url", "http://stellarium.org/json/quasars.j
son").toString();
updateFrequencyDays = conf->value("update_frequency_days", 100).toIn
t();
lastUpdate = QDateTime::fromString(conf->value("last_update", "2012-
05-24T12:00:00").toString(), Qt::ISODate);
updatesEnabled = conf->value("updates_enabled", true).toBool();
distributionEnabled = conf->value("distribution_enabled", false).toB
ool();
conf->endGroup();
}
void Quasars::saveSettingsToConfig(void)
{
conf->beginGroup("Quasars");
conf->setValue("url", updateUrl);
conf->setValue("update_frequency_days", updateFrequencyDays);
conf->setValue("updates_enabled", updatesEnabled );
conf->setValue("distribution_enabled", distributionEnabled);
conf->endGroup();
}
int Quasars::getSecondsToUpdate(void)
{
QDateTime nextUpdate = lastUpdate.addSecs(updateFrequencyDays * 3600
* 24);
return QDateTime::currentDateTime().secsTo(nextUpdate);
}
void Quasars::checkForUpdate(void)
{
if (updatesEnabled && lastUpdate.addSecs(updateFrequencyDays * 3600
* 24) <= QDateTime::currentDateTime())
updateJSON();
}
void Quasars::updateJSON(void)
{
if (updateState==Quasars::Updating)
{
qWarning() << "Quasars: already updating... will not start
again current update is complete.";
return;
}
else
{
qDebug() << "Quasars: starting update...";
}
lastUpdate = QDateTime::currentDateTime();
conf->setValue("Quasars/last_update", lastUpdate.toString(Qt::ISODat
e));
emit(jsonUpdateComplete());
updateState = Quasars::Updating;
emit(updateStateChanged(updateState));
updateFile.clear();
if (progressBar==NULL)
progressBar = StelApp::getInstance().getGui()->addProgressBa
r();
progressBar->setValue(0);
progressBar->setMaximum(updateUrl.size());
progressBar->setVisible(true);
progressBar->setFormat("Update quasars");
QNetworkRequest request;
request.setUrl(QUrl(updateUrl));
request.setRawHeader("User-Agent", QString("Mozilla/5.0 (Stellarium
Quasars Plugin %1; http://stellarium.org/)").arg(QUASARS_PLUGIN_VERSION).to
Utf8());
downloadMgr->get(request);
progressBar->setValue(100);
delete progressBar;
progressBar = NULL;
updateState = CompleteUpdates;
emit(updateStateChanged(updateState));
emit(jsonUpdateComplete());
}
void Quasars::updateDownloadComplete(QNetworkReply* reply)
{
// check the download worked, and save the data to file if this is t
he case.
if (reply->error() != QNetworkReply::NoError)
{
qWarning() << "Quasars::updateDownloadComplete FAILED to dow
nload" << reply->url() << " Error: " << reply->errorString();
}
else
{
// download completed successfully.
try
{
QString jsonFilePath = StelFileMgr::findFile("module
s/Quasars", StelFileMgr::Flags(StelFileMgr::Writable|StelFileMgr::Directory
)) + "/quasars.json";
QFile jsonFile(jsonFilePath);
if (jsonFile.exists())
jsonFile.remove();
jsonFile.open(QIODevice::WriteOnly | QIODevice::Text
);
jsonFile.write(reply->readAll());
jsonFile.close();
}
catch (std::runtime_error &e)
{
qWarning() << "Quasars::updateDownloadComplete: cann
ot write JSON data to file:" << e.what();
}
}
if (progressBar)
progressBar->setValue(100);
}
void Quasars::displayMessage(const QString& message, const QString hexColor
)
{
messageIDs << GETSTELMODULE(LabelMgr)->labelScreen(message, 30, 30 +
(20*messageIDs.count()), true, 16, hexColor);
messageTimer->start();
}
void Quasars::messageTimeout(void)
{
foreach(int i, messageIDs)
{
GETSTELMODULE(LabelMgr)->deleteLabel(i);
}
}
 End of changes. 23 change blocks. 
19 lines changed or deleted 89 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/