From a65e96134dc8724db2a90772b75f2aeeaa1b9289 Mon Sep 17 00:00:00 2001 From: zhangyuanyuan1 Date: Wed, 19 Jul 2023 17:38:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9VPN=20DBus=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/vpn/itemframe.cpp | 2 +- plugins/vpn/vpn.cpp | 22 ++-- src-vpn/kylin-vpn-dbus/kylinvpndbus.cpp | 127 ++++++++++++++++++++++-- src-vpn/kylin-vpn-dbus/kylinvpndbus.h | 23 ++++- src-vpn/main.cpp | 4 +- src-vpn/src-vpn.pro | 2 +- 6 files changed, 151 insertions(+), 29 deletions(-) diff --git a/plugins/vpn/itemframe.cpp b/plugins/vpn/itemframe.cpp index 816828f7..be879f50 100644 --- a/plugins/vpn/itemframe.cpp +++ b/plugins/vpn/itemframe.cpp @@ -35,7 +35,7 @@ ItemFrame::ItemFrame(QWidget *parent) lanItemLayout->setContentsMargins(LAYOUT_MARGINS); lanItemLayout->setSpacing(1); addWlanWidget = new AddNetBtn(false, this); - addWlanWidget->setTextLabel(tr("Add Vpn")); + addWlanWidget->setTextLabel(tr("Add VPN")); deviceLanLayout->setSpacing(1); setLayout(deviceLanLayout); diff --git a/plugins/vpn/vpn.cpp b/plugins/vpn/vpn.cpp index 143e06da..6fba5756 100644 --- a/plugins/vpn/vpn.cpp +++ b/plugins/vpn/vpn.cpp @@ -65,7 +65,7 @@ Vpn::Vpn() : mFirstLoad(true) translator->load("/usr/share/kylin-nm/vpn/" + QLocale::system().name()); QApplication::installTranslator(translator); - pluginName = tr("Vpn"); + pluginName = tr("VPN"); pluginType = NETWORK; } @@ -96,9 +96,9 @@ QWidget *Vpn::pluginUi(){ ui->setupUi(pluginWidget); qDBusRegisterMetaType>(); - m_interface = new QDBusInterface("com.kylin.network", - "/com/kylin/vpnTool", - "com.kylin.vpnTool", + m_interface = new QDBusInterface("com.kylin.kylinvpn", + "/com/kylin/kylinvpn", + "com.kylin.kylinvpn", QDBusConnection::sessionBus()); if(!m_interface->isValid()) { qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message()); @@ -328,16 +328,12 @@ void Vpn::addOneVirtualItem(QStringList infoList) } connect(item->infoLabel, &InfoButton::clicked, this, [=]{ - QDBusInterface appManagerDbusInterface(KYLIN_APP_MANAGER_NAME, - KYLIN_APP_MANAGER_PATH, - KYLIN_APP_MANAGER_INTERFACE, - QDBusConnection::sessionBus()); - - if (!appManagerDbusInterface.isValid()) { - qWarning()<<"appManagerDbusInterface init error"; - } else { - QDBusReply reply = appManagerDbusInterface.call("LaunchApp", "nm-connection-editor.desktop"); + // open vpn detail page + if (!m_interface->isValid()) { + return; } + m_interface->call(QStringLiteral("showDetailPage"), item->uuid); + qDebug() << "[Vpn]call showDetailPage respond" << __LINE__; }); item->isAcitve = (status == 2); diff --git a/src-vpn/kylin-vpn-dbus/kylinvpndbus.cpp b/src-vpn/kylin-vpn-dbus/kylinvpndbus.cpp index 1f92b3c5..03e0ce96 100644 --- a/src-vpn/kylin-vpn-dbus/kylinvpndbus.cpp +++ b/src-vpn/kylin-vpn-dbus/kylinvpndbus.cpp @@ -1,41 +1,154 @@ #include "kylinvpndbus.h" +#include +#include + +#define LOG_FLAG "[KylinVpnDbus]" KylinVpnDbus::KylinVpnDbus(QObject *parent) : QObject(parent) { qDBusRegisterMetaType >(); + + 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); + } + + initDbusConnect(); } -void KylinVpnDbus::getVirtualList(QVector &vector) +void KylinVpnDbus::initDbusConnect() { + connect(m_manager, &KyNetworkManager::vpnConnectionAdd, this, &KylinVpnDbus::sendVpnAddSignal); + connect(m_manager, &KyNetworkManager::vpnConnectionUpdate, this, &KylinVpnDbus::sendVpnUpdateSignal); + connect(m_manager, &KyNetworkManager::vpnConnectionRemove, this, &KylinVpnDbus::vpnRemove); + connect(m_manager, &KyNetworkManager::vpnStateChange, this, &KylinVpnDbus::vpnActiveConnectionStateChanged); +} +QVector KylinVpnDbus::getVirtualList() +{ + QVector vector; + QList netConnectList; + + m_manager->getVpnAndVirtualConnections(netConnectList); + if (!netConnectList.isEmpty()) { + for (const auto vpnConnectItem : netConnectList) { + vector.append(QStringList() << vpnConnectItem->m_connectName + << vpnConnectItem->m_connectUuid + << vpnConnectItem->m_connectPath + << QString::number(vpnConnectItem->m_connectState)); + } + } + + return vector; } void KylinVpnDbus::deleteVpn(const QString &connUuid) { - + qDebug()<< LOG_FLAG << "delete vpn connect uuid " << connUuid; + m_manager->onDeleteConnect(connUuid); } void KylinVpnDbus::activateVpn(const QString &connUuid) { - + qDebug()<< LOG_FLAG << "activate vpn connect uuid " << connUuid; + m_manager->activateVpnConnection(connUuid); } void KylinVpnDbus::deactivateVpn(const QString &connUuid) { - + qDebug() << LOG_FLAG << "deactivate vpn connect uuid " << connUuid; + m_manager->onDeactivateConnection(connUuid); } void KylinVpnDbus::showKylinVpn() { - + parent()->onShowMainWindow(); } void KylinVpnDbus::showVpnAddWidget() { - +#ifdef VPNDETAIL + //todo 打开添加VPN界面(自研) +#else + runExternalApp(); + return; +#endif } void KylinVpnDbus::showDetailPage(const QString &connUuid) { - + qDebug() << LOG_FLAG << "show vpn detail page uuid " << connUuid; +#ifdef VPNDETAIL + //todo 打开VPN详情界面(自研) +#else + runExternalApp(); + return; +#endif +} + +void KylinVpnDbus::sendVpnAddSignal(QString connectUuid, QString connectName, QString connectPath) +{ + QStringList info; + QList vpnConnectList; + m_manager->getVpnAndVirtualConnections(vpnConnectList); + KyConnectState state = CONNECT_STATE_UNKONW; + if (!vpnConnectList.isEmpty()) { + for (const auto vpnConnectItem : vpnConnectList) { + if (connectUuid == vpnConnectItem->m_connectUuid) { + state = vpnConnectItem->m_connectState; + break; + } + } + } + + info << connectName + << connectUuid + << connectPath + << QString::number(state); + + Q_EMIT vpnAdd(info); +} + +void KylinVpnDbus::sendVpnUpdateSignal(QString connectUuid, QString connectName, QString connectPath) +{ + QStringList info; + info << connectName + << connectUuid + << connectPath; + Q_EMIT vpnUpdate(info); +} + + +#define KYLIN_APP_MANAGER_NAME "com.kylin.AppManager" +#define KYLIN_APP_MANAGER_PATH "/com/kylin/AppManager" +#define KYLIN_APP_MANAGER_INTERFACE "com.kylin.AppManager" +bool KylinVpnDbus::launchApp(QString desktopFile) +{ + QDBusInterface appManagerDbusInterface(KYLIN_APP_MANAGER_NAME, + KYLIN_APP_MANAGER_PATH, + KYLIN_APP_MANAGER_INTERFACE, + QDBusConnection::sessionBus()); + + if (!appManagerDbusInterface.isValid()) { + qWarning() << LOG_FLAG << "appManagerDbusInterface init error"; + return false; + } else { + QDBusReply reply = appManagerDbusInterface.call("LaunchApp", desktopFile); + return reply; + } +} + +void KylinVpnDbus::runExternalApp() +{ + if (!launchApp("nm-connection-editor.desktop")){ + QString cmd = "nm-connection-editor"; + QProcess process(this); + process.startDetached(cmd); + } } diff --git a/src-vpn/kylin-vpn-dbus/kylinvpndbus.h b/src-vpn/kylin-vpn-dbus/kylinvpndbus.h index f4880a43..f7bc3309 100644 --- a/src-vpn/kylin-vpn-dbus/kylinvpndbus.h +++ b/src-vpn/kylin-vpn-dbus/kylinvpndbus.h @@ -2,23 +2,35 @@ #define KYLINVPNDBUS_H #include +#include #include #include #include #include #include +#include "vpnobject.h" class KylinVpnDbus : public QObject { Q_OBJECT - Q_CLASSINFO("D-Bus Interface", "com.kylin.kylinvpntest") + Q_CLASSINFO("D-Bus Interface", "com.kylin.kylinvpn") public: explicit KylinVpnDbus(QObject *parent = nullptr); ~KylinVpnDbus() = default; + inline vpnObject *parent() const + { return static_cast(QObject::parent()); } + +private: + KyNetworkManager* m_manager; + QThread* m_thread; + void initDbusConnect(); + + bool launchApp(QString desktopFile); + void runExternalApp(); public Q_SLOTS: - Q_NOREPLY void getVirtualList(QVector &vector); + QVector getVirtualList(); //删除 Q_NOREPLY void deleteVpn(const QString &connUuid); //连接 @@ -31,14 +43,15 @@ public Q_SLOTS: Q_NOREPLY void showVpnAddWidget(); Q_NOREPLY void showDetailPage(const QString& connUuid); +private Q_SLOTS: + void sendVpnAddSignal(QString connectUuid, QString connectName, QString connectPath); + void sendVpnUpdateSignal(QString connectUuid, QString connectName, QString connectPath); + 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); }; #endif // KYLINVPNDBUS_H diff --git a/src-vpn/main.cpp b/src-vpn/main.cpp index 9eee5492..5ce8caec 100644 --- a/src-vpn/main.cpp +++ b/src-vpn/main.cpp @@ -127,8 +127,8 @@ int main(int argc, char *argv[]) auto connection = QDBusConnection::sessionBus(); KylinVpnDbus dbusObject(&vpnobject); - if (!connection.registerService("com.kylin.kylinvpntest") - || !connection.registerObject("/com/kylin/kylinvpntest", &dbusObject, + if (!connection.registerService("com.kylin.kylinvpn") + || !connection.registerObject("/com/kylin/kylinvpn", &dbusObject, QDBusConnection::ExportAllSlots | QDBusConnection :: ExportAllSignals)) { qCritical() << "QDbus register service failed reason:" << connection.lastError(); } diff --git a/src-vpn/src-vpn.pro b/src-vpn/src-vpn.pro index f635152d..9da03e7f 100644 --- a/src-vpn/src-vpn.pro +++ b/src-vpn/src-vpn.pro @@ -15,7 +15,7 @@ CONFIG += c++14 qt warn_on link_pkgconfig no_keywords #CONFIG += release PKGCONFIG +=gio-2.0 glib-2.0 gio-unix-2.0 libnm libnma libsecret-1 gtk+-3.0 gsettings-qt libcap kysdk-qtwidgets kysdk-waylandhelper -PKGCONFIG +=kysdk-sysinfo +PKGCONFIG +=kysdk-sysinfo kylin-nm-base INCLUDEPATH += /usr/include/KF5/NetworkManagerQt