Pulsars.cpp   Pulsars.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 "Pulsars.hpp" #include "LabelMgr.hpp"
#include "Pulsar.hpp" #include "Pulsar.hpp"
#include "Pulsars.hpp"
#include "PulsarsDialog.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 2 /* 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* PulsarsStelPluginInterface::getStelModule() const StelModule* PulsarsStelPluginInterface::getStelModule() const
{ {
return new Pulsars(); return new Pulsars();
} }
StelPluginInfo PulsarsStelPluginInterface::getPluginInfo() const StelPluginInfo PulsarsStelPluginInterface::getPluginInfo() const
{ {
Q_INIT_RESOURCE(Pulsars); Q_INIT_RESOURCE(Pulsars);
StelPluginInfo info; StelPluginInfo info;
info.id = "Pulsars"; info.id = "Pulsars";
info.displayedName = N_("Pulsars"); info.displayedName = N_("Pulsars");
info.authors = "Alexander Wolf"; info.authors = "Alexander Wolf";
info.contact = "alex.v.wolf@gmail.com"; info.contact = "alex.v.wolf@gmail.com";
info.description = N_("This plugin plots the position of various pul sars, with object information about each one. Pulsar data is derived from ' Catalog of Pulsars' (Taylor+ 1995).<br><br>Note: pulsar identifiers have th e prefix \"PSR\""); info.description = N_("This plugin plots the position of various pul sars, with object information about each one.");
return info; return info;
} }
Q_EXPORT_PLUGIN2(Pulsars, PulsarsStelPluginInterface) Q_EXPORT_PLUGIN2(Pulsars, PulsarsStelPluginInterface)
/* /*
Constructor Constructor
*/ */
Pulsars::Pulsars() Pulsars::Pulsars()
: progressBar(NULL)
{ {
setObjectName("Pulsars"); setObjectName("Pulsars");
font.setPixelSize(StelApp::getInstance().getSettings()->value("gui/b configDialog = new PulsarsDialog();
ase_font_size", 13).toInt()); conf = StelApp::getInstance().getSettings();
font.setPixelSize(conf->value("gui/base_font_size", 13).toInt());
} }
/* /*
Destructor Destructor
*/ */
Pulsars::~Pulsars() Pulsars::~Pulsars()
{ {
// delete configDialog;
} }
void Pulsars::deinit() void Pulsars::deinit()
{ {
psr.clear(); psr.clear();
Pulsar::markerTexture.clear(); Pulsar::markerTexture.clear();
texPointer.clear(); texPointer.clear();
} }
/* /*
skipping to change at line 111 skipping to change at line 125
/* /*
Init our module Init our module
*/ */
void Pulsars::init() void Pulsars::init()
{ {
try try
{ {
StelFileMgr::makeSureDirExistsAndIsWritable(StelFileMgr::get UserDir()+"/modules/Pulsars"); StelFileMgr::makeSureDirExistsAndIsWritable(StelFileMgr::get UserDir()+"/modules/Pulsars");
jsonCatalogPath = StelFileMgr::findFile("modules/Pulsars", ( // 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("Pulsars"))
{
qDebug() << "Pulsars::init no Pulsars section exists
in main config file - creating with defaults";
restoreDefaultConfigIni();
}
// populate settings from main config file.
readSettingsFromConfig();
jsonCatalogPath = StelFileMgr::findFile("modules/Pulsars", (
StelFileMgr::Flags)(StelFileMgr::Directory|StelFileMgr::Writable)) + "/puls
ars.json";
texPointer = StelApp::getInstance().getTextureManager().crea teTexture("textures/pointeur2.png"); texPointer = StelApp::getInstance().getTextureManager().crea teTexture("textures/pointeur2.png");
Pulsar::markerTexture = StelApp::getInstance().getTextureMan ager().createTexture(":/Pulsars/pulsar.png"); Pulsar::markerTexture = StelApp::getInstance().getTextureMan ager().createTexture(":/Pulsars/pulsar.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_Pulsars_ConfigDialog", N_("Pu
lsars configuration window"), "", groupName, true);
connect(gui->getGuiActions("actionShow_Pulsars_ConfigDialog"
), SIGNAL(toggled(bool)), configDialog, SLOT(setVisible(bool)));
connect(configDialog, SIGNAL(visibleChanged(bool)), gui->get
GuiActions("actionShow_Pulsars_ConfigDialog"), SLOT(setChecked(bool)));
} }
catch (std::runtime_error &e) catch (std::runtime_error &e)
{ {
qWarning() << "Pulsars::init error: " << e.what(); qWarning() << "Pulsars::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(jsonCatalogPath).exists()) if(QFileInfo(jsonCatalogPath).exists())
{ {
if (getJsonFileVersion() != PULSARS_PLUGIN_VERSION) if (getJsonFileVersion() < CATALOG_FORMAT_VERSION)
{ {
restoreDefaultJsonFile(); restoreDefaultJsonFile();
} }
} }
else else
{ {
qDebug() << "Pulsars::init catalog.json does not exist - cop ying default file to " << jsonCatalogPath; qDebug() << "Pulsars::init pulsars.json does not exist - cop ying default file to " << jsonCatalogPath;
restoreDefaultJsonFile(); restoreDefaultJsonFile();
} }
qDebug() << "Pulsars::init using catalog.json file: " << jsonCatalog Path; qDebug() << "Pulsars::init using pulsars.json file: " << jsonCatalog 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 PSR in the main window Draw our module. This should print name of first PSR in the main window
*/ */
void Pulsars::draw(StelCore* core) void Pulsars::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 267 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 Pulsars::restoreDefaultJsonFile(void) void Pulsars::restoreDefaultJsonFile(void)
{ {
if (QFileInfo(jsonCatalogPath).exists()) if (QFileInfo(jsonCatalogPath).exists())
backupJsonFile(true); backupJsonFile(true);
QFile src(":/Pulsars/catalog.json"); QFile src(":/Pulsars/pulsars.json");
if (!src.copy(jsonCatalogPath)) if (!src.copy(jsonCatalogPath))
{ {
qWarning() << "Pulsars::restoreDefaultJsonFile cannot copy j son resource to " + jsonCatalogPath; qWarning() << "Pulsars::restoreDefaultJsonFile cannot copy j son resource to " + jsonCatalogPath;
} }
else else
{ {
qDebug() << "Pulsars::init copied default catalog.json to " << jsonCatalogPath; qDebug() << "Pulsars::init copied default pulsars.json to " << jsonCatalogPath;
// 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(jsonCatalogPath); QFile dest(jsonCatalogPath);
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("Pulsars/last_update");
lastUpdate = QDateTime::fromString("2012-05-24T12:00:00", Qt
::ISODate);
} }
} }
/* /*
Creates a backup of the catalog.json file called catalog.json.old Creates a backup of the pulsars.json file called pulsars.json.old
*/ */
bool Pulsars::backupJsonFile(bool deleteOriginal) bool Pulsars::backupJsonFile(bool deleteOriginal)
{ {
QFile old(jsonCatalogPath); QFile old(jsonCatalogPath);
if (!old.exists()) if (!old.exists())
{ {
qWarning() << "Pulsars::backupJsonFile no file to backup"; qWarning() << "Pulsars::backupJsonFile no file to backup";
return false; return false;
} }
QString backupPath = jsonCatalogPath + ".old"; QString backupPath = jsonCatalogPath + ".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() << "Pulsars::backupJsonFile WARNI NG - could not remove old catalog.json file"; qWarning() << "Pulsars::backupJsonFile WARNI NG - could not remove old pulsars.json file";
return false; return false;
} }
} }
} }
else else
{ {
qWarning() << "Pulsars::backupJsonFile WARNING - failed to c opy catalog.json to catalog.json.old"; qWarning() << "Pulsars::backupJsonFile WARNING - failed to c opy pulsars.json to pulsars.json.old";
return false; return false;
} }
return true; return true;
} }
/* /*
Read the JSON file and create list of pulsars. Read the JSON file and create list of pulsars.
*/ */
void Pulsars::readJsonFile(void) void Pulsars::readJsonFile(void)
skipping to change at line 364 skipping to change at line 420
QVariantMap psrData = psrMap.value(psrKey).toMap(); QVariantMap psrData = psrMap.value(psrKey).toMap();
psrData["designation"] = psrKey; psrData["designation"] = psrKey;
PulsarP pulsar(new Pulsar(psrData)); PulsarP pulsar(new Pulsar(psrData));
if (pulsar->initialized) if (pulsar->initialized)
psr.append(pulsar); psr.append(pulsar);
} }
} }
const QString Pulsars::getJsonFileVersion(void) int Pulsars::getJsonFileVersion(void)
{ {
QString jsonVersion("unknown"); int jsonVersion = -1;
QFile jsonPSRCatalogFile(jsonCatalogPath); QFile jsonPSRCatalogFile(jsonCatalogPath);
if (!jsonPSRCatalogFile.open(QIODevice::ReadOnly)) if (!jsonPSRCatalogFile.open(QIODevice::ReadOnly))
{ {
qWarning() << "Pulsars::init cannot open " << jsonCatalogPat h; qWarning() << "Pulsars::init cannot open " << jsonCatalogPat h;
return jsonVersion; return jsonVersion;
} }
QVariantMap map; QVariantMap map;
map = StelJsonParser::parse(&jsonPSRCatalogFile).toMap(); map = StelJsonParser::parse(&jsonPSRCatalogFile).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);
}
} }
jsonPSRCatalogFile.close(); jsonPSRCatalogFile.close();
qDebug() << "Pulsars::getJsonFileVersion() version from file:" << js onVersion; qDebug() << "Pulsars::getJsonFileVersion() version from file:" << js onVersion;
return jsonVersion; return jsonVersion;
} }
PulsarP Pulsars::getByID(const QString& id) PulsarP Pulsars::getByID(const QString& id)
{ {
foreach(const PulsarP& pulsar, psr) foreach(const PulsarP& pulsar, psr)
{ {
if (pulsar->initialized && pulsar->designation == id) if (pulsar->initialized && pulsar->designation == id)
return pulsar; return pulsar;
} }
return PulsarP(); return PulsarP();
} }
bool Pulsars::configureGui(bool show)
{
if (show)
{
StelGui* gui = dynamic_cast<StelGui*>(StelApp::getInstance()
.getGui());
gui->getGuiActions("actionShow_Pulsars_ConfigDialog")->setCh
ecked(true);
}
return true;
}
void Pulsars::restoreDefaults(void)
{
restoreDefaultConfigIni();
restoreDefaultJsonFile();
readJsonFile();
readSettingsFromConfig();
}
void Pulsars::restoreDefaultConfigIni(void)
{
conf->beginGroup("Pulsars");
// delete all existing Pulsars settings...
conf->remove("");
conf->setValue("distribution_enabled", false);
conf->setValue("updates_enabled", true);
conf->setValue("url", "http://stellarium.org/json/pulsars.json");
conf->setValue("update_frequency_days", 100);
conf->endGroup();
}
void Pulsars::readSettingsFromConfig(void)
{
conf->beginGroup("Pulsars");
updateUrl = conf->value("url", "http://stellarium.org/json/pulsars.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 Pulsars::saveSettingsToConfig(void)
{
conf->beginGroup("Pulsars");
conf->setValue("url", updateUrl);
conf->setValue("update_frequency_days", updateFrequencyDays);
conf->setValue("updates_enabled", updatesEnabled );
conf->setValue("distribution_enabled", distributionEnabled);
conf->endGroup();
}
int Pulsars::getSecondsToUpdate(void)
{
QDateTime nextUpdate = lastUpdate.addSecs(updateFrequencyDays * 3600
* 24);
return QDateTime::currentDateTime().secsTo(nextUpdate);
}
void Pulsars::checkForUpdate(void)
{
if (updatesEnabled && lastUpdate.addSecs(updateFrequencyDays * 3600
* 24) <= QDateTime::currentDateTime())
updateJSON();
}
void Pulsars::updateJSON(void)
{
if (updateState==Pulsars::Updating)
{
qWarning() << "Pulsars: already updating... will not start
again current update is complete.";
return;
}
else
{
qDebug() << "Pulsars: starting update...";
}
lastUpdate = QDateTime::currentDateTime();
conf->setValue("Pulsars/last_update", lastUpdate.toString(Qt::ISODat
e));
emit(jsonUpdateComplete());
updateState = Pulsars::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 pulsars");
QNetworkRequest request;
request.setUrl(QUrl(updateUrl));
request.setRawHeader("User-Agent", QString("Mozilla/5.0 (Stellarium
Pulsars Plugin %1; http://stellarium.org/)").arg(PULSARS_PLUGIN_VERSION).to
Utf8());
downloadMgr->get(request);
progressBar->setValue(100);
delete progressBar;
progressBar = NULL;
updateState = CompleteUpdates;
emit(updateStateChanged(updateState));
emit(jsonUpdateComplete());
}
void Pulsars::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() << "Pulsars::updateDownloadComplete FAILED to dow
nload" << reply->url() << " Error: " << reply->errorString();
}
else
{
// download completed successfully.
try
{
QString jsonFilePath = StelFileMgr::findFile("module
s/Pulsars", StelFileMgr::Flags(StelFileMgr::Writable|StelFileMgr::Directory
)) + "/pulsars.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() << "Pulsars::updateDownloadComplete: cann
ot write JSON data to file:" << e.what();
}
}
if (progressBar)
progressBar->setValue(100);
}
void Pulsars::displayMessage(const QString& message, const QString hexColor
)
{
messageIDs << GETSTELMODULE(LabelMgr)->labelScreen(message, 30, 30 +
(20*messageIDs.count()), true, 16, hexColor);
messageTimer->start();
}
void Pulsars::messageTimeout(void)
{
foreach(int i, messageIDs)
{
GETSTELMODULE(LabelMgr)->deleteLabel(i);
}
}
 End of changes. 27 change blocks. 
24 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/