修改VPN DBus接口

This commit is contained in:
zhangyuanyuan1 2023-07-19 17:38:27 +08:00
parent e823aae0e2
commit a65e96134d
6 changed files with 151 additions and 29 deletions

View File

@ -35,7 +35,7 @@ ItemFrame::ItemFrame(QWidget *parent)
lanItemLayout->setContentsMargins(LAYOUT_MARGINS); lanItemLayout->setContentsMargins(LAYOUT_MARGINS);
lanItemLayout->setSpacing(1); lanItemLayout->setSpacing(1);
addWlanWidget = new AddNetBtn(false, this); addWlanWidget = new AddNetBtn(false, this);
addWlanWidget->setTextLabel(tr("Add Vpn")); addWlanWidget->setTextLabel(tr("Add VPN"));
deviceLanLayout->setSpacing(1); deviceLanLayout->setSpacing(1);
setLayout(deviceLanLayout); setLayout(deviceLanLayout);

View File

@ -65,7 +65,7 @@ Vpn::Vpn() : mFirstLoad(true)
translator->load("/usr/share/kylin-nm/vpn/" + QLocale::system().name()); translator->load("/usr/share/kylin-nm/vpn/" + QLocale::system().name());
QApplication::installTranslator(translator); QApplication::installTranslator(translator);
pluginName = tr("Vpn"); pluginName = tr("VPN");
pluginType = NETWORK; pluginType = NETWORK;
} }
@ -96,9 +96,9 @@ QWidget *Vpn::pluginUi(){
ui->setupUi(pluginWidget); ui->setupUi(pluginWidget);
qDBusRegisterMetaType<QVector<QStringList>>(); qDBusRegisterMetaType<QVector<QStringList>>();
m_interface = new QDBusInterface("com.kylin.network", m_interface = new QDBusInterface("com.kylin.kylinvpn",
"/com/kylin/vpnTool", "/com/kylin/kylinvpn",
"com.kylin.vpnTool", "com.kylin.kylinvpn",
QDBusConnection::sessionBus()); QDBusConnection::sessionBus());
if(!m_interface->isValid()) { if(!m_interface->isValid()) {
qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message()); qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message());
@ -328,16 +328,12 @@ void Vpn::addOneVirtualItem(QStringList infoList)
} }
connect(item->infoLabel, &InfoButton::clicked, this, [=]{ connect(item->infoLabel, &InfoButton::clicked, this, [=]{
QDBusInterface appManagerDbusInterface(KYLIN_APP_MANAGER_NAME, // open vpn detail page
KYLIN_APP_MANAGER_PATH, if (!m_interface->isValid()) {
KYLIN_APP_MANAGER_INTERFACE, return;
QDBusConnection::sessionBus());
if (!appManagerDbusInterface.isValid()) {
qWarning()<<"appManagerDbusInterface init error";
} else {
QDBusReply<bool> reply = appManagerDbusInterface.call("LaunchApp", "nm-connection-editor.desktop");
} }
m_interface->call(QStringLiteral("showDetailPage"), item->uuid);
qDebug() << "[Vpn]call showDetailPage respond" << __LINE__;
}); });
item->isAcitve = (status == 2); item->isAcitve = (status == 2);

View File

@ -1,41 +1,154 @@
#include "kylinvpndbus.h" #include "kylinvpndbus.h"
#include <QDBusReply>
#include <QProcess>
#define LOG_FLAG "[KylinVpnDbus]"
KylinVpnDbus::KylinVpnDbus(QObject *parent) : QObject(parent) KylinVpnDbus::KylinVpnDbus(QObject *parent) : QObject(parent)
{ {
qDBusRegisterMetaType<QVector<QStringList> >(); qDBusRegisterMetaType<QVector<QStringList> >();
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<QStringList> &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<QStringList> KylinVpnDbus::getVirtualList()
{
QVector<QStringList> vector;
QList<KyVirtualConnectItem *> 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) void KylinVpnDbus::deleteVpn(const QString &connUuid)
{ {
qDebug()<< LOG_FLAG << "delete vpn connect uuid " << connUuid;
m_manager->onDeleteConnect(connUuid);
} }
void KylinVpnDbus::activateVpn(const QString &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) void KylinVpnDbus::deactivateVpn(const QString &connUuid)
{ {
qDebug() << LOG_FLAG << "deactivate vpn connect uuid " << connUuid;
m_manager->onDeactivateConnection(connUuid);
} }
void KylinVpnDbus::showKylinVpn() void KylinVpnDbus::showKylinVpn()
{ {
parent()->onShowMainWindow();
} }
void KylinVpnDbus::showVpnAddWidget() void KylinVpnDbus::showVpnAddWidget()
{ {
#ifdef VPNDETAIL
//todo 打开添加VPN界面(自研)
#else
runExternalApp();
return;
#endif
} }
void KylinVpnDbus::showDetailPage(const QString &connUuid) 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<KyVirtualConnectItem *> 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<bool> 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);
}
} }

