summaryrefslogtreecommitdiff
path: root/plugin/src
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/src')
-rw-r--r--plugin/src/Caelestia/CMakeLists.txt1
-rw-r--r--plugin/src/Caelestia/requests.cpp35
-rw-r--r--plugin/src/Caelestia/requests.hpp23
3 files changed, 59 insertions, 0 deletions
diff --git a/plugin/src/Caelestia/CMakeLists.txt b/plugin/src/Caelestia/CMakeLists.txt
index 25cae86..05cc350 100644
--- a/plugin/src/Caelestia/CMakeLists.txt
+++ b/plugin/src/Caelestia/CMakeLists.txt
@@ -23,6 +23,7 @@ qt_add_qml_module(caelestia
audioprovider.hpp audioprovider.cpp
cavaprovider.hpp cavaprovider.cpp
appdb.hpp appdb.cpp
+ requests.hpp requests.cpp
)
qt_query_qml_module(caelestia
diff --git a/plugin/src/Caelestia/requests.cpp b/plugin/src/Caelestia/requests.cpp
new file mode 100644
index 0000000..2ceddb3
--- /dev/null
+++ b/plugin/src/Caelestia/requests.cpp
@@ -0,0 +1,35 @@
+#include "requests.hpp"
+
+#include <qnetworkaccessmanager.h>
+#include <qnetworkreply.h>
+#include <qnetworkrequest.h>
+
+namespace caelestia {
+
+Requests::Requests(QObject* parent)
+ : QObject(parent)
+ , m_manager(new QNetworkAccessManager(this)) {}
+
+void Requests::get(const QUrl& url, QJSValue onSuccess, QJSValue onError) const {
+ if (!onSuccess.isCallable()) {
+ qWarning() << "Requests::get: onSuccess is not callable";
+ return;
+ }
+
+ QNetworkRequest request(url);
+ auto reply = m_manager->get(request);
+
+ QObject::connect(reply, &QNetworkReply::finished, [reply, onSuccess, onError]() {
+ if (reply->error() == QNetworkReply::NoError) {
+ onSuccess.call({ QString(reply->readAll()) });
+ } else if (onError.isCallable()) {
+ onError.call({ reply->errorString() });
+ } else {
+ qWarning() << "Requests::get: request failed with error" << reply->errorString();
+ }
+
+ reply->deleteLater();
+ });
+}
+
+} // namespace caelestia
diff --git a/plugin/src/Caelestia/requests.hpp b/plugin/src/Caelestia/requests.hpp
new file mode 100644
index 0000000..1db2f4c
--- /dev/null
+++ b/plugin/src/Caelestia/requests.hpp
@@ -0,0 +1,23 @@
+#pragma once
+
+#include <qnetworkaccessmanager.h>
+#include <qobject.h>
+#include <qqmlengine.h>
+
+namespace caelestia {
+
+class Requests : public QObject {
+ Q_OBJECT
+ QML_ELEMENT
+ QML_SINGLETON
+
+public:
+ explicit Requests(QObject* parent = nullptr);
+
+ Q_INVOKABLE void get(const QUrl& url, QJSValue callback, QJSValue onError = QJSValue()) const;
+
+private:
+ QNetworkAccessManager* m_manager;
+};
+
+} // namespace caelestia