diff --git a/sources/awdebug.cpp b/sources/awdebug.cpp index be0deba..189b4ec 100644 --- a/sources/awdebug.cpp +++ b/sources/awdebug.cpp @@ -42,12 +42,14 @@ const QStringList getBuildData() metadata.append(QString(" BUILD_DATE: %1").arg(BUILD_DATE)); // configuration metadata.append(QString("API details:")); - metadata.append(QString(" AWGIAPI: %1").arg(AWGIAPI)); - metadata.append(QString(" AWEQAPI: %1").arg(AWEQAPI)); - metadata.append(QString(" AWESAPI: %1").arg(AWESAPI)); - metadata.append(QString(" AWEUAPI: %1").arg(AWEUAPI)); - metadata.append(QString(" AWEWAPI: %1").arg(AWEWAPI)); - metadata.append(QString(" AWEFAPI: %1").arg(AWEFAPI)); + metadata.append(QString(" AW_GRAPHITEM_API: %1").arg(AW_GRAPHITEM_API)); + metadata.append(QString(" AW_EXTQUOTES_API: %1").arg(AW_EXTQUOTES_API)); + metadata.append(QString(" AW_EXTSCRIPT_API: %1").arg(AW_EXTSCRIPT_API)); + metadata.append( + QString(" AW_EXTUPGRADE_API: %1").arg(AW_EXTUPGRADE_API)); + metadata.append( + QString(" AW_EXTWEATHER_API: %1").arg(AW_EXTWEATHER_API)); + metadata.append(QString(" AW_FORMATTER_API: %1").arg(AW_FORMATTER_API)); metadata.append(QString(" REQUEST_TIMEOUT: %1").arg(REQUEST_TIMEOUT)); metadata.append(QString(" TIME_KEYS: %1").arg(TIME_KEYS)); metadata.append(QString(" STATIC_KEYS: %1").arg(STATIC_KEYS)); diff --git a/sources/awesome-widget/package/contents/ui/dataengine.qml b/sources/awesome-widget/package/contents/ui/dataengine.qml index d5379b5..6dcdf0d 100644 --- a/sources/awesome-widget/package/contents/ui/dataengine.qml +++ b/sources/awesome-widget/package/contents/ui/dataengine.qml @@ -221,8 +221,8 @@ Item { } ButtonSelector { - value: i18n("Quotes monitor") - onButtonActivated: awKeys.editItem("extquotes") + value: i18n("Network requests") + onButtonActivated: awKeys.editItem("extnetworkrequest") } ButtonSelector { @@ -230,6 +230,11 @@ Item { onButtonActivated: awKeys.editItem("extupgrade") } + ButtonSelector { + value: i18n("Quotes monitor") + onButtonActivated: awKeys.editItem("extquotes") + } + ButtonSelector { value: i18n("Weather") onButtonActivated: awKeys.editItem("extweather") diff --git a/sources/awesome-widget/plugin/awformatterhelper.cpp b/sources/awesome-widget/plugin/awformatterhelper.cpp index 3f92cc6..d94bf39 100644 --- a/sources/awesome-widget/plugin/awformatterhelper.cpp +++ b/sources/awesome-widget/plugin/awformatterhelper.cpp @@ -327,7 +327,7 @@ void AWFormatterHelper::doCreateItem() case AWAbstractFormatter::FormatterClass::String: return createItem(); case AWAbstractFormatter::FormatterClass::Json: - return createItem(); + return createItem(); case AWAbstractFormatter::FormatterClass::NoFormat: return createItem(); } diff --git a/sources/awesome-widget/plugin/awkeyoperations.cpp b/sources/awesome-widget/plugin/awkeyoperations.cpp index b025552..d4cbd6c 100644 --- a/sources/awesome-widget/plugin/awkeyoperations.cpp +++ b/sources/awesome-widget/plugin/awkeyoperations.cpp @@ -26,6 +26,7 @@ #include "awkeycache.h" #include "awpatternfunctions.h" // extensions +#include "extnetworkrequest.h" #include "extquotes.h" #include "extscript.h" #include "extupgrade.h" @@ -46,6 +47,7 @@ AWKeyOperations::~AWKeyOperations() // extensions delete m_graphicalItems; + delete m_extNetRequest; delete m_extQuotes; delete m_extScripts; delete m_extUpgrade; @@ -151,6 +153,9 @@ QStringList AWKeyOperations::dictKeys() const // custom for (auto item : m_extScripts->activeItems()) allKeys.append(item->tag(QString("custom"))); + // network requests + for (auto item : m_extNetRequest->activeItems()) + allKeys.append(item->tag(QString("response"))); // bars for (auto item : m_graphicalItems->activeItems()) allKeys.append(item->tag(QString("bar"))); @@ -225,6 +230,10 @@ QString AWKeyOperations::infoByKey(QString key) const } else if (key.startsWith(QString("temp"))) { output = m_devices[QString("temp")][key.remove(QString("temp")).toInt()]; + } else if (key.startsWith(QString("response"))) { + AbstractExtItem *item = m_extNetRequest->itemByTag(key, stripped); + if (item) + output = item->uniq(); } else { output = QString("(none)"); } @@ -257,6 +266,8 @@ void AWKeyOperations::editItem(const QString type) keys.sort(); m_graphicalItems->setConfigArgs(keys); return m_graphicalItems->editItems(); + } else if (type == QString("extnetworkrequest")) { + return m_extNetRequest->editItems(); } else if (type == QString("extquotes")) { return m_extQuotes->editItems(); } else if (type == QString("extscript")) { @@ -308,6 +319,8 @@ void AWKeyOperations::reinitKeys() // delete them if any delete m_graphicalItems; m_graphicalItems = nullptr; + delete m_extNetRequest; + m_extNetRequest = nullptr; delete m_extQuotes; m_extQuotes = nullptr; delete m_extScripts; @@ -319,6 +332,8 @@ void AWKeyOperations::reinitKeys() // create m_graphicalItems = new ExtItemAggregator(nullptr, QString("desktops")); + m_extNetRequest = new ExtItemAggregator( + nullptr, QString("requests")); m_extQuotes = new ExtItemAggregator(nullptr, QString("quotes")); m_extScripts = new ExtItemAggregator(nullptr, QString("scripts")); diff --git a/sources/awesome-widget/plugin/awkeyoperations.h b/sources/awesome-widget/plugin/awkeyoperations.h index 9d92217..2a39c9c 100644 --- a/sources/awesome-widget/plugin/awkeyoperations.h +++ b/sources/awesome-widget/plugin/awkeyoperations.h @@ -30,6 +30,7 @@ class AWDataAggregator; class AWDataEngineAggregator; class AWKeysAggregator; +class ExtNetworkRequest; class ExtQuotes; class ExtScript; class ExtUpgrade; @@ -70,6 +71,7 @@ private: void reinitKeys(); // objects ExtItemAggregator *m_graphicalItems = nullptr; + ExtItemAggregator *m_extNetRequest = nullptr; ExtItemAggregator *m_extQuotes = nullptr; ExtItemAggregator *m_extScripts = nullptr; ExtItemAggregator *m_extUpgrade = nullptr; diff --git a/sources/awesome-widget/plugin/awkeysaggregator.cpp b/sources/awesome-widget/plugin/awkeysaggregator.cpp index 184cc04..7835418 100644 --- a/sources/awesome-widget/plugin/awkeysaggregator.cpp +++ b/sources/awesome-widget/plugin/awkeysaggregator.cpp @@ -441,6 +441,12 @@ QStringList AWKeysAggregator::registerSource(const QString &source, // network device m_map[source] = QString("netdev"); m_formatter[QString("netdev")] = FormatterType::NoFormat; + } else if (source.startsWith(QString("network/response"))) { + // network response + QString key = source; + key.remove(QString("network/")); + m_map[source] = key; + m_formatter[key] = FormatterType::NoFormat; } else if (source.contains(netRegExp)) { // network speed QString type = source.contains(QString("receiver")) ? QString("down") diff --git a/sources/awesome-widget/plugin/awtelemetryhandler.cpp b/sources/awesome-widget/plugin/awtelemetryhandler.cpp index b81fecd..7d0444f 100644 --- a/sources/awesome-widget/plugin/awtelemetryhandler.cpp +++ b/sources/awesome-widget/plugin/awtelemetryhandler.cpp @@ -144,7 +144,7 @@ void AWTelemetryHandler::uploadTelemetry(const QString group, // generate payload QVariantMap payload; - payload[QString("api")] = AWTEAPI; + payload[QString("api")] = AW_TELEMETRY_API; payload[QString("client_id")] = m_clientId; payload[QString("metadata")] = value; payload[QString("type")] = group; diff --git a/sources/awesomewidgets/CMakeLists.txt b/sources/awesomewidgets/CMakeLists.txt index a91ac8b..c8b6ef2 100644 --- a/sources/awesomewidgets/CMakeLists.txt +++ b/sources/awesomewidgets/CMakeLists.txt @@ -19,6 +19,7 @@ set(SUBPROJECT_FORMATTERS ${CMAKE_CURRENT_SOURCE_DIR}/formatters) set(SUBPROJECT_GRAPHITEMS ${CMAKE_CURRENT_SOURCE_DIR}/desktops) set(SUBPROJECT_QUOTES ${CMAKE_CURRENT_SOURCE_DIR}/quotes) set(SUBPROJECT_SCRIPTS ${CMAKE_CURRENT_SOURCE_DIR}/scripts) +set(SUBPROJECT_REQUESTS ${CMAKE_CURRENT_SOURCE_DIR}/requests) set(SUBPROJECT_UPGRADE ${CMAKE_CURRENT_SOURCE_DIR}/upgrade) set(SUBPROJECT_WEATHER ${CMAKE_CURRENT_SOURCE_DIR}/weather) file(GLOB SUBPROJECT_WEATHER_JSON_IN *.json) @@ -37,6 +38,7 @@ install(DIRECTORY ${SUBPROJECT_FORMATTERS} DESTINATION ${DATA_INSTALL_DIR}/${PRO install(DIRECTORY ${SUBPROJECT_GRAPHITEMS} DESTINATION ${DATA_INSTALL_DIR}/${PROJECT_NAME}) install(DIRECTORY ${SUBPROJECT_QUOTES} DESTINATION ${DATA_INSTALL_DIR}/${PROJECT_NAME}) install(DIRECTORY ${SUBPROJECT_SCRIPTS} DESTINATION ${DATA_INSTALL_DIR}/${PROJECT_NAME}) +install(DIRECTORY ${SUBPROJECT_REQUESTS} DESTINATION ${DATA_INSTALL_DIR}/${PROJECT_NAME}) install(DIRECTORY ${SUBPROJECT_UPGRADE} DESTINATION ${DATA_INSTALL_DIR}/${PROJECT_NAME}) install(DIRECTORY ${SUBPROJECT_WEATHER} DESTINATION ${DATA_INSTALL_DIR}/${PROJECT_NAME}) install(FILES ${SUBPROJECT_INI} DESTINATION ${CONFIG_INSTALL_DIR}) diff --git a/sources/awesomewidgets/awdatetimeformatter.cpp b/sources/awesomewidgets/awdatetimeformatter.cpp index e83ccca..4680da6 100644 --- a/sources/awesomewidgets/awdatetimeformatter.cpp +++ b/sources/awesomewidgets/awdatetimeformatter.cpp @@ -115,7 +115,7 @@ void AWDateTimeFormatter::readConfiguration() settings.value(QString("X-AW-Translate"), translateString()).toBool()); settings.endGroup(); - bumpApi(AWEFAPI); + bumpApi(AW_FORMATTER_API); } @@ -135,7 +135,7 @@ int AWDateTimeFormatter::showConfiguration(const QVariant args) return ret; setName(ui->lineEdit_name->text()); setComment(ui->lineEdit_comment->text()); - setApiVersion(AWEFAPI); + setApiVersion(AW_FORMATTER_API); setStrType(ui->label_typeValue->text()); setFormat(ui->lineEdit_format->text()); setTranslateString(ui->checkBox_translate->checkState() == Qt::Checked); diff --git a/sources/awesomewidgets/awfloatformatter.cpp b/sources/awesomewidgets/awfloatformatter.cpp index 24585bf..e39f8fd 100644 --- a/sources/awesomewidgets/awfloatformatter.cpp +++ b/sources/awesomewidgets/awfloatformatter.cpp @@ -210,7 +210,7 @@ void AWFloatFormatter::readConfiguration() setSummand(settings.value(QString("X-AW-Summand"), summand()).toDouble()); settings.endGroup(); - bumpApi(AWEFAPI); + bumpApi(AW_FORMATTER_API); } @@ -236,7 +236,7 @@ int AWFloatFormatter::showConfiguration(const QVariant args) return ret; setName(ui->lineEdit_name->text()); setComment(ui->lineEdit_comment->text()); - setApiVersion(AWEFAPI); + setApiVersion(AW_FORMATTER_API); setStrType(ui->label_typeValue->text()); setFormat(ui->comboBox_format->currentText().at(0).toLatin1()); setPrecision(ui->spinBox_precision->value()); diff --git a/sources/awesomewidgets/awjsonformatter.cpp b/sources/awesomewidgets/awjsonformatter.cpp index b0dd19e..81d12ee 100644 --- a/sources/awesomewidgets/awjsonformatter.cpp +++ b/sources/awesomewidgets/awjsonformatter.cpp @@ -21,6 +21,7 @@ #include +#include #include #include "awdebug.h" @@ -51,7 +52,12 @@ QString AWJsonFormatter::convert(const QVariant &_value) const { qCDebug(LOG_LIB) << "Convert value" << _value; - QVariant converted = _value; + // check if _value is string and parse first if required + QJsonDocument json + = _value.type() == QVariant::String + ? QJsonDocument::fromJson(_value.toString().toUtf8()) + : QJsonDocument::fromVariant(_value); + QVariant converted = json.toVariant(); for (auto &element : m_splittedPath) converted = getFromJson(converted, element); @@ -99,7 +105,7 @@ void AWJsonFormatter::readConfiguration() setPath(settings.value(QString("X-AW-Path"), path()).toString()); settings.endGroup(); - bumpApi(AWEFAPI); + bumpApi(AW_FORMATTER_API); } @@ -109,7 +115,7 @@ int AWJsonFormatter::showConfiguration(const QVariant args) ui->lineEdit_name->setText(name()); ui->lineEdit_comment->setText(comment()); - ui->label_typeValue->setText(QString("NoFormat")); + ui->label_typeValue->setText(QString("Json")); ui->lineEdit_path->setText(path()); int ret = exec(); @@ -117,7 +123,7 @@ int AWJsonFormatter::showConfiguration(const QVariant args) return ret; setName(ui->lineEdit_name->text()); setComment(ui->lineEdit_comment->text()); - setApiVersion(AWEFAPI); + setApiVersion(AW_FORMATTER_API); setStrType(ui->label_typeValue->text()); setPath(ui->lineEdit_path->text()); diff --git a/sources/awesomewidgets/awlistformatter.cpp b/sources/awesomewidgets/awlistformatter.cpp index 030db59..a72d97f 100644 --- a/sources/awesomewidgets/awlistformatter.cpp +++ b/sources/awesomewidgets/awlistformatter.cpp @@ -132,7 +132,7 @@ void AWListFormatter::readConfiguration() setSorted(settings.value(QString("X-AW-Sort"), isSorted()).toBool()); settings.endGroup(); - bumpApi(AWEFAPI); + bumpApi(AW_FORMATTER_API); } @@ -153,7 +153,7 @@ int AWListFormatter::showConfiguration(const QVariant args) return ret; setName(ui->lineEdit_name->text()); setComment(ui->lineEdit_comment->text()); - setApiVersion(AWEFAPI); + setApiVersion(AW_FORMATTER_API); setStrType(ui->label_typeValue->text()); setFilter(ui->lineEdit_filter->text()); setSeparator(ui->lineEdit_separator->text()); diff --git a/sources/awesomewidgets/awnoformatter.cpp b/sources/awesomewidgets/awnoformatter.cpp index 9c1734f..1ca4946 100644 --- a/sources/awesomewidgets/awnoformatter.cpp +++ b/sources/awesomewidgets/awnoformatter.cpp @@ -79,7 +79,7 @@ int AWNoFormatter::showConfiguration(const QVariant args) return ret; setName(ui->lineEdit_name->text()); setComment(ui->lineEdit_comment->text()); - setApiVersion(AWEFAPI); + setApiVersion(AW_FORMATTER_API); setStrType(ui->label_typeValue->text()); writeConfiguration(); diff --git a/sources/awesomewidgets/awscriptformatter.cpp b/sources/awesomewidgets/awscriptformatter.cpp index 3b15c9e..1393182 100644 --- a/sources/awesomewidgets/awscriptformatter.cpp +++ b/sources/awesomewidgets/awscriptformatter.cpp @@ -152,7 +152,7 @@ void AWScriptFormatter::readConfiguration() settings.value(QString("X-AW-HasReturn"), hasReturn()).toBool()); settings.endGroup(); - bumpApi(AWEFAPI); + bumpApi(AW_FORMATTER_API); } @@ -174,7 +174,7 @@ int AWScriptFormatter::showConfiguration(const QVariant args) return ret; setName(ui->lineEdit_name->text()); setComment(ui->lineEdit_comment->text()); - setApiVersion(AWEFAPI); + setApiVersion(AW_FORMATTER_API); setStrType(ui->label_typeValue->text()); setAppendCode(ui->checkBox_appendCode->checkState() == Qt::Checked); setHasReturn(ui->checkBox_hasReturn->checkState() == Qt::Checked); diff --git a/sources/awesomewidgets/awstringformatter.cpp b/sources/awesomewidgets/awstringformatter.cpp index a15e416..0b26cc8 100644 --- a/sources/awesomewidgets/awstringformatter.cpp +++ b/sources/awesomewidgets/awstringformatter.cpp @@ -133,7 +133,7 @@ void AWStringFormatter::readConfiguration() settings.value(QString("X-AW-ForceWidth"), forceWidth()).toBool()); settings.endGroup(); - bumpApi(AWEFAPI); + bumpApi(AW_FORMATTER_API); } @@ -154,7 +154,7 @@ int AWStringFormatter::showConfiguration(const QVariant args) return ret; setName(ui->lineEdit_name->text()); setComment(ui->lineEdit_comment->text()); - setApiVersion(AWEFAPI); + setApiVersion(AW_FORMATTER_API); setStrType(ui->label_typeValue->text()); setCount(ui->spinBox_width->value()); setFillChar(ui->lineEdit_fill->text().at(0)); diff --git a/sources/awesomewidgets/extnetworkrequest.cpp b/sources/awesomewidgets/extnetworkrequest.cpp new file mode 100644 index 0000000..ecc6afc --- /dev/null +++ b/sources/awesomewidgets/extnetworkrequest.cpp @@ -0,0 +1,210 @@ +/*************************************************************************** + * 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 "extnetworkrequest.h" +#include "ui_extnetworkrequest.h" + +#include + +#include +#include +#include +#include +#include + +#include + +#include "awdebug.h" + + +ExtNetworkRequest::ExtNetworkRequest(QWidget *parent, const QString filePath) + : AbstractExtItem(parent, filePath) + , ui(new Ui::ExtNetworkRequest) +{ + qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; + + if (!filePath.isEmpty()) + readConfiguration(); + ui->setupUi(this); + translate(); + + m_values[tag(QString("response"))] = QString(); + + // HACK declare as child of nullptr to avoid crash with plasmawindowed + // in the destructor + m_manager = new QNetworkAccessManager(nullptr); + connect(m_manager, SIGNAL(finished(QNetworkReply *)), this, + SLOT(networkReplyReceived(QNetworkReply *))); +} + + +ExtNetworkRequest::~ExtNetworkRequest() +{ + qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; + + disconnect(m_manager, SIGNAL(finished(QNetworkReply *)), this, + SLOT(networkReplyReceived(QNetworkReply *))); + + m_manager->deleteLater(); + delete ui; +} + + +ExtNetworkRequest *ExtNetworkRequest::copy(const QString _fileName, + const int _number) +{ + qCDebug(LOG_LIB) << "File" << _fileName << "with number" << _number; + + ExtNetworkRequest *item + = new ExtNetworkRequest(static_cast(parent()), _fileName); + copyDefaults(item); + item->setNumber(_number); + item->setStringUrl(stringUrl()); + + return item; +} + + +QString ExtNetworkRequest::stringUrl() const +{ + return m_stringUrl; +} + + +QString ExtNetworkRequest::uniq() const +{ + return m_url.toString(); +} + + +void ExtNetworkRequest::setStringUrl(const QString _url) +{ + qCDebug(LOG_LIB) << "Url" << _url; + + m_stringUrl = _url; + initUrl(); +} + + +void ExtNetworkRequest::readConfiguration() +{ + AbstractExtItem::readConfiguration(); + + QSettings settings(fileName(), QSettings::IniFormat); + + settings.beginGroup(QString("Desktop Entry")); + setStringUrl(settings.value(QString("X-AW-Url"), stringUrl()).toString()); + settings.endGroup(); + + bumpApi(AW_EXTNETREQUEST_API); +} + + +QVariantHash ExtNetworkRequest::run() +{ + if ((!isActive()) || (m_isRunning)) + return m_values; + + if (m_times == 1) { + qCInfo(LOG_LIB) << "Send request"; + m_isRunning = true; + QNetworkReply *reply = m_manager->get(QNetworkRequest(m_url)); + new QReplyTimeout(reply, REQUEST_TIMEOUT); + } + + // update value + if (m_times >= interval()) + m_times = 0; + m_times++; + + return m_values; +} + + +int ExtNetworkRequest::showConfiguration(const QVariant args) +{ + Q_UNUSED(args) + + ui->lineEdit_name->setText(name()); + ui->lineEdit_comment->setText(comment()); + ui->label_numberValue->setText(QString("%1").arg(number())); + ui->lineEdit_url->setText(stringUrl()); + ui->checkBox_active->setCheckState(isActive() ? Qt::Checked + : Qt::Unchecked); + ui->spinBox_interval->setValue(interval()); + + int ret = exec(); + if (ret != 1) + return ret; + setName(ui->lineEdit_name->text()); + setComment(ui->lineEdit_comment->text()); + setNumber(ui->label_numberValue->text().toInt()); + setApiVersion(AW_EXTNETREQUEST_API); + setStringUrl(ui->lineEdit_url->text()); + setActive(ui->checkBox_active->checkState() == Qt::Checked); + setInterval(ui->spinBox_interval->value()); + + writeConfiguration(); + return ret; +} + + +void ExtNetworkRequest::writeConfiguration() const +{ + AbstractExtItem::writeConfiguration(); + + QSettings settings(writtableConfig(), QSettings::IniFormat); + qCInfo(LOG_LIB) << "Configuration file" << settings.fileName(); + + settings.beginGroup(QString("Desktop Entry")); + settings.setValue(QString("X-AW-Url"), stringUrl()); + settings.endGroup(); + + settings.sync(); +} + +void ExtNetworkRequest::networkReplyReceived(QNetworkReply *reply) +{ + if (reply->error() != QNetworkReply::NoError) { + qCWarning(LOG_AW) << "An error occurs" << reply->error() + << "with message" << reply->errorString(); + return; + } + + m_isRunning = false; + m_values[tag(QString("response"))] + = QTextCodec::codecForMib(106)->toUnicode(reply->readAll()).trimmed(); + + emit(dataReceived(m_values)); +} + + +void ExtNetworkRequest::initUrl() +{ + m_url = QUrl(m_stringUrl); +} + + +void ExtNetworkRequest::translate() +{ + ui->label_name->setText(i18n("Name")); + ui->label_comment->setText(i18n("Comment")); + ui->label_number->setText(i18n("Tag")); + ui->label_url->setText(i18n("URL")); + ui->checkBox_active->setText(i18n("Active")); + ui->label_interval->setText(i18n("Interval")); +} diff --git a/sources/awesomewidgets/extnetworkrequest.h b/sources/awesomewidgets/extnetworkrequest.h new file mode 100644 index 0000000..78afdd0 --- /dev/null +++ b/sources/awesomewidgets/extnetworkrequest.h @@ -0,0 +1,71 @@ +/*************************************************************************** + * 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 EXTNETWORKREQUEST_H +#define EXTNETWORKREQUEST_H + +#include + +#include "abstractextitem.h" + + +namespace Ui +{ +class ExtNetworkRequest; +} + +class ExtNetworkRequest : public AbstractExtItem +{ + Q_OBJECT + Q_PROPERTY(QString stringUrl READ stringUrl WRITE setStringUrl) + +public: + explicit ExtNetworkRequest(QWidget *parent, + const QString filePath = QString()); + virtual ~ExtNetworkRequest(); + ExtNetworkRequest *copy(const QString _fileName, const int _number); + // get methods + QString stringUrl() const; + QString uniq() const; + // set methods + void setStringUrl(const QString _url = QString("https://httpbin.org/get")); + +public slots: + void readConfiguration(); + QVariantHash run(); + int showConfiguration(const QVariant args = QVariant()); + void writeConfiguration() const; + +private slots: + void networkReplyReceived(QNetworkReply *reply); + +private: + QNetworkAccessManager *m_manager = nullptr; + QUrl m_url; + bool m_isRunning = false; + Ui::ExtNetworkRequest *ui = nullptr; + void initUrl(); + void translate(); + // properties + QString m_stringUrl = QString("https://httpbin.org/get"); + // values + int m_times = 0; + QVariantHash m_values; +}; + + +#endif /* EXTNETWORKREQUEST_H */ diff --git a/sources/awesomewidgets/extnetworkrequest.ui b/sources/awesomewidgets/extnetworkrequest.ui new file mode 100644 index 0000000..3011853 --- /dev/null +++ b/sources/awesomewidgets/extnetworkrequest.ui @@ -0,0 +1,227 @@ + + + ExtNetworkRequest + + + + 0 + 0 + 420 + 301 + + + + Configuration + + + + + + + + + 0 + 0 + + + + Name + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + + 0 + 0 + + + + Comment + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + Tag + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + + + + + + 0 + 0 + + + + URL + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 0 + 0 + + + + Active + + + + + + + + + + + Interval + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + 1 + + + 10000 + + + 10 + + + 60 + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + ExtNetworkRequest + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + ExtNetworkRequest + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/sources/awesomewidgets/extquotes.cpp b/sources/awesomewidgets/extquotes.cpp index de54b63..045d8ab 100644 --- a/sources/awesomewidgets/extquotes.cpp +++ b/sources/awesomewidgets/extquotes.cpp @@ -119,7 +119,7 @@ void ExtQuotes::readConfiguration() setTicker(settings.value(QString("X-AW-Ticker"), ticker()).toString()); settings.endGroup(); - bumpApi(AWEQAPI); + bumpApi(AW_EXTQUOTES_API); } @@ -162,7 +162,7 @@ int ExtQuotes::showConfiguration(const QVariant args) setName(ui->lineEdit_name->text()); setComment(ui->lineEdit_comment->text()); setNumber(ui->label_numberValue->text().toInt()); - setApiVersion(AWEQAPI); + setApiVersion(AW_EXTQUOTES_API); setTicker(ui->lineEdit_ticker->text()); setActive(ui->checkBox_active->checkState() == Qt::Checked); setInterval(ui->spinBox_interval->value()); diff --git a/sources/awesomewidgets/extscript.cpp b/sources/awesomewidgets/extscript.cpp index 781413b..fd79c4f 100644 --- a/sources/awesomewidgets/extscript.cpp +++ b/sources/awesomewidgets/extscript.cpp @@ -229,7 +229,7 @@ void ExtScript::readConfiguration() .split(QChar(','), QString::SkipEmptyParts)); settings.endGroup(); - bumpApi(AWESAPI); + bumpApi(AW_EXTSCRIPT_API); } @@ -313,7 +313,7 @@ int ExtScript::showConfiguration(const QVariant args) setName(ui->lineEdit_name->text()); setComment(ui->lineEdit_comment->text()); setNumber(ui->label_numberValue->text().toInt()); - setApiVersion(AWESAPI); + setApiVersion(AW_EXTSCRIPT_API); setExecutable(ui->lineEdit_command->text()); setPrefix(ui->lineEdit_prefix->text()); setActive(ui->checkBox_active->checkState() == Qt::Checked); diff --git a/sources/awesomewidgets/extupgrade.cpp b/sources/awesomewidgets/extupgrade.cpp index ef73a6d..8afa5f4 100644 --- a/sources/awesomewidgets/extupgrade.cpp +++ b/sources/awesomewidgets/extupgrade.cpp @@ -136,7 +136,7 @@ void ExtUpgrade::readConfiguration() setFilter(settings.value(QString("X-AW-Filter"), filter()).toString()); settings.endGroup(); - bumpApi(AWEUAPI); + bumpApi(AW_EXTUPGRADE_API); } @@ -180,7 +180,7 @@ int ExtUpgrade::showConfiguration(const QVariant args) setName(ui->lineEdit_name->text()); setComment(ui->lineEdit_comment->text()); setNumber(ui->label_numberValue->text().toInt()); - setApiVersion(AWEUAPI); + setApiVersion(AW_EXTUPGRADE_API); setExecutable(ui->lineEdit_command->text()); setFilter(ui->lineEdit_filter->text()); setActive(ui->checkBox_active->checkState() == Qt::Checked); diff --git a/sources/awesomewidgets/extweather.cpp b/sources/awesomewidgets/extweather.cpp index 75da21d..9f5c403 100644 --- a/sources/awesomewidgets/extweather.cpp +++ b/sources/awesomewidgets/extweather.cpp @@ -227,7 +227,7 @@ void ExtWeather::readConfiguration() settings.value(QString("X-AW-Provider"), strProvider()).toString()); settings.endGroup(); - bumpApi(AWEWAPI); + bumpApi(AW_EXTWEATHER_API); } @@ -301,7 +301,7 @@ int ExtWeather::showConfiguration(const QVariant args) setName(ui->lineEdit_name->text()); setComment(ui->lineEdit_comment->text()); setNumber(ui->label_numberValue->text().toInt()); - setApiVersion(AWEWAPI); + setApiVersion(AW_EXTWEATHER_API); setCity(ui->lineEdit_city->text()); setCountry(ui->lineEdit_country->text()); setProvider(static_cast(ui->comboBox_provider->currentIndex())); diff --git a/sources/awesomewidgets/graphicalitem.cpp b/sources/awesomewidgets/graphicalitem.cpp index 8a9c32c..596cb17 100644 --- a/sources/awesomewidgets/graphicalitem.cpp +++ b/sources/awesomewidgets/graphicalitem.cpp @@ -455,7 +455,7 @@ void GraphicalItem::readConfiguration() } settings.endGroup(); - bumpApi(AWGIAPI); + bumpApi(AW_GRAPHITEM_API); initScene(); } @@ -504,7 +504,7 @@ int GraphicalItem::showConfiguration(const QVariant args) return ret; setName(ui->lineEdit_name->text()); setComment(ui->lineEdit_comment->text()); - setApiVersion(AWGIAPI); + setApiVersion(AW_GRAPHITEM_API); setCount(ui->spinBox_count->value()); setCustom(ui->checkBox_custom->isChecked()); setBar(m_custom ? ui->lineEdit_customValue->text() diff --git a/sources/awesomewidgets/requests/httpbin.desktop b/sources/awesomewidgets/requests/httpbin.desktop new file mode 100644 index 0000000..b7d729e --- /dev/null +++ b/sources/awesomewidgets/requests/httpbin.desktop @@ -0,0 +1,9 @@ +[Desktop Entry] +Encoding=UTF-8 +Name=httpbin +Comment=httpbin example +X-AW-Ticker=https://httpbin.org/get +X-AW-Active=false +X-AW-ApiVersion=1 +X-AW-Interval=10 +X-AW-Number=0 diff --git a/sources/extsysmon/extsysmonaggregator.cpp b/sources/extsysmon/extsysmonaggregator.cpp index 050a748..f750e61 100644 --- a/sources/extsysmon/extsysmonaggregator.cpp +++ b/sources/extsysmon/extsysmonaggregator.cpp @@ -29,6 +29,7 @@ #include "playersource.h" #include "processessource.h" #include "quotessource.h" +#include "requestsource.h" #include "upgradesource.h" #include "weathersource.h" @@ -134,6 +135,11 @@ void ExtSysMonAggregator::init(const QHash config) = new ProcessesSource(this, QStringList()); for (auto source : processesItem->sources()) m_map[source] = processesItem; + // network request + AbstractExtSysMonSource *requestItem + = new RequestSource(this, QStringList()); + for (auto source : requestItem->sources()) + m_map[source] = requestItem; // quotes AbstractExtSysMonSource *quotesItem = new QuotesSource(this, QStringList()); for (auto source : quotesItem->sources()) diff --git a/sources/extsysmonsources/requestsource.cpp b/sources/extsysmonsources/requestsource.cpp new file mode 100644 index 0000000..b52d8ea --- /dev/null +++ b/sources/extsysmonsources/requestsource.cpp @@ -0,0 +1,95 @@ +/*************************************************************************** + * 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 "requestsource.h" + +#include "awdebug.h" +#include "extnetworkrequest.h" + + +RequestSource::RequestSource(QObject *parent, const QStringList args) + : AbstractExtSysMonSource(parent, args) +{ + Q_ASSERT(args.count() == 0); + qCDebug(LOG_ESS) << __PRETTY_FUNCTION__; + + m_extNetRequest = new ExtItemAggregator( + nullptr, QString("requests")); + m_sources = getSources(); +} + + +RequestSource::~RequestSource() +{ + qCDebug(LOG_ESS) << __PRETTY_FUNCTION__; + + delete m_extNetRequest; +} + + +QVariant RequestSource::data(QString source) +{ + qCDebug(LOG_ESS) << "Source" << source; + + int ind = index(source); + source.remove(QString("network/")); + if (!m_values.contains(source)) { + QVariantHash data = m_extNetRequest->itemByTagNumber(ind)->run(); + for (auto key : data.keys()) + m_values[key] = data[key]; + } + QVariant value = m_values.take(source); + return value; +} + + +QVariantMap RequestSource::initialData(QString source) const +{ + qCDebug(LOG_ESS) << "Source" << source; + + int ind = index(source); + QVariantMap data; + if (source.startsWith(QString("network/response"))) { + data[QString("min")] = QString(""); + data[QString("max")] = QString(""); + data[QString("name")] + = QString("Network response for %1") + .arg(m_extNetRequest->itemByTagNumber(ind)->uniq()); + data[QString("type")] = QString("QString"); + data[QString("units")] = QString(""); + } + + return data; +} + + +QStringList RequestSource::sources() const +{ + return m_sources; +} + + +QStringList RequestSource::getSources() +{ + QStringList sources; + for (auto item : m_extNetRequest->activeItems()) + sources.append( + QString("network/%1").arg(item->tag(QString("response")))); + + return sources; +} diff --git a/sources/extsysmonsources/requestsource.h b/sources/extsysmonsources/requestsource.h new file mode 100644 index 0000000..02cb47c --- /dev/null +++ b/sources/extsysmonsources/requestsource.h @@ -0,0 +1,50 @@ +/*************************************************************************** + * 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 REQUESTSOURCE_H +#define REQUESTSOURCE_H + +#include + +#include "abstractextsysmonsource.h" +#include "extitemaggregator.h" + + +class ExtNetworkRequest; + +class RequestSource : public AbstractExtSysMonSource +{ + Q_OBJECT + +public: + explicit RequestSource(QObject *parent, const QStringList args); + virtual ~RequestSource(); + QVariant data(QString source); + QVariantMap initialData(QString source) const; + void run(){}; + QStringList sources() const; + +private: + QStringList getSources(); + // configuration and values + ExtItemAggregator *m_extNetRequest = nullptr; + QStringList m_sources; + QVariantHash m_values; +}; + + +#endif /* REQUESTSOURCE_H */ diff --git a/sources/version.h.in b/sources/version.h.in index b59f5bd..866127a 100644 --- a/sources/version.h.in +++ b/sources/version.h.in @@ -25,19 +25,21 @@ const char CHANGELOG[] = "@PROJECT_CHANGELOG@"; // configuraion // graphical items api version -const int AWGIAPI = 5; +const int AW_GRAPHITEM_API = 5; // extquotes api version -const int AWEQAPI = 3; +const int AW_EXTQUOTES_API = 3; // extscript api version -const int AWESAPI = 4; +const int AW_EXTSCRIPT_API = 4; // extupgrade api version -const int AWEUAPI = 3; +const int AW_EXTUPGRADE_API = 3; // extweather api version -const int AWEWAPI = 3; +const int AW_EXTWEATHER_API = 3; +// extnetworkrequest api version +const int AW_EXTNETREQUEST_API = 1; // formatter api version -const int AWEFAPI = 2; +const int AW_FORMATTER_API = 3; // telemetry api version -const int AWTEAPI = 1; +const int AW_TELEMETRY_API = 1; // dbus adaptor properties // use define here instead of normal const definition for moc #define AWDBUS_SERVICE_NAME "org.kde.plasma.awesomewidget"