View File

@ -2,23 +2,35 @@
#define KYLINVPNDBUS_H #define KYLINVPNDBUS_H
#include <QObject> #include <QObject>
#include <QThread>
#include <QDBusMetaType> #include <QDBusMetaType>
#include <QMetaObject> #include <QMetaObject>
#include <QMetaType> #include <QMetaType>
#include <QDBusConnection> #include <QDBusConnection>
#include <kylin-nm/kylinnetworkmanager.h> #include <kylin-nm/kylinnetworkmanager.h>
#include "vpnobject.h"
class KylinVpnDbus : public QObject class KylinVpnDbus : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "com.kylin.kylinvpntest") Q_CLASSINFO("D-Bus Interface", "com.kylin.kylinvpn")
public: public:
explicit KylinVpnDbus(QObject *parent = nullptr); explicit KylinVpnDbus(QObject *parent = nullptr);
~KylinVpnDbus() = default; ~KylinVpnDbus() = default;
inline vpnObject *parent() const
{ return static_cast<vpnObject *>(QObject::parent()); }
private:
KyNetworkManager* m_manager;
QThread* m_thread;
void initDbusConnect();
bool launchApp(QString desktopFile);
void runExternalApp();
public Q_SLOTS: public Q_SLOTS:
Q_NOREPLY void getVirtualList(QVector<QStringList> &vector); QVector<QStringList> getVirtualList();
//删除 //删除
Q_NOREPLY void deleteVpn(const QString &connUuid); Q_NOREPLY void deleteVpn(const QString &connUuid);
//连接 //连接
@ -31,14 +43,15 @@ public Q_SLOTS:
Q_NOREPLY void showVpnAddWidget(); Q_NOREPLY void showVpnAddWidget();
Q_NOREPLY void showDetailPage(const QString& connUuid); 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: Q_SIGNALS:
void vpnAdd(QStringList info); void vpnAdd(QStringList info);
void vpnRemove(QString dbusPath); void vpnRemove(QString dbusPath);
void vpnUpdate(QStringList info); void vpnUpdate(QStringList info);
void vpnActiveConnectionStateChanged(QString uuid, int status); void vpnActiveConnectionStateChanged(QString uuid, int status);
void activateFailed(QString errorMessage);
void deactivateFailed(QString errorMessage);
}; };
#endif // KYLINVPNDBUS_H #endif // KYLINVPNDBUS_H

View File

@ -127,8 +127,8 @@ int main(int argc, char *argv[])
auto connection = QDBusConnection::sessionBus(); auto connection = QDBusConnection::sessionBus();
KylinVpnDbus dbusObject(&vpnobject); KylinVpnDbus dbusObject(&vpnobject);
if (!connection.registerService("com.kylin.kylinvpntest") if (!connection.registerService("com.kylin.kylinvpn")
|| !connection.registerObject("/com/kylin/kylinvpntest", &dbusObject, || !connection.registerObject("/com/kylin/kylinvpn", &dbusObject,
QDBusConnection::ExportAllSlots | QDBusConnection :: ExportAllSignals)) { QDBusConnection::ExportAllSlots | QDBusConnection :: ExportAllSignals)) {
qCritical() << "QDbus register service failed reason:" << connection.lastError(); qCritical() << "QDbus register service failed reason:" << connection.lastError();
} }

View File

@ -15,7 +15,7 @@ CONFIG += c++14 qt warn_on link_pkgconfig no_keywords
#CONFIG += release #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 +=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 INCLUDEPATH += /usr/include/KF5/NetworkManagerQt