~ubuntu-branches/debian/experimental/stellarium/experimental

« back to all changes in this revision

Viewing changes to plugins/Supernovae/src/Supernovae.cpp

  • Committer: Package Import Robot
  • Author(s): Tomasz Buchert
  • Date: 2012-09-02 16:40:25 UTC
  • mfrom: (1.2.9)
  • Revision ID: package-import@ubuntu.com-20120902164025-wcm0g7j3uq09557q
Tags: 0.11.4a-1
* Imported Upstream version 0.11.4a
* Add 'DM-Upload-Allowed: yes' to debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include "StelApp.hpp"
22
22
#include "StelCore.hpp"
23
23
#include "StelGui.hpp"
 
24
#include "StelGuiItems.hpp"
24
25
#include "StelLocaleMgr.hpp"
25
26
#include "StelModuleMgr.hpp"
26
27
#include "StelObjectMgr.hpp"
29
30
#include "StelFileMgr.hpp"
30
31
#include "StelUtils.hpp"
31
32
#include "StelTranslator.hpp"
 
33
#include "LabelMgr.hpp"
 
34
#include "Supernova.hpp"
32
35
#include "Supernovae.hpp"
33
 
#include "Supernova.hpp"
 
36
#include "SupernovaeDialog.hpp"
34
37
 
 
38
#include <QNetworkAccessManager>
 
39
#include <QNetworkReply>
 
40
#include <QKeyEvent>
 
41
#include <QAction>
 
42
#include <QProgressBar>
35
43
#include <QDebug>
36
44
#include <QFileInfo>
37
45
#include <QFile>
 
46
#include <QTimer>
38
47
#include <QVariantMap>
39
48
#include <QVariant>
40
49
#include <QList>
41
50
#include <QSharedPointer>
42
51
#include <QStringList>
43
52
 
 
53
#define CATALOG_FORMAT_VERSION 1 /* Version of format of catalog */
 
54
 
44
55
/*
45
56
 This method is the one called automatically by the StelModuleMgr just 
46
57
 after loading the dynamic library
59
70
        info.displayedName = N_("Historical Supernovae");
60
71
        info.authors = "Alexander Wolf";
61
72
        info.contact = "alex.v.wolf@gmail.com";
62
 
        info.description = N_("A plugin that shows some historical supernovae brighter than 10 visual magnitude: SN 185A (7 December), SN 386A (24 April), 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 August), SN 1895B (5 July), SN 1937C (21 August), SN 1972E (8 May), SN 1987A (24 February) and SN 2011FE (13 September).");
 
73
        info.description = N_("A plugin that shows some historical supernovae brighter than 10 visual magnitude.");
63
74
        return info;
64
75
}
65
76
 
70
81
 Constructor
71
82
*/
72
83
Supernovae::Supernovae()
 
84
        : progressBar(NULL)
73
85
{
74
86
        setObjectName("Supernovae");
75
 
        font.setPixelSize(StelApp::getInstance().getSettings()->value("gui/base_font_size", 13).toInt());
 
87
        configDialog = new SupernovaeDialog();
 
88
        conf = StelApp::getInstance().getSettings();
 
89
        font.setPixelSize(conf->value("gui/base_font_size", 13).toInt());
76
90
}
77
91
 
78
92
/*
80
94
*/
81
95
Supernovae::~Supernovae()
82
96
{
83
 
        //
 
97
        delete configDialog;
84
98
}
85
99
 
86
100
void Supernovae::deinit()
108
122
        {
109
123
                StelFileMgr::makeSureDirExistsAndIsWritable(StelFileMgr::getUserDir()+"/modules/Supernovae");
110
124
 
 
125
                // If no settings in the main config file, create with defaults
 
126
                if (!conf->childGroups().contains("Supernovae"))
 
127
                {
 
128
                        qDebug() << "Supernovae::init no Supernovae section exists in main config file - creating with defaults";
 
129
                        restoreDefaultConfigIni();
 
130
                }
 
131
 
 
132
                // populate settings from main config file.
 
133
                readSettingsFromConfig();
 
134
 
111
135
                sneJsonPath = StelFileMgr::findFile("modules/Supernovae", (StelFileMgr::Flags)(StelFileMgr::Directory|StelFileMgr::Writable)) + "/supernovae.json";
112
136
 
113
137
                texPointer = StelApp::getInstance().getTextureManager().createTexture("textures/pointeur2.png");
 
138
 
 
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);
 
144
 
 
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)));
114
147
        }
115
148
        catch (std::runtime_error &e)
116
149
        {
118
151
                return;
119
152
        }
120
153
 
 
154
        // A timer for hiding alert messages
 
155
        messageTimer = new QTimer(this);
 
156
        messageTimer->setSingleShot(true);   // recurring check for update
 
157
        messageTimer->setInterval(9000);      // 6 seconds should be enough time
 
158
        messageTimer->stop();
 
159
        connect(messageTimer, SIGNAL(timeout()), this, SLOT(messageTimeout()));
 
160
 
121
161
        // If the json file does not already exist, create it from the resource in the Qt resource
