2022-07-05 10:13:48 +08:00
|
|
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
|
|
|
* Copyright (C) 2022 Tianjin KYLIN Information Technology Co., Ltd.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
2022-07-04 13:45:35 +08:00
|
|
|
#include "blacklistpage.h"
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
#define CONTENTS_MARGINS 0, 0, 0, 0
|
|
|
|
#define FRAME_MIN_SIZE 550, 60
|
|
|
|
#define FRAME_MAX_SIZE 16777215, 16777215
|
|
|
|
#define LINE_MAX_SIZE 16777215, 1
|
|
|
|
#define LINE_MIN_SIZE 0, 1
|
|
|
|
|
2022-11-09 15:14:18 +08:00
|
|
|
#define LOG_HEAD "[BlacklistPage]"
|
|
|
|
|
2022-07-04 13:45:35 +08:00
|
|
|
BlacklistPage::BlacklistPage(QWidget *parent) : QWidget(parent)
|
|
|
|
{
|
|
|
|
QVBoxLayout *Vlayout = new QVBoxLayout(this);
|
|
|
|
Vlayout->setContentsMargins(CONTENTS_MARGINS);
|
|
|
|
Vlayout->setSpacing(0);
|
|
|
|
|
2023-04-06 14:09:59 +08:00
|
|
|
m_blacklistFrame = new QFrame(this);
|
|
|
|
m_blacklistFrame->setMinimumSize(FRAME_MIN_SIZE);
|
|
|
|
m_blacklistFrame->setMaximumSize(FRAME_MAX_SIZE);
|
|
|
|
m_blacklistFrame->setFrameShape(QFrame::Box);
|
2022-07-04 13:45:35 +08:00
|
|
|
|
2023-04-06 14:09:59 +08:00
|
|
|
m_blacklistLayout = new QVBoxLayout(m_blacklistFrame);
|
2022-07-04 13:45:35 +08:00
|
|
|
m_blacklistLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
m_blacklistLayout->setSpacing(0);
|
|
|
|
|
|
|
|
m_titleLabel = new TitleLabel(this);
|
|
|
|
m_titleLabel->setText(tr("Blacklist"));
|
|
|
|
|
|
|
|
Vlayout->addWidget(m_titleLabel);
|
|
|
|
Vlayout->addSpacing(8);
|
2023-04-06 14:09:59 +08:00
|
|
|
Vlayout->addWidget(m_blacklistFrame);
|
2022-07-04 13:45:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QFrame* BlacklistPage::myLine()
|
|
|
|
{
|
|
|
|
QFrame *line = new QFrame(this);
|
|
|
|
line->setMinimumSize(QSize(LINE_MIN_SIZE));
|
|
|
|
line->setMaximumSize(QSize(LINE_MAX_SIZE));
|
|
|
|
line->setLineWidth(0);
|
|
|
|
line->setFrameShape(QFrame::HLine);
|
|
|
|
line->setFrameShadow(QFrame::Sunken);
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlacklistPage::getBlacklistDevice(QMap<QString, QString> &blacklistMap)
|
|
|
|
{
|
|
|
|
if (m_settingPathInterface == nullptr || !m_settingPathInterface->isValid()) {
|
2022-11-09 15:14:18 +08:00
|
|
|
qDebug() << LOG_HEAD << "dbus interface m_settingPathInterface is invaild";
|
2022-07-05 10:23:21 +08:00
|
|
|
return;
|
2022-07-04 13:45:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QDBusMessage reply = m_settingPathInterface->call("Getblacklist");
|
|
|
|
if(reply.type() == QDBusMessage::ErrorMessage)
|
|
|
|
{
|
2022-11-09 15:14:18 +08:00
|
|
|
qWarning() << LOG_HEAD << "Getblacklist error:" << reply.errorMessage();
|
2022-07-04 13:45:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (reply.arguments().isEmpty()
|
|
|
|
|| reply.arguments().at(0).toString() == ""
|
|
|
|
|| reply.arguments().at(1).toString() == "") {
|
2022-11-09 15:14:18 +08:00
|
|
|
qDebug() << LOG_HEAD << "Dbus interface call Getblacklist return is empty!";
|
2022-07-05 10:23:21 +08:00
|
|
|
return;
|
2022-07-04 13:45:35 +08:00
|
|
|
}
|
|
|
|
QStringList macList = reply.arguments().at(0).toString().split(";");
|
|
|
|
QStringList hostNameList = reply.arguments().at(1).toString().split(";");
|
|
|
|
for (int index = 0; index < macList.count() && macList.at(index) != nullptr; index ++) {
|
|
|
|
QString macTemp = macList.at(index);
|
|
|
|
macTemp = macTemp.trimmed();
|
2023-04-06 14:09:59 +08:00
|
|
|
if (!blacklistMap.contains(macTemp) && hostNameList.at(index) != nullptr) {
|
2022-07-04 13:45:35 +08:00
|
|
|
blacklistMap[macTemp] = hostNameList.at(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlacklistPage::initBlacklistDev()
|
|
|
|
{
|
|
|
|
QMap<QString, QString>::const_iterator item = m_blacklistMap.cbegin();
|
|
|
|
while (item != m_blacklistMap.cend()) {
|
|
|
|
addBlacklistDevFrame(item.key(), item.value());
|
|
|
|
item ++;
|
|
|
|
if (item != m_blacklistMap.cend()) {
|
|
|
|
m_blacklistLayout->addWidget(myLine());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 15:14:18 +08:00
|
|
|
void BlacklistPage::onsetStaIntoBlacklist(QString staMac, QString staName)
|
2022-07-04 13:45:35 +08:00
|
|
|
{
|
|
|
|
if (m_settingPathInterface == nullptr || !m_settingPathInterface->isValid()) {
|
2022-11-09 15:14:18 +08:00
|
|
|
qDebug() << LOG_HEAD << LOG_HEAD << "dbus interface m_settingPathInterface is invaild";
|
2022-07-05 10:23:21 +08:00
|
|
|
return;
|
2022-07-04 13:45:35 +08:00
|
|
|
}
|
|
|
|
|
2022-11-09 15:14:18 +08:00
|
|
|
QDBusMessage reply = m_settingPathInterface->call("Addblacklist", staMac, staName);
|
2022-07-04 13:45:35 +08:00
|
|
|
if(reply.type() == QDBusMessage::ErrorMessage)
|
|
|
|
{
|
2022-11-09 15:14:18 +08:00
|
|
|
qWarning() << LOG_HEAD << "Addblacklist error:" << reply.errorMessage();
|
2022-07-05 10:23:21 +08:00
|
|
|
return;
|
2022-07-04 13:45:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
refreshBlacklist();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlacklistPage::addBlacklistDevFrame(QString staMac, QString staName)
|
|
|
|
{
|
|
|
|
BlacklistItem *itemFrame = new BlacklistItem(staMac, staName, m_blacklistLayout->widget());
|
|
|
|
m_blacklistLayout->addWidget(itemFrame);
|
|
|
|
connect(itemFrame, &BlacklistItem::onBtnClicked, this, &BlacklistPage::onRemoveFromBlacklistBtnClicked);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlacklistPage::clearBlacklistLayout()
|
|
|
|
{
|
|
|
|
if (m_blacklistLayout->layout() != NULL) {
|
|
|
|
QLayoutItem* layoutItem;
|
|
|
|
while ((layoutItem = m_blacklistLayout->layout()->takeAt(0)) != NULL) {
|
|
|
|
delete layoutItem->widget();
|
|
|
|
delete layoutItem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 15:14:18 +08:00
|
|
|
bool BlacklistPage::removeStaFromBlacklist(QString staMac, QString staName)
|
2022-07-04 13:45:35 +08:00
|
|
|
{
|
|
|
|
if (m_settingPathInterface == nullptr || !m_settingPathInterface->isValid()) {
|
2022-11-09 15:14:18 +08:00
|
|
|
qDebug() << LOG_HEAD << "dbus interface m_settingPathInterface is invaild";
|
2022-07-04 13:45:35 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-09 15:14:18 +08:00
|
|
|
QDBusMessage reply = m_settingPathInterface->call("Delblacklist", staMac, staName);
|
2022-07-04 13:45:35 +08:00
|
|
|
if(reply.type() == QDBusMessage::ErrorMessage)
|
|
|
|
{
|
2022-11-09 15:14:18 +08:00
|
|
|
qWarning() << LOG_HEAD << "Delblacklist error:" << reply.errorMessage();
|
2022-07-04 13:45:35 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-22 17:48:57 +08:00
|
|
|
void BlacklistPage::resetLayoutHight()
|
2022-07-04 13:45:35 +08:00
|
|
|
{
|
|
|
|
int height = 0;
|
|
|
|
for (int i = 0; i < m_blacklistLayout->count(); i ++) {
|
|
|
|
QWidget *w = m_blacklistLayout->itemAt(i)->widget();
|
|
|
|
if (w != nullptr) {
|
|
|
|
height += w->height();
|
|
|
|
}
|
|
|
|
}
|
2023-04-06 14:09:59 +08:00
|
|
|
m_blacklistFrame->setFixedHeight(height);
|
2022-07-04 13:45:35 +08:00
|
|
|
|
2022-07-22 17:48:57 +08:00
|
|
|
if (m_blacklistMap.isEmpty()) {
|
2022-07-04 13:45:35 +08:00
|
|
|
this->hide();
|
2022-07-22 17:48:57 +08:00
|
|
|
} else {
|
|
|
|
this->show();
|
2022-07-04 13:45:35 +08:00
|
|
|
}
|
|
|
|
this->update();
|
|
|
|
}
|
|
|
|
|
2022-07-22 17:48:57 +08:00
|
|
|
void BlacklistPage::refreshBlacklist()
|
|
|
|
{
|
|
|
|
m_blacklistMap.clear();
|
|
|
|
getBlacklistDevice(m_blacklistMap);
|
|
|
|
clearBlacklistLayout();
|
|
|
|
initBlacklistDev();
|
|
|
|
resetLayoutHight();
|
|
|
|
}
|
|
|
|
|
2022-11-09 15:14:18 +08:00
|
|
|
void BlacklistPage::onRemoveFromBlacklistBtnClicked(QString staMac, QString staName)
|
2022-07-04 13:45:35 +08:00
|
|
|
{
|
2022-11-09 15:14:18 +08:00
|
|
|
if (staMac.isNull()
|
|
|
|
|| staMac.isEmpty()
|
|
|
|
|| staName.isNull()
|
|
|
|
|| staName.isEmpty()) {
|
|
|
|
qDebug() << LOG_HEAD <<"On remove from blacklist button clicked error! sta mac or name is empty!";
|
2022-07-04 13:45:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-11-09 15:14:18 +08:00
|
|
|
removeStaFromBlacklist(staMac, staName);
|
2022-07-04 15:44:42 +08:00
|
|
|
refreshBlacklist();
|
2022-07-04 13:45:35 +08:00
|
|
|
}
|
|
|
|
|