diff --git a/sources/awesomewidgets/abstractextitem.cpp b/sources/awesomewidgets/abstractextitem.cpp index 62e0e09..787acc0 100644 --- a/sources/awesomewidgets/abstractextitem.cpp +++ b/sources/awesomewidgets/abstractextitem.cpp @@ -178,7 +178,7 @@ void AbstractExtItem::setName(const QString _name) void AbstractExtItem::setNumber(int _number) { qCDebug(LOG_LIB) << "Number" << _number; - if (_number == -1) + if (_number == -1) { _number = []() { qCWarning(LOG_LIB) << "Number is empty, generate new one"; qsrand(QTime::currentTime().msec()); @@ -186,6 +186,8 @@ void AbstractExtItem::setNumber(int _number) qCInfo(LOG_LIB) << "Generated number is" << n; return n; }(); + writeConfiguration(); + } m_number = _number; } diff --git a/sources/awesomewidgets/abstractweatherprovider.h b/sources/awesomewidgets/abstractweatherprovider.h new file mode 100644 index 0000000..9582cac --- /dev/null +++ b/sources/awesomewidgets/abstractweatherprovider.h @@ -0,0 +1,47 @@ +/*************************************************************************** + * This file is part of awesome-widgets * + * * + * awesome-widgets is free software: you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * awesome-widgets is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with awesome-widgets. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ + +#ifndef ABSTRACTWEATHERPROVIDER_H +#define ABSTRACTWEATHERPROVIDER_H + +#include +#include + + +class AbstractWeatherProvider : public QObject +{ + Q_OBJECT + Q_PROPERTY(int number READ number) + +public: + explicit AbstractWeatherProvider(QObject *parent, const int number) + : QObject(parent) + , m_number(number){}; + virtual ~AbstractWeatherProvider(){}; + virtual void initUrl(const QString city, const QString country, + const int ts) + = 0; + virtual QVariantHash parse(const QVariantMap &json) const = 0; + virtual QUrl url() const = 0; + int number() const { return m_number; }; + +private: + int m_number; +}; + + +#endif /* ABSTRACTWEATHERPROVIDER_H */ diff --git a/sources/awesomewidgets/awesomewidgets-extweather-ids.json b/sources/awesomewidgets/awesomewidgets-extweather-ids.json index 69f9085..e8ec649 100644 --- a/sources/awesomewidgets/awesomewidgets-extweather-ids.json +++ b/sources/awesomewidgets/awesomewidgets-extweather-ids.json @@ -1,11 +1,75 @@ { - "__url": "https://developer.yahoo.com/weather/documentation.html", + "__url-yahoo": "https://developer.yahoo.com/weather/documentation.html", + "__url-owm": "http://openweathermap.org/weather-conditions", "image": { "__comment": "should be described as html image with full path inside", "default": "", + "800": "", + + "801": "", + + "802": "", + "803": "", + + "804": "", + + "300": "", + "301": "", + "302": "", + "310": "", + "311": "", + "312": "", + "313": "", + "314": "", + "321": "", + "520": "", + "521": "", + "522": "", + "531": "", + + "500": "", + "501": "", + "502": "", + "503": "", + "504": "", + + "200": "", + "201": "", + "202": "", + "210": "", + "211": "", + "212": "", + "221": "", + "230": "", + "231": "", + "232": "", + + "511": "", + "600": "", + "601": "", + "602": "", + "611": "", + "612": "", + "615": "", + "616": "", + "620": "", + "621": "", + "622": "", + + "701": "", + "711": "", + "721": "", + "731": "", + "741": "", + "751": "", + "761": "", + "762": "", + "771": "", + "781": "", + "0": "", "1": "", "2": "", @@ -59,8 +123,71 @@ "text": { "default": "\u2604", - "3200": "\u2604", + "800": "\u2600", + + "801": "\u26C5", + + "802": "\u2601", + "803": "\u2601", + + "804": "\u2601", + + "300": "\u2602", + "301": "\u2602", + "302": "\u2602", + "310": "\u2602", + "311": "\u2602", + "312": "\u2602", + "313": "\u2602", + "314": "\u2602", + "321": "\u2602", + "520": "\u2602", + "521": "\u2602", + "522": "\u2602", + "531": "\u2602", + + "500": "\u2614", + "501": "\u2614", + "502": "\u2614", + "503": "\u2614", + "504": "\u2614", + + "200": "\u2608", + "201": "\u2608", + "202": "\u2608", + "210": "\u2608", + "211": "\u2608", + "212": "\u2608", + "221": "\u2608", + "230": "\u2608", + "231": "\u2608", + "232": "\u2608", + + "511": "\u2603", + "600": "\u2603", + "601": "\u2603", + "602": "\u2603", + "611": "\u2603", + "612": "\u2603", + "615": "\u2603", + "616": "\u2603", + "620": "\u2603", + "621": "\u2603", + "622": "\u2603", + + "701": "\u26C5", + "711": "\u26C5", + "721": "\u26C5", + "731": "\u26C5", + "741": "\u26C5", + "751": "\u26C5", + "761": "\u26C5", + "762": "\u26C5", + "771": "\u26C5", + "781": "\u26C5", + + "3200": "\u2604", "0": "\u2604", "1": "\u2604", "2": "\u2604", diff --git a/sources/awesomewidgets/extscript.h b/sources/awesomewidgets/extscript.h index 1dca548..5189a63 100644 --- a/sources/awesomewidgets/extscript.h +++ b/sources/awesomewidgets/extscript.h @@ -37,7 +37,12 @@ class ExtScript : public AbstractExtItem Q_PROPERTY(Redirect redirect READ redirect WRITE setRedirect) public: - enum class Redirect { stdout2stderr = 0, nothing = 1, stderr2stdout = 2, swap = 3 }; + enum class Redirect { + stdout2stderr = 0, + nothing = 1, + stderr2stdout = 2, + swap = 3 + }; explicit ExtScript(QWidget *parent, const QString filePath = QString()); virtual ~ExtScript(); diff --git a/sources/awesomewidgets/extweather.cpp b/sources/awesomewidgets/extweather.cpp index 0aabd44..456d893 100644 --- a/sources/awesomewidgets/extweather.cpp +++ b/sources/awesomewidgets/extweather.cpp @@ -27,11 +27,12 @@ #include #include #include -#include #include #include "awdebug.h" +#include "owmweatherprovider.h" +#include "yahooweatherprovider.h" ExtWeather::ExtWeather(QWidget *parent, const QString filePath) @@ -68,6 +69,7 @@ ExtWeather::~ExtWeather() SLOT(weatherReplyReceived(QNetworkReply *))); m_manager->deleteLater(); + delete m_providerObject; delete ui; } @@ -83,6 +85,7 @@ ExtWeather *ExtWeather::copy(const QString _fileName, const int _number) item->setCountry(country()); item->setImage(image()); item->setNumber(_number); + item->setProvider(provider()); item->setTs(ts()); return item; @@ -117,6 +120,28 @@ bool ExtWeather::image() const } +ExtWeather::Provider ExtWeather::provider() const +{ + return m_provider; +} + + +QString ExtWeather::strProvider() const +{ + QString value; + switch (m_provider) { + case Provider::OWM: + value = QString("OWM"); + break; + case Provider::Yahoo: + value = QString("Yahoo"); + break; + } + + return value; +} + + int ExtWeather::ts() const { return m_ts; @@ -134,7 +159,7 @@ void ExtWeather::setCity(const QString _city) qCDebug(LOG_LIB) << "City" << _city; m_city = _city; - initUrl(); + initProvider(); } @@ -143,7 +168,7 @@ void ExtWeather::setCountry(const QString _country) qCDebug(LOG_LIB) << "Country" << _country; m_country = _country; - initUrl(); + initProvider(); } @@ -155,11 +180,32 @@ void ExtWeather::setImage(const bool _image) } +void ExtWeather::setProvider(const Provider _provider) +{ + qCDebug(LOG_LIB) << "Provider" << static_cast(_provider); + + m_provider = _provider; + initProvider(); +} + + +void ExtWeather::setStrProvider(const QString _provider) +{ + qCDebug(LOG_LIB) << "Provider" << _provider; + + if (_provider == QString("Yahoo")) + setProvider(Provider::Yahoo); + else + setProvider(Provider::OWM); +} + + void ExtWeather::setTs(const int _ts) { qCDebug(LOG_LIB) << "Timestamp" << _ts; m_ts = _ts; + initProvider(); } @@ -176,16 +222,11 @@ void ExtWeather::readConfiguration() // api == 2 setImage(settings.value(QString("X-AW-Image"), QVariant(m_image)).toString() == QString("true")); + // api == 3 + setStrProvider( + settings.value(QString("X-AW-Provider"), strProvider()).toString()); settings.endGroup(); - // update for current API - if ((apiVersion() > 0) && (apiVersion() < AWEWAPI)) { - qCWarning(LOG_LIB) << "Bump API version from" << apiVersion() << "to" - << AWEWAPI; - setApiVersion(AWEWAPI); - writeConfiguration(); - } - bumpApi(AWEWAPI); } @@ -224,7 +265,8 @@ QVariantHash ExtWeather::run() if (times == 1) { qCInfo(LOG_LIB) << "Send request"; isRunning = true; - QNetworkReply *reply = m_manager->get(QNetworkRequest(m_url)); + QNetworkReply *reply + = m_manager->get(QNetworkRequest(m_providerObject->url())); new QReplyTimeout(reply, REQUEST_TIMEOUT); } @@ -244,6 +286,7 @@ int ExtWeather::showConfiguration(const QVariant args) ui->lineEdit_name->setText(name()); ui->lineEdit_comment->setText(comment()); ui->label_numberValue->setText(QString("%1").arg(number())); + ui->comboBox_provider->setCurrentIndex(static_cast(m_provider)); ui->lineEdit_city->setText(m_city); ui->lineEdit_country->setText(m_country); ui->spinBox_timestamp->setValue(m_ts); @@ -261,6 +304,7 @@ int ExtWeather::showConfiguration(const QVariant args) setApiVersion(AWEWAPI); setCity(ui->lineEdit_city->text()); setCountry(ui->lineEdit_country->text()); + setProvider(static_cast(ui->comboBox_provider->currentIndex())); setTs(ui->spinBox_timestamp->value()); setImage(ui->checkBox_image->checkState() == Qt::Checked); setActive(ui->checkBox_active->checkState() == Qt::Checked); @@ -282,6 +326,7 @@ void ExtWeather::writeConfiguration() const settings.setValue(QString("X-AW-City"), m_city); settings.setValue(QString("X-AW-Country"), m_country); settings.setValue(QString("X-AW-Image"), m_image); + settings.setValue(QString("X-AW-Provider"), strProvider()); settings.setValue(QString("X-AW-TS"), m_ts); settings.endGroup(); @@ -304,69 +349,31 @@ void ExtWeather::weatherReplyReceived(QNetworkReply *reply) return; } - // convert to map - QVariantMap json = jsonDoc.toVariant().toMap()[QString("query")].toMap(); - if (json[QString("count")].toInt() != 1) { - qCWarning(LOG_LIB) << "Found data count" - << json[QString("count")].toInt() << "is not 1"; + QVariantHash data = m_providerObject->parse(jsonDoc.toVariant().toMap()); + if (data.isEmpty()) return; - } - QVariantMap results - = json[QString("results")].toMap()[QString("channel")].toMap(); - QVariantMap item = results[QString("item")].toMap(); - - if (m_ts == 0) { - // current weather - int id = item[QString("condition")].toMap()[QString("code")].toInt(); - values[tag(QString("weatherId"))] = id; - values[tag(QString("weather"))] = weatherFromInt(id); - values[tag(QString("temperature"))] - = item[QString("condition")].toMap()[QString("temp")].toInt(); - values[tag(QString("timestamp"))] - = item[QString("condition")].toMap()[QString("date")].toString(); - values[tag(QString("humidity"))] = results[QString("atmosphere")] - .toMap()[QString("humidity")] - .toInt(); - values[tag(QString("pressure"))] - = static_cast(results[QString("atmosphere")] - .toMap()[QString("pressure")] - .toFloat()); - } else { - // forecast weather - QVariantList weatherList = item[QString("forecast")].toList(); - QVariantMap weatherMap = weatherList.count() < m_ts - ? weatherList.last().toMap() - : weatherList.at(m_ts).toMap(); - int id = weatherMap[QString("code")].toInt(); - values[tag(QString("weatherId"))] = id; - values[tag(QString("weather"))] = weatherFromInt(id); - values[tag(QString("timestamp"))] - = weatherMap[QString("date")].toString(); - // yahoo provides high and low temperatures. Lets calculate average one - values[tag(QString("temperature"))] - = (weatherMap[QString("high")].toFloat() - + weatherMap[QString("low")].toFloat()) - / 2.0; - // ... and no forecast data for humidity and pressure - values[tag(QString("humidity"))] = 0; - values[tag(QString("pressure"))] = 0.0; - } + values = data; + values[tag(QString("weather"))] + = weatherFromInt(values[tag(QString("weatherId"))].toInt()); emit(dataReceived(values)); } -void ExtWeather::initUrl() +void ExtWeather::initProvider() { - // init query - m_url = QUrl(YAHOO_WEATHER_URL); - QUrlQuery params; - params.addQueryItem(QString("format"), QString("json")); - params.addQueryItem(QString("env"), - QString("store://datatables.org/alltableswithkeys")); - params.addQueryItem(QString("q"), - QString(YAHOO_WEATHER_QUERY).arg(m_city, m_country)); - m_url.setQuery(params); + delete m_providerObject; + + switch (m_provider) { + case Provider::OWM: + m_providerObject = new OWMWeatherProvider(this, number()); + break; + case Provider::Yahoo: + m_providerObject = new YahooWeatherProvider(this, number()); + break; + } + + return m_providerObject->initUrl(m_city, m_country, m_ts); } @@ -375,6 +382,7 @@ void ExtWeather::translate() ui->label_name->setText(i18n("Name")); ui->label_comment->setText(i18n("Comment")); ui->label_number->setText(i18n("Tag")); + ui->label_provider->setText(i18n("Provider")); ui->label_city->setText(i18n("City")); ui->label_country->setText(i18n("Country")); ui->label_timestamp->setText(i18n("Timestamp")); diff --git a/sources/awesomewidgets/extweather.h b/sources/awesomewidgets/extweather.h index 6b60583..8ee174c 100644 --- a/sources/awesomewidgets/extweather.h +++ b/sources/awesomewidgets/extweather.h @@ -22,12 +22,8 @@ #include "abstractextitem.h" -#define YAHOO_WEATHER_URL "https://query.yahooapis.com/v1/public/yql" -#define YAHOO_WEATHER_QUERY \ - "select * from weather.forecast where u='c' and woeid in (select woeid " \ - "from geo.places(1) where text='%1, %2')" - +class AbstractWeatherProvider; namespace Ui { class ExtWeather; @@ -39,9 +35,13 @@ class ExtWeather : public AbstractExtItem Q_PROPERTY(QString city READ city WRITE setCity) Q_PROPERTY(QString country READ country WRITE setCountry) Q_PROPERTY(bool image READ image WRITE setImage) + Q_PROPERTY(Provider povider READ provider WRITE setProvider) + Q_PROPERTY(QString strPovider READ strProvider WRITE setStrProvider) Q_PROPERTY(int ts READ ts WRITE setTs) public: + enum class Provider { OWM = 0, Yahoo = 1 }; + explicit ExtWeather(QWidget *parent, const QString filePath = QString()); virtual ~ExtWeather(); ExtWeather *copy(const QString _fileName, const int _number); @@ -50,12 +50,16 @@ public: QString city() const; QString country() const; bool image() const; + Provider provider() const; + QString strProvider() const; int ts() const; QString uniq() const; // set methods void setCity(const QString _city = QString("London")); void setCountry(const QString _country = QString("uk")); void setImage(const bool _image = false); + void setProvider(const Provider _provider = Provider::OWM); + void setStrProvider(const QString _provider = QString("OWM")); void setTs(const int _ts = 0); public slots: @@ -70,15 +74,16 @@ private slots: private: QNetworkAccessManager *m_manager = nullptr; - QUrl m_url; + AbstractWeatherProvider *m_providerObject = nullptr; bool isRunning = false; Ui::ExtWeather *ui = nullptr; - void initUrl(); + void initProvider(); void translate(); // properties QString m_city = QString("London"); QString m_country = QString("uk"); bool m_image = false; + Provider m_provider = Provider::OWM; int m_ts = 0; QVariantMap m_jsonMap = QVariantMap(); // values diff --git a/sources/awesomewidgets/extweather.ui b/sources/awesomewidgets/extweather.ui index 5015643..0b5bb43 100644 --- a/sources/awesomewidgets/extweather.ui +++ b/sources/awesomewidgets/extweather.ui @@ -7,7 +7,7 @@ 0 0 420 - 301 + 333 @@ -81,6 +81,46 @@ + + + + + + + 0 + 0 + + + + Provider + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + 0 + 0 + + + + + OWM + + + + + Yahoo + + + + + + diff --git a/sources/awesomewidgets/graphicalitem.cpp b/sources/awesomewidgets/graphicalitem.cpp index 313f022..d236cdc 100644 --- a/sources/awesomewidgets/graphicalitem.cpp +++ b/sources/awesomewidgets/graphicalitem.cpp @@ -514,7 +514,8 @@ int GraphicalItem::showConfiguration(const QVariant args) setActiveColor(ui->lineEdit_activeColor->text()); setInactiveColor(ui->lineEdit_inactiveColor->text()); setType(static_cast(ui->comboBox_type->currentIndex())); - setDirection(static_cast(ui->comboBox_direction->currentIndex())); + setDirection( + static_cast(ui->comboBox_direction->currentIndex())); setItemHeight(ui->spinBox_height->value()); setItemWidth(ui->spinBox_width->value()); diff --git a/sources/awesomewidgets/graphicalitem.h b/sources/awesomewidgets/graphicalitem.h index b7bc656..cbfe3a2 100644 --- a/sources/awesomewidgets/graphicalitem.h +++ b/sources/awesomewidgets/graphicalitem.h @@ -50,7 +50,13 @@ class GraphicalItem : public AbstractExtItem public: enum class Direction { LeftToRight = 0, RightToLeft = 1 }; - enum class Type { Horizontal = 0, Vertical = 1, Circle = 2, Graph = 3, Bars = 4 }; + enum class Type { + Horizontal = 0, + Vertical = 1, + Circle = 2, + Graph = 3, + Bars = 4 + }; explicit GraphicalItem(QWidget *parent, const QString filePath = QString()); virtual ~GraphicalItem(); diff --git a/sources/awesomewidgets/owmweatherprovider.cpp b/sources/awesomewidgets/owmweatherprovider.cpp new file mode 100644 index 0000000..f3057b9 --- /dev/null +++ b/sources/awesomewidgets/owmweatherprovider.cpp @@ -0,0 +1,112 @@ +/*************************************************************************** + * This file is part of awesome-widgets * + * * + * awesome-widgets is free software: you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * awesome-widgets is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with awesome-widgets. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ + +#include "owmweatherprovider.h" + +#include +#include + +#include "awdebug.h" + + +OWMWeatherProvider::OWMWeatherProvider(QObject *parent, const int number) + : AbstractWeatherProvider(parent, number) +{ + qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; +} + + +OWMWeatherProvider::~OWMWeatherProvider() +{ + qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; +} + + +void OWMWeatherProvider::initUrl(const QString city, const QString country, + const int ts) +{ + qCDebug(LOG_LIB) << "Init query for" << city << country << "with ts" << ts; + + m_ts = ts; + + if (m_ts == 0) + m_url = QUrl(OWM_WEATHER_URL); + else + m_url = QUrl(OWM_FORECAST_URL); + QUrlQuery params; + params.addQueryItem(QString("q"), QString("%1,%2").arg(city, country)); + params.addQueryItem(QString("units"), QString("metric")); + m_url.setQuery(params); +} + + +QVariantHash OWMWeatherProvider::parse(const QVariantMap &json) const +{ + qCDebug(LOG_LIB) << "Parse json" << json; + + if (json[QString("cod")].toInt() != 200) { + qCWarning(LOG_LIB) << "Invalid OpenWeatherMap return code" + << json[QString("cod")].toInt(); + return QVariantHash(); + } + + if (m_ts == 0) { + return parseSingleJson(json); + } else { + QVariantList list = json[QString("list")].toList(); + return parseSingleJson(list.count() <= m_ts ? list.at(m_ts - 1).toMap() + : list.last().toMap()); + } +} + + +QUrl OWMWeatherProvider::url() const +{ + return m_url; +} + + +QVariantHash OWMWeatherProvider::parseSingleJson(const QVariantMap &json) const +{ + qCDebug(LOG_LIB) << "Single json data" << json; + + QVariantHash output; + + // weather status + QVariantList weather = json[QString("weather")].toList(); + if (!weather.isEmpty()) { + int id = weather.first().toMap()[QString("id")].toInt(); + output[QString("weatherId%1").arg(number())] = id; + } + + // main data + QVariantMap mainWeather = json[QString("main")].toMap(); + if (!weather.isEmpty()) { + output[QString("humidity%1").arg(number())] + = mainWeather[QString("humidity")].toFloat(); + output[QString("pressure%1").arg(number())] + = mainWeather[QString("pressure")].toFloat(); + output[QString("temperature%1").arg(number())] + = mainWeather[QString("temp")].toFloat(); + } + + // timestamp + output[QString("timestamp%1").arg(number())] + = QDateTime::fromTime_t(json[QString("dt")].toUInt()).toUTC(); + + return output; +} diff --git a/sources/awesomewidgets/owmweatherprovider.h b/sources/awesomewidgets/owmweatherprovider.h new file mode 100644 index 0000000..6f8b639 --- /dev/null +++ b/sources/awesomewidgets/owmweatherprovider.h @@ -0,0 +1,43 @@ +/*************************************************************************** + * This file is part of awesome-widgets * + * * + * awesome-widgets is free software: you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * awesome-widgets is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with awesome-widgets. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ + +#ifndef OWMWEATHERPROVIDER_H +#define OWMWEATHERPROVIDER_H + +#include "abstractweatherprovider.h" + +#define OWM_WEATHER_URL "http://arcanis.me/weather" +#define OWM_FORECAST_URL "http://arcanis.me/forecast" + + +class OWMWeatherProvider : public AbstractWeatherProvider +{ +public: + explicit OWMWeatherProvider(QObject *parent, const int number); + virtual ~OWMWeatherProvider(); + void initUrl(const QString city, const QString country, const int); + QVariantHash parse(const QVariantMap &json) const; + QUrl url() const; + +private: + QVariantHash parseSingleJson(const QVariantMap &json) const; + int m_ts = 0; + QUrl m_url; +}; + + +#endif /* OWMWEATHERPROVIDER_H */ diff --git a/sources/awesomewidgets/weather/01d.png b/sources/awesomewidgets/weather/01d.png new file mode 100644 index 0000000..7d2f792 Binary files /dev/null and b/sources/awesomewidgets/weather/01d.png differ diff --git a/sources/awesomewidgets/weather/02d.png b/sources/awesomewidgets/weather/02d.png new file mode 100644 index 0000000..d86a99c Binary files /dev/null and b/sources/awesomewidgets/weather/02d.png differ diff --git a/sources/awesomewidgets/weather/03d.png b/sources/awesomewidgets/weather/03d.png new file mode 100644 index 0000000..8a4a8e9 Binary files /dev/null and b/sources/awesomewidgets/weather/03d.png differ diff --git a/sources/awesomewidgets/weather/04d.png b/sources/awesomewidgets/weather/04d.png new file mode 100644 index 0000000..4677b85 Binary files /dev/null and b/sources/awesomewidgets/weather/04d.png differ diff --git a/sources/awesomewidgets/weather/09d.png b/sources/awesomewidgets/weather/09d.png new file mode 100644 index 0000000..b7cb53b Binary files /dev/null and b/sources/awesomewidgets/weather/09d.png differ diff --git a/sources/awesomewidgets/weather/10d.png b/sources/awesomewidgets/weather/10d.png new file mode 100644 index 0000000..b1e1f83 Binary files /dev/null and b/sources/awesomewidgets/weather/10d.png differ diff --git a/sources/awesomewidgets/weather/11d.png b/sources/awesomewidgets/weather/11d.png new file mode 100644 index 0000000..f3ce654 Binary files /dev/null and b/sources/awesomewidgets/weather/11d.png differ diff --git a/sources/awesomewidgets/weather/13d.png b/sources/awesomewidgets/weather/13d.png new file mode 100644 index 0000000..7b64a6c Binary files /dev/null and b/sources/awesomewidgets/weather/13d.png differ diff --git a/sources/awesomewidgets/weather/50d.png b/sources/awesomewidgets/weather/50d.png new file mode 100644 index 0000000..18afb34 Binary files /dev/null and b/sources/awesomewidgets/weather/50d.png differ diff --git a/sources/awesomewidgets/weather/london.desktop b/sources/awesomewidgets/weather/london.desktop index c89d790..cddbedc 100644 --- a/sources/awesomewidgets/weather/london.desktop +++ b/sources/awesomewidgets/weather/london.desktop @@ -3,10 +3,11 @@ Comment=London current weather Encoding=UTF-8 Name=London current X-AW-Active=false -X-AW-ApiVersion=2 +X-AW-ApiVersion=3 X-AW-City=London X-AW-Country=uk X-AW-Image=false X-AW-Interval=3600 X-AW-Number=0 +X-AW-Provider=OWM X-AW-TS=0 diff --git a/sources/awesomewidgets/yahooweatherprovider.cpp b/sources/awesomewidgets/yahooweatherprovider.cpp new file mode 100644 index 0000000..569a269 --- /dev/null +++ b/sources/awesomewidgets/yahooweatherprovider.cpp @@ -0,0 +1,126 @@ +/*************************************************************************** + * This file is part of awesome-widgets * + * * + * awesome-widgets is free software: you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * awesome-widgets is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with awesome-widgets. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ + +#include "yahooweatherprovider.h" + +#include + +#include "awdebug.h" + + +YahooWeatherProvider::YahooWeatherProvider(QObject *parent, const int number) + : AbstractWeatherProvider(parent, number) +{ + qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; +} + + +YahooWeatherProvider::~YahooWeatherProvider() +{ + qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; +} + + +void YahooWeatherProvider::initUrl(const QString city, const QString country, + const int ts) +{ + qCDebug(LOG_LIB) << "Init query for" << city << country << "with ts" << ts; + + m_ts = ts; + + m_url = QUrl(YAHOO_WEATHER_URL); + QUrlQuery params; + params.addQueryItem(QString("format"), QString("json")); + params.addQueryItem(QString("env"), + QString("store://datatables.org/alltableswithkeys")); + params.addQueryItem(QString("q"), + QString(YAHOO_WEATHER_QUERY).arg(city, country)); + m_url.setQuery(params); +} + + +QVariantHash YahooWeatherProvider::parse(const QVariantMap &json) const +{ + qCDebug(LOG_LIB) << "Parse json" << json; + + QVariantMap jsonMap = json[QString("query")].toMap(); + if (jsonMap[QString("count")].toInt() != 1) { + qCWarning(LOG_LIB) << "Found data count" + << json[QString("count")].toInt() << "is not 1"; + return QVariantHash(); + } + QVariantMap results + = jsonMap[QString("results")].toMap()[QString("channel")].toMap(); + QVariantMap item = results[QString("item")].toMap(); + QVariantMap atmosphere = results[QString("atmosphere")].toMap(); + + return m_ts == 0 ? parseCurrent(item, atmosphere) : parseForecast(item); +} + + +QUrl YahooWeatherProvider::url() const +{ + return m_url; +} + + +QVariantHash +YahooWeatherProvider::parseCurrent(const QVariantMap &json, + const QVariantMap &atmosphere) const +{ + qCDebug(LOG_LIB) << "Parse current weather from" << json; + + QVariantHash values; + int id = json[QString("condition")].toMap()[QString("code")].toInt(); + values[QString("weatherId%1").arg(number())] = id; + values[QString("temperature%1").arg(number())] + = json[QString("condition")].toMap()[QString("temp")].toInt(); + values[QString("timestamp%1").arg(number())] + = json[QString("condition")].toMap()[QString("date")].toString(); + values[QString("humidity%1").arg(number())] + = atmosphere[QString("humidity")].toInt(); + values[QString("pressure%1").arg(number())] + = static_cast(atmosphere[QString("pressure")].toFloat()); + + return values; +} + + +QVariantHash YahooWeatherProvider::parseForecast(const QVariantMap &json) const +{ + qCDebug(LOG_LIB) << "Parse forecast from" << json; + + QVariantHash values; + QVariantList weatherList = json[QString("forecast")].toList(); + QVariantMap weatherMap = weatherList.count() < m_ts + ? weatherList.last().toMap() + : weatherList.at(m_ts).toMap(); + int id = weatherMap[QString("code")].toInt(); + values[QString("weatherId%1").arg(number())] = id; + values[QString("timestamp%1").arg(number())] + = weatherMap[QString("date")].toString(); + // yahoo provides high and low temperatures. Lets calculate average one + values[QString("temperature%1").arg(number())] + = (weatherMap[QString("high")].toFloat() + + weatherMap[QString("low")].toFloat()) + / 2.0; + // ... and no forecast data for humidity and pressure + values[QString("humidity%1").arg(number())] = 0; + values[QString("pressure%1").arg(number())] = 0.0; + + return values; +} diff --git a/sources/awesomewidgets/yahooweatherprovider.h b/sources/awesomewidgets/yahooweatherprovider.h new file mode 100644 index 0000000..d5c997f --- /dev/null +++ b/sources/awesomewidgets/yahooweatherprovider.h @@ -0,0 +1,47 @@ +/*************************************************************************** + * This file is part of awesome-widgets * + * * + * awesome-widgets is free software: you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * awesome-widgets is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with awesome-widgets. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ + +#ifndef YAHOOWEATHERPROVIDER_H +#define YAHOOWEATHERPROVIDER_H + +#include "abstractweatherprovider.h" + +#define YAHOO_WEATHER_URL "https://query.yahooapis.com/v1/public/yql" +#define YAHOO_WEATHER_QUERY \ + "select * from weather.forecast where u='c' and woeid in (select woeid " \ + "from geo.places(1) where text='%1, %2')" + + +class YahooWeatherProvider : public AbstractWeatherProvider +{ +public: + explicit YahooWeatherProvider(QObject *parent, const int number); + virtual ~YahooWeatherProvider(); + void initUrl(const QString city, const QString country, const int); + QVariantHash parse(const QVariantMap &json) const; + QUrl url() const; + +private: + QVariantHash parseCurrent(const QVariantMap &json, + const QVariantMap &atmosphere) const; + QVariantHash parseForecast(const QVariantMap &json) const; + int m_ts = 0; + QUrl m_url; +}; + + +#endif /* YAHOOWEATHERPROVIDER_H */ diff --git a/sources/test/testextweather.cpp b/sources/test/testextweather.cpp index bf41746..d1d38d9 100644 --- a/sources/test/testextweather.cpp +++ b/sources/test/testextweather.cpp @@ -30,6 +30,7 @@ void TestExtWeather::initTestCase() extWeather->setCity(city); extWeather->setCountry(country); extWeather->setNumber(0); + extWeather->setProvider(ExtWeather::Provider::OWM); extWeather->run(); } @@ -47,62 +48,28 @@ void TestExtWeather::test_values() QCOMPARE(extWeather->number(), 0); QCOMPARE(extWeather->city(), city); QCOMPARE(extWeather->country(), country); + QCOMPARE(extWeather->provider(), ExtWeather::Provider::OWM); } -void TestExtWeather::test_run() +void TestExtWeather::test_runOWM() { - // init spy - QSignalSpy spy(extWeather, SIGNAL(dataReceived(const QVariantHash &))); - QVariantHash firstValue = extWeather->run(); + run(); +} - // check values - QVERIFY(spy.wait(5000)); - QVariantHash arguments = spy.takeFirst().at(0).toHash(); - QEXPECT_FAIL("", "WeatherID should not be 0", Continue); - QCOMPARE(arguments[extWeather->tag(QString("weatherId"))].toInt(), 0); - QVERIFY((arguments[extWeather->tag(QString("humidity"))].toInt() - > humidity.first) - && (arguments[extWeather->tag(QString("humidity"))].toInt() - < humidity.second)); - QEXPECT_FAIL("", "https://yahoo.uservoice.com/forums/207813-us-weather/" - "suggestions/14209233-invalid-pressure-calculation", - Continue); - QVERIFY((arguments[extWeather->tag(QString("pressure"))].toFloat() - > pressure.first) - && (arguments[extWeather->tag(QString("pressure"))].toInt() - < pressure.second)); - QVERIFY((arguments[extWeather->tag(QString("temperature"))].toFloat() - > temp.first) - && (arguments[extWeather->tag(QString("temperature"))].toInt() - < temp.second)); - // image should be only one symbol here - QCOMPARE(arguments[extWeather->tag(QString("weather"))].toString().count(), - 1); + +void TestExtWeather::test_runYahoo() +{ + extWeather->setProvider(ExtWeather::Provider::Yahoo); + run(); + extWeather->setProvider(ExtWeather::Provider::OWM); } void TestExtWeather::test_ts() { extWeather->setTs(1); - // init spy - QSignalSpy spy(extWeather, SIGNAL(dataReceived(const QVariantHash &))); - QVariantHash firstValue = extWeather->run(); - - // check values - QVERIFY(spy.wait(5000)); - QVariantHash arguments = spy.takeFirst().at(0).toHash(); - QEXPECT_FAIL("", "WeatherID should not be 0", Continue); - QCOMPARE(arguments[extWeather->tag(QString("weatherId"))].toInt(), 0); - QCOMPARE(arguments[extWeather->tag(QString("humidity"))].toInt(), 0); - QCOMPARE(arguments[extWeather->tag(QString("pressure"))].toFloat(), 0.0f); - QVERIFY((arguments[extWeather->tag(QString("temperature"))].toFloat() - > temp.first) - && (arguments[extWeather->tag(QString("temperature"))].toInt() - < temp.second)); - // image should be only one symbol here - QCOMPARE(arguments[extWeather->tag(QString("weather"))].toString().count(), - 1); + run(); } @@ -131,10 +98,43 @@ void TestExtWeather::test_copy() QCOMPARE(newExtWeather->country(), extWeather->country()); QCOMPARE(newExtWeather->ts(), extWeather->ts()); QCOMPARE(newExtWeather->image(), extWeather->image()); + QCOMPARE(newExtWeather->provider(), extWeather->provider()); QCOMPARE(newExtWeather->number(), 1); delete newExtWeather; } +void TestExtWeather::run() +{ + // init spy + QSignalSpy spy(extWeather, SIGNAL(dataReceived(const QVariantHash &))); + QVariantHash firstValue = extWeather->run(); + + // check values + QVERIFY(spy.wait(5000)); + QVariantHash arguments = spy.takeFirst().at(0).toHash(); + QEXPECT_FAIL("", "WeatherID should not be 0", Continue); + QCOMPARE(arguments[extWeather->tag(QString("weatherId"))].toInt(), 0); + QVERIFY((arguments[extWeather->tag(QString("humidity"))].toInt() + >= humidity.first) + && (arguments[extWeather->tag(QString("humidity"))].toInt() + <= humidity.second)); + QWARN("May fail here for Yahoo! Weather, see " + "https://yahoo.uservoice.com/forums/207813-us-weather/suggestions/" + "14209233-invalid-pressure-calculation"); + QVERIFY((arguments[extWeather->tag(QString("pressure"))].toFloat() + > pressure.first) + && (arguments[extWeather->tag(QString("pressure"))].toInt() + < pressure.second)); + QVERIFY((arguments[extWeather->tag(QString("temperature"))].toFloat() + > temp.first) + && (arguments[extWeather->tag(QString("temperature"))].toInt() + < temp.second)); + // image should be only one symbol here + QCOMPARE(arguments[extWeather->tag(QString("weather"))].toString().count(), + 1); +} + + QTEST_MAIN(TestExtWeather); diff --git a/sources/test/testextweather.h b/sources/test/testextweather.h index 8e28e05..e06cae2 100644 --- a/sources/test/testextweather.h +++ b/sources/test/testextweather.h @@ -34,12 +34,14 @@ private slots: void cleanupTestCase(); // test void test_values(); - void test_run(); + void test_runOWM(); + void test_runYahoo(); void test_ts(); void test_image(); void test_copy(); private: + void run(); ExtWeather *extWeather = nullptr; QString city = QString("London"); QString country = QString("uk"); diff --git a/sources/version.h.in b/sources/version.h.in index 5803bec..4c89f7b 100644 --- a/sources/version.h.in +++ b/sources/version.h.in @@ -19,7 +19,8 @@ "qnetworkreply-network-reply-timeout-helper" #define SPECIAL_THANKS \ "Yahoo! Finance,https://finance.yahoo.com/;Yahoo! " \ - "Weather,https://weather.yahoo.com/;JetBrains,https://www.jetbrains.com/" + "Weather,https://weather.yahoo.com/;JetBrains,https://www.jetbrains.com/" \ + ";OpenWeatherMap,http://openweathermap.org/" #define CHANGELOG "@PROJECT_CHANGELOG@" // configuraion @@ -32,7 +33,7 @@ // extupgrade api version #define AWEUAPI 3 // extweather api version -#define AWEWAPI 2 +#define AWEWAPI 3 // formatter api version #define AWEFAPI 1 // network requests timeout, ms