122
162
        if(QFileInfo(sneJsonPath).exists())
123
163
        {
124
 
                if (getJsonFileVersion() != PLUGIN_VERSION)
 
164
                if (getJsonFileVersion() < CATALOG_FORMAT_VERSION)
125
165
                {
126
166
                        restoreDefaultJsonFile();
127
167
                }
136
176
 
137
177
        readJsonFile();
138
178
 
 
179
        // Set up download manager and the update schedule
 
180
        downloadMgr = new QNetworkAccessManager(this);
 
181
        connect(downloadMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(updateDownloadComplete(QNetworkReply*)));
 
182
        updateState = CompleteNoUpdates;
 
183
        updateTimer = new QTimer(this);
 
184
        updateTimer->setSingleShot(false);   // recurring check for update
 
185
        updateTimer->setInterval(13000);     // check once every 13 seconds to see if it is time for an update
 
186
        connect(updateTimer, SIGNAL(timeout()), this, SLOT(checkForUpdate()));
 
187
        updateTimer->start();
 
188
 
139
189
        GETSTELMODULE(StelObjectMgr)->registerStelObjectMgr(this);
140
190
}
141
191
 
275
325
                // is writable by the Stellarium process so that updates can be done.
276
326
                QFile dest(sneJsonPath);
277
327
                dest.setPermissions(dest.permissions() | QFile::WriteOwner);
 
328
 
 
329
                // Make sure that in the case where an online update has previously been done, but
 
330
                // the json file has been manually removed, that an update is schreduled in a timely
 
331
                // manner
 
332
                conf->remove("Supernovae/last_update");
 
333
                lastUpdate = QDateTime::fromString("2012-05-24T12:00:00", Qt::ISODate);
278
334
        }
279
335
}
280
336
 
347
403
void Supernovae::setSNeMap(const QVariantMap& map)
348
404
{
349
405
        snstar.clear();
 
406
        snlist.clear();
350
407
        QVariantMap sneMap = map.value("supernova").toMap();
351
408
        foreach(QString sneKey, sneMap.keys())
352
409
        {
353
410
                QVariantMap sneData = sneMap.value(sneKey).toMap();
354
411
                sneData["designation"] = QString("SN %1").arg(sneKey);
355
412
 
 
413
                snlist.insert(sneData.value("designation").toString(), sneData.value("peakJD").toDouble());
 
414
 
356
415
                SupernovaP sn(new Supernova(sneData));
357
416
                if (sn->initialized)
358
417
                        snstar.append(sn);
360
419
        }
361
420
}
362
421
 
363
 
const QString Supernovae::getJsonFileVersion(void)
364
 
{
365
 
        QString jsonVersion("unknown");
 
422
int Supernovae::getJsonFileVersion(void)
 
423
{       
 
424
        int jsonVersion = -1;
366
425
        QFile sneJsonFile(sneJsonPath);
367
426
        if (!sneJsonFile.open(QIODevice::ReadOnly))
368
427
        {
374
433
        map = StelJsonParser::parse(&sneJsonFile).toMap();
375
434
        if (map.contains("version"))
376
435
        {
377
 
                QString creator = map.value("version").toString();
378
 
                QRegExp vRx(".*(\\d+\\.\\d+\\.\\d+).*");
379
 
                if (vRx.exactMatch(creator))
380
 
                {
381
 
                        jsonVersion = vRx.capturedTexts().at(1);
382
 
                }
 
436
                jsonVersion = map.value("version").toInt();
383
437
        }
384
438
 
385
439
        sneJsonFile.close();
396
450
        }
397
451
        return SupernovaP();
398
452
}
 
453
 
 
454
bool Supernovae::configureGui(bool show)
 
455
{
 
456
        if (show)
 
457
        {
 
458
                StelGui* gui = dynamic_cast<StelGui*>(StelApp::getInstance().getGui());
 
459
                gui->getGuiActions("actionShow_Supernovae_ConfigDialog")->setChecked(true);
 
460
        }
 
461
 
 
462
        return true;
 
463
}
 
464
 
 
465
void Supernovae::restoreDefaults(void)
 
466
{
 
467
        restoreDefaultConfigIni();
 
468
        restoreDefaultJsonFile();
 
469
        readJsonFile();
 
470
        readSettingsFromConfig();
 
471
}
 
472
 
 
473
void Supernovae::restoreDefaultConfigIni(void)
 
474
{
 
475
        conf->beginGroup("Supernovae");
 
476
 
 
477
        // delete all existing Supernovae settings...
 
478
        conf->remove("");
 
479
 
 
480
        conf->setValue("updates_enabled", true);
 
481
        conf->setValue("url", "http://stellarium.org/json/supernovae.json");
 
482
        conf->setValue("update_frequency_days", 100);
 
483
        conf->endGroup();
 
484
}
 
485
 
 
486
void Supernovae::readSettingsFromConfig(void)
 
487
{
 
488
        conf->beginGroup("Supernovae");
 
489
 
 
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();
 
494
 
 
495
        conf->endGroup();
 
496
}
 
497
 
 
498
void Supernovae::saveSettingsToConfig(void)
 
499
{
 
500
        conf->beginGroup("Supernovae");
 
501
 
 
502
        conf->setValue("url", updateUrl);
 
503
        conf->setValue("update_frequency_days", updateFrequencyDays);
 
504
        conf->setValue("updates_enabled", updatesEnabled );
 
505
 
 
506
        conf->endGroup();
 
507
}
 
508
 
 
509
int Supernovae::getSecondsToUpdate(void)
 
510
{
 
511
        QDateTime nextUpdate = lastUpdate.addSecs(updateFrequencyDays * 3600 * 24);
 
512
        return QDateTime::currentDateTime().secsTo(nextUpdate);
 
513
}
 
514
 
 
515
void Supernovae::checkForUpdate(void)
 
516
{
 
517
        if (updatesEnabled && lastUpdate.addSecs(updateFrequencyDays * 3600 * 24) <= QDateTime::currentDateTime())
 
518
                updateJSON();
 
519
}
 
520
 
 
521
void Supernovae::updateJSON(void)
 
522
{
 
523
        if (updateState==Supernovae::Updating)
 
524
        {
 
525
                qWarning() << "Supernovae: already updating...  will not start again current update is complete.";
 
526
                return;
 
527
        }
 
528
        else
 
529
        {
 
530
                qDebug() << "Supernovae: starting update...";
 
531
        }
 
532
 
 
533
        lastUpdate = QDateTime::currentDateTime();
 
534
        conf->setValue("Supernovae/last_update", lastUpdate.toString(Qt::ISODate));
 
535
 
 
536
        emit(jsonUpdateComplete());
 
537
 
 
538
        updateState = Supernovae::Updating;
 
539
 
 
540
        emit(updateStateChanged(updateState));
 
541
        updateFile.clear();
 
542
 
 
543
        if (progressBar==NULL)
 
544
                progressBar = StelApp::getInstance().getGui()->addProgressBar();
 
545
 
 
546
        progressBar->setValue(0);
 
547
        progressBar->setMaximum(updateUrl.size());
 
548
        progressBar->setVisible(true);
 
549
        progressBar->setFormat("Update historical supernovae");
 
550
 
 
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);
 
555
 
 
556
        progressBar->setValue(100);
 
557
        delete progressBar;
 
558
        progressBar = NULL;
 
559
 
 
560
        updateState = CompleteUpdates;
 
561
 
 
562
        emit(updateStateChanged(updateState));
 
563
        emit(jsonUpdateComplete());
 
564
}
 
