Refactor account labels

Move them out of NeoChatUser, where they don't make any sense
This commit is contained in:
Tobias Fella
2023-04-17 20:56:10 +02:00
committed by Carl Schwan
parent de55253e54
commit f60114c7f6
6 changed files with 49 additions and 32 deletions

View File

@@ -593,6 +593,7 @@ void Controller::setActiveConnection(Connection *connection)
} }
if (m_connection != nullptr) { if (m_connection != nullptr) {
disconnect(m_connection, &Connection::syncError, this, nullptr); disconnect(m_connection, &Connection::syncError, this, nullptr);
disconnect(m_connection, &Connection::accountDataChanged, this, nullptr);
} }
m_connection = connection; m_connection = connection;
if (connection != nullptr) { 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.")); 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 { } else {
NeoChatConfig::self()->setActiveConnection(QString()); NeoChatConfig::self()->setActiveConnection(QString());
} }
NeoChatConfig::self()->save(); NeoChatConfig::self()->save();
Q_EMIT activeConnectionChanged(); Q_EMIT activeConnectionChanged();
Q_EMIT activeConnectionIndexChanged(); Q_EMIT activeConnectionIndexChanged();
Q_EMIT activeAccountLabelChanged();
} }
void Controller::saveWindowGeometry() void Controller::saveWindowGeometry()
@@ -809,3 +816,22 @@ bool Controller::isFlatpak() const
return false; return false;
#endif #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();
}

View File

@@ -42,6 +42,7 @@ class Controller : public QObject
Q_PROPERTY(int activeConnectionIndex READ activeConnectionIndex NOTIFY activeConnectionIndexChanged) Q_PROPERTY(int activeConnectionIndex READ activeConnectionIndex NOTIFY activeConnectionIndexChanged)
Q_PROPERTY(int quotientMinorVersion READ quotientMinorVersion CONSTANT) Q_PROPERTY(int quotientMinorVersion READ quotientMinorVersion CONSTANT)
Q_PROPERTY(bool isFlatpak READ isFlatpak CONSTANT) Q_PROPERTY(bool isFlatpak READ isFlatpak CONSTANT)
Q_PROPERTY(QString activeAccountLabel READ activeAccountLabel WRITE setActiveAccountLabel NOTIFY activeAccountLabelChanged)
public: public:
static Controller &instance(); static Controller &instance();
@@ -103,6 +104,23 @@ public:
int quotientMinorVersion() const; int quotientMinorVersion() const;
bool isFlatpak() 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: private:
explicit Controller(QObject *parent = nullptr); explicit Controller(QObject *parent = nullptr);
@@ -152,6 +170,7 @@ Q_SIGNALS:
void keyVerificationKey(const QString &sas); void keyVerificationKey(const QString &sas);
void activeConnectionIndexChanged(); void activeConnectionIndexChanged();
void roomAdded(NeoChatRoom *room); void roomAdded(NeoChatRoom *room);
void activeAccountLabelChanged();
public Q_SLOTS: public Q_SLOTS:
void logout(Quotient::Connection *conn, bool serverSideLogout); void logout(Quotient::Connection *conn, bool serverSideLogout);

View File

@@ -16,13 +16,6 @@ NeoChatUser::NeoChatUser(QString userId, Connection *connection)
{ {
connect(static_cast<QGuiApplication *>(QGuiApplication::instance()), &QGuiApplication::paletteChanged, this, &NeoChatUser::polishColor); connect(static_cast<QGuiApplication *>(QGuiApplication::instance()), &QGuiApplication::paletteChanged, this, &NeoChatUser::polishColor);
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() QColor NeoChatUser::color()
@@ -46,17 +39,3 @@ void NeoChatUser::polishColor()
// https://github.com/quotient-im/libQuotient/wiki/User-color-coding-standard-draft-proposal // https://github.com/quotient-im/libQuotient/wiki/User-color-coding-standard-draft-proposal
setColor(QColor::fromHslF(hueF(), 1, -0.7 * lightness + 0.9, 1)); 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();
}

View File

@@ -11,8 +11,6 @@ class NeoChatUser : public Quotient::User
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) 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: public:
NeoChatUser(QString userId, Quotient::Connection *connection); NeoChatUser(QString userId, Quotient::Connection *connection);
@@ -20,13 +18,8 @@ public Q_SLOTS:
QColor color(); QColor color();
void setColor(const QColor &color); void setColor(const QColor &color);
// Only valid for the local user
QString accountLabel() const;
void setAccountLabel(const QString &accountLabel);
Q_SIGNALS: Q_SIGNALS:
void colorChanged(QColor _t1); void colorChanged(QColor _t1);
void accountLabelChanged();
private: private:
QColor m_color; QColor m_color;

View File

@@ -168,7 +168,7 @@ QQC2.ToolBar {
Layout.fillWidth: true Layout.fillWidth: true
} }
QQC2.Label { 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 font.pointSize: displayNameLabel.font.pointSize * 0.8
opacity: 0.7 opacity: 0.7
textFormat: Text.PlainText textFormat: Text.PlainText

View File

@@ -92,7 +92,7 @@ Kirigami.ScrollablePage {
MobileForm.FormTextFieldDelegate { MobileForm.FormTextFieldDelegate {
id: accountLabel id: accountLabel
label: i18n("Label:") label: i18n("Label:")
text: root.connection ? root.connection.localUser.accountLabel : "" text: root.connection ? Controller.activeAccountLabel : ""
} }
MobileForm.AbstractFormDelegate { MobileForm.AbstractFormDelegate {
Layout.fillWidth: true Layout.fillWidth: true
@@ -112,8 +112,8 @@ Kirigami.ScrollablePage {
if (root.connection.localUser.displayName !== name.text) { if (root.connection.localUser.displayName !== name.text) {
root.connection.localUser.rename(name.text); root.connection.localUser.rename(name.text);
} }
if (root.connection.localUser.accountLabel !== accountLabel.text) { if (Controller.activeAccountLabel !== accountLabel.text) {
root.connection.localUser.setAccountLabel(accountLabel.text); Controller.activeAccountLabel = accountLabel.text;
} }
} }
} }