From 77c05dd5f94bcb521cfbf094b593597c1fdea4f6 Mon Sep 17 00:00:00 2001 From: chenxuechao Date: Fri, 18 Nov 2022 11:31:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EVPN=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E6=94=B9=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/vpndbusadaptor.cpp | 72 +++++++++++++++++++ src/backend/vpndbusadaptor.h | 71 +++++++++++++++++++ src/frontend/vpnobject.cpp | 122 +++++++++++++++++++++++++++++++++ src/frontend/vpnobject.h | 68 ++++++++++++++++++ 4 files changed, 333 insertions(+) create mode 100644 src/backend/vpndbusadaptor.cpp create mode 100644 src/backend/vpndbusadaptor.h create mode 100644 src/frontend/vpnobject.cpp create mode 100644 src/frontend/vpnobject.h diff --git a/src/backend/vpndbusadaptor.cpp b/src/backend/vpndbusadaptor.cpp new file mode 100644 index 00000000..4d3aa93d --- /dev/null +++ b/src/backend/vpndbusadaptor.cpp @@ -0,0 +1,72 @@ +/* + * This file was generated by qdbusxml2cpp version 0.8 + * Command line was: qdbusxml2cpp com.kylin.weather.xml -a VpnDbusAdaptor -c VpnDbusAdaptor -l MainWindow + * + * qdbusxml2cpp is Copyright (C) 2015 The Qt Company Ltd. + * + * This is an auto-generated file. + * Do not edit! All changes made to it will be lost. + */ + +#include "vpndbusadaptor.h" +#include +#include +#include +#include +#include +#include +#include + +/* + * Implementation of adaptor class VpnDbusAdaptor + */ + +VpnDbusAdaptor::VpnDbusAdaptor(vpnObject *parent) + : QDBusAbstractAdaptor(parent) +{ + // constructor + qDBusRegisterMetaType >(); + qDBusRegisterMetaType >(); + qDBusRegisterMetaType >(); + //setAutoRelaySignals(true)后会自动转发mainwindow发出的同名信号,因此不必再额外写一个转发 + setAutoRelaySignals(true); +} + +VpnDbusAdaptor::~VpnDbusAdaptor() +{ + // destructor +} + +//虚拟连接列表 +QVector VpnDbusAdaptor::getVirtualList() +{ + QVector vector; + parent()->getVirtualList(vector); + return vector; +} + +//删除 +void VpnDbusAdaptor::deleteVpn(QString uuid) +{ + qDebug() << "delete vpn" << uuid; + parent()->deleteVpn(uuid); +} + +//连接 根据网卡类型 参数1 0:lan 1:wlan 参数3 为ssid/uuid +void VpnDbusAdaptor::activateVpn(const QString& connUuid) +{ + qDebug() << "activate vpn" << connUuid; + parent()->activateVpn(connUuid); +} + +//断开连接 根据网卡类型 参数1 0:lan 1:wlan 参数3 为ssid/uuid +void VpnDbusAdaptor::deactivateVpn(const QString& connUuid) +{ + qDebug() << "deactivate vpn" << connUuid; + parent()->deactivateVpn(connUuid); +} + +void VpnDbusAdaptor::showKylinVpn() +{ + parent()->onShowMainWindow(); +} diff --git a/src/backend/vpndbusadaptor.h b/src/backend/vpndbusadaptor.h new file mode 100644 index 00000000..06c09f30 --- /dev/null +++ b/src/backend/vpndbusadaptor.h @@ -0,0 +1,71 @@ +/* + * This file was generated by qdbusxml2cpp version 0.8 + * Command line was: qdbusxml2cpp com.kylin.weather.xml -a dbusadaptor -c DbusAdaptor -l MainWindow + * + * qdbusxml2cpp is Copyright (C) 2015 The Qt Company Ltd. + * + * This is an auto-generated file. + * This file may have been hand-edited. Look for HAND-EDIT comments + * before re-generating it. + */ + +#ifndef VPNDBUSADAPTOR_H +#define VPNDBUSADAPTOR_H + +#include +#include +#include + +#include "singlepage.h" +#include "../dbus-interface/kylinnetworkdeviceresource.h" +QT_BEGIN_NAMESPACE +class QByteArray; +//template class QList; +template class QMap; +class QString; +class QStringList; +class QVariant; +template class QVector; +QT_END_NAMESPACE + +/* + * Adaptor class for interface com.kylin.weather + */ + +#include "vpnobject.h" + +class VpnDbusAdaptor: public QDBusAbstractAdaptor +{ + Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "com.kylin.vpnTool") +public: + VpnDbusAdaptor(vpnObject *parent); + virtual ~VpnDbusAdaptor(); + + inline vpnObject *parent() const + { return static_cast(QObject::parent()); } + +public: // PROPERTIES +public Q_SLOTS: // METHODS + //虚拟连接列表 + QVector getVirtualList(); + //刪除 根据网络名称 参数1 0:lan 1:wlan 参数2 为ssid/uuid + Q_NOREPLY void deleteVpn(QString uuid); + //连接 根据网卡类型 参数1 0:lan 1:wlan 参数3 为ssid/uuid + Q_NOREPLY void activateVpn(const QString& connUuid); + //断开连接 根据网卡类型 参数1 0:lan 1:wlan 参数3 为ssid/uuid + Q_NOREPLY void deactivateVpn(const QString& connUuid); + //just show + void showKylinVpn(); + +Q_SIGNALS: // SIGNALS + void vpnAdd(QStringList info); + void vpnRemove(QString dbusPath); + void vpnUpdate(QStringList info); + void vpnActiveConnectionStateChanged(QString uuid, int status); + + void activateFailed(QString errorMessage); + void deactivateFailed(QString errorMessage); +}; + +#endif diff --git a/src/frontend/vpnobject.cpp b/src/frontend/vpnobject.cpp new file mode 100644 index 00000000..62d33bd6 --- /dev/null +++ b/src/frontend/vpnobject.cpp @@ -0,0 +1,122 @@ +#include "vpnobject.h" +#include + +#include "ukuistylehelper/ukuistylehelper.h" + +vpnObject::vpnObject(QObject *parent) : QObject(parent) +{ + initUI(); + initTrayIcon(); + initDbusConnnect(); +} + +vpnObject::~vpnObject() +{ + if (m_vpnGsettings != nullptr) { + delete m_vpnGsettings; + m_vpnGsettings = nullptr; + } +} + +void vpnObject::initUI() +{ + m_vpnPage = new VpnPage(nullptr); + m_vpnPage->update(); + +} + +void vpnObject::initTrayIcon() +{ + m_vpnTrayIcon = new QSystemTrayIcon(this); + m_vpnTrayIcon->setToolTip(QString(tr("vpn tool"))); + m_vpnTrayIcon->setIcon(QIcon::fromTheme("ukui-vpn-symbolic")); + m_vpnTrayIcon->setVisible(true); + initVpnIconVisible(); + connect(m_vpnTrayIcon, &QSystemTrayIcon::activated, this, &vpnObject::onTrayIconActivated); +} + +void vpnObject::initVpnIconVisible() +{ + if (QGSettings::isSchemaInstalled(QByteArray(GSETTINGS_VPNICON_VISIBLE))) { + m_vpnGsettings = new QGSettings(QByteArray(GSETTINGS_VPNICON_VISIBLE)); + if (m_vpnGsettings->keys().contains(QString(VISIBLE))) { + m_vpnTrayIcon->setVisible(m_vpnGsettings->get(VISIBLE).toBool()); + connect(m_vpnGsettings, &QGSettings::changed, this, [=]() { + m_vpnTrayIcon->setVisible(m_vpnGsettings->get(VISIBLE).toBool()); + }); + } + } +} + +/** + * @brief vpnObject::onTrayIconActivated 点击托盘图标的槽函数 + */ +void vpnObject::onTrayIconActivated(QSystemTrayIcon::ActivationReason reason) +{ + switch(reason) { + case QSystemTrayIcon::Trigger: + if(m_vpnPage->isActiveWindow()) { + m_vpnPage->hide(); + } else { + onShowMainWindow(); + } + break; + + default: + break; + } +} + +void vpnObject::getVirtualList(QVector &vector) +{ + vector.clear(); + if (nullptr != m_vpnPage) { + m_vpnPage->getVirtualList(vector); + } +} + +//Vpn连接删除 +void vpnObject::deleteVpn(const QString &connUuid) +{ + m_vpnPage->deleteVpn(connUuid); +} + +//Vpn连接断开 +void vpnObject::activateVpn(const QString& connUuid) +{ + m_vpnPage->activateVpn(connUuid); +} +void vpnObject::deactivateVpn(const QString& connUuid) +{ + m_vpnPage->deactivateVpn(connUuid); +} + +void vpnObject::onShowMainWindow() +{ + kdk::UkuiStyleHelper::self()->removeHeader(m_vpnPage); + m_vpnPage->showUI();; +} + +void vpnObject::initDbusConnnect() +{ + connect(m_vpnPage, &VpnPage::activateFailed, this, &vpnObject::activateFailed); + connect(m_vpnPage, &VpnPage::deactivateFailed, this, &vpnObject::deactivateFailed); + + connect(m_vpnPage, &VpnPage::vpnAdd, this, &vpnObject::vpnAdd); + connect(m_vpnPage, &VpnPage::vpnRemove, this, &vpnObject::vpnRemove); + connect(m_vpnPage, &VpnPage::vpnUpdate, this, &vpnObject::vpnUpdate); + connect(m_vpnPage, &VpnPage::vpnActiveConnectionStateChanged, this, &vpnObject::vpnActiveConnectionStateChanged); + //模式切换 + QDBusConnection::sessionBus().connect(QString("com.kylin.statusmanager.interface"), + QString("/"), + QString("com.kylin.statusmanager.interface"), + QString("mode_change_signal"), this, SLOT(onTabletModeChanged(bool))); +} + +void vpnObject::onTabletModeChanged(bool mode) +{ + qDebug() << "TabletMode change" << mode; + Q_UNUSED(mode) + //模式切换时,隐藏主界面 + m_vpnPage->hide(); +} diff --git a/src/frontend/vpnobject.h b/src/frontend/vpnobject.h new file mode 100644 index 00000000..5ab8cfae --- /dev/null +++ b/src/frontend/vpnobject.h @@ -0,0 +1,68 @@ +#ifndef VPNOBJECT_H +#define VPNOBJECT_H + +#include +#include +#include +#include +#include + +#include "vpnpage.h" +class VpnPage; + +#define VISIBLE "visible" +#define GSETTINGS_VPNICON_VISIBLE "org.ukui.kylin-nm.vpnicon" +//const QByteArray GSETTINGS_VPNICON_VISIBLE = "org.ukui.kylin-nm.vpnicon"; + + +class vpnObject : public QObject +{ + Q_OBJECT +public: + explicit vpnObject(QObject *parent = nullptr); + ~vpnObject(); + + void getVirtualList(QVector &vector); + //Vpn连接删除 + void deleteVpn(const QString &connUuid); + //有线连接断开 + void activateVpn(const QString& connUuid); + void deactivateVpn(const QString& connUuid); + +private: + void initUI(); + void initTrayIcon(); + void initVpnIconVisible(); + void initDbusConnnect(); + + +private: + VpnPage *m_vpnPage = nullptr; + QSystemTrayIcon * m_vpnTrayIcon = nullptr; + QGSettings * m_vpnGsettings; //VPN配置文件 + double tran =1; + QGSettings *StyleSettings = nullptr; + QWidget * vpnWidget = nullptr; + + QDBusInterface * m_positionInterface = nullptr; + bool m_isShowInCenter = false; + +public Q_SLOTS: + void onShowMainWindow(); + +private Q_SLOTS: + void onTrayIconActivated(QSystemTrayIcon::ActivationReason reason); + void onTabletModeChanged(bool mode); + + +Q_SIGNALS: + void vpnAdd(QStringList info); + void vpnRemove(QString dbusPath); + void vpnUpdate(QStringList info); + void vpnActiveConnectionStateChanged(QString uuid, int status); + void activateFailed(QString errorMessage); + void deactivateFailed(QString errorMessage); + void mainWindowVisibleChanged(const bool &visible); +}; + +#endif // VPNOBJECT_H