diff --git a/imports/Spectral/Dialog/AccountDetailDialog.qml b/imports/Spectral/Dialog/AccountDetailDialog.qml index 5b7298b34..f7d1bccd5 100644 --- a/imports/Spectral/Dialog/AccountDetailDialog.qml +++ b/imports/Spectral/Dialog/AccountDetailDialog.qml @@ -31,9 +31,7 @@ Dialog { spacing: 16 - model: AccountListModel{ - controller: spectralController - } + model: AccountListModel{ } delegate: Kirigami.Avatar { width: 48 @@ -48,13 +46,13 @@ Dialog { MenuItem { text: "Mark all as read" - onClicked: spectralController.markAllMessagesAsRead(connection) + onClicked: Controller.markAllMessagesAsRead(connection) } MenuItem { text: "Logout" - onClicked: spectralController.logout(connection) + onClicked: Controller.logout(connection) } } @@ -63,7 +61,7 @@ Dialog { circular: true - onPrimaryClicked: spectralController.connection = connection + onPrimaryClicked: Controller.connection = connection onSecondaryClicked: contextMenu.popup() } } @@ -113,7 +111,7 @@ Dialog { anchors.fill: parent onPrimaryClicked: { - joinRoomDialog.createObject(ApplicationWindow.overlay, {"controller": spectralController, "connection": spectralController.connection}).open() + joinRoomDialog.createObject(ApplicationWindow.overlay, {"connection": Controller.connection}).open() root.close() } } @@ -143,7 +141,7 @@ Dialog { anchors.fill: parent onPrimaryClicked: { - startChatDialog.createObject(ApplicationWindow.overlay, {"controller": spectralController, "connection": spectralController.connection}).open() + startChatDialog.createObject(ApplicationWindow.overlay, {"connection": Controller.connection}).open() root.close() } } diff --git a/imports/Spectral/Dialog/CreateRoomDialog.qml b/imports/Spectral/Dialog/CreateRoomDialog.qml index e6efb6401..6c29f8a2f 100644 --- a/imports/Spectral/Dialog/CreateRoomDialog.qml +++ b/imports/Spectral/Dialog/CreateRoomDialog.qml @@ -4,6 +4,8 @@ import QtQuick.Layouts 1.12 import Spectral.Component 2.0 +import Spectral 0.1 + Dialog { anchors.centerIn: parent width: 360 @@ -32,7 +34,7 @@ Dialog { standardButtons: Dialog.Ok | Dialog.Cancel - onAccepted: spectralController.createRoom(spectralController.connection, roomNameField.text, roomTopicField.text) + onAccepted: Controller.createRoom(Controller.connection, roomNameField.text, roomTopicField.text) onClosed: destroy() } diff --git a/imports/Spectral/Dialog/InviteUserDialog.qml b/imports/Spectral/Dialog/InviteUserDialog.qml index d5a72bcd4..d2be719a7 100644 --- a/imports/Spectral/Dialog/InviteUserDialog.qml +++ b/imports/Spectral/Dialog/InviteUserDialog.qml @@ -10,7 +10,6 @@ import Spectral.Setting 0.1 import Spectral 0.1 Dialog { - property var controller property var room anchors.centerIn: parent diff --git a/imports/Spectral/Dialog/JoinRoomDialog.qml b/imports/Spectral/Dialog/JoinRoomDialog.qml index 687637fe1..e642d0920 100644 --- a/imports/Spectral/Dialog/JoinRoomDialog.qml +++ b/imports/Spectral/Dialog/JoinRoomDialog.qml @@ -10,7 +10,6 @@ import Spectral.Setting 0.1 import Spectral 0.1 Dialog { - property var controller property var connection property string keyword @@ -59,7 +58,7 @@ Dialog { if (identifierField.isJoined) { roomListForm.joinRoom(identifierField.room) } else { - controller.joinRoom(connection, identifierField.text) + Controller.joinRoom(connection, identifierField.text) } } } @@ -249,7 +248,7 @@ Dialog { circular: true onClicked: { - controller.joinRoom(connection, roomID) + Controller.joinRoom(connection, roomID) root.close() } } diff --git a/imports/Spectral/Dialog/RoomSettingsDialog.qml b/imports/Spectral/Dialog/RoomSettingsDialog.qml index 1091e5c3e..2032490a6 100644 --- a/imports/Spectral/Dialog/RoomSettingsDialog.qml +++ b/imports/Spectral/Dialog/RoomSettingsDialog.qml @@ -7,6 +7,8 @@ import Spectral.Component 2.0 import Spectral.Effect 2.0 import Spectral.Setting 0.1 +import Spectral 0.1 + Dialog { property var room @@ -168,7 +170,7 @@ Dialog { anchors.fill: parent onClicked: { - roomListForm.enteredRoom = spectralController.connection.room(room.predecessorId) + roomListForm.enteredRoom = Controller.connection.room(room.predecessorId) root.close() } } @@ -219,7 +221,7 @@ Dialog { anchors.fill: parent onClicked: { - roomListForm.enteredRoom = spectralController.connection.room(room.successorId) + roomListForm.enteredRoom = Controller.connection.room(room.successorId) root.close() } } diff --git a/imports/Spectral/Dialog/StartChatDialog.qml b/imports/Spectral/Dialog/StartChatDialog.qml index e427eb34d..481c7c14d 100644 --- a/imports/Spectral/Dialog/StartChatDialog.qml +++ b/imports/Spectral/Dialog/StartChatDialog.qml @@ -9,7 +9,6 @@ import Spectral.Setting 0.1 import Spectral 0.1 Dialog { - property var controller property var connection anchors.centerIn: parent @@ -47,7 +46,7 @@ Dialog { highlighted: true onClicked: { - controller.createDirectChat(connection, identifierField.text) + Controller.createDirectChat(connection, identifierField.text) } } } @@ -157,7 +156,7 @@ Dialog { circular: true onClicked: { - controller.createDirectChat(connection, userID) + Controller.createDirectChat(connection, userID) root.close() } } diff --git a/imports/Spectral/Panel/RoomDrawer.qml b/imports/Spectral/Panel/RoomDrawer.qml index bf8149de9..6b43adf11 100644 --- a/imports/Spectral/Panel/RoomDrawer.qml +++ b/imports/Spectral/Panel/RoomDrawer.qml @@ -115,7 +115,7 @@ Kirigami.OverlayDrawer { Layout.preferredHeight: Kirigami.Units.gridUnit icon.name: "list-user-add" - onClicked: inviteUserDialog.createObject(ApplicationWindow.overlay, {"controller": spectralController, "room": room}).open() + onClicked: inviteUserDialog.createObject(ApplicationWindow.overlay, {"room": room}).open() } } diff --git a/qml/LoginPage.qml b/qml/LoginPage.qml index 842747a93..230b0826d 100644 --- a/qml/LoginPage.qml +++ b/qml/LoginPage.qml @@ -8,6 +8,8 @@ import QtQuick 2.12 import QtQuick.Controls 2.12 as QQC2 import QtQuick.Layouts 1.12 +import Spectral 0.1 + import Spectral.Component 2.0 import org.kde.kirigami 2.12 as Kirigami @@ -17,8 +19,6 @@ Kirigami.ScrollablePage { title: i18n("Login") - required property var spectralController - Kirigami.FormLayout { id: formLayout QQC2.TextField { @@ -55,9 +55,9 @@ Kirigami.ScrollablePage { function doLogin() { if (accessTokenField.text.length > 0) { - spectralController.loginWithAccessToken(serverField.text, usernameField.text, accessTokenField.text, deviceNameField.text) + Controller.loginWithAccessToken(serverField.text, usernameField.text, accessTokenField.text, deviceNameField.text) } else { - spectralController.loginWithCredentials(serverField.text, usernameField.text, passwordField.text, deviceNameField.text) + Controller.loginWithCredentials(serverField.text, usernameField.text, passwordField.text, deviceNameField.text) } } } diff --git a/qml/main.qml b/qml/main.qml index acfad5010..d571eb1f8 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -46,42 +46,36 @@ Kirigami.ApplicationWindow { } } - Controller { - id: spectralController - - quitOnLastWindowClosed: true - - onErrorOccured: showPassiveNotification(error + ": " + detail) + Connections { + target: Controller onInitiated: { - - if (spectralController.accountCount === 0) { - pageStack.replace("qrc:/qml/LoginPage.qml", { - 'spectralController': spectralController - }); + if (Controller.accountCount === 0) { + pageStack.replace("qrc:/qml/LoginPage.qml", {}); } else { pageStack.replace(roomListComponent); } } onConnectionAdded: { - if (spectralController.accountCount === 1) { + if (Controller.accountCount === 1) { console.log("roomListComponent") pageStack.replace(roomListComponent); } } + onErrorOccured: showPassiveNotification(error + ": " + detail) } Binding { target: imageProvider property: "connection" - value: spectralController.connection + value: Controller.connection } RoomListModel { id: spectralRoomListModel - connection: spectralController.connection + connection: Controller.connection } Component { diff --git a/src/accountlistmodel.cpp b/src/accountlistmodel.cpp index 245d3f314..88bcb383c 100644 --- a/src/accountlistmodel.cpp +++ b/src/accountlistmodel.cpp @@ -10,21 +10,10 @@ AccountListModel::AccountListModel(QObject *parent) : QAbstractListModel(parent) { -} - -void AccountListModel::setController(Controller *value) -{ - if (m_controller == value) { - return; - } - - beginResetModel(); - m_connections.clear(); - m_controller = value; - m_connections += m_controller->connections(); + m_connections += Controller::instance().connections(); - connect(m_controller, &Controller::connectionAdded, this, [=](Connection *conn) { + connect(&Controller::instance(), &Controller::connectionAdded, this, [=](Connection *conn) { if (!conn) { return; } @@ -32,7 +21,7 @@ void AccountListModel::setController(Controller *value) m_connections.append(conn); endInsertRows(); }); - connect(m_controller, &Controller::connectionDropped, this, [=](Connection *conn) { + connect(&Controller::instance(), &Controller::connectionDropped, this, [=](Connection *conn) { qDebug() << "Dropping connection" << conn->userId(); if (!conn) { qDebug() << "Trying to remove null connection"; @@ -47,7 +36,6 @@ void AccountListModel::setController(Controller *value) m_connections.erase(it); endRemoveRows(); }); - Q_EMIT controllerChanged(); } QVariant AccountListModel::data(const QModelIndex &index, int role) const diff --git a/src/accountlistmodel.h b/src/accountlistmodel.h index a7c41a38b..5bc789bc8 100644 --- a/src/accountlistmodel.h +++ b/src/accountlistmodel.h @@ -14,7 +14,6 @@ class AccountListModel : public QAbstractListModel { Q_OBJECT - Q_PROPERTY(Controller *controller READ controller WRITE setController NOTIFY controllerChanged) public: enum EventRoles { UserRole = Qt::UserRole + 1, ConnectionRole }; @@ -25,18 +24,8 @@ public: QHash roleNames() const override; - Controller *controller() const - { - return m_controller; - } - void setController(Controller *value); - private: - Controller *m_controller = nullptr; QVector m_connections; - -Q_SIGNALS: - void controllerChanged(); }; #endif // ACCOUNTLISTMODEL_H diff --git a/src/controller.h b/src/controller.h index 2179fc556..5629cef51 100644 --- a/src/controller.h +++ b/src/controller.h @@ -31,8 +31,11 @@ class Controller : public QObject Q_PROPERTY(bool busy READ busy WRITE setBusy NOTIFY busyChanged) public: - explicit Controller(QObject *parent = nullptr); - ~Controller(); + static Controller &instance() + { + static Controller _instance; + return _instance; + } Q_INVOKABLE void loginWithCredentials(QString, QString, QString, QString); Q_INVOKABLE void loginWithAccessToken(QString, QString, QString, QString); @@ -100,6 +103,9 @@ public: } private: + explicit Controller(QObject *parent = nullptr); + ~Controller(); + QVector m_connections; QPointer m_connection; QNetworkConfigurationManager m_ncm; diff --git a/src/main.cpp b/src/main.cpp index 4ab143727..47addd12b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,7 +55,7 @@ int main(int argc, char *argv[]) app.setApplicationName("neochat"); app.setWindowIcon(QIcon(":/assets/img/icon.png")); - qmlRegisterType("Spectral", 0, 1, "Controller"); + qmlRegisterSingletonInstance("Spectral", 0, 1, "Controller", &Controller::instance()); qmlRegisterType("Spectral", 0, 1, "AccountListModel"); qmlRegisterType("Spectral", 0, 1, "RoomListModel"); qmlRegisterType("Spectral", 0, 1, "UserListModel");