diff --git a/src/controller.cpp b/src/controller.cpp index b71953fd7..9d16f1983 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -593,6 +593,7 @@ void Controller::setActiveConnection(Connection *connection) } if (m_connection != nullptr) { disconnect(m_connection, &Connection::syncError, this, nullptr); + disconnect(m_connection, &Connection::accountDataChanged, this, nullptr); } m_connection = connection; if (connection != nullptr) { @@ -616,12 +617,18 @@ void Controller::setActiveConnection(Connection *connection) RoomManager::instance().warning(i18n("File too large to download."), i18n("Contact your matrix server administrator for support.")); } }); + connect(connection, &Connection::accountDataChanged, this, [this](const QString &type) { + if (type == QLatin1String("org.kde.neochat.account_label")) { + Q_EMIT activeAccountLabelChanged(); + } + }); } else { NeoChatConfig::self()->setActiveConnection(QString()); } NeoChatConfig::self()->save(); Q_EMIT activeConnectionChanged(); Q_EMIT activeConnectionIndexChanged(); + Q_EMIT activeAccountLabelChanged(); } void Controller::saveWindowGeometry() @@ -809,3 +816,22 @@ bool Controller::isFlatpak() const return false; #endif } + +void Controller::setActiveAccountLabel(const QString &label) +{ + if (!m_connection) { + return; + } + QJsonObject json{ + {"account_label", label}, + }; + m_connection->setAccountData("org.kde.neochat.account_label", json); +} + +QString Controller::activeAccountLabel() const +{ + if (!m_connection) { + return {}; + } + return m_connection->accountDataJson("org.kde.neochat.account_label")["account_label"].toString(); +} \ No newline at end of file diff --git a/src/controller.h b/src/controller.h index 90806d374..2cbd90f82 100644 --- a/src/controller.h +++ b/src/controller.h @@ -42,6 +42,7 @@ class Controller : public QObject Q_PROPERTY(int activeConnectionIndex READ activeConnectionIndex NOTIFY activeConnectionIndexChanged) Q_PROPERTY(int quotientMinorVersion READ quotientMinorVersion CONSTANT) Q_PROPERTY(bool isFlatpak READ isFlatpak CONSTANT) + Q_PROPERTY(QString activeAccountLabel READ activeAccountLabel WRITE setActiveAccountLabel NOTIFY activeAccountLabelChanged) public: static Controller &instance(); @@ -103,6 +104,23 @@ public: int quotientMinorVersion() const; bool isFlatpak() const; + /** + * @brief The label for this account. + * + * Account labels are a concept Specific to NeoChat, allowing accounts to be labelled, e.g. for "Work", "Private", etc. + * @return The label, if it exists, otherwise an empty string + */ + [[nodiscard]] QString activeAccountLabel() const; + + /** + * @brief Set the label for this account. + * + * Set to an empty string to remove the label + * @sa Controller::activeAccountLabel + * @param label The label to use, or an empty string + */ + void setActiveAccountLabel(const QString &label); + private: explicit Controller(QObject *parent = nullptr); @@ -152,6 +170,7 @@ Q_SIGNALS: void keyVerificationKey(const QString &sas); void activeConnectionIndexChanged(); void roomAdded(NeoChatRoom *room); + void activeAccountLabelChanged(); public Q_SLOTS: void logout(Quotient::Connection *conn, bool serverSideLogout); diff --git a/src/neochatuser.cpp b/src/neochatuser.cpp index d3fca6826..18f043102 100644 --- a/src/neochatuser.cpp +++ b/src/neochatuser.cpp @@ -16,13 +16,6 @@ NeoChatUser::NeoChatUser(QString userId, Connection *connection) { connect(static_cast(QGuiApplication::instance()), &QGuiApplication::paletteChanged, this, &NeoChatUser::polishColor); polishColor(); - if (connection->userId() == id()) { - connect(connection, &Connection::accountDataChanged, this, [this](QString type) { - if (type == QLatin1String("org.kde.neochat.account_label")) { - Q_EMIT accountLabelChanged(); - } - }); - } } QColor NeoChatUser::color() @@ -46,17 +39,3 @@ void NeoChatUser::polishColor() // https://github.com/quotient-im/libQuotient/wiki/User-color-coding-standard-draft-proposal setColor(QColor::fromHslF(hueF(), 1, -0.7 * lightness + 0.9, 1)); } - -void NeoChatUser::setAccountLabel(const QString &accountLabel) -{ - Q_ASSERT(connection()->user()->id() == id()); - QJsonObject json; - json["account_label"] = accountLabel; - connection()->setAccountData("org.kde.neochat.account_label", json); -} - -QString NeoChatUser::accountLabel() const -{ - Q_ASSERT(connection()->user()->id() == id()); - return connection()->accountDataJson("org.kde.neochat.account_label")["account_label"].toString(); -} diff --git a/src/neochatuser.h b/src/neochatuser.h index 5a523e73b..ae8dc79b9 100644 --- a/src/neochatuser.h +++ b/src/neochatuser.h @@ -11,8 +11,6 @@ class NeoChatUser : public Quotient::User { Q_OBJECT Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) - // Only valid for the local user - Q_PROPERTY(QString accountLabel READ accountLabel WRITE setAccountLabel NOTIFY accountLabelChanged) public: NeoChatUser(QString userId, Quotient::Connection *connection); @@ -20,13 +18,8 @@ public Q_SLOTS: QColor color(); void setColor(const QColor &color); - // Only valid for the local user - QString accountLabel() const; - void setAccountLabel(const QString &accountLabel); - Q_SIGNALS: void colorChanged(QColor _t1); - void accountLabelChanged(); private: QColor m_color; diff --git a/src/qml/Page/RoomList/UserInfo.qml b/src/qml/Page/RoomList/UserInfo.qml index 32cff67d7..715db1a29 100644 --- a/src/qml/Page/RoomList/UserInfo.qml +++ b/src/qml/Page/RoomList/UserInfo.qml @@ -168,7 +168,7 @@ QQC2.ToolBar { Layout.fillWidth: true } QQC2.Label { - text: (Controller.activeConnection.localUser.accountLabel.length > 0 ? (Controller.activeConnection.localUser.accountLabel + " ") : "") + Controller.activeConnection.localUser.id + text: (Controller.activeAccountLabel.length > 0 ? (Controller.activeAccountLabel + " ") : "") + Controller.activeConnection.localUser.id font.pointSize: displayNameLabel.font.pointSize * 0.8 opacity: 0.7 textFormat: Text.PlainText diff --git a/src/qml/Settings/AccountEditorPage.qml b/src/qml/Settings/AccountEditorPage.qml index 82894c212..50f9083f6 100644 --- a/src/qml/Settings/AccountEditorPage.qml +++ b/src/qml/Settings/AccountEditorPage.qml @@ -92,7 +92,7 @@ Kirigami.ScrollablePage { MobileForm.FormTextFieldDelegate { id: accountLabel label: i18n("Label:") - text: root.connection ? root.connection.localUser.accountLabel : "" + text: root.connection ? Controller.activeAccountLabel : "" } MobileForm.AbstractFormDelegate { Layout.fillWidth: true @@ -112,8 +112,8 @@ Kirigami.ScrollablePage { if (root.connection.localUser.displayName !== name.text) { root.connection.localUser.rename(name.text); } - if (root.connection.localUser.accountLabel !== accountLabel.text) { - root.connection.localUser.setAccountLabel(accountLabel.text); + if (Controller.activeAccountLabel !== accountLabel.text) { + Controller.activeAccountLabel = accountLabel.text; } } }