some tinkering

This commit is contained in:
Blue 2023-12-16 21:06:04 -03:00
parent c966d95058
commit ed1ed9fb49
Signed by: blue
GPG Key ID: 9B203B252A63EE38
10 changed files with 230 additions and 229 deletions

View File

@ -45,32 +45,24 @@ void API::setAddress(const QUrl& path) {
void API::test(const QString& path, const QJSValue& finished) {
qDebug() << "Testing" << path;
if (state == Offline)
return callCallback(finished, "Need to be online to test");
if (state == Offline) {
QString err = "Need to be online to test";
qDebug() << "Test for" << path << "failed:" << err;
callCallback(finished, err);
return;
}
QUrl address(path);
QNetworkRequest request(path + "/info");
request.setHeader(QNetworkRequest::ContentTypeHeader, json);
QNetworkReply* reply = network.get(request);
connect(reply, &QNetworkReply::finished,
std::bind(&API::onTestFinished, this, reply, finished)
std::bind(&API::onTestFinished, this, reply, address, finished)
);
}
void API::onTestFinished(QNetworkReply* reply, const QJSValue& finished) const {
void API::onTestFinished(QNetworkReply* reply, const QUrl& addr, const QJSValue& finished) {
std::unique_ptr<QNetworkReply, NetworkReplyDeleter> rpl(reply);
QNetworkReply::NetworkError error = reply->error();
if (error != QNetworkReply::NoError) {
QString err = reply->errorString();
qDebug() << "Test for" << reply->url() << "failed:" << err;
callCallback(finished, err);
return;
}
if (error != QNetworkReply::NoError)
return callCallback(finished, reply->errorString());
QVariant contentType = reply->header(QNetworkRequest::ContentTypeHeader);
if (!
@ -78,10 +70,7 @@ void API::onTestFinished(QNetworkReply* reply, const QJSValue& finished) const {
!contentType.canConvert<QString>() ||
contentType.toString() != json
) {
QString err("wrong response content type");
qDebug() << "Test for" << reply->url() << "failed:" << err;
callCallback(finished, err);
return;
return callCallback(finished, "wrong response content type");
}
QByteArray data = reply->readAll();
@ -90,39 +79,25 @@ void API::onTestFinished(QNetworkReply* reply, const QJSValue& finished) const {
QJsonValue type = rootObj.value("type");
QJsonValue version = rootObj.value("version");
if (!type.isString() || !version.isString()) {
QString err("malformed json");
qDebug() << "Test for" << reply->url() << "failed:" << err;
callCallback(finished, err);
return;
}
if (!type.isString() || !version.isString())
return callCallback(finished, "malformed json");
if (type.toString() != "pica") {
QString err("server of this type (" + type.toString() + ") is not supported");
qDebug() << "Test for" << reply->url() << "failed:" << err;
callCallback(finished, err);
return;
}
if (type.toString() != "pica")
return callCallback(finished, "server of this type (" + type.toString() + ") is not supported");
if (version.toString() != "0.0.1") {
QString err("server of this version (" + version.toString() + ") is not supported");
qDebug() << "Test for" << reply->url() << "failed:" << err;
callCallback(finished, err);
return;
}
if (version.toString() != "0.0.1")
return callCallback(finished, "server of this version (" + version.toString() + ") is not supported");
callCallback(finished, QString(), {QJSValue(true)});
address = ""; //to provoke singal change even if it's the same server
setAddress(addr);
}
void API::sendRegister(const QString& login, const QString& password, const QJSValue &finished) {
qDebug() << "Registering...";
if (state != NotAuthenticated) {
QString err = "Can not register in current state";
qDebug() << "Register failed:" << err;
callCallback(finished, err);
if (state != NotAuthenticated)
callCallback(finished, "Can not register in current state");
return;
}
QUrlQuery params({
{"login", login},
@ -141,12 +116,8 @@ void API::sendRegister(const QString& login, const QString& password, const QJSV
void API::onRegisterFinished(QNetworkReply *reply, const QJSValue &finished) const {
std::unique_ptr<QNetworkReply, NetworkReplyDeleter> rpl(reply);
QNetworkReply::NetworkError error = reply->error();
if (error != QNetworkReply::NoError) {
QString err = reply->errorString();
qDebug() << "Register failed:" << err;
callCallback(finished, err);
return;
}
if (error != QNetworkReply::NoError)
return callCallback(finished, reply->errorString());
QVariant contentType = reply->header(QNetworkRequest::ContentTypeHeader);
if (!
@ -165,19 +136,11 @@ void API::onRegisterFinished(QNetworkReply *reply, const QJSValue &finished) con
QJsonObject rootObj = document.object();
QJsonValue result = rootObj.value("result");
if (!result.isString()) {
QString err("malformed json");
qDebug() << "Register failed:" << err;
callCallback(finished, err);
return;
}
if (!result.isString())
return callCallback(finished, "malformed json");
if (result.toString() != "ok") {
QString err("Registration result was not okay");
qDebug() << "Register failed:" << err;
callCallback(finished, err);
return;
}
if (result.toString() != "ok")
return callCallback(finished, "Registration result was not okay");
callCallback(finished, QString(), {QJSValue(true)});
}

View File

@ -9,14 +9,16 @@
#include <QNetworkAccessManager>
#include <QNetworkReply>
class Root;
class API : public QObject {
friend class Root;
Q_OBJECT
public:
enum State {Offline, NoServer, NotAuthenticated, Authenticated};
Q_ENUM(State)
private:
Q_PROPERTY(QUrl address READ getAddress WRITE setAddress NOTIFY addressChanged)
Q_PROPERTY(QUrl address READ getAddress NOTIFY addressChanged)
Q_PROPERTY(State state READ getState NOTIFY stateChanged)
public:
@ -25,8 +27,6 @@ public:
QUrl getAddress() const;
State getState() const;
void setAddress(const QUrl& path);
signals:
void addressChanged(const QUrl& path);
void stateChanged(State state);
@ -36,11 +36,12 @@ public slots:
void sendRegister(const QString& login, const QString& password, const QJSValue& finished = QJSValue());
private slots:
void onTestFinished(QNetworkReply* reply, const QJSValue& finished) const;
void onTestFinished(QNetworkReply* reply, const QUrl& addr, const QJSValue& finished);
void onRegisterFinished(QNetworkReply* reply, const QJSValue& finished) const;
private:
void callCallback(const QJSValue& callback, const QString& error = QString(), const QJSValueList& arguments = QJSValueList()) const;
void setAddress(const QUrl& path);
private:
QUrl address;

View File

@ -1,5 +1,5 @@
qt_add_qml_module(magpieQml
URI "qml"
URI qml
VERSION 1.0
STATIC
RESOURCE_PREFIX /
@ -11,3 +11,5 @@ qt_add_qml_module(magpieQml
)
target_link_libraries(magpie PRIVATE magpieQml)
add_subdirectory(Forms)

12
qml/Forms/CMakeLists.txt Normal file
View File

@ -0,0 +1,12 @@
qt_add_qml_module(magpieForms
URI "qml.Forms"
VERSION 1.0
STATIC
RESOURCE_PREFIX /
NO_PLUGIN
QML_FILES
Login.qml
Register.qml
)
target_link_libraries(magpie PRIVATE magpieForms)

74
qml/Forms/Login.qml Normal file
View File

@ -0,0 +1,74 @@
import QtQuick
import QtQuick.Controls
import magpie.API
Column {
signal register()
spacing: 10
Label {
anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Please, log in to your account")
font {
pixelSize: 14
}
}
Grid {
anchors.horizontalCenter: parent.horizontalCenter
columns: 2
columnSpacing: 10
rowSpacing: 5
verticalItemAlignment: Grid.AlignVCenter
horizontalItemAlignment: Grid.AlignRight
Label {
text: qsTr("Login") + ":";
}
TextField {
id: login
}
Label {
text: qsTr("Password") + ":";
}
TextField {
id: password
echoMode: TextField.Password
}
}
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Login")
onClicked: function () {
console.log("Not implemented");
}
}
Row {
anchors.horizontalCenter: parent.horizontalCenter
spacing: 5
topPadding: 10
Label {
text: qsTr("Don't have account?")
}
Label {
text: qsTr("Sign up") + "!"
font {
italic: true
underline: true
}
MouseArea {
anchors.fill: parent
onClicked: register()
}
}
}
}

77
qml/Forms/Register.qml Normal file
View File

@ -0,0 +1,77 @@
import QtQuick
import QtQuick.Controls
import magpie.API
Column {
signal login()
spacing: 10
Label {
anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Please, chose login and password")
font {
pixelSize: 14
}
}
Grid {
anchors.horizontalCenter: parent.horizontalCenter
columns: 2
columnSpacing: 10
rowSpacing: 5
verticalItemAlignment: Grid.AlignVCenter
horizontalItemAlignment: Grid.AlignRight
Label {
text: qsTr("Login") + ":";
}
TextField {
id: newLogin
}
Label {
text: qsTr("Password") + ":";
}
TextField {
id: newPassword
echoMode: TextField.Password
}
}
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Register")
onClicked: API.sendRegister(newLogin.text, newPassword.text, function (err, result) {
if (err)
console.error("err")
console.log(result);
})
}
Row {
anchors.horizontalCenter: parent.horizontalCenter
spacing: 5
topPadding: 10
Label {
text: qsTr("Already have an account?")
}
Label {
text: qsTr("Log in") + "!"
font {
italic: true
underline: true
}
MouseArea {
anchors.fill: parent
onClicked: login()
}
}
}
}

View File

@ -2,14 +2,12 @@ import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import API
import magpie.API
Page {
property string address
property bool valid: false
signal back()
signal success(address: string)
title: qsTr("Chosing a server")
@ -59,11 +57,7 @@ Page {
width: column.width + 60
height: column.height + 60
closePolicy: Popup.CloseOnEscape
onClosed: function () {
modal.inProgress = false;
if (valid)
success(address);
}
onClosed: modal.inProgress = false
Column {
id: column
@ -113,10 +107,8 @@ Page {
}
function check () {
if (valid) {
success(address);
if (modal.inProgress)
return;
}
modal.inProgress = true;
status.text = qsTr("Checking") + " " + address + "...";
@ -132,11 +124,8 @@ Page {
else
status.text = qsTr("Success");
valid = !!success;
if (valid) {
address = input.text;
if (!!success)
modal.close()
}
});
}
}

View File

@ -2,7 +2,8 @@ import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import API
import magpie.API
import "Forms" as Forms
Page {
signal pickServer(address: string)
@ -53,145 +54,16 @@ Page {
anchors.horizontalCenter: parent.horizontalCenter
topPadding: 10
Column {
visible: forms.registering === false
spacing: 10
Label {
anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Please, log in to your account")
font {
pixelSize: 14
}
}
Grid {
anchors.horizontalCenter: parent.horizontalCenter
columns: 2
columnSpacing: 10
rowSpacing: 5
verticalItemAlignment: Grid.AlignVCenter
horizontalItemAlignment: Grid.AlignRight
Label {
text: qsTr("Login") + ":";
}
TextField {
Forms.Login {
id: login
visible: !forms.registering
onRegister: forms.registering = true
}
Label {
text: qsTr("Password") + ":";
}
TextField {
id: password
echoMode: TextField.Password
}
}
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Login")
onClicked: function () {
console.log("Not implemented");
}
}
Row {
anchors.horizontalCenter: parent.horizontalCenter
spacing: 5
topPadding: 10
Label {
text: qsTr("Don't have account?")
}
Label {
text: qsTr("Sign up") + "!"
font {
italic: true
underline: true
}
MouseArea {
anchors.fill: parent
onClicked: forms.registering = true
}
}
}
}
Column {
visible: forms.registering === true
spacing: 10
Label {
anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Please, chose login and password")
font {
pixelSize: 14
}
}
Grid {
anchors.horizontalCenter: parent.horizontalCenter
columns: 2
columnSpacing: 10
rowSpacing: 5
verticalItemAlignment: Grid.AlignVCenter
horizontalItemAlignment: Grid.AlignRight
Label {
text: qsTr("Login") + ":";
}
TextField {
id: newLogin
}
Label {
text: qsTr("Password") + ":";
}
TextField {
id: newPassword
echoMode: TextField.Password
}
}
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Register")
onClicked: API.sendRegister(newLogin.text, newPassword.text, function (err, result) {
if (err)
console.error("err")
console.log(result);
})
}
Row {
anchors.horizontalCenter: parent.horizontalCenter
spacing: 5
topPadding: 10
Label {
text: qsTr("Already have an account?")
}
Label {
text: qsTr("Log in") + "!"
font {
italic: true
underline: true
}
MouseArea {
anchors.fill: parent
onClicked: forms.registering = false
}
}
}
Forms.Register {
id: register
visible: forms.registering
onLogin: forms.registering = false
}
}
}

View File

@ -4,10 +4,11 @@ import QtQuick.Controls
import QtQuick.Layouts
import QtCore
import API
import magpie.API
ApplicationWindow {
property int counter: 0
property bool pickingServer: false
id: window
width: 640
@ -24,23 +25,32 @@ ApplicationWindow {
id: stack
initialItem: welcome
anchors.fill: parent
StackView.onRemoved: pickingServer = false
}
Connections {
target: API
function onAddressChanged (url) {
if (pickingServer && url.toString().length > 0)
stack.pop()
}
}
Welcome {
id: welcome
onPickServer: function (address) {
const pick = pickComponent.createObject(stack);
pick.address = address;
stack.push(pick)
stack.push(pick);
pickingServer = true;
}
}
Component {
id: pickComponent
ServerPick {
visible: false
id: pick
StackView.onRemoved: destroy()
onBack: stack.pop()
onSuccess: function (address) {
API.address = address
stack.pop();
}
}
}

View File

@ -29,10 +29,11 @@ Root::Root(const QUrl& root, int& argc, char* argv[]) :
qRegisterMetaType<API>("API");
connect(&api, &API::addressChanged, this, &Root::onAPIAddressChanged);
qmlRegisterSingletonType<API>("API", 1, 0, "API", [this] (QQmlEngine *engine, QJSEngine *scriptEngine) {
qmlRegisterSingletonType<API>("magpie.API", 1, 0, "API", [this] (QQmlEngine *engine, QJSEngine *scriptEngine) {
return &api;
});
engine.addImportPath(":/");
engine.load(root);
if (engine.rootObjects().isEmpty())
throw std::runtime_error("Couldn't looad root qml object");