Skip to content

Commit

Permalink
Fix roles and mounts editor for broker with SHV ver. 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Fanda Vacek committed Nov 8, 2024
1 parent 3e288f5 commit e61880b
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 112 deletions.
2 changes: 1 addition & 1 deletion shvspy/src/appversion.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once

#define APP_VERSION "1.9.12"
#define APP_VERSION "1.10.0"

2 changes: 1 addition & 1 deletion shvspy/src/dlgbrokerproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ DlgBrokerProperties::DlgBrokerProperties(QWidget *parent) :
ui->cbxConnectionType->setCurrentIndex(0);

ui->rpc_protocolType->addItem(Rpc::protocolTypeToString(Rpc::ProtocolType::ChainPack), static_cast<int>(Rpc::ProtocolType::ChainPack));
//ui->rpc_protocolType->addItem(Rpc::protocolTypeToString(Rpc::ProtocolType::Cpon), static_cast<int>(Rpc::ProtocolType::Cpon));
ui->rpc_protocolType->addItem(Rpc::protocolTypeToString(Rpc::ProtocolType::Cpon), static_cast<int>(Rpc::ProtocolType::Cpon));
//ui->rpc_protocolType->addItem(Rpc::protocolTypeToString(Rpc::ProtocolType::JsonRpc), static_cast<int>(Rpc::ProtocolType::JsonRpc));
ui->rpc_protocolType->setCurrentIndex(0);

Expand Down
19 changes: 9 additions & 10 deletions shvspy/src/dlgmountseditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ui_dlgmountseditor.h"

#include "dlgaddeditmount.h"
#include "theapp.h"

#include <shv/core/assert.h>
#include <shv/iotqt/rpc/rpccall.h>
Expand All @@ -14,7 +15,7 @@
static const std::string METHOD_VALUE = "value";
static const std::string METHOD_SET_VALUE = "setValue";

