// SPDX-License-Identifier: LGPL-3.0-or-later /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright (C) 2013 - 2024, nymea GmbH * Copyright (C) 2024 - 2025, chargebyte austria GmbH * * This file is part of libnymea-app. * * libnymea-app is free software: you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation, either version 3 * of the License, or (at your option) any later version. * * libnymea-app is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with libnymea-app. If not, see . * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include "ruleactionparams.h" #include "ruleactionparam.h" RuleActionParams::RuleActionParams(QObject *parent) : QAbstractListModel(parent) { } int RuleActionParams::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent) return static_cast(m_list.count()); } QVariant RuleActionParams::data(const QModelIndex &index, int role) const { switch (role) { case RoleParamTypeId: return m_list.at(index.row())->paramTypeId(); case RoleValue: return m_list.at(index.row())->value(); case RoleEventTypeId: return m_list.at(index.row())->eventTypeId(); case RoleEventParamTypeId: return m_list.at(index.row())->eventParamTypeId(); } return QVariant(); } QHash RuleActionParams::roleNames() const { QHash roles; roles.insert(RoleParamTypeId, "paramTypeId"); roles.insert(RoleValue, "value"); roles.insert(RoleEventTypeId, "eventTypeId"); roles.insert(RoleEventParamTypeId, "eventParamTypeId"); return roles; } void RuleActionParams::addRuleActionParam(RuleActionParam *ruleActionParam) { ruleActionParam->setParent(this); beginInsertRows(QModelIndex(), static_cast(m_list.count()), static_cast(m_list.count())); m_list.append(ruleActionParam); endInsertRows(); emit countChanged(); } void RuleActionParams::setRuleActionParam(const QUuid ¶mTypeId, const QVariant &value) { foreach (RuleActionParam *rap, m_list) { if (rap->paramTypeId() == paramTypeId) { rap->setValue(value); return; } } // Still here? Need to add it RuleActionParam *rap = new RuleActionParam(this); rap->setParamTypeId(paramTypeId); rap->setValue(value); addRuleActionParam(rap); } void RuleActionParams::setRuleActionParamByName(const QString ¶mName, const QVariant &value) { foreach (RuleActionParam *rap, m_list) { if (rap->paramName() == paramName) { rap->setValue(value); return; } } // Still here? Need to add it RuleActionParam *rap = new RuleActionParam(this); rap->setParamName(paramName); rap->setValue(value); addRuleActionParam(rap); } void RuleActionParams::setRuleActionParamEvent(const QUuid ¶mTypeId, const QString &eventTypeId, const QString &eventParamTypeId) { foreach (RuleActionParam *rap, m_list) { if (rap->paramTypeId() == paramTypeId) { rap->setEventTypeId(eventTypeId); rap->setEventParamTypeId(eventParamTypeId); return; } } RuleActionParam *rap = new RuleActionParam(this); rap->setParamTypeId(paramTypeId); rap->setEventTypeId(eventTypeId); rap->setEventParamTypeId(eventParamTypeId); addRuleActionParam(rap); } void RuleActionParams::setRuleActionParamEventByName(const QString ¶mName, const QString &eventTypeId, const QString &eventParamTypeId) { foreach (RuleActionParam *rap, m_list) { if (rap->paramName() == paramName) { rap->setEventTypeId(eventTypeId); rap->setEventParamTypeId(eventParamTypeId); return; } } RuleActionParam *rap = new RuleActionParam(this); rap->setParamName(paramName); rap->setEventTypeId(eventTypeId); rap->setEventParamTypeId(eventParamTypeId); addRuleActionParam(rap); } void RuleActionParams::setRuleActionParamState(const QUuid ¶mTypeId, const QString &stateThingId, const QString &stateTypeId) { foreach (RuleActionParam *rap, m_list) { if (rap->paramTypeId() == paramTypeId) { rap->setStateThingId(stateThingId); rap->setStateTypeId(stateTypeId); return; } } RuleActionParam *rap = new RuleActionParam(this); rap->setParamTypeId(paramTypeId); rap->setStateThingId(stateThingId); rap->setStateTypeId(stateTypeId); addRuleActionParam(rap); } void RuleActionParams::setRuleActionParamStateByName(const QString ¶mName, const QString &stateThingId, const QString &stateTypeId) { foreach (RuleActionParam *rap, m_list) { if (rap->paramName() == paramName) { rap->setStateThingId(stateThingId); rap->setStateTypeId(stateTypeId); return; } } RuleActionParam *rap = new RuleActionParam(this); rap->setParamName(paramName); rap->setStateThingId(stateThingId); rap->setStateTypeId(stateTypeId); addRuleActionParam(rap); } RuleActionParam *RuleActionParams::get(int index) const { if (index < 0 || index >= m_list.count()) { return nullptr; } return m_list.at(index); } RuleActionParam *RuleActionParams::getParam(const QUuid ¶mTypeId) { for (int i = 0; i < m_list.count(); i++) { if (m_list.at(i)->paramTypeId() == paramTypeId) { return m_list.at(i); } } return nullptr; } bool RuleActionParams::hasRuleActionParam(const QUuid ¶mTypeId) const { for (int i = 0; i < m_list.count(); i++) { if (m_list.at(i)->paramTypeId() == paramTypeId) { return true; } } return false; } void RuleActionParams::clear() { beginResetModel(); foreach (RuleActionParam *param, m_list) param->deleteLater(); m_list.clear(); endResetModel(); emit countChanged(); } bool RuleActionParams::operator==(RuleActionParams *other) const { if (rowCount() != other->rowCount()) { return false; } for (int i = 0; i < rowCount(); i++) { if (!get(i)->operator==(other->get(i))) { return false; } } return true; }