109
123
StelFileMgr::makeSureDirExistsAndIsWritable(StelFileMgr::getUserDir()+"/modules/Supernovae");
125
// If no settings in the main config file, create with defaults
126
if (!conf->childGroups().contains("Supernovae"))
128
qDebug() << "Supernovae::init no Supernovae section exists in main config file - creating with defaults";
129
restoreDefaultConfigIni();
132
// populate settings from main config file.
133
readSettingsFromConfig();
111
135
sneJsonPath = StelFileMgr::findFile("modules/Supernovae", (StelFileMgr::Flags)(StelFileMgr::Directory|StelFileMgr::Writable)) + "/supernovae.json";
113
137
texPointer = StelApp::getInstance().getTextureManager().createTexture("textures/pointeur2.png");
139
// key bindings and other actions
140
// TRANSLATORS: Title of a group of key bindings in the Help window
141
QString groupName = N_("Plugin Key Bindings");
142
StelGui* gui = dynamic_cast<StelGui*>(StelApp::getInstance().getGui());
143
gui->addGuiActions("actionShow_Supernovae_ConfigDialog", N_("Historical Supernovae configuration window"), "", groupName, true);
145
connect(gui->getGuiActions("actionShow_Supernovae_ConfigDialog"), SIGNAL(toggled(bool)), configDialog, SLOT(setVisible(bool)));
146
connect(configDialog, SIGNAL(visibleChanged(bool)), gui->getGuiActions("actionShow_Supernovae_ConfigDialog"), SLOT(setChecked(bool)));
115
148
catch (std::runtime_error &e)
397
451
return SupernovaP();
454
bool Supernovae::configureGui(bool show)
458
StelGui* gui = dynamic_cast<StelGui*>(StelApp::getInstance().getGui());
459
gui->getGuiActions("actionShow_Supernovae_ConfigDialog")->setChecked(true);
465
void Supernovae::restoreDefaults(void)
467
restoreDefaultConfigIni();
468
restoreDefaultJsonFile();
470
readSettingsFromConfig();
473
void Supernovae::restoreDefaultConfigIni(void)
475
conf->beginGroup("Supernovae");
477
// delete all existing Supernovae settings...
480
conf->setValue("updates_enabled", true);
481
conf->setValue("url", "http://stellarium.org/json/supernovae.json");
482
conf->setValue("update_frequency_days", 100);
486
void Supernovae::readSettingsFromConfig(void)
488
conf->beginGroup("Supernovae");
490
updateUrl = conf->value("url", "http://stellarium.org/json/supernovae.json").toString();
491
updateFrequencyDays = conf->value("update_frequency_days", 100).toInt();
492
lastUpdate = QDateTime::fromString(conf->value("last_update", "2012-06-11T12:00:00").toString(), Qt::ISODate);
493
updatesEnabled = conf->value("updates_enabled", true).toBool();
498
void Supernovae::saveSettingsToConfig(void)
500
conf->beginGroup("Supernovae");
502
conf->setValue("url", updateUrl);
503
conf->setValue("update_frequency_days", updateFrequencyDays);
504
conf->setValue("updates_enabled", updatesEnabled );
509
int Supernovae::getSecondsToUpdate(void)
511
QDateTime nextUpdate = lastUpdate.addSecs(updateFrequencyDays * 3600 * 24);
512
return QDateTime::currentDateTime().secsTo(nextUpdate);
515
void Supernovae::checkForUpdate(void)
517
if (updatesEnabled && lastUpdate.addSecs(updateFrequencyDays * 3600 * 24) <= QDateTime::currentDateTime())
521
void Supernovae::updateJSON(void)
523
if (updateState==Supernovae::Updating)
525
qWarning() << "Supernovae: already updating... will not start again current update is complete.";
530
qDebug() << "Supernovae: starting update...";
533
lastUpdate = QDateTime::currentDateTime();
534
conf->setValue("Supernovae/last_update", lastUpdate.toString(Qt::ISODate));
536
emit(jsonUpdateComplete());
538
updateState = Supernovae::Updating;
540
emit(updateStateChanged(updateState));
543
if (progressBar==NULL)
544
progressBar = StelApp::getInstance().getGui()->addProgressBar();
546
progressBar->setValue(0);
547
progressBar->setMaximum(updateUrl.size());
548
progressBar->setVisible(true);
549
progressBar->setFormat("Update historical supernovae");
551
QNetworkRequest request;
552
request.setUrl(QUrl(updateUrl));
553
request.setRawHeader("User-Agent", QString("Mozilla/5.0 (Stellarium Historical Supernovae Plugin %1; http://stellarium.org/)").arg(SUPERNOVAE_PLUGIN_VERSION).toUtf8());
554
downloadMgr->get(request);
556
progressBar->setValue(100);
560
updateState = CompleteUpdates;
562
emit(updateStateChanged(updateState));
563
emit(jsonUpdateComplete());
566
void Supernovae::updateDownloadComplete(QNetworkReply* reply)
568
// check the download worked, and save the data to file if this is the case.
569
if (reply->error() != QNetworkReply::NoError)
571
qWarning() << "Supernovae::updateDownloadComplete FAILED to download" << reply->url() << " Error: " << reply->errorString();
575
// download completed successfully.
578
QString jsonFilePath = StelFileMgr::findFile("modules/Supernovae", StelFileMgr::Flags(StelFileMgr::Writable|StelFileMgr::Directory)) + "/supernovae.json";
579
QFile jsonFile(jsonFilePath);
580
if (jsonFile.exists())
583
jsonFile.open(QIODevice::WriteOnly | QIODevice::Text);
584
jsonFile.write(reply->readAll());
587
catch (std::runtime_error &e)
589
qWarning() << "Supernovae::updateDownloadComplete: cannot write JSON data to file:" << e.what();
595
progressBar->setValue(100);
598
void Supernovae::displayMessage(const QString& message, const QString hexColor)
600
messageIDs << GETSTELMODULE(LabelMgr)->labelScreen(message, 30, 30 + (20*messageIDs.count()), true, 16, hexColor);
601
messageTimer->start();
604
void Supernovae::messageTimeout(void)
606
foreach(int i, messageIDs)
608
GETSTELMODULE(LabelMgr)->deleteLabel(i);
612
QString Supernovae::getSupernovaeList()
614
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")};
616
int year, month, day;
617
QList<double> vals = snlist.values();
619
foreach(double val, vals)
621
StelUtils::getDateFromJulianDay(val, &year, &month, &day);
622
out << QString("%1 (%2 %3)").arg(snlist.key(val)).arg(day).arg(smonth[month-1]);
625
return out.join(", ");