From a6ab447955edb5890fa8879aaf1f484d78e89bad Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Mon, 31 Jan 2022 22:45:17 +0100 Subject: [PATCH] Implement adding labels for account This gives the user the ability to label different account (e.g. "work", "private") and shows this label in the account switcher. Showing the label in more places will be done in future MRs. The label is stored in the user's account data and thus transfers automatically to other instances of neochat --- imports/NeoChat/Page/RoomListPage.qml | 1 + imports/NeoChat/Settings/AccountsPage.qml | 7 +++++++ src/neochatuser.cpp | 24 +++++++++++++++++++++++ src/neochatuser.h | 7 +++++++ 4 files changed, 39 insertions(+) diff --git a/imports/NeoChat/Page/RoomListPage.qml b/imports/NeoChat/Page/RoomListPage.qml index c19c5fb28..0447e40c7 100644 --- a/imports/NeoChat/Page/RoomListPage.qml +++ b/imports/NeoChat/Page/RoomListPage.qml @@ -321,6 +321,7 @@ Kirigami.ScrollablePage { Layout.fillWidth: true Layout.fillHeight: true text: model.connection.localUserId + subtitle: model.connection.localUser.accountLabel } } } diff --git a/imports/NeoChat/Settings/AccountsPage.qml b/imports/NeoChat/Settings/AccountsPage.qml index ee5c233f7..23cd3b090 100644 --- a/imports/NeoChat/Settings/AccountsPage.qml +++ b/imports/NeoChat/Settings/AccountsPage.qml @@ -151,6 +151,11 @@ Kirigami.ScrollablePage { text: userEditSheet.connection ? userEditSheet.connection.localUser.displayName : "" Kirigami.FormData.label: i18n("Name:") } + Controls.TextField { + id: accountLabel + text: userEditSheet.connection ? userEditSheet.connection.localUser.accountLabel : "" + Kirigami.FormData.label: i18n("Label:") + } Controls.TextField { id: currentPassword Kirigami.FormData.label: i18n("Current Password:") @@ -179,6 +184,8 @@ Kirigami.ScrollablePage { showPassiveNotification("The Avatar could not be set") if(userEditSheet.connection.localUser.displayName !== name.text) userEditSheet.connection.localUser.rename(name.text) + if(userEditSheet.connection.localUser.accountLabel !== accountLabel.text) + userEditSheet.connection.localUser.setAccountLabel(accountLabel.text) if(currentPassword.text !== "" && newPassword.text !== "" && confirmPassword.text !== "") { if(newPassword.text === confirmPassword.text) { Controller.changePassword(userEditSheet.connection, currentPassword.text, newPassword.text) diff --git a/src/neochatuser.cpp b/src/neochatuser.cpp index a1f30e48f..d90a5ccd8 100644 --- a/src/neochatuser.cpp +++ b/src/neochatuser.cpp @@ -4,13 +4,23 @@ #include "neochatuser.h" #include +#include #include +#include + NeoChatUser::NeoChatUser(QString userId, Connection *connection) : User(std::move(userId), 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() @@ -34,3 +44,17 @@ 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 e49f1d7d8..bcc4304f2 100644 --- a/src/neochatuser.h +++ b/src/neochatuser.h @@ -13,6 +13,8 @@ class NeoChatUser : public 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, Connection *connection); @@ -20,8 +22,13 @@ 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;