From 4c8b7a37c3a6e0daa9b9e3807d0e73b140a7c8c2 Mon Sep 17 00:00:00 2001 From: zhangyuanyuan1 Date: Mon, 6 Mar 2023 10:48:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E9=98=B2=E7=81=AB=E5=A2=99?= =?UTF-8?q?=E7=BD=91=E7=BB=9C=E6=A8=A1=E5=BC=8F=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/networkmode/firewalldialog.cpp | 165 +++++++++++++ src/frontend/networkmode/firewalldialog.h | 92 +++++++ src/frontend/networkmode/networkmode.pri | 10 + .../networkmode/networkmodeconfig.cpp | 226 ++++++++++++++++++ src/frontend/networkmode/networkmodeconfig.h | 63 +++++ 5 files changed, 556 insertions(+) create mode 100644 src/frontend/networkmode/firewalldialog.cpp create mode 100644 src/frontend/networkmode/firewalldialog.h create mode 100644 src/frontend/networkmode/networkmode.pri create mode 100644 src/frontend/networkmode/networkmodeconfig.cpp create mode 100644 src/frontend/networkmode/networkmodeconfig.h diff --git a/src/frontend/networkmode/firewalldialog.cpp b/src/frontend/networkmode/firewalldialog.cpp new file mode 100644 index 00000000..bc433030 --- /dev/null +++ b/src/frontend/networkmode/firewalldialog.cpp @@ -0,0 +1,165 @@ +/* -*- 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. + * + */ +#include "firewalldialog.h" + +#include + +#define THEME_SCHAME "org.ukui.style" +#define COLOR_THEME "styleName" +#define ICON_SIZE 16,16 + +FixLabel::FixLabel(QWidget *parent) : + QLabel(parent) +{ + const QByteArray id("org.ukui.style"); + QGSettings * fontSetting = new QGSettings(id, QByteArray(), this); + connect(fontSetting, &QGSettings::changed,[=](QString key) { + if ("systemFont" == key || "systemFontSize" ==key) { + changedLabelSlot(); + } + }); +} + + +void FixLabel::setLabelText(QString text) { + + mStr = text; + changedLabelSlot(); +} + +QString FixLabel::getText(){ + return mStr; +} + +void FixLabel::changedLabelSlot() { + QFontMetrics fontMetrics(this->font()); + int fontSize = fontMetrics.width(mStr); + if (fontSize > this->width()) { + setText(fontMetrics.elidedText(mStr, Qt::ElideRight, this->width())); + setToolTip(mStr); + } else { + setText(mStr); + setToolTip(""); + } +} + +FirewallDialog::FirewallDialog(QWidget *parent): KDialog(parent) +{ + initUI(); + this->setWindowIcon(QIcon::fromTheme("kylin-network")); + this->setFixedSize(480, 204); + setAttribute(Qt::WA_DeleteOnClose); +// centerToScreen(); + connect(qApp, &QApplication::paletteChanged, this, &FirewallDialog::onPaletteChanged); +} + +FirewallDialog::~FirewallDialog() +{ + +} + +void FirewallDialog::initUI() +{ + m_iconLabel = new QLabel(this); + m_contentLabel = new FixLabel(this); + m_suggestLabel = new FixLabel(this); + m_PublicBtn = new QPushButton(this); + m_PrivateBtn = new QPushButton(this); + m_dialogLayout = new QVBoxLayout(this); + + m_contentLabel->setFixedWidth(405); + m_suggestLabel->setFixedWidth(405); + + QWidget *contentWidget = new QWidget(this); + QGridLayout *contentLayout = new QGridLayout(contentWidget); + contentLayout->setHorizontalSpacing(0); + contentLayout->setContentsMargins(0, 0, 0, 0); + contentLayout->addWidget(m_iconLabel, 0, 0); + contentLayout->addWidget(m_contentLabel, 0, 1); + contentLayout->addWidget(m_suggestLabel, 1, 1); + m_iconLabel->setFixedWidth(24); + + QWidget *btnWidget = new QWidget(this); + QHBoxLayout *btnLayout = new QHBoxLayout(btnWidget); + btnLayout->setContentsMargins(0, 0, 0, 0); + btnLayout->setSpacing(16); + btnLayout->addStretch(); + btnLayout->addWidget(m_PublicBtn); + btnLayout->addWidget(m_PrivateBtn); + + m_dialogLayout->setContentsMargins(22, 16, 22, 24); + m_dialogLayout->setSpacing(0); + m_dialogLayout->addWidget(contentWidget); + m_dialogLayout->addStretch(); + m_dialogLayout->addWidget(btnWidget); + + QIcon icon = QIcon::fromTheme("dialog-info"); + m_iconLabel->setPixmap(icon.pixmap(ICON_SIZE)); + + QFont font = m_contentLabel->font(); + font.setWeight(57); + m_contentLabel->setFont(font); + //是否允许此网络上的其他设备发现这台电脑? + m_contentLabel->setLabelText(tr("Allow other devices on this network to discover this computer?")); + //不建议在公共网络上开启此功能 + m_suggestLabel->setLabelText(tr("It is not recommended to enable this feature on public networks")); + + m_PublicBtn->setText(tr("Not allowed (recommended)")); + m_PrivateBtn->setText(tr("Allowed")); + + this->closeButton(); + this->mainWidget()->setLayout(m_dialogLayout); + onPaletteChanged(); + + connect(m_PublicBtn, &QPushButton::clicked, this, &FirewallDialog::setPublicNetMode); + connect(m_PrivateBtn, &QPushButton::clicked, this, &FirewallDialog::setPrivateNetMode); +} + +void FirewallDialog::centerToScreen() +{ + QDesktopWidget* m = QApplication::desktop(); + QRect desk_rect = m->screenGeometry(m->screenNumber(QCursor::pos())); + int desk_x = desk_rect.width(); + int desk_y = desk_rect.height(); + int x = this->width(); + int y = this->height(); + this->move(desk_x / 2 - x / 2 + desk_rect.left(), desk_y / 2 - y / 2 + desk_rect.top()); +} + +void FirewallDialog::onPaletteChanged() +{ + QPalette pal = qApp->palette(); + +// QGSettings * styleGsettings = nullptr; +// const QByteArray style_id(THEME_SCHAME); +// if (QGSettings::isSchemaInstalled(style_id)) { +// styleGsettings = new QGSettings(style_id); +// QString currentTheme = styleGsettings->get(COLOR_THEME).toString(); +// if(currentTheme == "ukui-default"){ +// pal = lightPalette(this); +// } +// } + this->setPalette(pal); + +// if (styleGsettings != nullptr) { +// delete styleGsettings; +// styleGsettings = nullptr; +// } +} diff --git a/src/frontend/networkmode/firewalldialog.h b/src/frontend/networkmode/firewalldialog.h new file mode 100644 index 00000000..823cc763 --- /dev/null +++ b/src/frontend/networkmode/firewalldialog.h @@ -0,0 +1,92 @@ +/* -*- 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. + * + */ +#ifndef FIREWALLDIALOG_H +#define FIREWALLDIALOG_H + +#include +#include +#include +#include +#include +#include + +#include "kwidget.h" +#include "kdialog.h" + +using namespace kdk; + +#include + +class FixLabel : public QLabel +{ + Q_OBJECT +public: + explicit FixLabel(QWidget *parent = 0); + +public: + void setLabelText(QString text); + QString getText(); + +private Q_SLOTS: + void changedLabelSlot(); +private: + QString mStr; + +}; + +class FirewallDialog : public KDialog +{ + Q_OBJECT +public: + FirewallDialog(QWidget *parent = nullptr); + ~FirewallDialog(); + void setUuid(QString uuid) { + m_uuid = uuid; + } + + void centerToScreen(); + +private: + void initUI(); + + QString m_uuid; + QLabel * m_iconLabel = nullptr; + FixLabel * m_contentLabel = nullptr; + FixLabel * m_suggestLabel = nullptr; + QVBoxLayout *m_dialogLayout = nullptr; + QPushButton *m_PublicBtn = nullptr; + QPushButton *m_PrivateBtn = nullptr; + +Q_SIGNALS: + void setPublicNetMode(); + void setPrivateNetMode(); + +private Q_SLOTS: + void onPaletteChanged(); + + +public Q_SLOTS: + void closeMyself(QString uuid, int status) { + if (uuid == m_uuid && status == 4) { + this->close(); + } + } +}; +#endif // FIREWALLDIALOG_H diff --git a/src/frontend/networkmode/networkmode.pri b/src/frontend/networkmode/networkmode.pri new file mode 100644 index 00000000..6459b81c --- /dev/null +++ b/src/frontend/networkmode/networkmode.pri @@ -0,0 +1,10 @@ +INCLUDEPATH += $$PWD + +HEADERS += \ + $$PWD/firewalldialog.h \ + $$PWD/networkmodeconfig.h + +SOURCES += \ + $$PWD/firewalldialog.cpp \ + $$PWD/networkmodeconfig.cpp + diff --git a/src/frontend/networkmode/networkmodeconfig.cpp b/src/frontend/networkmode/networkmodeconfig.cpp new file mode 100644 index 00000000..19831a22 --- /dev/null +++ b/src/frontend/networkmode/networkmodeconfig.cpp @@ -0,0 +1,226 @@ +/* -*- 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. + * + */ +#include "networkmodeconfig.h" +#include "firewalldialog.h" +#include + +#define LOG_FLAG "[NetworkModeConfig]" + +NetworkModeConfig::NetworkModeConfig(QObject *parent) + :QObject(parent) +{ + qRegisterMetaType("KyConnectState"); + + m_thread = new QThread; + m_manager = new KyNetworkManager(); + m_manager->moveToThread(m_thread); + connect(m_thread, &QThread::started, m_manager, &KyNetworkManager::kylinNetworkManagerInit); + connect(m_thread, &QThread::finished, m_manager, &KyNetworkManager::deleteLater); + m_thread->start(); + while (!m_manager->isInitFinished()) { + ::usleep(1000); + } + + //init dbus + m_firewallDbusIface = new QDBusInterface("com.ksc.defender", + "/firewall", + "com.ksc.defender.firewall", + QDBusConnection::systemBus()); + if(!m_firewallDbusIface->isValid()) { + qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message()); + } + + //connect + connect(m_manager, &KyNetworkManager::wiredStateChange, this, [=](QString deviceName, QString uuid, KyConnectState status) { + KyConnectSetting connectSetting; + m_manager->getConnectIpInfo(uuid, connectSetting); + onActiveConnectionChanged(deviceName, connectSetting.m_connectName, uuid, status); + }); + connect(m_manager, &KyNetworkManager::wirelessStateChange, this, &NetworkModeConfig::onActiveConnectionChanged); +} + +void NetworkModeConfig::initWiredNetworkMode() +{ + qDebug()<< LOG_FLAG << "initWiredNetworkMode"; + QStringList wiredDevList; + m_manager->getNetworkDeviceList(DEVICE_TYPE_ETHERNET, wiredDevList); + if (wiredDevList.isEmpty()) { + return; + } + + for (auto devName : wiredDevList) { + QList activedList; + m_manager->getActiveConnectionList(devName, CONNECT_TYPE_WIRED, activedList); + if (activedList.isEmpty()) { + continue; + } + + int configType = getNetworkModeConfig(activedList.at(0).m_uuid); + if (configType == DBUS_INVAILD) { + return; + } else if (configType == NO_CONFIG) { + //已连接网络无配置 默认公有配置 + setNetworkModeConfig(activedList.at(0).m_uuid, devName, + activedList.at(0).m_connName, KSC_FIREWALL_PUBLIC); + } else { + setNetworkModeConfig(activedList.at(0).m_uuid, devName, + activedList.at(0).m_connName, configType); + } + } +} + +void NetworkModeConfig::initWirelessNetworkMode() +{ + qDebug()<< LOG_FLAG << "initWirelessNetworkMode"; + QStringList wirelessDevList; + m_manager->getNetworkDeviceList(DEVICE_TYPE_WIFI, wirelessDevList); + if (wirelessDevList.isEmpty()) { + return; + } + + for (auto devName : wirelessDevList) { + QList activedList; + m_manager->getActiveConnectionList(devName, CONNECT_TYPE_WIRELESS, activedList); + if (activedList.isEmpty()) { + return; + } + + int configType = getNetworkModeConfig(activedList.at(0).m_uuid); + if (configType == DBUS_INVAILD) { + return; + } else if (configType == NO_CONFIG) { + //已连接网络无配置 默认公有配置 + setNetworkModeConfig(activedList.at(0).m_uuid, devName, + activedList.at(0).m_ssid, KSC_FIREWALL_PUBLIC); + } else { + setNetworkModeConfig(activedList.at(0).m_uuid, devName, + activedList.at(0).m_ssid, configType); + } + } +} + +int NetworkModeConfig::getNetworkModeConfig(QString uuid) +{ + if (uuid.isEmpty()) { + qWarning()<< /*LOG_FLAG <<*/ "uuid is empty, so can not get network mode config"; + return NO_CONFIG; + } + + if(m_firewallDbusIface == nullptr || !m_firewallDbusIface->isValid()) { + qWarning () << "com.ksc.defender dbus is invalid"; + return DBUS_INVAILD; + } + + QDBusReply reply = m_firewallDbusIface->call("get_networkModeConfig", uuid); + if (reply.isValid()) { + return reply.value(); + } else { + qWarning() << "call get_networkModeConfig failed" << reply.error().message(); + } + return NO_CONFIG; +} + +void NetworkModeConfig::setNetworkModeConfig(QString uuid, QString cardName, QString ssid, int mode) +{ + if(m_firewallDbusIface == nullptr || !m_firewallDbusIface->isValid()) { + qWarning () << "com.ksc.defender dbus is invalid"; + return; + } + + QDBusReply reply = m_firewallDbusIface->call("set_networkModeConfig", uuid, cardName, ssid, mode); + if (reply.isValid()) { + qDebug() << "set_networkModeConfig" << ssid << uuid << cardName << mode << ",result" << reply.value(); + } else { + qWarning() << "call set_networkModeConfig" << reply.error().message(); + } +} + +int NetworkModeConfig::breakNetworkConnect(QString uuid, QString cardName, QString ssid) +{ + if(m_firewallDbusIface == nullptr || !m_firewallDbusIface->isValid()) { + qWarning () << "com.ksc.defender dbus is invalid"; + return -1; + } + + QDBusReply reply = m_firewallDbusIface->call("break_networkConnect", uuid, cardName, ssid); + if (reply.isValid()) { + qDebug() << "break_networkConnect" << ssid << uuid << cardName << ",result" << reply.value(); + return reply.value(); + } else { + qWarning() << "call break_networkConnect failed" << reply.error().message(); + return -1; + } +} + +void NetworkModeConfig::setFirstConnectNetworkMode(QString uuid, QString deviceName, QString ssid) +{ + setNetworkModeConfig(uuid, deviceName, ssid, KSC_FIREWALL_PUBLIC); //默认公有配置 + FirewallDialog *fireWallDialog = new FirewallDialog(); + fireWallDialog->setUuid(uuid); + fireWallDialog->setWindowTitle(ssid); + + connect(fireWallDialog, &FirewallDialog::setPrivateNetMode, this, [=](){ + fireWallDialog->hide(); + setNetworkModeConfig(uuid, deviceName, ssid, KSC_FIREWALL_PRIVATE); + }); + + connect(fireWallDialog, &FirewallDialog::setPublicNetMode, this, [=](){ + fireWallDialog->hide(); + setNetworkModeConfig(uuid, deviceName, ssid, KSC_FIREWALL_PUBLIC); + }); + + connect(m_manager, &KyNetworkManager::wiredStateChange, this, [=](QString devName, QString connUuid, KyConnectState state) { + Q_UNUSED(devName) + fireWallDialog->closeMyself(connUuid, state); + }); + connect(m_manager, &KyNetworkManager::wirelessStateChange, this, [=](QString devName, + QString connsSsid, QString connUuid, KyConnectState state) { + Q_UNUSED(devName) + Q_UNUSED(connsSsid) + fireWallDialog->closeMyself(connUuid, state); + }); + + fireWallDialog->show(); + fireWallDialog->centerToScreen(); +} + +void NetworkModeConfig::onActiveConnectionChanged(QString deviceName, QString ssid, QString uuid, KyConnectState status) +{ + qDebug()<< LOG_FLAG << "onActiveConnectionChanged" << deviceName << ssid << uuid << status; + + if (ssid.isEmpty()) { + breakNetworkConnect(uuid, deviceName, ssid); + return; + } + + if (status == CONNECT_STATE_ACTIVATED) { + int configType = getNetworkModeConfig(uuid); + + if (configType == NO_CONFIG) { + setFirstConnectNetworkMode(uuid, deviceName, ssid); + } else if (configType == KSC_FIREWALL_PUBLIC) { + setNetworkModeConfig(uuid, deviceName, ssid, KSC_FIREWALL_PUBLIC); + } else if (configType == KSC_FIREWALL_PRIVATE) { + setNetworkModeConfig(uuid, deviceName, ssid, KSC_FIREWALL_PRIVATE); + } + } else if (status == CONNECT_STATE_DEACTIVATED) { + breakNetworkConnect(uuid, deviceName, ssid); + } +} diff --git a/src/frontend/networkmode/networkmodeconfig.h b/src/frontend/networkmode/networkmodeconfig.h new file mode 100644 index 00000000..8935d976 --- /dev/null +++ b/src/frontend/networkmode/networkmodeconfig.h @@ -0,0 +1,63 @@ +/* -*- 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. + * + */ +#ifndef NETWORKMODECONFIG_H +#define NETWORKMODECONFIG_H + +#include +#include +#include +#include +#include + +enum NetworkModeType { + DBUS_INVAILD = -2, + NO_CONFIG = -1, + KSC_FIREWALL_PUBLIC = 0, + KSC_FIREWALL_PRIVATE +}; + +class NetworkModeConfig : public QObject +{ + Q_OBJECT +public: + NetworkModeConfig(QObject *parent = 0); + ~NetworkModeConfig() = default; + void initWiredNetworkMode(); + void initWirelessNetworkMode(); + +private: + KyNetworkManager* m_manager; + QThread* m_thread; + QDBusInterface *m_firewallDbusIface = nullptr; + + //安全中心-获取网络模式配置 + int getNetworkModeConfig(QString uuid); + //安全中心-设置网络模式配置 + void setNetworkModeConfig(QString uuid, QString cardName, QString ssid, int mode); + //安全中心-解除连接(用于防火墙处从正在使用的网络中删除) + int breakNetworkConnect(QString uuid, QString cardName, QString ssid); + + void setFirstConnectNetworkMode(QString uuid, QString deviceName, QString ssid); + +private Q_SLOTS: + void onActiveConnectionChanged(QString deviceName, QString ssid, QString uuid, KyConnectState status); +}; + +#endif // NETWORKMODECONFIG_H