From ed1ed9fb49dd814d01cec142ce875ce67bb3481c Mon Sep 17 00:00:00 2001 From: blue Date: Sat, 16 Dec 2023 21:06:04 -0300 Subject: [PATCH] some tinkering --- API/api.cpp | 85 +++++++--------------- API/api.h | 9 +-- qml/CMakeLists.txt | 4 +- qml/Forms/CMakeLists.txt | 12 ++++ qml/Forms/Login.qml | 74 ++++++++++++++++++++ qml/Forms/Register.qml | 77 ++++++++++++++++++++ qml/ServerPick.qml | 19 ++--- qml/Welcome.qml | 148 +++------------------------------------ qml/main.qml | 28 +++++--- root.cpp | 3 +- 10 files changed, 230 insertions(+), 229 deletions(-) create mode 100644 qml/Forms/CMakeLists.txt create mode 100644 qml/Forms/Login.qml create mode 100644 qml/Forms/Register.qml diff --git a/API/api.cpp b/API/api.cpp index 77d7940..883656a 100644 --- a/API/api.cpp +++ b/API/api.cpp @@ -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 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() || 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 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)}); } diff --git a/API/api.h b/API/api.h index fde275a..6ba0df8 100644 --- a/API/api.h +++ b/API/api.h @@ -9,14 +9,16 @@ #include #include +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; diff --git a/qml/CMakeLists.txt b/qml/CMakeLists.txt index d57f6cb..49f5936 100644 --- a/qml/CMakeLists.txt +++ b/qml/CMakeLists.txt @@ -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) diff --git a/qml/Forms/CMakeLists.txt b/qml/Forms/CMakeLists.txt new file mode 100644 index 0000000..eb7aa3e --- /dev/null +++ b/qml/Forms/CMakeLists.txt @@ -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) diff --git a/qml/Forms/Login.qml b/qml/Forms/Login.qml new file mode 100644 index 0000000..698ab2c --- /dev/null +++ b/qml/Forms/Login.qml @@ -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() + } + } + } +} diff --git a/qml/Forms/Register.qml b/qml/Forms/Register.qml new file mode 100644 index 0000000..673efb3 --- /dev/null +++ b/qml/Forms/Register.qml @@ -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() + } + } + } +} diff --git a/qml/ServerPick.qml b/qml/ServerPick.qml index 3d94015..83413f9 100644 --- a/qml/ServerPick.qml +++ b/qml/ServerPick.qml @@ -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() - } }); } } diff --git a/qml/Welcome.qml b/qml/Welcome.qml index b24a020..340b91c 100644 --- a/qml/Welcome.qml +++ b/qml/Welcome.qml @@ -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 { - 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: forms.registering = true - } - } - } + Forms.Login { + id: login + visible: !forms.registering + onRegister: 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 } } } diff --git a/qml/main.qml b/qml/main.qml index 3793c65..23160d6 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -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; } } - ServerPick { - visible: false - id: pick - onBack: stack.pop() - onSuccess: function (address) { - API.address = address - stack.pop(); + Component { + id: pickComponent + ServerPick { + StackView.onRemoved: destroy() + onBack: stack.pop() } } } diff --git a/root.cpp b/root.cpp index 8b9efd3..9028662 100644 --- a/root.cpp +++ b/root.cpp @@ -29,10 +29,11 @@ Root::Root(const QUrl& root, int& argc, char* argv[]) : qRegisterMetaType("API"); connect(&api, &API::addressChanged, this, &Root::onAPIAddressChanged); - qmlRegisterSingletonType("API", 1, 0, "API", [this] (QQmlEngine *engine, QJSEngine *scriptEngine) { + qmlRegisterSingletonType("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");