Supernovae.cpp   Supernovae.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 "Supernovae.hpp" #include "LabelMgr.hpp"
#include "Supernova.hpp" #include "Supernova.hpp"
#include "Supernovae.hpp"
#include "SupernovaeDialog.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* SupernovaeStelPluginInterface::getStelModule() const StelModule* SupernovaeStelPluginInterface::getStelModule() const
{ {
return new Supernovae(); return new Supernovae();
} }
StelPluginInfo SupernovaeStelPluginInterface::getPluginInfo() const StelPluginInfo SupernovaeStelPluginInterface::getPluginInfo() const
{ {
Q_INIT_RESOURCE(Supernovae); Q_INIT_RESOURCE(Supernovae);
StelPluginInfo info; StelPluginInfo info;
info.id = "Supernovae"; info.id = "Supernovae";
info.displayedName = N_("Historical Supernovae"); info.displayedName = N_("Historical Supernovae");
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_("A plugin that shows some historical supernova e brighter than 10 visual magnitude: SN 185A (7 December), SN 386A (24 Apri l), SN 1006A (29 April), SN 1054A (3 July), SN 1181A (4 August), SN 1572A ( 5 November), SN 1604A (8 October), SN 1680A (15 August), SN 1885A (17 Augus t), SN 1895B (5 July), SN 1937C (21 August), SN 1972E (8 May), SN 1987A (24 February) and SN 2011FE (13 September)."); info.description = N_("A plugin that shows some historical supernova e brighter than 10 visual magnitude.");
return info; return info;
} }
Q_EXPORT_PLUGIN2(Supernovae, SupernovaeStelPluginInterface) Q_EXPORT_PLUGIN2(Supernovae, SupernovaeStelPluginInterface)
/* /*
Constructor Constructor
*/ */
Supernovae::Supernovae() Supernovae::Supernovae()
: progressBar(NULL)
{ {
setObjectName("Supernovae"); setObjectName("Supernovae");
font.setPixelSize(StelApp::getInstance().getSettings()->value("gui/b configDialog = new SupernovaeDialog();
ase_font_size", 13).toInt()); conf = StelApp::getInstance().getSettings();
font.setPixelSize(conf->value("gui/base_font_size", 13).toInt());
} }
/* /*
Destructor Destructor
*/ */
Supernovae::~Supernovae() Supernovae::~Supernovae()
{ {
// delete configDialog;
} }
void Supernovae::deinit() void Supernovae::deinit()
{ {
texPointer.clear(); texPointer.clear();
} }
/* /*
Reimplementation of the getCallOrder method Reimplementation of the getCallOrder method
*/ */
skipping to change at line 109 skipping to change at line 123
/* /*
Init our module Init our module
*/ */
void Supernovae::init() void Supernovae::init()
{ {
try try
{ {
StelFileMgr::makeSureDirExistsAndIsWritable(StelFileMgr::get UserDir()+"/modules/Supernovae"); StelFileMgr::makeSureDirExistsAndIsWritable(StelFileMgr::get UserDir()+"/modules/Supernovae");
// If no settings in the main config file, create with defau
lts
if (!conf->childGroups().contains("Supernovae"))
{
qDebug() << "Supernovae::init no Supernovae section
exists in main config file - creating with defaults";
restoreDefaultConfigIni();
}
// populate settings from main config file.
readSettingsFromConfig();
sneJsonPath = StelFileMgr::findFile("modules/Supernovae", (S telFileMgr::Flags)(StelFileMgr::Directory|StelFileMgr::Writable)) + "/super novae.json"; sneJsonPath = StelFileMgr::findFile("modules/Supernovae", (S telFileMgr::Flags)(StelFileMgr::Directory|StelFileMgr::Writable)) + "/super novae.json";
texPointer = StelApp::getInstance().getTextureManager().crea teTexture("textures/pointeur2.png"); texPointer = StelApp::getInstance().getTextureManager().crea teTexture("textures/pointeur2.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_Supernovae_ConfigDialog", N_(
"Historical Supernovae configuration window"), "", groupName, true);
connect(gui->getGuiActions("actionShow_Supernovae_ConfigDial
og"), SIGNAL(toggled(bool)), configDialog, SLOT(setVisible(bool)));
connect(configDialog, SIGNAL(visibleChanged(bool)), gui->get
GuiActions("actionShow_Supernovae_ConfigDialog"), SLOT(setChecked(bool)));
} }
catch (std::runtime_error &e) catch (std::runtime_error &e)
{ {
qWarning() << "Supernovas::init error: " << e.what(); qWarning() << "Supernovas::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(sneJsonPath).exists()) if(QFileInfo(sneJsonPath).exists())
{ {
if (getJsonFileVersion() != PLUGIN_VERSION) if (getJsonFileVersion() < CATALOG_FORMAT_VERSION)
{ {
restoreDefaultJsonFile(); restoreDefaultJsonFile();
} }
} }
else else
{ {
qDebug() << "Supernovae::init supernovae.json does not exist - copying default file to " << sneJsonPath; qDebug() << "Supernovae::init supernovae.json does not exist - copying default file to " << sneJsonPath;
restoreDefaultJsonFile(); restoreDefaultJsonFile();
} }
qDebug() << "Supernovae::init using supernovae.json file: " << sneJs onPath; qDebug() << "Supernovae::init using supernovae.json file: " << sneJs onPath;
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 SNe in the main window Draw our module. This should print name of first SNe in the main window
*/ */
void Supernovae::draw(StelCore* core) void Supernovae::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 276 skipping to change at line 326
{ {
qWarning() << "Supernovae::restoreDefaultJsonFile cannot cop y json resource to " + sneJsonPath; qWarning() << "Supernovae::restoreDefaultJsonFile cannot cop y json resource to " + sneJsonPath;
} }
else else
{ {
qDebug() << "Supernovae::init copied default supernovae.json to " << sneJsonPath; qDebug() << "Supernovae::init copied default supernovae.json to " << sneJsonPath;
// 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(sneJsonPath); QFile dest(sneJsonPath);
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("Supernovae/last_update");
lastUpdate = QDateTime::fromString("2012-05-24T12:00:00", Qt
::ISODate);
} }
} }
/* /*
Creates a backup of the supernovae.json file called supernovae.json.old Creates a backup of the supernovae.json file called supernovae.json.old
*/ */
bool Supernovae::backupJsonFile(bool deleteOriginal) bool Supernovae::backupJsonFile(bool deleteOriginal)
{ {
QFile old(sneJsonPath); QFile old(sneJsonPath);
if (!old.exists()) if (!old.exists())
skipping to change at line 348 skipping to change at line 404
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
*/ */
void Supernovae::setSNeMap(const QVariantMap& map) void Supernovae::setSNeMap(const QVariantMap& map)
{ {
snstar.clear(); snstar.clear();
snlist.clear();
QVariantMap sneMap = map.value("supernova").toMap(); QVariantMap sneMap = map.value("supernova").toMap();
foreach(QString sneKey, sneMap.keys()) foreach(QString sneKey, sneMap.keys())
{ {
QVariantMap sneData = sneMap.value(sneKey).toMap(); QVariantMap sneData = sneMap.value(sneKey).toMap();
sneData["designation"] = QString("SN %1").arg(sneKey); sneData["designation"] = QString("SN %1").arg(sneKey);
snlist.insert(sneData.value("designation").toString(), sneDa
ta.value("peakJD").toDouble());
SupernovaP sn(new Supernova(sneData)); SupernovaP sn(new Supernova(sneData));
if (sn->initialized) if (sn->initialized)
snstar.append(sn); snstar.append(sn);
} }
} }
const QString Supernovae::getJsonFileVersion(void) int Supernovae::getJsonFileVersion(void)
{ {
QString jsonVersion("unknown"); int jsonVersion = -1;
QFile sneJsonFile(sneJsonPath); QFile sneJsonFile(sneJsonPath);
if (!sneJsonFile.open(QIODevice::ReadOnly)) if (!sneJsonFile.open(QIODevice::ReadOnly))
{ {
qWarning() << "Supernovae::init cannot open " << sneJsonPath ; qWarning() << "Supernovae::init cannot open " << sneJsonPath ;
return jsonVersion; return jsonVersion;
} }
QVariantMap map; QVariantMap map;
map = StelJsonParser::parse(&sneJsonFile).toMap(); map = StelJsonParser::parse(&sneJsonFile).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);
}
} }
sneJsonFile.close(); sneJsonFile.close();
qDebug() << "Supernovae::getJsonFileVersion() version from file:" << jsonVersion; qDebug() << "Supernovae::getJsonFileVersion() version from file:" << jsonVersion;
return jsonVersion; return jsonVersion;
} }
SupernovaP Supernovae::getByID(const QString& id) SupernovaP Supernovae::getByID(const QString& id)
{ {
foreach(const SupernovaP& sn, snstar) foreach(const SupernovaP& sn, snstar)
{ {
if (sn->initialized && sn->designation == id) if (sn->initialized && sn->designation == id)
return sn; return sn;
} }
return SupernovaP(); return SupernovaP();
} }
bool Supernovae::configureGui(bool show)
{
if (show)
{
StelGui* gui = dynamic_cast<StelGui*>(StelApp::getInstance()
.getGui());
gui->getGuiActions("actionShow_Supernovae_ConfigDialog")->se
tChecked(true);
}
return true;
}
void Supernovae::restoreDefaults(void)
{
restoreDefaultConfigIni();
restoreDefaultJsonFile();
readJsonFile();
readSettingsFromConfig();
}
void Supernovae::restoreDefaultConfigIni(void)
{
conf->beginGroup("Supernovae");
// delete all existing Supernovae settings...
conf->remove("");
conf->setValue("updates_enabled", true);
conf->setValue("url", "http://stellarium.org/json/supernovae.json");
conf->setValue("update_frequency_days", 100);
conf->endGroup();
}
void Supernovae::readSettingsFromConfig(void)
{
conf->beginGroup("Supernovae");
updateUrl = conf->value("url", "http://stellarium.org/json/supernova
e.json").toString();
updateFrequencyDays = conf->value("update_frequency_days", 100).toIn
t();
lastUpdate = QDateTime::fromString(conf->value("last_update", "2012-
06-11T12:00:00").toString(), Qt::ISODate);
updatesEnabled = conf->value("updates_enabled", true).toBool();
conf->endGroup();
}
void Supernovae::saveSettingsToConfig(void)
{
conf->beginGroup("Supernovae");
conf->setValue("url", updateUrl);
conf->setValue("update_frequency_days", updateFrequencyDays);
conf->setValue("updates_enabled", updatesEnabled );
conf->endGroup();
}
int Supernovae::getSecondsToUpdate(void)
{
QDateTime nextUpdate = lastUpdate.addSecs(updateFrequencyDays * 3600
* 24);
return QDateTime::currentDateTime().secsTo(nextUpdate);
}
void Supernovae::checkForUpdate(void)
{
if (updatesEnabled && lastUpdate.addSecs(updateFrequencyDays * 3600
* 24) <= QDateTime::currentDateTime())
updateJSON();
}
void Supernovae::updateJSON(void)
{
if (updateState==Supernovae::Updating)
{
qWarning() << "Supernovae: already updating... will not sta
rt again current update is complete.";
return;
}
else
{
qDebug() << "Supernovae: starting update...";
}
lastUpdate = QDateTime::currentDateTime();
conf->setValue("Supernovae/last_update", lastUpdate.toString(Qt::ISO
Date));
emit(jsonUpdateComplete());
updateState = Supernovae::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 historical supernovae");
QNetworkRequest request;
request.setUrl(QUrl(updateUrl));
request.setRawHeader("User-Agent", QString("Mozilla/5.0 (Stellarium
Historical Supernovae Plugin %1; http://stellarium.org/)").arg(SUPERNOVAE_P
LUGIN_VERSION).toUtf8());
downloadMgr->get(request);
progressBar->setValue(100);
delete progressBar;
progressBar = NULL;
updateState = CompleteUpdates;
emit(updateStateChanged(updateState));
emit(jsonUpdateComplete());
}
void Supernovae::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() << "Supernovae::updateDownloadComplete FAILED to
download" << reply->url() << " Error: " << reply->errorString();
}
else
{
// download completed successfully.
try
{
QString jsonFilePath = StelFileMgr::findFile("module
s/Supernovae", StelFileMgr::Flags(StelFileMgr::Writable|StelFileMgr::Direct
ory)) + "/supernovae.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() << "Supernovae::updateDownloadComplete: c
annot write JSON data to file:" << e.what();
}
}
if (progressBar)
progressBar->setValue(100);
}
void Supernovae::displayMessage(const QString& message, const QString hexCo
lor)
{
messageIDs << GETSTELMODULE(LabelMgr)->labelScreen(message, 30, 30 +
(20*messageIDs.count()), true, 16, hexColor);
messageTimer->start();
}
void Supernovae::messageTimeout(void)
{
foreach(int i, messageIDs)
{
GETSTELMODULE(LabelMgr)->deleteLabel(i);
}
}
QString Supernovae::getSupernovaeList()
{
QString smonth[] = {q_("January"), q_("February"), q_("March"), q_("
April"), q_("May"), q_("June"), q_("July"), q_("August"), q_("September"),
q_("October"), q_("November"), q_("December")};
QStringList out;
int year, month, day;
QList<double> vals = snlist.values();
qSort(vals);
foreach(double val, vals)
{
StelUtils::getDateFromJulianDay(val, &year, &month, &day);
out << QString("%1 (%2 %3)").arg(snlist.key(val)).arg(day).a
rg(smonth[month-1]);
}
return out.join(", ");
}
 End of changes. 22 change blocks. 
14 lines changed or deleted 83 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/