From 298043b3cba41136ed14b32ce45d2dbc09661ddd Mon Sep 17 00:00:00 2001 From: miruka Date: Fri, 13 Mar 2020 12:10:47 -0400 Subject: [PATCH] Prevent saving alias taken by other account --- TODO.md | 1 - src/gui/Base/HLabeledTextField.qml | 13 ++++++++++ src/gui/Pages/AccountSettings/Profile.qml | 30 ++++++++++++++++++----- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/TODO.md b/TODO.md index 4ca3df19..41185f44 100644 --- a/TODO.md +++ b/TODO.md @@ -89,7 +89,6 @@ - Adapt shortcuts flicking speed to font size - Multiaccount aliases: - - Warn when conflict with another alias - Prevent sending messages with a user not in the current room - Accept drag and drop to upload files or set a new avatar diff --git a/src/gui/Base/HLabeledTextField.qml b/src/gui/Base/HLabeledTextField.qml index 8d9f2caa..3a271b6f 100644 --- a/src/gui/Base/HLabeledTextField.qml +++ b/src/gui/Base/HLabeledTextField.qml @@ -8,6 +8,7 @@ HColumnLayout { property alias label: label + property alias errorLabel: errorLabel property alias field: field property alias toolTip: toolTip @@ -51,4 +52,16 @@ HColumnLayout { Layout.fillWidth: true } + + HLabel { + id: errorLabel + visible: Layout.maximumHeight > 0 + wrapMode: Text.Wrap + color: theme.colors.errorText + + Layout.maximumHeight: text ? implicitHeight : 0 + Layout.fillWidth: true + + Behavior on Layout.maximumHeight { HNumberAnimation {} } + } } diff --git a/src/gui/Pages/AccountSettings/Profile.qml b/src/gui/Pages/AccountSettings/Profile.qml index 8f88f739..6d08e2f8 100644 --- a/src/gui/Pages/AccountSettings/Profile.qml +++ b/src/gui/Pages/AccountSettings/Profile.qml @@ -166,14 +166,31 @@ HGridLayout { } HLabeledTextField { - property string currentAlias: - window.settings.writeAliases[userId] || "" - + property string currentAlias: aliases[userId] || "" property bool changed: field.text !== currentAlias + readonly property var aliases: window.settings.writeAliases + + readonly property string alreadyTakenBy: { + if (! field.text) return "" + + for (const [id, idAlias] of Object.entries(aliases)) + if (id !== userId && idAlias === field.text) return id + + return "" + } + id: aliasField + label.text: qsTr("Multi-account composer alias:") + + errorLabel.text: + alreadyTakenBy ? + qsTr("Taken by %1").arg(alreadyTakenBy) : + "" + + field.error: alreadyTakenBy !== "" field.onAccepted: applyChanges() field.placeholderText: ( nameField.field.text || @@ -197,8 +214,6 @@ HGridLayout { } } - HSpacer {} - HRowLayout { Layout.alignment: Qt.AlignBottom @@ -212,7 +227,10 @@ HGridLayout { text: qsTr("Save") loading: nameChangeRunning || avatarChangeRunning enabled: - nameField.changed || aliasField.changed || avatar.changed + avatar.changed || + nameField.changed || + (aliasField.changed && ! aliasField.alreadyTakenBy) + onClicked: applyChanges() Layout.fillWidth: true