DlgMountsEditor::DlgMountsEditor(QWidget *parent, shv::iotqt::rpc::ClientConnection *rpc_connection) :
DlgMountsEditor::DlgMountsEditor(QWidget *parent, shv::iotqt::rpc::ClientConnection *rpc_connection, const std::string &broker_path) :
QDialog(parent),
ui(new Ui::DlgMountsEditor)
{
Expand All @@ -24,6 +25,8 @@ DlgMountsEditor::DlgMountsEditor(QWidget *parent, shv::iotqt::rpc::ClientConnect

m_rpcConnection = rpc_connection;

m_aclNodePath = TheApp::aclAccessPath(broker_path, m_rpcConnection->shvApiVersion());

static constexpr double ROW_HEIGHT_RATIO = 1.3;
static QStringList INFO_HEADER_NAMES { tr("Device ID"), tr("Mount point"), tr("Description") };

Expand All @@ -49,22 +52,18 @@ DlgMountsEditor::DlgMountsEditor(QWidget *parent, shv::iotqt::rpc::ClientConnect
connect(ui->leFilter, &QLineEdit::textChanged, m_modelProxy, &QSortFilterProxyModel::setFilterFixedString);

setStatusText(QString());

listMounts();
}

DlgMountsEditor::~DlgMountsEditor()
{
delete ui;
}

void DlgMountsEditor::init(const std::string &acl_node_path)
{
m_aclEtcNodePath = acl_node_path;
listMounts();
}

std::string DlgMountsEditor::aclEtcMountsNodePath()
{
return m_aclEtcNodePath + "/mounts";
return m_aclNodePath + "/mounts";
}

QString DlgMountsEditor::selectedMount()
Expand All @@ -79,7 +78,7 @@ QString DlgMountsEditor::selectedMount()

void DlgMountsEditor::onAddMountClicked()
{
auto dlg = new DlgAddEditMount(this, m_rpcConnection, m_aclEtcNodePath, DlgAddEditMount::DialogType::Add);
auto dlg = new DlgAddEditMount(this, m_rpcConnection, m_aclNodePath, DlgAddEditMount::DialogType::Add);
connect(dlg, &QDialog::finished, dlg, [this, dlg] (int result) {
if (result == QDialog::Accepted){
listMounts();
Expand Down Expand Up @@ -135,7 +134,7 @@ void DlgMountsEditor::onEditMountClicked()

setStatusText(QString());

auto dlg = new DlgAddEditMount(this, m_rpcConnection, m_aclEtcNodePath, DlgAddEditMount::DialogType::Edit);
auto dlg = new DlgAddEditMount(this, m_rpcConnection, m_aclNodePath, DlgAddEditMount::DialogType::Edit);
dlg->init(mount);
connect(dlg, &QDialog::finished, dlg, [this, dlg] (int result) {
if (result == QDialog::Accepted){
Expand Down
6 changes: 2 additions & 4 deletions shvspy/src/dlgmountseditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ class DlgMountsEditor : public QDialog
Q_OBJECT

public:
explicit DlgMountsEditor(QWidget *parent, shv::iotqt::rpc::ClientConnection *rpc_connection);
explicit DlgMountsEditor(QWidget *parent, shv::iotqt::rpc::ClientConnection *rpc_connection, const std::string &broker_path);
~DlgMountsEditor() override;
void init(const std::string &acl_node_path);

private:
std::string aclEtcMountsNodePath();

Expand All @@ -45,7 +43,7 @@ class DlgMountsEditor : public QDialog
RpcCallStatus status = Unfinished;
};
Ui::DlgMountsEditor *ui;
std::string m_aclEtcNodePath;
std::string m_aclNodePath;
shv::iotqt::rpc::ClientConnection *m_rpcConnection = nullptr;
QMap<QString, MountPointInfo> m_mountPoints;
QString m_lastCurrentId;
Expand Down
29 changes: 16 additions & 13 deletions shvspy/src/dlgroleseditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ui_dlgroleseditor.h"

#include "dlgaddeditrole.h"
#include "theapp.h"

#include <shv/core/assert.h>
#include <shv/iotqt/rpc/rpccall.h>
Expand All @@ -13,9 +14,10 @@

static const std::string SET_VALUE_METHOD = "setValue";

DlgRolesEditor::DlgRolesEditor(QWidget *parent, shv::iotqt::rpc::ClientConnection *rpc_connection) :
QDialog(parent),
ui(new Ui::DlgRolesEditor)
DlgRolesEditor::DlgRolesEditor(QWidget *parent, shv::iotqt::rpc::ClientConnection *rpc_connection, const std::string &broker_path)
: QDialog(parent)
, ui(new Ui::DlgRolesEditor)
, m_brokerPath(broker_path)
{
ui->setupUi(this);

Expand Down Expand Up @@ -45,27 +47,23 @@ DlgRolesEditor::DlgRolesEditor(QWidget *parent, shv::iotqt::rpc::ClientConnectio
connect(ui->leFilter, &QLineEdit::textChanged, m_modelProxy, &QSortFilterProxyModel::setFilterFixedString);

setStatusText(QString());

listRoles();
}

DlgRolesEditor::~DlgRolesEditor()
{
delete ui;
}

void DlgRolesEditor::init(const std::string &acl_node_path)
{
m_aclEtcNodePath = acl_node_path;
listRoles();
}

std::string DlgRolesEditor::aclEtcRolesNodePath()
{
return m_aclEtcNodePath + "/roles";
return aclAccessPath() + "/roles";
}

std::string DlgRolesEditor::aclEtcAccessNodePath()
{
return m_aclEtcNodePath + "/access";
return aclAccessPath() + "/access";
}

QString DlgRolesEditor::selectedRole()
Expand All @@ -75,7 +73,7 @@ QString DlgRolesEditor::selectedRole()

void DlgRolesEditor::onAddRoleClicked()
{
auto dlg = new DlgAddEditRole(this, m_rpcConnection, m_aclEtcNodePath, DlgAddEditRole::DialogType::Add);
auto dlg = new DlgAddEditRole(this, m_rpcConnection, aclAccessPath(), DlgAddEditRole::DialogType::Add);
connect(dlg, &QDialog::finished, dlg, [this, dlg] (int result) {
if (result == QDialog::Accepted){
listRoles();
Expand All @@ -97,7 +95,7 @@ void DlgRolesEditor::onEditRoleClicked()

setStatusText(QString());

auto dlg = new DlgAddEditRole(this, m_rpcConnection, m_aclEtcNodePath, DlgAddEditRole::DialogType::Edit);
auto dlg = new DlgAddEditRole(this, m_rpcConnection, aclAccessPath(), DlgAddEditRole::DialogType::Edit);
dlg->init(role);
connect(dlg, &QDialog::finished, dlg, [this, dlg] (int result) {
if (result == QDialog::Accepted){
Expand Down Expand Up @@ -190,3 +188,8 @@ void DlgRolesEditor::setStatusText(const QString &txt)
ui->lblStatus->setText(txt);
}
}

std::string DlgRolesEditor::aclAccessPath()
{
return TheApp::aclAccessPath(m_brokerPath, m_rpcConnection->shvApiVersion());
}
7 changes: 3 additions & 4 deletions shvspy/src/dlgroleseditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ class DlgRolesEditor : public QDialog
Q_OBJECT

public:
explicit DlgRolesEditor(QWidget *parent, shv::iotqt::rpc::ClientConnection *rpc_connection);
explicit DlgRolesEditor(QWidget *parent, shv::iotqt::rpc::ClientConnection *rpc_connection, const std::string &broker_path);
~DlgRolesEditor() override;
void init(const std::string &acl_node_path);

private:
std::string aclEtcRolesNodePath();
Expand All @@ -33,10 +32,10 @@ class DlgRolesEditor : public QDialog
void onTableRoleDoubleClicked(QModelIndex ix);

void setStatusText(const QString &txt);

std::string aclAccessPath();
private:
Ui::DlgRolesEditor *ui;
std::string m_aclEtcNodePath;
std::string m_brokerPath;
shv::iotqt::rpc::ClientConnection *m_rpcConnection = nullptr;
QStandardItemModel *m_dataModel;
QSortFilterProxyModel *m_modelProxy;
Expand Down
126 changes: 63 additions & 63 deletions shvspy/src/dlgselectroles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,39 @@ DlgSelectRoles::DlgSelectRoles(QWidget *parent):

void DlgSelectRoles::init(shv::iotqt::rpc::ClientConnection *rpc_connection, const std::string &acl_etc_node_path, const std::vector<std::string> &roles)
{
m_rpcConnection = rpc_connection;
m_aclEtcNodePath = acl_etc_node_path;
m_rpcConnection = rpc_connection;
m_aclEtcNodePath = acl_etc_node_path;

m_rolesTreeModel = new RolesTreeModel(this);
m_rolesTreeModel->load(rpc_connection, aclEtcRolesNodePath());
ui->tvRoles->setModel(m_rolesTreeModel);
ui->tvRoles->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
ui->tvRoles->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
m_userRoles = roles;

connect(ui->tvRoles, &QTreeView::customContextMenuRequested, this, &DlgSelectRoles::contextMenu);
connect(ui->tvRoles->selectionModel(), &QItemSelectionModel::currentChanged, this, [this]() {
ui->editRoleButton->setEnabled(ui->tvRoles->currentIndex().isValid());
});
connect(ui->editRoleButton, &QPushButton::clicked, this, &DlgSelectRoles::editRole);
connect(ui->tvRoles, &QTreeView::customContextMenuRequested, this, &DlgSelectRoles::contextMenu);
connect(ui->tvRoles->selectionModel(), &QItemSelectionModel::currentChanged, this, [this]() {
ui->editRoleButton->setEnabled(ui->tvRoles->currentIndex().isValid());
});
connect(ui->editRoleButton, &QPushButton::clicked, this, &DlgSelectRoles::editRole);

ui->lblStatus->setText(tr("Loading..."));
ui->tvRoles->setEnabled(false);
ui->editRoleButton->setEnabled(false);
ui->lblStatus->setText(tr("Loading..."));
ui->tvRoles->setEnabled(false);
ui->editRoleButton->setEnabled(false);

connect(m_rolesTreeModel, &RolesTreeModel::loadFinished, this, [this](){
ui->lblStatus->clear();
ui->tvRoles->setEnabled(true);
ui->lblStatus->clear();
ui->tvRoles->setEnabled(true);
m_rolesTreeModel->setSelectedRoles(m_userRoles);

if (!m_currentItemPath.isEmpty()) {
QStandardItem *item = findChildItem(m_rolesTreeModel->invisibleRootItem(), m_currentItemPath);
if (item) {
QModelIndex ix = m_rolesTreeModel->indexFromItem(item);
ui->tvRoles->setCurrentIndex(ix);
ui->tvRoles->scrollTo(ix);
}
}
});
if (!m_currentItemPath.isEmpty()) {
QStandardItem *item = findChildItem(m_rolesTreeModel->invisibleRootItem(), m_currentItemPath);
if (item) {
QModelIndex ix = m_rolesTreeModel->indexFromItem(item);
ui->tvRoles->setCurrentIndex(ix);
ui->tvRoles->scrollTo(ix);
}
}
});

connect(m_rolesTreeModel, &RolesTreeModel::loadError, this, [this](QString error){
ui->lblStatus->setText(error);
Expand All @@ -60,73 +60,73 @@ std::vector<std::string> DlgSelectRoles::selectedRoles()

void DlgSelectRoles::setUserRoles(const std::vector<std::string> &roles)
{
m_rolesTreeModel->setSelectedRoles(roles);
m_rolesTreeModel->setSelectedRoles(roles);
}

void DlgSelectRoles::contextMenu(const QPoint &glob_pos)
{
QMenu menu(this);
auto *edit_role = new QAction(tr("&Edit role"), &menu);
connect(edit_role, &QAction::triggered, this, &DlgSelectRoles::editRole);
edit_role->setEnabled(ui->tvRoles->currentIndex().isValid());
menu.addAction(edit_role);
menu.exec(mapToGlobal(glob_pos));
QMenu menu(this);
auto *edit_role = new QAction(tr("&Edit role"), &menu);
connect(edit_role, &QAction::triggered, this, &DlgSelectRoles::editRole);
edit_role->setEnabled(ui->tvRoles->currentIndex().isValid());
menu.addAction(edit_role);
menu.exec(mapToGlobal(glob_pos));
}

void DlgSelectRoles::editRole()
{
QStandardItem *item = m_rolesTreeModel->itemFromIndex(ui->tvRoles->currentIndex());
QString role_name = item->data(RolesTreeModel::NameRole).toString();
QStandardItem *item = m_rolesTreeModel->itemFromIndex(ui->tvRoles->currentIndex());
QString role_name = item->data(RolesTreeModel::NameRole).toString();

auto dlg = new DlgAddEditRole (this, m_rpcConnection, m_aclEtcNodePath, DlgAddEditRole::DialogType::Edit);
auto dlg = new DlgAddEditRole (this, m_rpcConnection, m_aclEtcNodePath, DlgAddEditRole::DialogType::Edit);
dlg->init(role_name);
connect(dlg, &QDialog::finished, dlg, [this, dlg, item] (int result) mutable {
if (result == QDialog::Accepted) {
m_currentItemPath << item->text();
while (item->parent()) {
item = item->parent();
m_currentItemPath.prepend(item->text());
}
m_userRoles = selectedRoles();

m_rolesTreeModel->clear();
ui->tvRoles->setEnabled(false);
ui->lblStatus->setText(tr("Reloading roles..."));
ui->editRoleButton->setEnabled(false);
m_rolesTreeModel->load(m_rpcConnection, aclEtcRolesNodePath());
}
if (result == QDialog::Accepted) {
m_currentItemPath << item->text();
while (item->parent()) {
item = item->parent();
m_currentItemPath.prepend(item->text());
}
m_userRoles = selectedRoles();

m_rolesTreeModel->clear();
ui->tvRoles->setEnabled(false);
ui->lblStatus->setText(tr("Reloading roles..."));
ui->editRoleButton->setEnabled(false);
m_rolesTreeModel->load(m_rpcConnection, aclEtcRolesNodePath());
}

dlg->deleteLater();
});
dlg->open();
dlg->open();
}

QStandardItem *DlgSelectRoles::findChildItem(QStandardItem *item, const QStringList &path, int ix)
{
QStandardItem *child = findChildItem(item, path[ix]);
if (!child) {
return nullptr;
}
if (++ix == path.count()) {
return child;
}
return findChildItem(child, path, ix);
QStandardItem *child = findChildItem(item, path[ix]);
if (!child) {
return nullptr;
}
if (++ix == path.count()) {
return child;
}
return findChildItem(child, path, ix);
}

QStandardItem *DlgSelectRoles::findChildItem(QStandardItem *item, const QString &text)
{
for (int i = 0; i < item->rowCount(); ++i) {
QStandardItem *child = item->child(i, 0);
if (child->text() == text) {
return child;
}
}
return nullptr;
for (int i = 0; i < item->rowCount(); ++i) {
QStandardItem *child = item->child(i, 0);
if (child->text() == text) {
return child;
}
}
return nullptr;
}

std::string DlgSelectRoles::aclEtcRolesNodePath()
{
return m_aclEtcNodePath + "/roles";
return m_aclEtcNodePath + "/roles";
}

DlgSelectRoles::~DlgSelectRoles()
Expand Down
Loading

0 comments on commit e61880b

Please sign in to comment.