magpie/API/api.h

53 lines
1.5 KiB
C
Raw Normal View History

2023-11-24 23:48:01 +00:00
#pragma once
#include <memory>
#include <QObject>
#include <QString>
#include <QUrl>
#include <QJSValue>
#include <QNetworkAccessManager>
#include <QNetworkReply>
2023-12-17 00:06:04 +00:00
class Root;
2023-11-24 23:48:01 +00:00
class API : public QObject {
2023-12-17 00:06:04 +00:00
friend class Root;
2023-11-24 23:48:01 +00:00
Q_OBJECT
2023-12-16 01:44:25 +00:00
public:
enum State {Offline, NoServer, NotAuthenticated, Authenticated};
Q_ENUM(State)
private:
2023-12-17 00:06:04 +00:00
Q_PROPERTY(QUrl address READ getAddress NOTIFY addressChanged)
2023-12-16 01:44:25 +00:00
Q_PROPERTY(State state READ getState NOTIFY stateChanged)
2023-11-24 23:48:01 +00:00
public:
explicit API(const QUrl& path = QString(), QObject* parent = nullptr);
2023-12-16 01:44:25 +00:00
QUrl getAddress() const;
State getState() const;
signals:
void addressChanged(const QUrl& path);
void stateChanged(State state);
public slots:
void test(const QString& path, const QJSValue& finished = QJSValue());
void sendRegister(const QString& login, const QString& password, const QJSValue& finished = QJSValue());
void sendLogin(const QString& login, const QString& password, const QJSValue& finished = QJSValue());
2023-11-24 23:48:01 +00:00
private slots:
2023-12-17 00:06:04 +00:00
void onTestFinished(QNetworkReply* reply, const QUrl& addr, const QJSValue& finished);
2023-12-16 01:44:25 +00:00
void onRegisterFinished(QNetworkReply* reply, const QJSValue& finished) const;
void onLoginFinished(QNetworkReply* reply, const QJSValue& finished);
2023-11-24 23:48:01 +00:00
private:
void callCallback(const QJSValue& callback, const QString& error = QString(), const QJSValueList& arguments = QJSValueList()) const;
2023-12-17 00:06:04 +00:00
void setAddress(const QUrl& path);
2023-11-24 23:48:01 +00:00
private:
QUrl address;
QNetworkAccessManager network;
2023-12-16 01:44:25 +00:00
State state;
2023-11-24 23:48:01 +00:00
};