feat(token-permissions): display channel names in SharedAddressesPanel
Modify the PermissionUtils a bit to return an array of `[key, channelName]` instead of just the keys and use it to display the channel names in the permissions overview panel (I could have used `QVariantMap` or `QJSonObject` here but those are always sorted by `key`, so had to resort to using a plain vector/array) Fixes #11584
This commit is contained in:
parent
d05d743d80
commit
e744c847ad
|
@ -504,10 +504,12 @@ QtObject {
|
||||||
function createChannelsModel1() {
|
function createChannelsModel1() {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
key: "_welcome"
|
key: "_welcome",
|
||||||
|
channelName: "Intro/welcome channel"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: "_general"
|
key: "_general",
|
||||||
|
channelName: "General"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -517,6 +519,11 @@ QtObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
function createChannelsModel3() {
|
function createChannelsModel3() {
|
||||||
return [{ key: "_vip" } ]
|
return [
|
||||||
|
{
|
||||||
|
key: "_vip",
|
||||||
|
channelName: "Club VIP"
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
#include <QJsonArray>
|
||||||
|
|
||||||
class QAbstractItemModel;
|
class QAbstractItemModel;
|
||||||
|
|
||||||
|
@ -14,6 +15,7 @@ public:
|
||||||
//!< traverse the permissions @p model, and look for unique token keys recursively under holdingsListModel->key
|
//!< traverse the permissions @p model, and look for unique token keys recursively under holdingsListModel->key
|
||||||
Q_INVOKABLE QStringList getUniquePermissionTokenKeys(QAbstractItemModel *model) const;
|
Q_INVOKABLE QStringList getUniquePermissionTokenKeys(QAbstractItemModel *model) const;
|
||||||
|
|
||||||
//!< traverse the permissions @p model, and look for unique channel keys recursively under channelsListModel->key; filtering out @permissionTypes ([PermissionTypes.Type.FOO])
|
//!< traverse the permissions @p model, and look for unique channels recursively under channelsListModel->key; filtering out @permissionTypes ([PermissionTypes.Type.FOO])
|
||||||
Q_INVOKABLE QStringList getUniquePermissionChannels(QAbstractItemModel *model, const QList<int> &permissionTypes = {}) const;
|
//! @return an array of array<key,channelName>
|
||||||
|
Q_INVOKABLE QJsonArray getUniquePermissionChannels(QAbstractItemModel *model, const QList<int> &permissionTypes = {}) const;
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,6 +4,8 @@
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
#include <set>
|
#include <set>
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
int roleByName(QAbstractItemModel* model, const QString &roleName)
|
int roleByName(QAbstractItemModel* model, const QString &roleName)
|
||||||
|
@ -61,8 +63,7 @@ QStringList PermissionUtilsInternal::getUniquePermissionTokenKeys(QAbstractItemM
|
||||||
return {result.cbegin(), result.cend()};
|
return {result.cbegin(), result.cend()};
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO return a QVariantMap (https://github.com/status-im/status-desktop/issues/11481) with key->channelName
|
QJsonArray PermissionUtilsInternal::getUniquePermissionChannels(QAbstractItemModel* model, const QList<int> &permissionTypes) const
|
||||||
QStringList PermissionUtilsInternal::getUniquePermissionChannels(QAbstractItemModel* model, const QList<int> &permissionTypes) const
|
|
||||||
{
|
{
|
||||||
if (!model)
|
if (!model)
|
||||||
return {};
|
return {};
|
||||||
|
@ -75,7 +76,7 @@ QStringList PermissionUtilsInternal::getUniquePermissionChannels(QAbstractItemMo
|
||||||
|
|
||||||
const auto permissionTypeRole = roleByName(model, QStringLiteral("permissionType"));
|
const auto permissionTypeRole = roleByName(model, QStringLiteral("permissionType"));
|
||||||
|
|
||||||
std::set<QString> result; // unique, sorted by default
|
std::vector<std::pair<QString,QString>> tmpRes; // key,channelName
|
||||||
|
|
||||||
const auto permissionsCount = model->rowCount();
|
const auto permissionsCount = model->rowCount();
|
||||||
for (int i = 0; i < permissionsCount; i++) {
|
for (int i = 0; i < permissionsCount; i++) {
|
||||||
|
@ -96,14 +97,30 @@ QStringList PermissionUtilsInternal::getUniquePermissionChannels(QAbstractItemMo
|
||||||
const auto channelItemsCount = channelItems->rowCount();
|
const auto channelItemsCount = channelItems->rowCount();
|
||||||
for (int j = 0; j < channelItemsCount; j++) {
|
for (int j = 0; j < channelItemsCount; j++) {
|
||||||
const auto keyRole = roleByName(channelItems, QStringLiteral("key"));
|
const auto keyRole = roleByName(channelItems, QStringLiteral("key"));
|
||||||
|
const auto nameRole = roleByName(channelItems, QStringLiteral("channelName"));
|
||||||
if (keyRole == -1) {
|
if (keyRole == -1) {
|
||||||
qWarning() << Q_FUNC_INFO << "Requested roleName 'key' not found!";
|
qWarning() << Q_FUNC_INFO << "Requested roleName 'key' not found!";
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
result.insert(channelItems->data(channelItems->index(j, 0), keyRole).toString());
|
tmpRes.emplace_back(channelItems->data(channelItems->index(j, 0), keyRole).toString(),
|
||||||
|
channelItems->data(channelItems->index(j, 0), nameRole).toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {result.cbegin(), result.cend()};
|
// sort by value (channel name)
|
||||||
|
std::sort(tmpRes.begin(), tmpRes.end(), [](const auto& lhs, const auto& rhs) {
|
||||||
|
return lhs.second.localeAwareCompare(rhs.second) < 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
// remove dupes
|
||||||
|
tmpRes.erase(std::unique(tmpRes.begin(), tmpRes.end()), tmpRes.end());
|
||||||
|
|
||||||
|
// construct the (sorted) result
|
||||||
|
QJsonArray result;
|
||||||
|
std::transform(tmpRes.cbegin(), tmpRes.cend(), std::back_inserter(result), [](const auto& channel) -> QJsonArray {
|
||||||
|
return {channel.first, channel.second};
|
||||||
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -103,7 +103,7 @@ Rectangle {
|
||||||
}
|
}
|
||||||
|
|
||||||
Repeater { // channel repeater
|
Repeater { // channel repeater
|
||||||
model: d.uniquePermissionChannels // TODO get channelName in addition (https://github.com/status-im/status-desktop/issues/11481)
|
model: d.uniquePermissionChannels
|
||||||
delegate: ChannelPermissionPanel {}
|
delegate: ChannelPermissionPanel {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -144,7 +144,7 @@ Rectangle {
|
||||||
case PermissionTypes.Type.Member:
|
case PermissionTypes.Type.Member:
|
||||||
return qsTr("Join %1").arg(root.communityName)
|
return qsTr("Join %1").arg(root.communityName)
|
||||||
default:
|
default:
|
||||||
return modelData // TODO display channel name https://github.com/status-im/status-desktop/issues/11481
|
return d.uniquePermissionChannels[index][1]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -304,7 +304,7 @@ Rectangle {
|
||||||
padding: d.absLeftMargin
|
padding: d.absLeftMargin
|
||||||
background: PanelBg {}
|
background: PanelBg {}
|
||||||
|
|
||||||
readonly property string channelKey: modelData
|
readonly property string channelKey: d.uniquePermissionChannels[index][0]
|
||||||
|
|
||||||
contentItem: RowLayout {
|
contentItem: RowLayout {
|
||||||
spacing: Style.current.padding
|
spacing: Style.current.padding
|
||||||
|
|
Loading…
Reference in New Issue