summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJavier S. Pedro <maemo@javispedro.com>2012-08-25 20:18:38 +0200
committerJavier S. Pedro <maemo@javispedro.com>2012-08-25 20:18:38 +0200
commitd6d10c69ba454a6e555733815680acdfc0ee5402 (patch)
tree34b354ea35801e8a4e918b9687655aa3751a6884
parentd2e8f4fab19d76e2f00ddbaca0c78ee1276651a6 (diff)
downloadsowatch-d6d10c69ba454a6e555733815680acdfc0ee5402.tar.gz
sowatch-d6d10c69ba454a6e555733815680acdfc0ee5402.zip
fix meecast non-automatic C -> F conversion
-rw-r--r--libsowatch/weathernotification.cpp21
-rw-r--r--libsowatch/weathernotification.h6
-rw-r--r--meecastweather/meecastweather.cpp26
-rw-r--r--meecastweather/meecastweather.h3
4 files changed, 55 insertions, 1 deletions
diff --git a/libsowatch/weathernotification.cpp b/libsowatch/weathernotification.cpp
index fa82f3d..ddfd60b 100644
--- a/libsowatch/weathernotification.cpp
+++ b/libsowatch/weathernotification.cpp
@@ -6,3 +6,24 @@ WeatherNotification::WeatherNotification(QObject *parent) :
Notification(parent)
{
}
+
+qreal WeatherNotification::convertTemperature(qreal temp, Unit from, Unit to)
+{
+ if (from == to) {
+ return temp;
+ } else if (from == Celsius && to == Fahrenheit) {
+ return temp * (9.0f/5.0f) + 32.0f;
+ } else if (from == Fahrenheit && to == Celsius) {
+ return (temp - 32.0f) * (5.0f/9.0f);
+ }
+}
+
+
+int WeatherNotification::convertTemperature(int temp, Unit from, Unit to)
+{
+ if (from == to) {
+ return temp;
+ } else {
+ return qRound(convertTemperature(static_cast<qreal>(temp), from, to));
+ }
+}
diff --git a/libsowatch/weathernotification.h b/libsowatch/weathernotification.h
index de3152a..843a844 100644
--- a/libsowatch/weathernotification.h
+++ b/libsowatch/weathernotification.h
@@ -32,8 +32,14 @@ public:
virtual WeatherType forecast() = 0;
virtual int temperature() = 0;
virtual Unit temperatureUnits() = 0;
+
+ /** Quite useful helper function. */
+ static qreal convertTemperature(qreal temp, Unit from, Unit to);
+ static int convertTemperature(int temp, Unit from, Unit to);
};
+
+
}
#endif // WEATHERNOTIFICATION_H
diff --git a/meecastweather/meecastweather.cpp b/meecastweather/meecastweather.cpp
index 0263a82..fa62ab7 100644
--- a/meecastweather/meecastweather.cpp
+++ b/meecastweather/meecastweather.cpp
@@ -145,6 +145,11 @@ void MeeCastWeather::dismiss()
// Do nothing
}
+int MeeCastWeather::convertTemperatureToUserUnit(int temp, Unit unit)
+{
+ return convertTemperature(temp, unit, _tempUnit);
+}
+
void MeeCastWeather::fileChanged(const QString &path)
{
qDebug() << "meecast config file changed: " << path;
@@ -207,7 +212,24 @@ void MeeCastWeather::parseStationFile()
qDebug() << "meecast reading weather info";
QDomElement root = doc.documentElement();
- QDomNodeList list = root.elementsByTagName("period");
+ QDomNodeList list;
+ Unit tempUnit = _tempUnit;
+
+ list = root.elementsByTagName("units");
+ if (!list.isEmpty()) {
+ QDomElement units = list.at(0).toElement();
+ list = units.elementsByTagName("t");
+ if (!list.isEmpty()) {
+ QDomElement e = list.at(0).toElement();
+ if (e.text() == "C") {
+ tempUnit = Celsius;
+ } else if (e.text() == "F") {
+ tempUnit = Fahrenheit;
+ }
+ }
+ }
+
+ list = root.elementsByTagName("period");
for (int index = 0; index < list.size(); index++) {
QDomElement e = list.item(index).toElement();
if (e.attribute("current") == "true") {
@@ -223,6 +245,8 @@ void MeeCastWeather::parseStationFile()
int temp = e.firstChildElement("temperature").text().toInt();
qDebug() << "temp" << temp;
+ temp = convertTemperatureToUserUnit(temp, tempUnit);
+ qDebug() << " -> " << temp;
if (temp != _lastTemp) {
anythingChanged = true;
_lastTemp = temp;
diff --git a/meecastweather/meecastweather.h b/meecastweather/meecastweather.h
index 9526726..a1ac393 100644
--- a/meecastweather/meecastweather.h
+++ b/meecastweather/meecastweather.h
@@ -31,6 +31,9 @@ public:
void activate();
void dismiss();
+private:
+ int convertTemperatureToUserUnit(int temp, Unit unit);
+
private slots:
void fileChanged(const QString& path);
void parseConfigFile();