Make the Controller a singleton

This commit is contained in:
Tobias Fella
2020-11-04 01:43:13 +00:00
committed by Nicolas Fella
parent 1739a454da
commit 2d1a7d6500
13 changed files with 42 additions and 66 deletions

View File

@@ -31,9 +31,7 @@ Dialog {
spacing: 16 spacing: 16
model: AccountListModel{ model: AccountListModel{ }
controller: spectralController
}
delegate: Kirigami.Avatar { delegate: Kirigami.Avatar {
width: 48 width: 48
@@ -48,13 +46,13 @@ Dialog {
MenuItem { MenuItem {
text: "Mark all as read" text: "Mark all as read"
onClicked: spectralController.markAllMessagesAsRead(connection) onClicked: Controller.markAllMessagesAsRead(connection)
} }
MenuItem { MenuItem {
text: "Logout" text: "Logout"
onClicked: spectralController.logout(connection) onClicked: Controller.logout(connection)
} }
} }
@@ -63,7 +61,7 @@ Dialog {
circular: true circular: true
onPrimaryClicked: spectralController.connection = connection onPrimaryClicked: Controller.connection = connection
onSecondaryClicked: contextMenu.popup() onSecondaryClicked: contextMenu.popup()
} }
} }
@@ -113,7 +111,7 @@ Dialog {
anchors.fill: parent anchors.fill: parent
onPrimaryClicked: { onPrimaryClicked: {
joinRoomDialog.createObject(ApplicationWindow.overlay, {"controller": spectralController, "connection": spectralController.connection}).open() joinRoomDialog.createObject(ApplicationWindow.overlay, {"connection": Controller.connection}).open()
root.close() root.close()
} }
} }
@@ -143,7 +141,7 @@ Dialog {
anchors.fill: parent anchors.fill: parent
onPrimaryClicked: { onPrimaryClicked: {
startChatDialog.createObject(ApplicationWindow.overlay, {"controller": spectralController, "connection": spectralController.connection}).open() startChatDialog.createObject(ApplicationWindow.overlay, {"connection": Controller.connection}).open()
root.close() root.close()
} }
} }

View File

@@ -4,6 +4,8 @@ import QtQuick.Layouts 1.12
import Spectral.Component 2.0 import Spectral.Component 2.0
import Spectral 0.1
Dialog { Dialog {
anchors.centerIn: parent anchors.centerIn: parent
width: 360 width: 360
@@ -32,7 +34,7 @@ Dialog {
standardButtons: Dialog.Ok | Dialog.Cancel 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() onClosed: destroy()
} }

View File

@@ -10,7 +10,6 @@ import Spectral.Setting 0.1
import Spectral 0.1 import Spectral 0.1
Dialog { Dialog {
property var controller
property var room property var room
anchors.centerIn: parent anchors.centerIn: parent

View File

@@ -10,7 +10,6 @@ import Spectral.Setting 0.1
import Spectral 0.1 import Spectral 0.1
Dialog { Dialog {
property var controller
property var connection property var connection
property string keyword property string keyword
@@ -59,7 +58,7 @@ Dialog {
if (identifierField.isJoined) { if (identifierField.isJoined) {
roomListForm.joinRoom(identifierField.room) roomListForm.joinRoom(identifierField.room)
} else { } else {
controller.joinRoom(connection, identifierField.text) Controller.joinRoom(connection, identifierField.text)
} }
} }
} }
@@ -249,7 +248,7 @@ Dialog {
circular: true circular: true
onClicked: { onClicked: {
controller.joinRoom(connection, roomID) Controller.joinRoom(connection, roomID)
root.close() root.close()
} }
} }

View File

@@ -7,6 +7,8 @@ import Spectral.Component 2.0
import Spectral.Effect 2.0 import Spectral.Effect 2.0
import Spectral.Setting 0.1 import Spectral.Setting 0.1
import Spectral 0.1
Dialog { Dialog {
property var room property var room
@@ -168,7 +170,7 @@ Dialog {
anchors.fill: parent anchors.fill: parent
onClicked: { onClicked: {
roomListForm.enteredRoom = spectralController.connection.room(room.predecessorId) roomListForm.enteredRoom = Controller.connection.room(room.predecessorId)
root.close() root.close()
} }
} }
@@ -219,7 +221,7 @@ Dialog {
anchors.fill: parent anchors.fill: parent
onClicked: { onClicked: {
roomListForm.enteredRoom = spectralController.connection.room(room.successorId) roomListForm.enteredRoom = Controller.connection.room(room.successorId)
root.close() root.close()
} }
} }

View File

@@ -9,7 +9,6 @@ import Spectral.Setting 0.1
import Spectral 0.1 import Spectral 0.1
Dialog { Dialog {
property var controller
property var connection property var connection
anchors.centerIn: parent anchors.centerIn: parent
@@ -47,7 +46,7 @@ Dialog {
highlighted: true highlighted: true
onClicked: { onClicked: {
controller.createDirectChat(connection, identifierField.text) Controller.createDirectChat(connection, identifierField.text)
} }
} }
} }
@@ -157,7 +156,7 @@ Dialog {
circular: true circular: true
onClicked: { onClicked: {
controller.createDirectChat(connection, userID) Controller.createDirectChat(connection, userID)
root.close() root.close()
} }
} }