565
 
 
566
void Supernovae::updateDownloadComplete(QNetworkReply* reply)
 
567
{
 
568
        // check the download worked, and save the data to file if this is the case.
 
569
        if (reply->error() != QNetworkReply::NoError)
 
570
        {
 
571
                qWarning() << "Supernovae::updateDownloadComplete FAILED to download" << reply->url() << " Error: " << reply->errorString();
 
572
        }
 
573
        else
 
574
        {
 
575
                // download completed successfully.
 
576
                try
 
577
                {
 
578
                        QString jsonFilePath = StelFileMgr::findFile("modules/Supernovae", StelFileMgr::Flags(StelFileMgr::Writable|StelFileMgr::Directory)) + "/supernovae.json";
 
579
                        QFile jsonFile(jsonFilePath);
 
580
                        if (jsonFile.exists())
 
581
                                jsonFile.remove();
 
582
 
 
583
                        jsonFile.open(QIODevice::WriteOnly | QIODevice::Text);
 
584
                        jsonFile.write(reply->readAll());
 
585
                        jsonFile.close();
 
586
                }
 
587
                catch (std::runtime_error &e)
 
588
                {
 
589
                        qWarning() << "Supernovae::updateDownloadComplete: cannot write JSON data to file:" << e.what();
 
590
                }
 
591
 
 
592
        }
 
593
 
 
594
        if (progressBar)
 
595
                progressBar->setValue(100);
 
596
}
 
597
 
 
598
void Supernovae::displayMessage(const QString& message, const QString hexColor)
 
599
{
 
600
        messageIDs << GETSTELMODULE(LabelMgr)->labelScreen(message, 30, 30 + (20*messageIDs.count()), true, 16, hexColor);
 
601
        messageTimer->start();
 
602
}
 
603
 
 
604
void Supernovae::messageTimeout(void)
 
605
{
 
606
        foreach(int i, messageIDs)
 
607
        {
 
608
                GETSTELMODULE(LabelMgr)->deleteLabel(i);
 
609
        }
 
610
}
 
611
 
 
612
QString Supernovae::getSupernovaeList()
 
613
{
 
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")};
 
615
        QStringList out;
 
616
        int year, month, day;
 
617
        QList<double> vals = snlist.values();
 
618
        qSort(vals);
 
619
        foreach(double val, vals)
 
620
        {
 
621
                StelUtils::getDateFromJulianDay(val, &year, &month, &day);
 
622
                out << QString("%1 (%2 %3)").arg(snlist.key(val)).arg(day).arg(smonth[month-1]);
 
623
        }
 
624
 
 
625
        return out.join(", ");
 
626
}