mirror of
https://github.com/arcan1s/awesome-widgets.git
synced 2025-07-03 08:55:54 +00:00
refactor: review delete and new operators
This commit is contained in:
@ -36,6 +36,12 @@ AbstractExtItem::AbstractExtItem(QObject *_parent, const QString &_filePath)
|
||||
qCDebug(LOG_LIB) << "Desktop name" << _filePath;
|
||||
|
||||
m_name = m_filePath;
|
||||
|
||||
m_scheduler = new QCronScheduler(this);
|
||||
connect(m_scheduler, &QCronScheduler::activated, this, &AbstractExtItem::requestDataUpdate);
|
||||
|
||||
m_socket = new QLocalServer(this);
|
||||
connect(m_socket, &QLocalServer::newConnection, this, &AbstractExtItem::requestDataUpdate);
|
||||
}
|
||||
|
||||
|
||||
@ -43,11 +49,11 @@ AbstractExtItem::~AbstractExtItem()
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
|
||||
if (m_socket) {
|
||||
m_socket->close();
|
||||
QLocalServer::removeServer(socket());
|
||||
m_socket->deleteLater();
|
||||
}
|
||||
m_scheduler->stop();
|
||||
m_scheduler->deleteLater();
|
||||
|
||||
m_socket->close();
|
||||
m_socket->deleteLater();
|
||||
}
|
||||
|
||||
|
||||
@ -203,20 +209,14 @@ void AbstractExtItem::setComment(const QString &_comment)
|
||||
void AbstractExtItem::setCron(const QString &_cron)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Cron string" << _cron;
|
||||
// deinit module first
|
||||
if (m_scheduler) {
|
||||
disconnect(m_scheduler, &QCronScheduler::activated, this, &AbstractExtItem::requestDataUpdate);
|
||||
delete m_scheduler;
|
||||
}
|
||||
|
||||
m_cron = _cron;
|
||||
if (cron().isEmpty())
|
||||
return;
|
||||
|
||||
// init scheduler
|
||||
m_scheduler = new QCronScheduler(this);
|
||||
m_scheduler->parse(cron());
|
||||
connect(m_scheduler, &QCronScheduler::activated, this, &AbstractExtItem::requestDataUpdate);
|
||||
if (m_cron.isEmpty()) { // disable cron timer
|
||||
m_scheduler->stop();
|
||||
} else {
|
||||
m_scheduler->parse(m_cron);
|
||||
m_scheduler->start();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -260,34 +260,18 @@ void AbstractExtItem::setNumber(int _number)
|
||||
void AbstractExtItem::setSocket(const QString &_socket)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Socket" << _socket;
|
||||
// remove old socket first
|
||||
deinitSocket();
|
||||
|
||||
m_socketFile = _socket;
|
||||
}
|
||||
|
||||
|
||||
void AbstractExtItem::deinitSocket()
|
||||
{
|
||||
if (!m_socket)
|
||||
return;
|
||||
|
||||
m_socket->close();
|
||||
QLocalServer::removeServer(socket());
|
||||
disconnect(m_socket, &QLocalServer::newConnection, this, &AbstractExtItem::requestDataUpdate);
|
||||
delete m_socket;
|
||||
}
|
||||
|
||||
|
||||
void AbstractExtItem::initSocket()
|
||||
{
|
||||
// remove old socket first
|
||||
deinitSocket();
|
||||
// reload local socket
|
||||
m_socket->close();
|
||||
|
||||
m_socket = new QLocalServer(this);
|
||||
auto listening = m_socket->listen(socket());
|
||||
qCInfo(LOG_LIB) << "Server listening on" << socket() << listening;
|
||||
connect(m_socket, &QLocalServer::newConnection, this, &AbstractExtItem::requestDataUpdate);
|
||||
auto listening = m_socket->listen(m_socketFile);
|
||||
qCInfo(LOG_LIB) << "Server listening on" << m_socketFile << listening;
|
||||
}
|
||||
|
||||
|
||||
|
@ -75,7 +75,6 @@ signals:
|
||||
void requestDataUpdate();
|
||||
|
||||
public slots:
|
||||
virtual void deinitSocket();
|
||||
virtual void initSocket();
|
||||
virtual void readConfiguration();
|
||||
virtual QVariantHash run() = 0;
|
||||
|
@ -17,25 +17,15 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QUrl>
|
||||
|
||||
#include "abstractextitem.h"
|
||||
#include <QVariant>
|
||||
|
||||
|
||||
class AbstractQuotesProvider : public QObject
|
||||
class AbstractQuotesProvider
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit AbstractQuotesProvider(QObject *_parent)
|
||||
: QObject(_parent){};
|
||||
~AbstractQuotesProvider() override = default;
|
||||
virtual ~AbstractQuotesProvider() = default;
|
||||
virtual void initUrl(const QString &_asset) = 0;
|
||||
[[nodiscard]] virtual QVariantHash parse(const QByteArray &_source, const QVariantHash &_oldValues) const = 0;
|
||||
[[nodiscard]] QString tag(const QString &_type) const
|
||||
{
|
||||
return dynamic_cast<AbstractExtItem *>(parent())->tag(_type);
|
||||
};
|
||||
[[nodiscard]] virtual QVariantHash parse(const QByteArray &_source) = 0;
|
||||
[[nodiscard]] virtual QUrl url() const = 0;
|
||||
};
|
||||
|
@ -17,25 +17,15 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QUrl>
|
||||
|
||||
#include "abstractextitem.h"
|
||||
#include <QVariant>
|
||||
|
||||
|
||||
class AbstractWeatherProvider : public QObject
|
||||
class AbstractWeatherProvider
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit AbstractWeatherProvider(QObject *_parent)
|
||||
: QObject(_parent){};
|
||||
~AbstractWeatherProvider() override = default;
|
||||
virtual ~AbstractWeatherProvider() = default;
|
||||
virtual void initUrl(const QString &_city, const QString &_country, int _ts) = 0;
|
||||
[[nodiscard]] virtual QVariantHash parse(const QVariantMap &_json) const = 0;
|
||||
[[nodiscard]] QString tag(const QString &_type) const
|
||||
{
|
||||
return dynamic_cast<AbstractExtItem *>(parent())->tag(_type);
|
||||
};
|
||||
[[nodiscard]] virtual QUrl url() const = 0;
|
||||
};
|
||||
|
@ -49,9 +49,6 @@ ExtNetworkRequest::~ExtNetworkRequest()
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
|
||||
disconnect(m_manager, &QNetworkAccessManager::finished, this, &ExtNetworkRequest::networkReplyReceived);
|
||||
disconnect(this, &ExtNetworkRequest::requestDataUpdate, this, &ExtNetworkRequest::sendRequest);
|
||||
|
||||
m_manager->deleteLater();
|
||||
}
|
||||
|
||||
|
@ -55,9 +55,6 @@ ExtQuotes::~ExtQuotes()
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
|
||||
disconnect(m_manager, &QNetworkAccessManager::finished, this, &ExtQuotes::quotesReplyReceived);
|
||||
disconnect(this, &ExtQuotes::requestDataUpdate, this, &ExtQuotes::sendRequest);
|
||||
|
||||
m_manager->deleteLater();
|
||||
}
|
||||
|
||||
@ -186,10 +183,9 @@ void ExtQuotes::quotesReplyReceived(QNetworkReply *_reply)
|
||||
auto text = _reply->readAll();
|
||||
_reply->deleteLater();
|
||||
|
||||
auto data = m_providerObject->parse(text, m_values);
|
||||
if (data.isEmpty())
|
||||
return;
|
||||
m_values = data;
|
||||
auto data = m_providerObject->parse(text);
|
||||
for (auto [key, value] : data.asKeyValueRange())
|
||||
m_values[tag(key)] = value;
|
||||
|
||||
emit(dataReceived(m_values));
|
||||
}
|
||||
@ -205,10 +201,8 @@ void ExtQuotes::sendRequest()
|
||||
|
||||
void ExtQuotes::initProvider()
|
||||
{
|
||||
delete m_providerObject;
|
||||
|
||||
// in the future release it is possible to change provider here
|
||||
m_providerObject = new StooqQuotesProvider(this);
|
||||
m_providerObject = std::make_unique<StooqQuotesProvider>();
|
||||
|
||||
return m_providerObject->initUrl(ticker());
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ private slots:
|
||||
void sendRequest();
|
||||
|
||||
private:
|
||||
AbstractQuotesProvider *m_providerObject = nullptr;
|
||||
std::unique_ptr<AbstractQuotesProvider> m_providerObject;
|
||||
QNetworkAccessManager *m_manager = nullptr;
|
||||
bool m_isRunning = false;
|
||||
void initProvider();
|
||||
|
@ -53,7 +53,6 @@ ExtScript::~ExtScript()
|
||||
|
||||
m_process->kill();
|
||||
m_process->deleteLater();
|
||||
disconnect(this, &ExtScript::requestDataUpdate, this, &ExtScript::startProcess);
|
||||
}
|
||||
|
||||
|
||||
|
@ -36,7 +36,7 @@ ExtUpgrade::ExtUpgrade(QObject *_parent, const QString &_filePath)
|
||||
m_values[tag("pkgcount")] = 0;
|
||||
|
||||
m_process = new QProcess(nullptr);
|
||||
connect(m_process, &QProcess::finished, [this]() { return updateValue(); });
|
||||
connect(m_process, &QProcess::finished, this, &ExtUpgrade::updateValue);
|
||||
m_process->waitForFinished(0);
|
||||
|
||||
connect(this, &ExtUpgrade::requestDataUpdate, this, &ExtUpgrade::startProcess);
|
||||
@ -49,7 +49,6 @@ ExtUpgrade::~ExtUpgrade()
|
||||
|
||||
m_process->kill();
|
||||
m_process->deleteLater();
|
||||
disconnect(this, &ExtUpgrade::requestDataUpdate, this, &ExtUpgrade::startProcess);
|
||||
}
|
||||
|
||||
|
||||
|
@ -58,9 +58,6 @@ ExtWeather::~ExtWeather()
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
|
||||
disconnect(m_manager, &QNetworkAccessManager::finished, this, &ExtWeather::weatherReplyReceived);
|
||||
disconnect(this, &ExtWeather::requestDataUpdate, this, &ExtWeather::sendRequest);
|
||||
|
||||
m_manager->deleteLater();
|
||||
}
|
||||
|
||||
@ -354,9 +351,8 @@ void ExtWeather::weatherReplyReceived(QNetworkReply *_reply)
|
||||
}
|
||||
|
||||
auto data = m_providerObject->parse(jsonDoc.toVariant().toMap());
|
||||
if (data.isEmpty())
|
||||
return;
|
||||
m_values = data;
|
||||
for (auto [key, value] : data.asKeyValueRange())
|
||||
m_values[tag(key)] = value;
|
||||
m_values[tag("weather")] = weatherFromInt(m_values[tag("weatherId")].toInt());
|
||||
|
||||
emit(dataReceived(m_values));
|
||||
@ -365,10 +361,8 @@ void ExtWeather::weatherReplyReceived(QNetworkReply *_reply)
|
||||
|
||||
void ExtWeather::initProvider()
|
||||
{
|
||||
delete m_providerObject;
|
||||
|
||||
// in the future release it is possible to change provider here
|
||||
m_providerObject = new OWMWeatherProvider(this);
|
||||
m_providerObject = std::make_unique<OWMWeatherProvider>();
|
||||
|
||||
return m_providerObject->initUrl(city(), country(), ts());
|
||||
}
|
||||
|
@ -74,7 +74,7 @@ private slots:
|
||||
void weatherReplyReceived(QNetworkReply *_reply);
|
||||
|
||||
private:
|
||||
AbstractWeatherProvider *m_providerObject = nullptr;
|
||||
std::unique_ptr<AbstractWeatherProvider> m_providerObject;
|
||||
QNetworkAccessManager *m_manager = nullptr;
|
||||
bool m_isRunning = false;
|
||||
void initProvider();
|
||||
|
@ -55,6 +55,15 @@ GraphicalItem::GraphicalItem(QObject *_parent, const QString &_filePath)
|
||||
}
|
||||
|
||||
|
||||
GraphicalItem::~GraphicalItem()
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
|
||||
m_view->deleteLater();
|
||||
m_scene->deleteLater();
|
||||
}
|
||||
|
||||
|
||||
GraphicalItem *GraphicalItem::copy(const QString &_fileName, const int _number)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "File" << _fileName << "with number" << _number;
|
||||
|
@ -52,6 +52,7 @@ public:
|
||||
enum class Type { Horizontal = 0, Vertical = 1, Circle = 2, Graph = 3, Bars = 4 };
|
||||
|
||||
explicit GraphicalItem(QObject *_parent = nullptr, const QString &_filePath = "");
|
||||
~GraphicalItem() override;
|
||||
GraphicalItem *copy(const QString &_fileName, int _number) override;
|
||||
QString image(const QVariant &value);
|
||||
void initScene();
|
||||
|
@ -23,14 +23,8 @@
|
||||
#include "awdebug.h"
|
||||
|
||||
|
||||
OWMWeatherProvider::OWMWeatherProvider(QObject *_parent)
|
||||
: AbstractWeatherProvider(_parent)
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
}
|
||||
|
||||
|
||||
OWMWeatherProvider::~OWMWeatherProvider()
|
||||
OWMWeatherProvider::OWMWeatherProvider()
|
||||
: AbstractWeatherProvider()
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
}
|
||||
@ -66,7 +60,7 @@ QVariantHash OWMWeatherProvider::parse(const QVariantMap &_json) const
|
||||
if (m_ts == 0) {
|
||||
return parseSingleJson(_json);
|
||||
} else {
|
||||
QVariantList list = _json["list"].toList();
|
||||
auto list = _json["list"].toList();
|
||||
return parseSingleJson(list.count() <= m_ts ? list.at(m_ts - 1).toMap() : list.last().toMap());
|
||||
}
|
||||
}
|
||||
@ -78,7 +72,7 @@ QUrl OWMWeatherProvider::url() const
|
||||
}
|
||||
|
||||
|
||||
QVariantHash OWMWeatherProvider::parseSingleJson(const QVariantMap &_json) const
|
||||
QVariantHash OWMWeatherProvider::parseSingleJson(const QVariantMap &_json)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Single json data" << _json;
|
||||
|
||||
@ -88,19 +82,19 @@ QVariantHash OWMWeatherProvider::parseSingleJson(const QVariantMap &_json) const
|
||||
auto weather = _json["weather"].toList();
|
||||
if (!weather.isEmpty()) {
|
||||
int id = weather.first().toMap()["id"].toInt();
|
||||
output[tag("weatherId")] = id;
|
||||
output["weatherId"] = id;
|
||||
}
|
||||
|
||||
// main data
|
||||
auto mainWeather = _json["main"].toMap();
|
||||
if (!weather.isEmpty()) {
|
||||
output[tag("humidity")] = mainWeather["humidity"].toDouble();
|
||||
output[tag("pressure")] = mainWeather["pressure"].toDouble();
|
||||
output[tag("temperature")] = mainWeather["temp"].toDouble();
|
||||
output["humidity"] = mainWeather["humidity"].toDouble();
|
||||
output["pressure"] = mainWeather["pressure"].toDouble();
|
||||
output["temperature"] = mainWeather["temp"].toDouble();
|
||||
}
|
||||
|
||||
// timestamp
|
||||
output[tag("timestamp")] = QDateTime::fromSecsSinceEpoch(_json["dt"].toUInt()).toUTC();
|
||||
output["timestamp"] = QDateTime::fromSecsSinceEpoch(_json["dt"].toUInt()).toUTC();
|
||||
|
||||
return output;
|
||||
}
|
||||
|
@ -22,22 +22,20 @@
|
||||
|
||||
class OWMWeatherProvider : public AbstractWeatherProvider
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
// we are using own server to pass requests to OpenWeatherMap because it
|
||||
// requires specific APPID which belongs to developer not user
|
||||
const char *OWM_WEATHER_URL = "https://arcanis.me/weather";
|
||||
const char *OWM_FORECAST_URL = "https://arcanis.me/forecast";
|
||||
|
||||
explicit OWMWeatherProvider(QObject *_parent);
|
||||
~OWMWeatherProvider() override;
|
||||
explicit OWMWeatherProvider();
|
||||
~OWMWeatherProvider() override = default;
|
||||
void initUrl(const QString &_city, const QString &_country, int) override;
|
||||
[[nodiscard]] QVariantHash parse(const QVariantMap &_json) const override;
|
||||
[[nodiscard]] QUrl url() const override;
|
||||
|
||||
private:
|
||||
[[nodiscard]] QVariantHash parseSingleJson(const QVariantMap &_json) const;
|
||||
[[nodiscard]] static QVariantHash parseSingleJson(const QVariantMap &_json);
|
||||
int m_ts = 0;
|
||||
QUrl m_url;
|
||||
};
|
||||
|
@ -34,8 +34,6 @@ QCronScheduler::QCronScheduler(QObject *_parent)
|
||||
m_timer->setInterval(60 * 1000);
|
||||
|
||||
connect(m_timer, &QTimer::timeout, this, &QCronScheduler::expired);
|
||||
|
||||
m_timer->start();
|
||||
}
|
||||
|
||||
|
||||
@ -44,6 +42,7 @@ QCronScheduler::~QCronScheduler()
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
|
||||
m_timer->stop();
|
||||
m_timer->deleteLater();
|
||||
}
|
||||
|
||||
|
||||
@ -63,6 +62,22 @@ void QCronScheduler::parse(const QString &_timer)
|
||||
}
|
||||
|
||||
|
||||
void QCronScheduler::start()
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Start cron timer";
|
||||
|
||||
m_timer->start();
|
||||
}
|
||||
|
||||
|
||||
void QCronScheduler::stop()
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Stop cron timer";
|
||||
|
||||
m_timer->stop();
|
||||
}
|
||||
|
||||
|
||||
void QCronScheduler::expired()
|
||||
{
|
||||
auto now = QDateTime::currentDateTime();
|
||||
|
@ -39,12 +39,14 @@ public:
|
||||
int maxValue = -1;
|
||||
int div = 1;
|
||||
void fromRange(const QString &_range, int _min, int _max);
|
||||
QList<int> toList() const;
|
||||
[[nodiscard]] QList<int> toList() const;
|
||||
};
|
||||
|
||||
explicit QCronScheduler(QObject *_parent = nullptr);
|
||||
~QCronScheduler() override;
|
||||
void parse(const QString &_timer);
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
signals:
|
||||
void activated();
|
||||
|
@ -22,14 +22,8 @@
|
||||
#include "awdebug.h"
|
||||
|
||||
|
||||
StooqQuotesProvider::StooqQuotesProvider(QObject *_parent)
|
||||
: AbstractQuotesProvider(_parent)
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
}
|
||||
|
||||
|
||||
StooqQuotesProvider::~StooqQuotesProvider()
|
||||
StooqQuotesProvider::StooqQuotesProvider()
|
||||
: AbstractQuotesProvider()
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
}
|
||||
@ -49,7 +43,7 @@ void StooqQuotesProvider::initUrl(const QString &_asset)
|
||||
}
|
||||
|
||||
|
||||
QVariantHash StooqQuotesProvider::parse(const QByteArray &_source, const QVariantHash &_oldValues) const
|
||||
QVariantHash StooqQuotesProvider::parse(const QByteArray &_source)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Parse csv" << _source;
|
||||
|
||||
@ -61,20 +55,18 @@ QVariantHash StooqQuotesProvider::parse(const QByteArray &_source, const QVarian
|
||||
return values;
|
||||
}
|
||||
|
||||
// extract old data
|
||||
auto oldPrice = _oldValues[tag("price")].toDouble();
|
||||
auto oldVolume = _oldValues[tag("volume")].toInt();
|
||||
|
||||
// last trade
|
||||
auto price = sourceValues.at(0).toDouble();
|
||||
values[tag("pricechg")] = oldPrice == 0.0 ? 0.0 : price - oldPrice;
|
||||
values[tag("percpricechg")] = 100.0 * values[tag("pricechg")].toDouble() / oldPrice;
|
||||
values[tag("price")] = price;
|
||||
values["pricechg"] = m_price == 0.0 ? 0.0 : price - m_price;
|
||||
values["percpricechg"] = 100.0 * values["pricechg"].toDouble() / m_price;
|
||||
values["price"] = price;
|
||||
m_price = price;
|
||||
// volume
|
||||
auto volume = sourceValues.at(1).toInt();
|
||||
values[tag("volumechg")] = oldVolume == 0 ? 0 : volume - oldVolume;
|
||||
values[tag("percvolumechg")] = 100.0 * values[tag("volumechg")].toDouble() / oldVolume;
|
||||
values[tag("volume")] = volume;
|
||||
values["volumechg"] = m_volume == 0 ? 0 : volume - m_volume;
|
||||
values["percvolumechg"] = 100.0 * values["volumechg"].toDouble() / m_volume;
|
||||
values["volume"] = volume;
|
||||
m_volume = volume;
|
||||
|
||||
return values;
|
||||
}
|
||||
|
@ -22,17 +22,17 @@
|
||||
|
||||
class StooqQuotesProvider : public AbstractQuotesProvider
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
const char *STOOQ_QUOTES_URL = "https://stooq.com/q/l/";
|
||||
|
||||
explicit StooqQuotesProvider(QObject *_parent);
|
||||
~StooqQuotesProvider() override;
|
||||
explicit StooqQuotesProvider();
|
||||
~StooqQuotesProvider() override = default;
|
||||
void initUrl(const QString &_asset) override;
|
||||
[[nodiscard]] QVariantHash parse(const QByteArray &_source, const QVariantHash &_oldValues) const override;
|
||||
[[nodiscard]] QVariantHash parse(const QByteArray &_source) override;
|
||||
[[nodiscard]] QUrl url() const override;
|
||||
|
||||
private:
|
||||
double m_price = 0.0;
|
||||
QUrl m_url;
|
||||
int m_volume = 0;
|
||||
};
|
||||
|
@ -23,14 +23,8 @@
|
||||
#include "awdebug.h"
|
||||
|
||||
|
||||
YahooQuotesProvider::YahooQuotesProvider(QObject *_parent)
|
||||
: AbstractQuotesProvider(_parent)
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
}
|
||||
|
||||
|
||||
YahooQuotesProvider::~YahooQuotesProvider()
|
||||
YahooQuotesProvider::YahooQuotesProvider()
|
||||
: AbstractQuotesProvider()
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
}
|
||||
@ -50,7 +44,7 @@ void YahooQuotesProvider::initUrl(const QString &_asset)
|
||||
}
|
||||
|
||||
|
||||
QVariantHash YahooQuotesProvider::parse(const QByteArray &_source, const QVariantHash &_oldValues) const
|
||||
QVariantHash YahooQuotesProvider::parse(const QByteArray &_source)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Parse json" << _source;
|
||||
|
||||
@ -65,18 +59,16 @@ QVariantHash YahooQuotesProvider::parse(const QByteArray &_source, const QVarian
|
||||
auto jsonQuotes = jsonDoc.toVariant().toMap()["query"].toMap();
|
||||
jsonQuotes = jsonQuotes["results"].toMap()["quote"].toMap();
|
||||
|
||||
// extract old data
|
||||
auto oldPrice = _oldValues[tag("price")].toDouble();
|
||||
|
||||
// last trade
|
||||
auto value = jsonQuotes["LastTradePriceOnly"].toString().toDouble();
|
||||
values[tag("pricechg")] = oldPrice == 0.0 ? 0.0 : value - oldPrice;
|
||||
values[tag("percpricechg")] = 100.0 * values[tag("pricechg")].toDouble() / value;
|
||||
values[tag("price")] = value;
|
||||
auto price = jsonQuotes["LastTradePriceOnly"].toString().toDouble();
|
||||
values["pricechg"] = m_price == 0.0 ? 0.0 : price - m_price;
|
||||
values["percpricechg"] = 100.0 * values["pricechg"].toDouble() / price;
|
||||
values["price"] = price;
|
||||
m_price = price;
|
||||
// volume
|
||||
values[tag("volume")] = 0;
|
||||
values[tag("volumechg")] = 0;
|
||||
values[tag("percvolumechg")] = 0.0;
|
||||
values["volume"] = 0;
|
||||
values["volumechg"] = 0;
|
||||
values["percvolumechg"] = 0.0;
|
||||
|
||||
return values;
|
||||
}
|
||||
|
@ -22,18 +22,17 @@
|
||||
|
||||
class YahooQuotesProvider : public AbstractQuotesProvider
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
const char *YAHOO_QUOTES_URL = "https://query.yahooapis.com/v1/public/yql";
|
||||
const char *YAHOO_QUOTES_QUERY = "select * from yahoo.finance.quotes where symbol='%1'";
|
||||
|
||||
explicit YahooQuotesProvider(QObject *_parent);
|
||||
~YahooQuotesProvider() override;
|
||||
explicit YahooQuotesProvider();
|
||||
~YahooQuotesProvider() override = default;
|
||||
void initUrl(const QString &_asset) override;
|
||||
[[nodiscard]] QVariantHash parse(const QByteArray &_source, const QVariantHash &_oldValues) const override;
|
||||
[[nodiscard]] QVariantHash parse(const QByteArray &_source) override;
|
||||
[[nodiscard]] QUrl url() const override;
|
||||
|
||||
private:
|
||||
double m_price = 0.0;
|
||||
QUrl m_url;
|
||||
};
|
||||
|
@ -22,14 +22,8 @@
|
||||
#include "awdebug.h"
|
||||
|
||||
|
||||
YahooWeatherProvider::YahooWeatherProvider(QObject *_parent)
|
||||
: AbstractWeatherProvider(_parent)
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
}
|
||||
|
||||
|
||||
YahooWeatherProvider::~YahooWeatherProvider()
|
||||
YahooWeatherProvider::YahooWeatherProvider()
|
||||
: AbstractWeatherProvider()
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
}
|
||||
@ -74,7 +68,7 @@ QUrl YahooWeatherProvider::url() const
|
||||
}
|
||||
|
||||
|
||||
QVariantHash YahooWeatherProvider::parseCurrent(const QVariantMap &_json, const QVariantMap &_atmosphere) const
|
||||
QVariantHash YahooWeatherProvider::parseCurrent(const QVariantMap &_json, const QVariantMap &_atmosphere)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Parse current weather from" << _json;
|
||||
|
||||
@ -82,12 +76,12 @@ QVariantHash YahooWeatherProvider::parseCurrent(const QVariantMap &_json, const
|
||||
|
||||
QVariantHash values;
|
||||
auto id = _json["condition"].toMap()["code"].toInt();
|
||||
values[tag("weatherId")] = id;
|
||||
values[tag("temperature")] = condition["temp"].toInt();
|
||||
values[tag("timestamp")] = condition["date"].toString();
|
||||
values[tag("humidity")] = _atmosphere["humidity"].toInt();
|
||||
values["weatherId"] = id;
|
||||
values["temperature"] = condition["temp"].toInt();
|
||||
values["timestamp"] = condition["date"].toString();
|
||||
values["humidity"] = _atmosphere["humidity"].toInt();
|
||||
// HACK temporary fix of invalid values on Yahoo! side
|
||||
values[tag("pressure")] = static_cast<int>(_atmosphere["pressure"].toDouble() / 33.863753);
|
||||
values["pressure"] = static_cast<int>(_atmosphere["pressure"].toDouble() / 33.863753);
|
||||
|
||||
return values;
|
||||
}
|
||||
@ -101,13 +95,13 @@ QVariantHash YahooWeatherProvider::parseForecast(const QVariantMap &_json) const
|
||||
auto weatherList = _json["forecast"].toList();
|
||||
auto weatherMap = weatherList.count() < m_ts ? weatherList.last().toMap() : weatherList.at(m_ts).toMap();
|
||||
auto id = weatherMap["code"].toInt();
|
||||
values[tag("weatherId")] = id;
|
||||
values[tag("timestamp")] = weatherMap["date"].toString();
|
||||
values["weatherId"] = id;
|
||||
values["timestamp"] = weatherMap["date"].toString();
|
||||
// yahoo provides high and low temperatures. Lets calculate average one
|
||||
values[tag("temperature")] = (weatherMap["high"].toDouble() + weatherMap["low"].toDouble()) / 2.0;
|
||||
values["temperature"] = (weatherMap["high"].toDouble() + weatherMap["low"].toDouble()) / 2.0;
|
||||
// ... and no forecast data for humidity and pressure
|
||||
values[tag("humidity")] = 0;
|
||||
values[tag("pressure")] = 0.0;
|
||||
values["humidity"] = 0;
|
||||
values["pressure"] = 0.0;
|
||||
|
||||
return values;
|
||||
}
|
||||
|
@ -22,22 +22,20 @@
|
||||
|
||||
class YahooWeatherProvider : public AbstractWeatherProvider
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
const char *YAHOO_WEATHER_URL = "https://query.yahooapis.com/v1/public/yql";
|
||||
const char *YAHOO_WEATHER_QUERY = "select * from weather.forecast where "
|
||||
"u='c' and woeid in (select woeid from "
|
||||
"geo.places(1) where text='%1, %2')";
|
||||
|
||||
explicit YahooWeatherProvider(QObject *_parent);
|
||||
~YahooWeatherProvider() override;
|
||||
explicit YahooWeatherProvider();
|
||||
~YahooWeatherProvider() override = default;
|
||||
void initUrl(const QString &_city, const QString &_country, int) override;
|
||||
[[nodiscard]] QVariantHash parse(const QVariantMap &_json) const override;
|
||||
[[nodiscard]] QUrl url() const override;
|
||||
|
||||
private:
|
||||
[[nodiscard]] QVariantHash parseCurrent(const QVariantMap &_json, const QVariantMap &_atmosphere) const;
|
||||
[[nodiscard]] static QVariantHash parseCurrent(const QVariantMap &_json, const QVariantMap &_atmosphere);
|
||||
[[nodiscard]] QVariantHash parseForecast(const QVariantMap &_json) const;
|
||||
int m_ts = 0;
|
||||
QUrl m_url;
|
||||
|
Reference in New Issue
Block a user