From f9ac9d207025fb8d40d1be753cde78beb77aa202 Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Wed, 15 Aug 2012 23:52:30 +0200 Subject: meecast plugin --- meecastweather/meecastprovider.cpp | 2 +- meecastweather/meecastweather.cpp | 311 ++++++++++++++++++------------------- meecastweather/meecastweather.h | 19 ++- 3 files changed, 168 insertions(+), 164 deletions(-) diff --git a/meecastweather/meecastprovider.cpp b/meecastweather/meecastprovider.cpp index 0da042a..aaecb8b 100644 --- a/meecastweather/meecastprovider.cpp +++ b/meecastweather/meecastprovider.cpp @@ -20,7 +20,7 @@ MeeCastProvider::~MeeCastProvider() void MeeCastProvider::generateNotification() { - if (QFile::exists(MeeCastWeather::configFilePath())) { + if (QFile::exists(MeeCastWeather::configFilePath)) { emit incomingNotification(new MeeCastWeather(this)); } } diff --git a/meecastweather/meecastweather.cpp b/meecastweather/meecastweather.cpp index 407e359..0263a82 100644 --- a/meecastweather/meecastweather.cpp +++ b/meecastweather/meecastweather.cpp @@ -1,30 +1,30 @@ #include #include +#include #include "meecastweather.h" using namespace sowatch; +const QLatin1String MeeCastWeather::configFilePath("/home/user/.config/com.meecast.omweather/config.xml"); + MeeCastWeather::MeeCastWeather(QObject *parent) : WeatherNotification(parent), _watcher(new QFileSystemWatcher(this)), _timer(new QTimer(this)), + _configFileChanged(true), _stationFileChanged(true), _lastUpdate(QDateTime::fromTime_t(0)) { - _watcher->addPath(configFilePath()); + _watcher->addPath(configFilePath); connect(_watcher, SIGNAL(fileChanged(QString)), SLOT(fileChanged(QString))); - _timer->setInterval(5000); + _timer->setInterval(10000); _timer->setSingleShot(true); - connect(_timer, SIGNAL(timeout()), SLOT(update())); + connect(_timer, SIGNAL(timeout()), SLOT(handleTimeout())); // Perform an initial update - update(); -} - -QString MeeCastWeather::configFilePath() -{ - return QDir::home().absoluteFilePath(".config/com.meecast.omweather/config.xml"); + parseConfigFile(); + parseStationFile(); } Notification::Type MeeCastWeather::type() const @@ -44,152 +44,80 @@ QDateTime MeeCastWeather::dateTime() const QString MeeCastWeather::title() const { - return _lastLocation; + return _location; } QString MeeCastWeather::body() const { - switch (_lastWxCode) { - case 1: - case 2: - return tr("Sunny"); - - case 3: - case 4: - case 5: - return tr("Partly cloudy"); - case 6: - return tr("Mostly cloudy"); - case 7: - case 8: - return tr("Cloudy"); - - case 11: - return tr("Fog"); + return _lastText; +} - case 12: - return tr("Light rain"); - case 13: - case 14: - return tr("Light rain with sun"); - case 18: +WeatherNotification::WeatherType MeeCastWeather::forecast() +{ + switch (_lastCode) { + // Day versions + case 23: + case 24: + case 25: + case 32: + case 36: + return Sunny; + case 30: + case 34: + case 44: + return PartlyCloudy; case 26: - return tr("Heavy rain"); - - case 15: - case 16: - return tr("Thunderstorm"); - case 17: - return tr("Thunderstorm with sun"); - + case 28: + return Cloudy; case 19: - return tr("Light snow"); case 20: case 21: - return tr("Light snow with sun"); case 22: - return tr("Heavy snow"); - case 29: - return tr("Heavy rain and snow"); - - case 25: - return tr("Blizzard"); - - case 30: - return tr("Hot"); - case 31: - return tr("Cold"); - case 32: - return tr("Wind"); - - - // Night versions - case 33: - case 34: - return tr("Clear"); - case 35: - return tr("Partly cloudy"); - case 36: - case 37: - return tr("Mostly cloudy"); - case 38: - return tr("Cloudy"); - case 39: - case 40: - return tr("Light rain"); - case 41: - case 42: - return tr("Thunderstorm"); - case 43: - return tr("Light snow"); - case 44: - return tr("Heavy snow"); - - default: - return QString("? %1").arg(_lastWxCode); - } -} - -WeatherNotification::WeatherType MeeCastWeather::forecast() -{ - switch (_lastWxCode) { + return Fog; case 1: case 2: - return Sunny; - + case 9: + case 10: + case 11: + case 12: + case 39: + case 40: + return Rain; + case 0: case 3: case 4: + case 17: + case 35: + case 37: + case 38: + return Thunderstorm; case 5: - return PartlyCloudy; - case 6: case 7: case 8: - return Cloudy; - - case 11: - return Fog; - - case 12: case 13: case 14: - case 18: - case 26: - return Rain; - case 15: case 16: - case 17: - return Thunderstorm; - - case 19: - case 20: - case 21: - case 22: - case 23: - case 24: - case 25: - case 29: + case 18: + case 41: + case 42: + case 43: return Snow; // Night versions - case 33: - case 34: + case 31: return Sunny; - case 35: + case 29: + case 33: return PartlyCloudy; - case 36: - case 37: - case 38: + case 27: return Cloudy; - case 39: - case 40: + case 45: return Rain; - case 41: - case 42: + case 47: return Thunderstorm; - case 43: - case 44: + case 46: return Snow; default: @@ -204,7 +132,7 @@ int MeeCastWeather::temperature() WeatherNotification::Unit MeeCastWeather::temperatureUnits() { - return _metric ? Celsius : Fahrenheit; + return _tempUnit; } void MeeCastWeather::activate() @@ -219,53 +147,120 @@ void MeeCastWeather::dismiss() void MeeCastWeather::fileChanged(const QString &path) { - Q_UNUSED(path); qDebug() << "meecast config file changed: " << path; + if (path == configFilePath) { + _configFileChanged = true; + } else if (path == _stationFilePath) { + _stationFileChanged = true; + } _timer->start(); } -void MeeCastWeather::update() +void MeeCastWeather::parseConfigFile() { -#if 0 - qDebug() << "reading accuweather config file"; + QFile file(configFilePath); - QDateTime lastUpdate = s->value("LastUpdate").toDateTime(); - if (lastUpdate > _lastUpdate) { - _lastUpdate = lastUpdate; - bool anythingChanged = false; + if (!_stationFilePath.isEmpty()) { + _watcher->removePath(_stationFilePath); + } + _stationFilePath.clear(); - qDebug() << "reading weather info at" << _lastUpdate; + if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { + QDomDocument doc; + doc.setContent(&file); - bool useMetric = s->value("useMetric").toBool(); - if (useMetric != _metric) { - _metric = useMetric; - anythingChanged = true; - } + QDomElement root = doc.documentElement(); + QDomElement e = root.firstChildElement("current_station_id"); + int stationId = e.text().toInt(); - int temp = s->value("LastTemp").toInt(); - if (_lastTemp != temp) { - _lastTemp = temp; - anythingChanged = true; + e = root.firstChildElement("temperature_unit"); + if (e.text() == "C") { + _tempUnit = Celsius; + } else if (e.text() == "F") { + _tempUnit = Fahrenheit; } - QString location = s->value("LastLocation").toString(); - if (_lastLocation != location) { - _lastLocation = location; - anythingChanged = true; - } + QDomNodeList list = root.elementsByTagName("station"); + if (stationId >= 0 && stationId < list.size()) { + QDomNode n = list.item(stationId); + QDomElement e = n.toElement().firstChildElement("file_name"); - int wxCode = s->value("LastWxCode").toInt(); - if (_lastWxCode != wxCode) { - _lastWxCode = wxCode; - anythingChanged = true; + _stationFilePath = e.text(); + _watcher->addPath(_stationFilePath); + qDebug() << "meecast found station file path: " << _stationFilePath; + + _location = n.toElement().firstChildElement("station_name").text(); } + } - if (anythingChanged) { - qDebug() << "weather info changed wxcode=" << wxCode; - emit changed(); + _configFileChanged = false; +} + +void MeeCastWeather::parseStationFile() +{ + if (!_stationFilePath.isEmpty()) { + QFile file(_stationFilePath); + if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { + QDomDocument doc; + doc.setContent(&file); + + qDebug() << "meecast reading weather info"; + + QDomElement root = doc.documentElement(); + QDomNodeList list = root.elementsByTagName("period"); + for (int index = 0; index < list.size(); index++) { + QDomElement e = list.item(index).toElement(); + if (e.attribute("current") == "true") { + qDebug() << "meecast current found"; + bool anythingChanged = false; + uint periodUnix = e.attribute("start").toUInt(); + QDateTime period = QDateTime::fromTime_t(periodUnix); + qDebug() << "period" << periodUnix << period; + if (_lastUpdate != period) { + anythingChanged = true; + _lastUpdate = period; + } + + int temp = e.firstChildElement("temperature").text().toInt(); + qDebug() << "temp" << temp; + if (temp != _lastTemp) { + anythingChanged = true; + _lastTemp = temp; + } + + int code = e.firstChildElement("icon").text().toInt(); + qDebug() << "code" << code; + if (code != _lastCode) { + anythingChanged = true; + _lastCode = code; + } + + QString text = e.firstChildElement("description").text(); + if (text != _lastText) { + anythingChanged = true; + _lastText = text; + } + + if (anythingChanged) { + qDebug() << "something changed"; + emit changed(); + } + + break; + } + } } } - delete s; -#endif + _stationFileChanged = false; +} + +void MeeCastWeather::handleTimeout() +{ + if (_configFileChanged) { + parseConfigFile(); + } + if (_stationFileChanged) { + parseStationFile(); + } } diff --git a/meecastweather/meecastweather.h b/meecastweather/meecastweather.h index 7bd1561..9526726 100644 --- a/meecastweather/meecastweather.h +++ b/meecastweather/meecastweather.h @@ -16,7 +16,7 @@ class MeeCastWeather : public WeatherNotification public: explicit MeeCastWeather(QObject *parent = 0); - static QString configFilePath(); + const static QLatin1String configFilePath; Type type() const; uint count() const; @@ -33,17 +33,26 @@ public: private slots: void fileChanged(const QString& path); - void update(); + void parseConfigFile(); + void parseStationFile(); + void handleTimeout(); private: QFileSystemWatcher* _watcher; QTimer* _timer; - bool _metric; + QString _stationFilePath; + + bool _configFileChanged; + bool _stationFileChanged; + + Unit _tempUnit; + QString _location; + QDateTime _lastUpdate; - QString _lastLocation; int _lastTemp; - int _lastWxCode; + int _lastCode; + QString _lastText; }; -- cgit v1.2.3