View File

@@ -115,7 +115,7 @@ Kirigami.OverlayDrawer {
Layout.preferredHeight: Kirigami.Units.gridUnit Layout.preferredHeight: Kirigami.Units.gridUnit
icon.name: "list-user-add" icon.name: "list-user-add"
onClicked: inviteUserDialog.createObject(ApplicationWindow.overlay, {"controller": spectralController, "room": room}).open() onClicked: inviteUserDialog.createObject(ApplicationWindow.overlay, {"room": room}).open()
} }
} }

View File

@@ -8,6 +8,8 @@ import QtQuick 2.12
import QtQuick.Controls 2.12 as QQC2 import QtQuick.Controls 2.12 as QQC2
import QtQuick.Layouts 1.12 import QtQuick.Layouts 1.12
import Spectral 0.1
import Spectral.Component 2.0 import Spectral.Component 2.0
import org.kde.kirigami 2.12 as Kirigami import org.kde.kirigami 2.12 as Kirigami
@@ -17,8 +19,6 @@ Kirigami.ScrollablePage {
title: i18n("Login") title: i18n("Login")
required property var spectralController
Kirigami.FormLayout { Kirigami.FormLayout {
id: formLayout id: formLayout
QQC2.TextField { QQC2.TextField {
@@ -55,9 +55,9 @@ Kirigami.ScrollablePage {
function doLogin() { function doLogin() {
if (accessTokenField.text.length > 0) { 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 { } else {
spectralController.loginWithCredentials(serverField.text, usernameField.text, passwordField.text, deviceNameField.text) Controller.loginWithCredentials(serverField.text, usernameField.text, passwordField.text, deviceNameField.text)
} }
} }
} }

View File

@@ -46,42 +46,36 @@ Kirigami.ApplicationWindow {
} }
} }
Controller { Connections {
id: spectralController target: Controller
quitOnLastWindowClosed: true
onErrorOccured: showPassiveNotification(error + ": " + detail)
onInitiated: { onInitiated: {
if (Controller.accountCount === 0) {
if (spectralController.accountCount === 0) { pageStack.replace("qrc:/qml/LoginPage.qml", {});
pageStack.replace("qrc:/qml/LoginPage.qml", {
'spectralController': spectralController
});
} else { } else {
pageStack.replace(roomListComponent); pageStack.replace(roomListComponent);
} }
} }
onConnectionAdded: { onConnectionAdded: {
if (spectralController.accountCount === 1) { if (Controller.accountCount === 1) {
console.log("roomListComponent") console.log("roomListComponent")
pageStack.replace(roomListComponent); pageStack.replace(roomListComponent);
} }
} }
onErrorOccured: showPassiveNotification(error + ": " + detail)
} }
Binding { Binding {
target: imageProvider target: imageProvider
property: "connection" property: "connection"
value: spectralController.connection value: Controller.connection
} }
RoomListModel { RoomListModel {
id: spectralRoomListModel id: spectralRoomListModel
connection: spectralController.connection connection: Controller.connection
} }
Component { Component {

View File

@@ -10,21 +10,10 @@
AccountListModel::AccountListModel(QObject *parent) AccountListModel::AccountListModel(QObject *parent)
: QAbstractListModel(parent) : QAbstractListModel(parent)
{ {
}
void AccountListModel::setController(Controller *value)
{
if (m_controller == value) {
return;
}
beginResetModel();
m_connections.clear(); m_connections.clear();
m_controller = value; m_connections += Controller::instance().connections();
m_connections += m_controller->connections();
connect(m_controller, &Controller::connectionAdded, this, [=](Connection *conn) { connect(&Controller::instance(), &Controller::connectionAdded, this, [=](Connection *conn) {
if (!conn) { if (!conn) {
return; return;
} }
@@ -32,7 +21,7 @@ void AccountListModel::setController(Controller *value)
m_connections.append(conn); m_connections.append(conn);
endInsertRows(); endInsertRows();
}); });
connect(m_controller, &Controller::connectionDropped, this, [=](Connection *conn) { connect(&Controller::instance(), &Controller::connectionDropped, this, [=](Connection *conn) {
qDebug() << "Dropping connection" << conn->userId(); qDebug() << "Dropping connection" << conn->userId();
if (!conn) { if (!conn) {
qDebug() << "Trying to remove null connection"; qDebug() << "Trying to remove null connection";
@@ -47,7 +36,6 @@ void AccountListModel::setController(Controller *value)
m_connections.erase(it); m_connections.erase(it);
endRemoveRows(); endRemoveRows();
}); });
Q_EMIT controllerChanged();
} }
QVariant AccountListModel::data(const QModelIndex &index, int role) const QVariant AccountListModel::data(const QModelIndex &index, int role) const

View File

@@ -14,7 +14,6 @@
class AccountListModel : public QAbstractListModel class AccountListModel : public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(Controller *controller READ controller WRITE setController NOTIFY controllerChanged)
public: public:
enum EventRoles { UserRole = Qt::UserRole + 1, ConnectionRole }; enum EventRoles { UserRole = Qt::UserRole + 1, ConnectionRole };
@@ -25,18 +24,8 @@ public:
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;
Controller *controller() const
{
return m_controller;
}
void setController(Controller *value);
private: private:
Controller *m_controller = nullptr;
QVector<Connection *> m_connections; QVector<Connection *> m_connections;
Q_SIGNALS:
void controllerChanged();
}; };
#endif // ACCOUNTLISTMODEL_H #endif // ACCOUNTLISTMODEL_H

View File

@@ -31,8 +31,11 @@ class Controller : public QObject
Q_PROPERTY(bool busy READ busy WRITE setBusy NOTIFY busyChanged) Q_PROPERTY(bool busy READ busy WRITE setBusy NOTIFY busyChanged)
public: public:
explicit Controller(QObject *parent = nullptr); static Controller &instance()
~Controller(); {
static Controller _instance;
return _instance;
}
Q_INVOKABLE void loginWithCredentials(QString, QString, QString, QString); Q_INVOKABLE void loginWithCredentials(QString, QString, QString, QString);
Q_INVOKABLE void loginWithAccessToken(QString, QString, QString, QString); Q_INVOKABLE void loginWithAccessToken(QString, QString, QString, QString);
@@ -100,6 +103,9 @@ public:
} }
private: private:
explicit Controller(QObject *parent = nullptr);
~Controller();
QVector<Connection *> m_connections; QVector<Connection *> m_connections;
QPointer<Connection> m_connection; QPointer<Connection> m_connection;
QNetworkConfigurationManager m_ncm; QNetworkConfigurationManager m_ncm;

View File

@@ -55,7 +55,7 @@ int main(int argc, char *argv[])
app.setApplicationName("neochat"); app.setApplicationName("neochat");
app.setWindowIcon(QIcon(":/assets/img/icon.png")); app.setWindowIcon(QIcon(":/assets/img/icon.png"));
qmlRegisterType<Controller>("Spectral", 0, 1, "Controller"); qmlRegisterSingletonInstance("Spectral", 0, 1, "Controller", &Controller::instance());
qmlRegisterType<AccountListModel>("Spectral", 0, 1, "AccountListModel"); qmlRegisterType<AccountListModel>("Spectral", 0, 1, "AccountListModel");
qmlRegisterType<RoomListModel>("Spectral", 0, 1, "RoomListModel"); qmlRegisterType<RoomListModel>("Spectral", 0, 1, "RoomListModel");
qmlRegisterType<UserListModel>("Spectral", 0, 1, "UserListModel"); qmlRegisterType<UserListModel>("Spectral", 0, 1, "UserListModel");