diff --git a/main.cpp b/main.cpp index 65b650f..2b0e68e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,17 +1,9 @@ #include #include -#include "connection.h" -#include "room.h" -#include "user.h" -#include "jobs/syncjob.h" -#include "settings.h" +#include "matrix/controller.h" using namespace QMatrixClient; -// https://forum.qt.io/topic/57809 -Q_DECLARE_METATYPE(SyncJob*) -Q_DECLARE_METATYPE(Room*) - int main(int argc, char *argv[]) { #if defined(Q_OS_WIN) @@ -20,14 +12,7 @@ int main(int argc, char *argv[]) QGuiApplication app(argc, argv); - qmlRegisterType(); - qRegisterMetaType ("SyncJob*"); - qmlRegisterType(); - qRegisterMetaType("Room*"); - qmlRegisterType(); - qRegisterMetaType("User*"); - - qmlRegisterType("Matrique", 0, 1, "Connection"); + qmlRegisterType("Matrique", 0, 1, "Controller"); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml"))); diff --git a/matrix/controller.cpp b/matrix/controller.cpp index 01567c1..b2f74e9 100644 --- a/matrix/controller.cpp +++ b/matrix/controller.cpp @@ -1,6 +1,54 @@ #include "controller.h" -Controller::Controller(QObject *parent) : QObject(parent) -{ +#include "libqmatrixclient/connection.h" + +Controller::Controller(QObject *parent) : QObject(parent) { } + +Controller::~Controller() { + +} + +void Controller::init() { + connect(connection, &Connection::connected, + [=](){ + qInfo() << "Matrix connected."; + setUserID(connection->userId()); + setToken(connection->accessToken()); + } + ); + + connect(connection, &Connection::resolveError, this, &Controller::reconnect); + connect(connection, &Connection::syncError, this, &Controller::reconnect); + connect(connection, &Connection::syncDone, this, &Controller::resync); +} + +void Controller::login(QString home, QString user, QString pass) { + qInfo() << "UserID:" << userID; + qInfo() << "Token:" << token; + qInfo() << "Home:" << home; + qInfo() << "User:" << user; + qInfo() << "Pass:" << pass; + if(!userID.isEmpty() && !token.isEmpty()) { + qInfo() << "Using token."; + connection->connectWithToken(userID, token, ""); + + } else if(!user.isEmpty() && !pass.isEmpty()) { + qInfo() << "Using given credential."; + connection->connectToServer("@"+user+":"+home, pass, ""); + } +} + +void Controller::logout() { + userID = ""; + token = ""; +} + +void Controller::resync() { + connection->sync(30000); +} + +void Controller::reconnect() { + Controller::connection->connectWithToken(userID, token, ""); +} diff --git a/matrix/controller.h b/matrix/controller.h index b069c82..5420e6b 100644 --- a/matrix/controller.h +++ b/matrix/controller.h @@ -3,15 +3,57 @@ #include +#include "libqmatrixclient/connection.h" + +using namespace QMatrixClient; + class Controller : public QObject { Q_OBJECT + + Q_PROPERTY(QString userID READ getUserID WRITE setUserID NOTIFY userIDChanged) + Q_PROPERTY(QByteArray token READ getToken WRITE setToken NOTIFY tokenChanged) public: explicit Controller(QObject *parent = nullptr); + ~Controller(); + + // All the Q_INVOKABLEs. + Q_INVOKABLE void init(); + Q_INVOKABLE void login(QString, QString, QString); + Q_INVOKABLE void logout(); + + // All the non-Q_INVOKABLE functions. + + // All the Q_PROPERTYs. + QString userID; + QString getUserID() { return userID; } + void setUserID(QString n) { + if(n != userID) { + userID = n; + emit userIDChanged(); + } + } + + QByteArray token; + QByteArray getToken() { return token; } + void setToken(QByteArray n) { + if(n != token) { + token = n; + emit tokenChanged(); + } + } + +private: + QMatrixClient::Connection *connection = new QMatrixClient::Connection(); + void resync(); + void reconnect(); signals: + void userIDChanged(); + void tokenChanged(); + void homeServerChanged(); public slots: }; -#endif // CONTROLLER_H \ No newline at end of file +#endif // CONTROLLER_H diff --git a/qml/Login.qml b/qml/Login.qml index c48f348..20dd3a9 100644 --- a/qml/Login.qml +++ b/qml/Login.qml @@ -8,6 +8,7 @@ import "qrc:/qml/component" Page { property var window + property var controller property alias homeserver: settings.server property alias username: settings.user @@ -79,35 +80,53 @@ Page { anchors.horizontalCenter: parent.horizontalCenter } - TextField { - id: serverField + RowLayout { width: parent.width height: 48 - placeholderText: "Server" - leftPadding: 16 - topPadding: 0 - bottomPadding: 0 + spacing: 0 - background: Rectangle { - color: "#eaeaea" - border.color: parent.activeFocus ? Material.accent : "transparent" - border.width: 2 + Text { + text: "@" + horizontalAlignment: Text.AlignHCenter + Layout.preferredWidth: parent.width * 0.05 } - } - TextField { - id: usernameField - width: parent.width - height: 48 - placeholderText: "Username" - leftPadding: 16 - topPadding: 0 - bottomPadding: 0 + TextField { + id: usernameField + Layout.preferredWidth: parent.width * 0.45 + Layout.fillHeight: true + placeholderText: "Username" + leftPadding: 16 + topPadding: 0 + bottomPadding: 0 - background: Rectangle { - color: "#eaeaea" - border.color: parent.activeFocus ? Material.accent : "transparent" - border.width: 2 + background: Rectangle { + color: "#eaeaea" + border.color: parent.activeFocus ? Material.accent : "transparent" + border.width: 2 + } + } + + Text { + text: ":" + horizontalAlignment: Text.AlignHCenter + Layout.preferredWidth: parent.width * 0.05 + } + + TextField { + id: serverField + Layout.preferredWidth: parent.width * 0.45 + Layout.fillHeight: true + placeholderText: "Server" + leftPadding: 16 + topPadding: 0 + bottomPadding: 0 + + background: Rectangle { + color: "#eaeaea" + border.color: parent.activeFocus ? Material.accent : "transparent" + border.width: 2 + } } } @@ -133,7 +152,7 @@ Page { highlighted: true width: parent.width - onClicked: window.login() + onClicked: controller.login(homeserver, username, password) } Button { @@ -142,7 +161,7 @@ Page { flat: true width: parent.width - onClicked: window.logout() + onClicked: controller.logout() } } } diff --git a/qml/main.qml b/qml/main.qml index 2266ba6..f47600e 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -17,79 +17,19 @@ ApplicationWindow { height: 640 title: qsTr("Matrique") - Connection { - id: connection - homeserver: settings.homeserver + Controller { + id: controller } Settings { id: settings - property string homeserver - - property string userID - property string token - property string deviceID + property alias userID: controller.userID + property alias token: controller.token } FontLoader { id: materialFont; source: "qrc:/asset/font/material.ttf" } - function init() { - connection.connected.connect(function() { - console.info("Matrix connected.") - - connection.syncError.connect(reconnect) - connection.resolveError.connect(reconnect) - connection.syncDone.connect(resync) - }) - } - - function resync() { - if(!initialised) { - } - connection.sync(30000) - } - - function reconnect() { - connection.connectWithToken(connection.localUserId, - connection.accessToken, - connection.deviceId) - } - - function login() { - if(!settings.homeserver) settings.homeserver = "https://matrix.org" - - console.info("Homeserver:", connection.homeserver) - console.info("UserID:", settings.userID) - console.info("Token:", settings.token) - console.info("DeviceID:", settings.deviceID) - - if(!settings.token || !settings.userID) { - console.info("Using server address.") - settings.homeserver = loginPage.homeserver - - function saveCredentials() { - settings.userID = connection.localUserId - settings.token = connection.accessToken - - connection.connected.disconnect(saveCredentials) - } - - connection.connected.connect(saveCredentials) - - connection.connectToServer(loginPage.username, loginPage.password, connection.deviceId) - } else { - console.info("Using token") - connection.connectWithToken(settings.userID, settings.token, connection.deviceId) - } - } - - function logout() { - settings.homeserver = null; - settings.userID = null; - settings.token = null; - } - SideNav { id: sideNav width: 80 @@ -185,6 +125,7 @@ ApplicationWindow { Login { id: loginPage window: window + controller: controller } Contact { @@ -197,6 +138,6 @@ ApplicationWindow { } Component.onCompleted: { - init() + controller.init() } }