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

View File

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

View File

@ -1,5 +1,5 @@
qt_add_qml_module(magpieQml qt_add_qml_module(magpieQml
URI "qml" URI qml
VERSION 1.0 VERSION 1.0
STATIC STATIC
RESOURCE_PREFIX / RESOURCE_PREFIX /
@ -11,3 +11,5 @@ qt_add_qml_module(magpieQml
) )
target_link_libraries(magpie PRIVATE 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.Controls
import QtQuick.Layouts import QtQuick.Layouts
import API import magpie.API
Page { Page {
property string address property string address
property bool valid: false
signal back() signal back()
signal success(address: string)
title: qsTr("Chosing a server") title: qsTr("Chosing a server")
@ -59,11 +57,7 @@ Page {
width: column.width + 60 width: column.width + 60
height: column.height + 60 height: column.height + 60
closePolicy: Popup.CloseOnEscape closePolicy: Popup.CloseOnEscape
onClosed: function () { onClosed: modal.inProgress = false
modal.inProgress = false;
if (valid)
success(address);
}
Column { Column {
id: column id: column
@ -113,10 +107,8 @@ Page {
} }
function check () { function check () {
if (valid) { if (modal.inProgress)
success(address);
return; return;
}
modal.inProgress = true; modal.inProgress = true;
status.text = qsTr("Checking") + " " + address + "..."; status.text = qsTr("Checking") + " " + address + "...";
@ -132,11 +124,8 @@ Page {
else else
status.text = qsTr("Success"); status.text = qsTr("Success");
valid = !!success; if (!!success)
if (valid) {
address = input.text;
modal.close() modal.close()
}
}); });
} }
} }

View File

@ -2,7 +2,8 @@ import QtQuick
import QtQuick.Controls import QtQuick.Controls
import QtQuick.Layouts import QtQuick.Layouts
import API import magpie.API
import "Forms" as Forms
Page { Page {
signal pickServer(address: string) signal pickServer(address: string)
@ -53,145 +54,16 @@ Page {
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
topPadding: 10 topPadding: 10
Column { Forms.Login {
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 {
id: login id: login
visible: !forms.registering
onRegister: forms.registering = true
} }
Label { Forms.Register {
text: qsTr("Password") + ":"; id: register
} visible: forms.registering
onLogin: forms.registering = false
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
}
}
}
} }
} }
} }

View File

@ -4,10 +4,11 @@ import QtQuick.Controls
import QtQuick.Layouts import QtQuick.Layouts
import QtCore import QtCore
import API import magpie.API
ApplicationWindow { ApplicationWindow {
property int counter: 0 property int counter: 0
property bool pickingServer: false
id: window id: window
width: 640 width: 640
@ -24,23 +25,32 @@ ApplicationWindow {
id: stack id: stack
initialItem: welcome initialItem: welcome
anchors.fill: parent anchors.fill: parent
StackView.onRemoved: pickingServer = false
}
Connections {
target: API
function onAddressChanged (url) {
if (pickingServer && url.toString().length > 0)
stack.pop()
}
} }
Welcome { Welcome {
id: welcome id: welcome
onPickServer: function (address) { onPickServer: function (address) {
const pick = pickComponent.createObject(stack);
pick.address = address; pick.address = address;
stack.push(pick) stack.push(pick);
pickingServer = true;
} }
} }
Component {
id: pickComponent
ServerPick { ServerPick {
visible: false StackView.onRemoved: destroy()
id: pick
onBack: stack.pop() 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"); qRegisterMetaType<API>("API");
connect(&api, &API::addressChanged, this, &Root::onAPIAddressChanged); 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; return &api;
}); });
engine.addImportPath(":/");
engine.load(root); engine.load(root);
if (engine.rootObjects().isEmpty()) if (engine.rootObjects().isEmpty())
throw std::runtime_error("Couldn't looad root qml object"); throw std::runtime_error("Couldn't looad root qml object");