kylin-nm/plugins/wlanconnect/wlanconnect.cpp

1105 lines
39 KiB
C++
Raw Normal View History

2022-06-02 16:27:45 +08:00
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* Copyright (C) 2019 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 "wlanconnect.h"
#include "ui_wlanconnect.h"
#include <QGSettings>
#include <QProcess>
#include <QTimer>
#include <QtDBus>
#include <QDir>
#include <QDebug>
#include <QtAlgorithms>
#define WIRELESS_TYPE 1
#define SCANTIMER 20 * 1000
#define UPDATETIMER 5 * 1000
#define SPACING 8
2022-06-17 18:48:59 +08:00
#define EXCELLENT_SIGNAL 80
#define GOOD_SIGNAL 55
#define OK_SIGNAL 30
#define LOW_SIGNAL 5
#define NONE_SIGNAL 0
#define SIGNAL_EXCELLENT 1
#define SIGNAL_GOOD 2
#define SIGNAL_OK 3
#define SIGNAL_LOW 4
#define SIGNAL_NONE 5
#define ICON_SIZE 16,16
2022-06-02 16:27:45 +08:00
const QString WIRELESS_SWITCH = "wirelessswitch";
const QByteArray GSETTINGS_SCHEMA = "org.ukui.kylin-nm.switch";
const QString KWifiSymbolic = "network-wireless-signal-excellent";
const QString KWifiLockSymbolic = "network-wireless-secure-signal-excellent";
const QString KWifiGood = "network-wireless-signal-good";
const QString KWifiLockGood = "network-wireless-secure-signal-good";
const QString KWifiOK = "network-wireless-signal-ok";
const QString KWifiLockOK = "network-wireless-secure-signal-ok";
const QString KWifiLow = "network-wireless-signal-low";
const QString KWifiLockLow = "network-wireless-secure-signal-low";
const QString KWifiNone = "network-wireless-signal-none";
const QString KWifiLockNone = "network-wireless-secure-signal-none";
2022-06-17 18:48:59 +08:00
const QString KWifi6Symbolic = "ukui-wifi6-full-symbolic";
const QString KWifi6PlusSymbolic = "ukui-wifi6+-full-symbolic";
const QString KWifi6LockSymbolic = "ukui-wifi6-full-pwd-symbolic";
const QString KWifi6PlusLockSymbolic= "ukui-wifi6+-full-pwd-symbolic";
const QString KWifi6Good = "ukui-wifi6-high-symbolic";
const QString KWifi6PlusGood = "ukui-wifi6+-high-symbolic";
const QString KWifi6LockGood = "ukui-wifi6-high-pwd-symbolic";
const QString KWifi6PlusLockGood = "ukui-wifi6+-high-pwd-symbolic";
const QString KWifi6OK = "ukui-wifi6-medium-symbolic";
const QString KWifi6PlusOK = "ukui-wifi6-high+-medium-symbolic";
const QString KWifi6LockOK = "ukui-wifi6-medium-pwd-symbolic";
const QString KWifi6PlusLockOK = "ukui-wifi6+-medium-pwd-symbolic";
const QString KWifi6Low = "ukui-wifi6-low-symbolic";
const QString KWifi6PlusLow = "ukui-wifi6+-low-symbolic";
const QString KWifi6LockLow = "ukui-wifi6-low-pwd-symbolic";
const QString KWifi6PlusLockLow = "ukui-wifi6+-low-pwd-symbolic";
const QString KWifi6None = "ukui-wifi6-none-symbolic";
const QString KWifi6PlusNone = "ukui-wifi6+-none-symbolic";
const QString KWifi6LockNone = "ukui-wifi6-none-pwd-symbolic";
const QString KWifi6PlusLockNone = "ukui-wifi6+-none-pwd-symbolic";
2022-06-02 16:27:45 +08:00
const QString KLanSymbolic = ":/img/plugins/netconnect/eth.svg";
const QString NoNetSymbolic = ":/img/plugins/netconnect/nonet.svg";
const QString KApSymbolic = "network-wireless-hotspot-symbolic";
const QString IsApConnection = "1";
#define ACTIVATING 1
#define ACTIVATED 2
#define DEACTIVATING 3
#define DEACTIVATED 4
#define NO_MARGINS 0,0,0,0
#define MAIN_LAYOUT_MARGINS 0,0,0,8
bool intThan(int sign1, int sign2)
{
return sign1 < sign2;
}
void WlanConnect::showDesktopNotify(const QString &message)
{
QDBusInterface iface("org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
"org.freedesktop.Notifications",
QDBusConnection::sessionBus());
QList<QVariant> args;
args<<(tr("ukui control center"))
<<((unsigned int) 0)
<<QString("gnome-dev-ethernet")
<<tr("ukui control center desktop message") //显示的是什么类型的信息
<<message //显示的具体信息
<<QStringList()
<<QVariantMap()
<<(int)-1;
iface.callWithArgumentList(QDBus::AutoDetect,"Notify",args);
}
WlanConnect::WlanConnect() : m_firstLoad(true) {
QTranslator* translator = new QTranslator(this);
translator->load("/usr/share/kylin-nm/wlanconnect/" + QLocale::system().name());
QApplication::installTranslator(translator);
pluginName = tr("WlanConnect");
pluginType = NETWORK;
}
WlanConnect::~WlanConnect()
{
if (!m_firstLoad) {
delete ui;
ui = nullptr;
}
delete m_interface;
2023-03-03 15:49:48 +08:00
delete m_interfaceUi;
2022-06-02 16:27:45 +08:00
delete m_switchGsettings;
}
QString WlanConnect::plugini18nName() {
return pluginName;
}
int WlanConnect::pluginTypes() {
return pluginType;
}
QWidget *WlanConnect::pluginUi() {
if (m_firstLoad) {
m_firstLoad = false;
ui = new Ui::WlanConnect;
pluginWidget = new QWidget;
pluginWidget->setAttribute(Qt::WA_DeleteOnClose);
ui->setupUi(pluginWidget);
qDBusRegisterMetaType<QVector<QStringList>>();
m_interface = new QDBusInterface("com.kylin.network", "/com/kylin/network",
"com.kylin.network",
QDBusConnection::sessionBus());
if(!m_interface->isValid()) {
qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message());
}
2023-03-03 15:49:48 +08:00
m_interfaceUi = new QDBusInterface("com.kylin.network", "/com/kylin/network/interface",
"com.kylin.network.interface",
QDBusConnection::sessionBus());
if(!m_interfaceUi->isValid()) {
qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message());
}
2022-06-02 16:27:45 +08:00
initComponent();
}
return pluginWidget;
}
const QString WlanConnect::name() const {
return QStringLiteral("wlanconnect");
}
bool WlanConnect::isEnable() const
{
return true;
}
bool WlanConnect::isShowOnHomePage() const
{
return true;
}
QIcon WlanConnect::icon() const
{
2022-06-17 18:48:59 +08:00
return QIcon::fromTheme("network-wireless-signal-excellent-symbolic");
2022-06-02 16:27:45 +08:00
}
QString WlanConnect::translationPath() const
{
return "/usr/share/kylin-nm/wlanconnect/%1.ts";
}
void WlanConnect::initSearchText() {
//~ contents_path /wlanconnect/Advanced settings"
ui->detailBtn->setText(tr("Advanced settings"));
ui->titleLabel->setText(tr("WLAN"));
//~ contents_path /wlanconnect/open
ui->openLabel->setText(tr("open"));
}
bool WlanConnect::eventFilter(QObject *w, QEvent *e) {
if (e->type() == QEvent::Enter) {
if (w->findChild<QWidget*>())
w->findChild<QWidget*>()->setStyleSheet("QWidget{background: palette(button);border-radius:4px;}");
} else if (e->type() == QEvent::Leave) {
if (w->findChild<QWidget*>())
w->findChild<QWidget*>()->setStyleSheet("QWidget{background: palette(base);border-radius:4px;}");
}
2022-06-17 18:48:59 +08:00
if (w == m_wifiSwitch) {
if (e->type() == QMouseEvent::MouseButtonRelease) {
if (!m_wifiSwitch->isCheckable()) {
showDesktopNotify(tr("No wireless network card detected"));
} else {
m_interface->call(QStringLiteral("setWirelessSwitchEnable"), !m_wifiSwitch->isChecked());
return true;
}
}
}
2022-06-02 16:27:45 +08:00
return QObject::eventFilter(w,e);
}
void WlanConnect::initComponent() {
2022-06-17 18:48:59 +08:00
m_wifiSwitch = new KSwitchButton(pluginWidget);
2022-06-02 16:27:45 +08:00
ui->openWIifLayout->addWidget(m_wifiSwitch);
ui->detailLayOut_3->setContentsMargins(MAIN_LAYOUT_MARGINS);
ui->verticalLayout_3->setContentsMargins(NO_MARGINS);
ui->availableLayout->setSpacing(SPACING);
2022-06-17 18:48:59 +08:00
m_wifiSwitch->installEventFilter(this);
2022-06-02 16:27:45 +08:00
//开关
if (QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA)) {
m_switchGsettings = new QGSettings(GSETTINGS_SCHEMA);
2022-06-17 18:48:59 +08:00
2022-06-02 16:27:45 +08:00
setSwitchStatus();
2022-06-17 18:48:59 +08:00
2022-06-02 16:27:45 +08:00
connect(m_switchGsettings, &QGSettings::changed, this, [=] (const QString &key) {
if (key == WIRELESS_SWITCH) {
setSwitchStatus();
}
});
} else {
m_wifiSwitch->blockSignals(true);
m_wifiSwitch->setChecked(true);
m_wifiSwitch->blockSignals(false);
qDebug()<<"[netconnect] org.ukui.kylin-nm.switch is not installed!";
}
//获取设备列表
getDeviceList(deviceList);
if (deviceList.isEmpty()) {
qDebug() << "[WlanConnect]no device exist when init, set switch disable";
2022-06-17 18:48:59 +08:00
m_wifiSwitch->setCheckable(false);
2022-06-02 16:27:45 +08:00
m_wifiSwitch->setChecked(false);
}
initNet();
2022-07-18 11:04:06 +08:00
if (!m_wifiSwitch->isChecked() || deviceList.isEmpty() || !m_interface->isValid()) {
2022-06-02 16:27:45 +08:00
hideLayout(ui->availableLayout);
}
// 有线网络断开或连接时刷新可用网络列表
connect(m_interface, SIGNAL(wlanactiveConnectionStateChanged(QString, QString, QString, int)), this, SLOT(onActiveConnectionChanged(QString, QString, QString, int)), Qt::QueuedConnection);
//无线网络新增时添加网络
connect(m_interface, SIGNAL(wlanAdd(QString, QStringList)), this, SLOT(onNetworkAdd(QString, QStringList)), Qt::QueuedConnection);
//删除无线网络
connect(m_interface, SIGNAL(wlanRemove(QString, QString)), this, SLOT(onNetworkRemove(QString, QString)), Qt::QueuedConnection);
//网卡插拔处理
connect(m_interface, SIGNAL(deviceStatusChanged()), this, SLOT(onDeviceStatusChanged()), Qt::QueuedConnection);
//信号更新处理 改为每过固定时间 主动获取
// connect(m_interface, SIGNAL(signalStrengthChange(QString, QString, int)), this, SLOT(updateStrengthList(QString, QString, int)));
//网卡name处理
connect(m_interface, SIGNAL(deviceNameChanged(QString, QString, int)), this, SLOT(onDeviceNameChanged(QString, QString, int)), Qt::QueuedConnection);
2023-03-03 15:49:48 +08:00
connect(m_interface, SIGNAL(updateList()), this, SLOT(updateList()), Qt::QueuedConnection);
2022-06-02 16:27:45 +08:00
//高级设置
connect(ui->detailBtn, &QPushButton::clicked, this, [=](bool checked) {
Q_UNUSED(checked)
runExternalApp();
});
//定时20s扫描
m_scanTimer = new QTimer(this);
m_scanTimer->start(SCANTIMER);
connect(m_scanTimer, &QTimer::timeout, this, &WlanConnect::reScan, Qt::QueuedConnection);
reScan();
// m_updateTimer = new QTimer(this);
// m_updateTimer->start(UPDATETIMER);
}
void WlanConnect::reScan()
{
qDebug() << "time to rescan wifi";
if (m_interface->isValid()) {
qDebug() << "[WlanConnect]call reScan" << __LINE__;
m_interface->call("reScan");
qDebug() << "[WlanConnect]call reScan respond" << __LINE__;
}
}
//更新列表顺序
2023-03-03 15:49:48 +08:00
void WlanConnect::updateList(QMap<QString, QVector<QStringList> > variantList)
2022-06-02 16:27:45 +08:00
{
if (!m_wifiSwitch->isChecked()) {
return;
}
qDebug() << "update list";
2023-03-03 15:49:48 +08:00
// if(m_interface->isValid()) {
// qDebug() << "[WlanConnect]call getWirelessList" << __LINE__;
// QDBusMessage result = m_interface->call(QStringLiteral("getWirelessList"));
// qDebug() << "[WlanConnect]call getWirelessList respond" << __LINE__;
// if(result.type() == QDBusMessage::ErrorMessage)
// {
// qWarning() << "getWirelessList error:" << result.errorMessage();
// return;
// }
// auto dbusArg = result.arguments().at(0).value<QDBusArgument>();
// QMap<QString, QVector<QStringList>> variantList;
// dbusArg >> variantList;
2022-06-02 16:27:45 +08:00
2023-03-03 15:49:48 +08:00
// if (variantList.size() == 0) {
// qDebug() << "[WlanConnect]updateList " << " list empty";
// return;
// }
2022-06-02 16:27:45 +08:00
QMap<QString, QVector<QStringList>>::iterator iter;
for (iter = variantList.begin(); iter != variantList.end(); iter++) {
if (deviceFrameMap.contains(iter.key())) {
QVector<QStringList> wifiList = iter.value();
resortWifiList(deviceFrameMap[iter.key()], wifiList);
deviceFrameMap[iter.key()]->filletStyleChange();
}
}
2023-03-03 15:49:48 +08:00
// }
2022-06-02 16:27:45 +08:00
}
void WlanConnect::resortWifiList(ItemFrame *frame, QVector<QStringList> list)
{
if(nullptr == frame || frame->lanItemLayout->count() <= 0 || list.isEmpty()) {
return;
}
qDebug() << "begin resort" << frame->deviceFrame->deviceLabel->text();
int frameIndex = 0;
int listIndex = 1;
if (list.at(0).size() > 1) {
if (frame->itemMap.contains(list.at(0).at(0))) {
frame->lanItemLayout->removeWidget(frame->itemMap[list.at(0).at(0)]);
frame->lanItemLayout->insertWidget(0, frame->itemMap[list.at(0).at(0)]);
qDebug() << "active resort insert position 0" << list.at(0).at(0);
frame->itemMap[list.at(0).at(0)]->isAcitve = true;
frame->itemMap[list.at(0).at(0)]->uuid = list.at(0).at(3);
frame->uuid = list.at(0).at(3);
frame->itemMap[list.at(0).at(0)]->statusLabel->setText(tr("connected"));
2022-06-17 18:48:59 +08:00
if (list.at(0).size() > 5) {
updateIcon(frame->itemMap[list.at(0).at(0)], list.at(0).at(1), list.at(0).at(2), list.at(0).at(4), list.at(0).at(5).toInt());
}
2022-06-02 16:27:45 +08:00
frameIndex ++;
}
} else {
qDebug() << " no active connection when resort";
if (!frame->uuid.isEmpty()) {
QMap<QString, WlanItem*>::iterator itemIter;
for (itemIter = frame->itemMap.begin(); itemIter != frame->itemMap.end(); itemIter++) {
if (itemIter.value()->uuid == frame->uuid ) {
WlanItem * item= nullptr;
item = itemIter.value();
qDebug() << "a active connect missing when resort";
itemIter.value()->uuid.clear();
itemActiveConnectionStatusChanged(item, DEACTIVATED);
break;
}
}
}
frame->uuid.clear();
}
for ( ; listIndex < list.size(); listIndex++) {
if (frameIndex > frame->lanItemLayout->count() - 1) {
return;
}
if (frame->itemMap.contains(list.at(listIndex).at(0))) {
frame->lanItemLayout->removeWidget(frame->itemMap[list.at(listIndex).at(0)]);
frame->lanItemLayout->insertWidget(frameIndex, frame->itemMap[list.at(listIndex).at(0)]);
qDebug() << "custom resort " << list.at(listIndex).at(0) <<" insert position" << frameIndex;
if (frame->itemMap[list.at(listIndex).at(0)]->isAcitve) {
frame->itemMap[list.at(listIndex).at(0)]->isAcitve = false;
frame->itemMap[list.at(listIndex).at(0)]->uuid.clear();
frame->itemMap[list.at(listIndex).at(0)]->statusLabel->setText("");
}
2022-06-17 18:48:59 +08:00
if (list.at(listIndex).size() > 4) {
updateIcon(frame->itemMap[list.at(listIndex).at(0)], list.at(listIndex).at(1), list.at(listIndex).at(2), list.at(listIndex).at(3), list.at(listIndex).at(4).toInt());
}
2022-06-02 16:27:45 +08:00
frameIndex++;
} else {
qDebug() << "not find " << list.at(listIndex).at(0) << " in current list, ignore";
}
}
qDebug() << "resort finish";
}
2022-06-17 18:48:59 +08:00
void WlanConnect::updateIcon(WlanItem *item, QString signalStrength, QString security, QString isApConnection, int category)
2022-06-02 16:27:45 +08:00
{
qDebug() << "updateIcon" << item->titileLabel->text();
int sign = setSignal(signalStrength);
bool isLock = true;
if (security.isEmpty()) {
isLock = false;
} else {
isLock = true;
}
QString iconamePath;
if (isApConnection == IsApConnection) {
iconamePath = KApSymbolic;
} else {
2022-06-17 18:48:59 +08:00
iconamePath = wifiIcon(isLock, sign, category);
2022-06-02 16:27:45 +08:00
}
QIcon searchIcon = QIcon::fromTheme(iconamePath);
if (iconamePath != KLanSymbolic && iconamePath != NoNetSymbolic) {
item->iconLabel->setProperty("useIconHighlightEffect", 0x10);
}
2022-06-17 18:48:59 +08:00
item->iconLabel->setPixmap(searchIcon.pixmap(searchIcon.actualSize(QSize(ICON_SIZE))));
2022-06-02 16:27:45 +08:00
qDebug() << "updateIcon" << item->titileLabel->text() << " finish";
}
//wifi strength update
//void WlanConnect::updateStrengthList(QString deviceName, QString ssid, int strength)
//{
// return;
// if(!m_wifiSwitch->isChecked()) {
// return;
// }
// qDebug()<<"[WlanConnect]Update wireless network signal strength" << deviceName <<ssid << strength;
// bool isLock = false;
// QMap<QString, ItemFrame *>::iterator iters;
// for (iters = deviceWlanlistInfo.deviceLayoutMap.begin(); iters != deviceWlanlistInfo.deviceLayoutMap.end(); iters++) {
// if (iters.key() == deviceName) {
// qDebug() << "[WlanConnect] updateStrengthList find " << deviceName << " in deviceWlanlistInfo.deviceLayoutMap";
// if (iters.value()->lanItemLayout->layout() != NULL) {
// WlanItem* item = nullptr;
// QMap<QString, WlanItem*>::iterator iter;
// for (iter = deviceWlanlistInfo.wlanItemMap.begin(); iter != deviceWlanlistInfo.wlanItemMap.end(); iter++) {
// if (iter.key() == ssid) {
// item = iter.value();
// break;
// }
// }
// isLock = item->isLock;
// //remove the item from layout
// iters.value()->lanItemLayout->removeWidget(item);
// //get position
// int index = sortWlanNet(deviceName,ssid, QString::number(strength));
// //add the item to new position
// qDebug()<<"更新后位置:"<<index;
// int sign = setSignal(QString::number(strength));
// QString iconamePath = wifiIcon(isLock, sign);
// QIcon searchIcon = QIcon::fromTheme(iconamePath);
// if (iconamePath != KLanSymbolic && iconamePath != NoNetSymbolic) {
// item->iconLabel->setProperty("useIconHighlightEffect", 0x10);
// }
// item->iconLabel->setPixmap(searchIcon.pixmap(searchIcon.actualSize(QSize(24, 24))));
// iters.value()->lanItemLayout->insertWidget(index, item);
// }
// }
// }
//}
//device add or remove=================================
void WlanConnect::onDeviceStatusChanged()
{
qDebug()<<"[WlanConnect]onDeviceStatusChanged";
QEventLoop eventloop;
QTimer::singleShot(300, &eventloop, SLOT(quit()));
eventloop.exec();
QStringList list;
getDeviceList(list);
QStringList removeList,addList;
//remove的设备
for (int i = 0; i< deviceList.size(); ++i) {
if (!list.contains(deviceList.at(i))) {
qDebug() << "[WlanConnect]onDeviceStatusChanged " << deviceList.at(i) << "was removed";
removeList << deviceList.at(i);
}
}
//add的设备
for (int i = 0; i< list.size(); ++i) {
if (!deviceList.contains(list.at(i))) {
qDebug() << "[WlanConnect]onDeviceStatusChanged " << list.at(i) << "was add";
addList << list.at(i);
}
}
for (int i = 0; i < removeList.size(); ++i) {
removeDeviceFrame(removeList.at(i));
}
for (int i = 0; i < addList.size(); ++i) {
addDeviceFrame(addList.at(i));
}
deviceList = list;
if (deviceList.isEmpty()) {
2022-06-17 18:48:59 +08:00
m_wifiSwitch->setCheckable(false);
2022-06-02 16:27:45 +08:00
m_wifiSwitch->setChecked(false);
} else {
2022-06-17 18:48:59 +08:00
m_wifiSwitch->setCheckable(true);
2022-06-02 16:27:45 +08:00
setSwitchStatus();
}
}
void WlanConnect::onDeviceNameChanged(QString oldName, QString newName, int type)
{
if (WIRELESS_TYPE != type || !deviceFrameMap.contains(oldName) || !deviceList.contains(oldName)) {
qDebug() << "[WlanConnect]onDeviceNameChanged no such device " << oldName;
return;
}
if (deviceFrameMap.contains(newName) && deviceList.contains(newName)) {
qDebug() << "[WlanConnect]onDeviceNameChanged already has device " << newName;
return;
}
qDebug() << "[WlanConnect]onDeviceNameChanged " << oldName << "change to" << newName;
//shan chu chong jian
removeDeviceFrame(oldName);
removeDeviceFrame(newName);
getDeviceList(deviceList);
if (deviceList.contains(newName)) {
addDeviceFrame(newName);
initNetListFromDevice(newName);
}
}
//activeconnect status change
void WlanConnect::onActiveConnectionChanged(QString deviceName, QString ssid, QString uuid, int status)
{
if (!m_wifiSwitch->isChecked()) {
return;
}
if (uuid.isEmpty()) {
return;
}
WlanItem * item= nullptr;
//device ssid 有可能均为空
if (deviceName.isEmpty() || ssid.isEmpty()) {
if (status == ACTIVATING || status == ACTIVATED) {
return;
}
QMap<QString, ItemFrame*>::iterator iter;
for (iter = deviceFrameMap.begin(); iter != deviceFrameMap.end(); iter++) {
if (uuid == iter.value()->uuid) {
QMap<QString, WlanItem*>::iterator itemIter;
for (itemIter = iter.value()->itemMap.begin(); itemIter != iter.value()->itemMap.end(); itemIter++) {
if (itemIter.value()->uuid == uuid ) {
item = itemIter.value();
if (status == DEACTIVATED) {
itemIter.value()->uuid.clear();
}
break;
}
}
break;
}
}
} else {
if (!deviceFrameMap.contains(deviceName)) {
return;
}
for (int i = 0; i < deviceFrameMap[deviceName]->itemMap.size(); ++i) {
if (deviceFrameMap[deviceName]->itemMap.contains(ssid)) {
item = deviceFrameMap[deviceName]->itemMap[ssid];
if (status == ACTIVATED || status == ACTIVATING) {
deviceFrameMap[deviceName]->itemMap[ssid]->uuid = uuid;
deviceFrameMap[deviceName]->uuid = uuid;
if (status == ACTIVATED) {
deviceFrameMap[deviceName]->lanItemLayout->removeWidget(item);
deviceFrameMap[deviceName]->lanItemLayout->insertWidget(0,item);
deviceFrameMap[deviceName]->filletStyleChange();
}
} else if (status == DEACTIVATED) {
deviceFrameMap[deviceName]->itemMap[ssid]->uuid.clear();
deviceFrameMap[deviceName]->uuid.clear();
//todo 断开后排序 现在等下次更新列表 自动排序
}
break;
}
}
}
if (nullptr != item) {
itemActiveConnectionStatusChanged(item, status);
}
}
//wifi add===============================================================
void WlanConnect::onNetworkAdd(QString deviceName, QStringList wlanInfo)
{
qDebug()<<"[WlanConnect]onNetworkAdd "<< deviceName << " " << wlanInfo;
if(!m_wifiSwitch->isChecked() || deviceName.isEmpty()) {
return;
}
if (!deviceList.contains(deviceName)) {
qDebug() << "[WlanConnect]onNetworkAdd not contain " << deviceName << "then add";
deviceList.append(deviceName);
addDeviceFrame(deviceName);
onNetworkAdd(deviceName, wlanInfo);
return;
}
bool isLock = true;
if (wlanInfo.at(2) == "") {
isLock = false;
} else {
isLock = true;
}
QMap<QString, ItemFrame *>::iterator iter;
for (iter = deviceFrameMap.begin(); iter != deviceFrameMap.end(); iter++) {
if (deviceName == iter.key()) {
2022-06-17 18:48:59 +08:00
addOneWlanFrame(iter.value(), deviceName, wlanInfo.at(0), wlanInfo.at(1), "", isLock, false, WIRELESS_TYPE, wlanInfo.at(3), wlanInfo.at(3).toInt());
2022-06-02 16:27:45 +08:00
}
}
}
//wifi remove =============================================================
void WlanConnect::onNetworkRemove(QString deviceName, QString wlannName)
{
//开关已关闭 忽略
// if (!m_wifiSwitch->isChecked()) {
// qDebug() << "[WlanConnect]recieve network remove,but wireless switch is off";
// return;
// }
//当前无此设备 忽略
if (deviceName.isEmpty() || !deviceFrameMap.contains(deviceName)) {
qDebug() << "[WlanConnect]recieve network remove,but no such device:" << deviceName;
return;
}
qDebug()<<"[WlanConnect]Wifi remove device:" << deviceName << ",wlan name:" << wlannName;
QMap<QString, ItemFrame *>::iterator iter;
for (iter = deviceFrameMap.begin(); iter != deviceFrameMap.end(); iter++) {
if (deviceName == iter.key()) {
removeOneWlanFrame(iter.value(), deviceName, wlannName);
}
}
}
//获取设备列表=======================================================
void WlanConnect::getDeviceList(QStringList &list)
{
if (!m_interface->isValid()) {
return;
}
qDebug() << "[WlanConnect]call getDeviceListAndEnabled" << __LINE__;
QDBusMessage result = m_interface->call(QStringLiteral("getDeviceListAndEnabled"),1);
qDebug() << "[WlanConnect]call getDeviceListAndEnabled respond" << __LINE__;
if(result.type() == QDBusMessage::ErrorMessage)
{
qWarning() << "[WlanConnect]getWirelessDeviceList error:" << result.errorMessage();
return;
}
auto dbusArg = result.arguments().at(0).value<QDBusArgument>();
QMap<QString,bool> map;
dbusArg >> map;
list = map.keys();
}
//设置开关
void WlanConnect::setSwitchStatus()
{
// if (deviceList.size() == 0) {
// m_wifiSwitch->blockSignals(true);
// m_wifiSwitch->setChecked(false);
// m_wifiSwitch->blockSignals(false);
// return;
// }
bool status = m_switchGsettings->get(WIRELESS_SWITCH).toBool();
qDebug() << "[WlanConnect]setSwitchStatus" << status;
m_wifiSwitch->blockSignals(true);
m_wifiSwitch->setChecked(status);
m_wifiSwitch->blockSignals(false);
if (!m_wifiSwitch->isChecked()) {
hideLayout(ui->availableLayout);
} else {
showLayout(ui->availableLayout);
}
}
//初始化整体列表和单设备列表
void WlanConnect::initNet() {
// int count = 1;
//先构建每个设备的列表头
for (int i = 0; i < deviceList.size(); ++i) {
addDeviceFrame(deviceList.at(i));
}
//再填充每个设备的列表
for (int i = 0; i < deviceList.size(); ++i) {
initNetListFromDevice(deviceList.at(i));
}
}
//初始化设备列表 网卡标号问题?
void WlanConnect::initNetListFromDevice(QString deviceName)
{
qDebug() << "[WlanConnect]initNetListFromDevice " << deviceName;
if (!m_wifiSwitch->isChecked()) {
qDebug() << "[WlanConnect]initNetListFromDevice " << deviceName << " switch off";
return;
}
if (!deviceFrameMap.contains(deviceName)) {
qDebug() << "[WlanConnect]initNetListFromDevice " << deviceName << " not exist";
return;
}
if (!m_interface->isValid()) {
return;
}
qDebug() << "[WlanConnect]call getWirelessList" << __LINE__;
QDBusMessage result = m_interface->call(QStringLiteral("getWirelessList"));
qDebug() << "[WlanConnect]call getWirelessList respond" << __LINE__;
if(result.type() == QDBusMessage::ErrorMessage)
{
qWarning() << "getWirelessList error:" << result.errorMessage();
return;
}
auto dbusArg = result.arguments().at(0).value<QDBusArgument>();
QMap<QString, QVector<QStringList>> variantList;
dbusArg >> variantList;
if (variantList.size() == 0) {
qDebug() << "[WlanConnect]initNetListFromDevice " << deviceName << " list empty";
return;
}
QMap<QString, QVector<QStringList>>::iterator iter;
for (iter = variantList.begin(); iter != variantList.end(); iter++) {
if (deviceName == iter.key()) {
QVector<QStringList> wlanListInfo = iter.value();
//处理列表 已连接
qDebug() << "[WlanConnect]initNetListFromDevice " << deviceName << " acitved wifi " << wlanListInfo.at(0);
addActiveItem(deviceFrameMap[deviceName], deviceName, wlanListInfo.at(0));
//处理列表 未连接
for (int i = 1; i < wlanListInfo.length(); i++) {
qDebug() << "[WlanConnect]initNetListFromDevice " << deviceName << " deacitved wifi " << wlanListInfo.at(i);
addCustomItem(deviceFrameMap[deviceName], deviceName, wlanListInfo.at(i));
}
}
}
return;
}
//高级设置
void WlanConnect::runExternalApp() {
QString cmd = "nm-connection-editor";
QProcess process(this);
process.startDetached(cmd);
}
//根据信号强度分级+安全性分图标
2022-06-17 18:48:59 +08:00
QString WlanConnect::wifiIcon(bool isLock, int strength, int category) {
if (category == 0) {
switch (strength) {
case SIGNAL_EXCELLENT:
return isLock ? KWifiLockSymbolic : KWifiSymbolic;
case SIGNAL_GOOD:
return isLock ? KWifiLockGood : KWifiGood;
case SIGNAL_OK:
return isLock ? KWifiLockOK : KWifiOK;
case SIGNAL_LOW:
return isLock ? KWifiLockLow : KWifiLow;
case SIGNAL_NONE:
return isLock ? KWifiLockNone : KWifiNone;
default:
return "";
}
} else if (category == 1) {
switch (strength) {
case SIGNAL_EXCELLENT:
return isLock ? KWifi6LockSymbolic : KWifi6Symbolic;
case SIGNAL_GOOD:
return isLock ? KWifi6LockGood : KWifi6Good;
case SIGNAL_OK:
return isLock ? KWifi6LockOK : KWifi6OK;
case SIGNAL_LOW:
return isLock ? KWifi6LockLow : KWifi6Low;
case SIGNAL_NONE:
return isLock ? KWifi6LockNone : KWifi6None;
default:
return "";
}
} else {
switch (strength) {
case SIGNAL_EXCELLENT:
return isLock ? KWifi6PlusLockSymbolic : KWifi6PlusSymbolic;
case SIGNAL_GOOD:
return isLock ? KWifi6PlusLockGood : KWifi6PlusGood;
case SIGNAL_OK:
return isLock ? KWifi6PlusLockOK : KWifi6PlusOK;
case SIGNAL_LOW:
return isLock ? KWifi6PlusLockLow : KWifi6PlusLow;
case SIGNAL_NONE:
return isLock ? KWifi6PlusLockNone : KWifi6PlusNone;
default:
return "";
}
}
2022-06-02 16:27:45 +08:00
}
//根据信号强度分级
int WlanConnect::setSignal(QString lv) {
int signal = lv.toInt();
int signalLv = 0;
2022-06-17 18:48:59 +08:00
if (signal > EXCELLENT_SIGNAL) {
2022-06-02 16:27:45 +08:00
signalLv = 1;
2022-06-17 18:48:59 +08:00
} else if (signal > GOOD_SIGNAL) {
2022-06-02 16:27:45 +08:00
signalLv = 2;
2022-06-17 18:48:59 +08:00
} else if (signal > OK_SIGNAL) {
2022-06-02 16:27:45 +08:00
signalLv = 3;
2022-06-17 18:48:59 +08:00
} else if (signal > LOW_SIGNAL) {
2022-06-02 16:27:45 +08:00
signalLv = 4;
2022-06-17 18:48:59 +08:00
} else {
2022-06-02 16:27:45 +08:00
signalLv = 5;
}
return signalLv;
}
//隐藏
void WlanConnect::hideLayout(QVBoxLayout * layout) {
for (int i = layout->layout()->count()-1; i >= 0; --i) {
QLayoutItem *it = layout->layout()->itemAt(i);
ItemFrame *itemFrame = qobject_cast<ItemFrame *>(it->widget());
itemFrame->hide();
}
}
//显示
void WlanConnect::showLayout(QVBoxLayout * layout) {
for (int i = layout->layout()->count()-1; i >= 0; --i) {
QLayoutItem *it = layout->layout()->itemAt(i);
ItemFrame *itemFrame = qobject_cast<ItemFrame *>(it->widget());
itemFrame->show();
}
}
//获取应该插入哪个位置
int WlanConnect::sortWlanNet(QString deviceName, QString name, QString signal)
{
qDebug() << "[WlanConnect]call getWirelessList" << __LINE__;
QDBusMessage result = m_interface->call(QStringLiteral("getWirelessList"));
qDebug() << "[WlanConnect]call getWirelessList respond" << __LINE__;
if(result.type() == QDBusMessage::ErrorMessage)
{
qWarning() << "getWirelessList error:" << result.errorMessage();
return 0;
}
auto dbusArg = result.arguments().at(0).value<QDBusArgument>();
QMap<QString, QVector<QStringList>> variantList;
dbusArg >> variantList;
QMap<QString, QVector<QStringList>>::iterator iter;
for (iter = variantList.begin(); iter != variantList.end(); iter++) {
if (deviceName == iter.key()) {
QVector<QStringList> wlanListInfo = iter.value();
for (int i = 0; i < wlanListInfo.size(); i++) {
if (name == wlanListInfo.at(i).at(0)) {
return i;
}
}
}
}
return 0;
}
void WlanConnect::activeConnect(QString netName, QString deviceName, int type) {
2023-03-03 15:49:48 +08:00
if (!m_interfaceUi->isValid()) {
2022-06-02 16:27:45 +08:00
return;
}
qDebug() << "[WlanConnect]call activateConnect" << __LINE__;
2023-03-03 15:49:48 +08:00
m_interfaceUi->call("activateConnect",type, deviceName, netName);
2022-06-02 16:27:45 +08:00
qDebug() << "[WlanConnect]call activateConnect respond" << __LINE__;
}
void WlanConnect::deActiveConnect(QString netName, QString deviceName, int type) {
if (!m_interface->isValid()) {
return;
}
qDebug() << "[WlanConnect]call deActivateConnect" << __LINE__;
m_interface->call("deActivateConnect",type, deviceName, netName);
qDebug() << "[WlanConnect]call deActivateConnect respond" << __LINE__;
}
//处理列表 已连接
void WlanConnect::addActiveItem(ItemFrame *frame, QString devName, QStringList infoList)
{
if (frame == nullptr) {
return;
}
if (infoList.size() == 1) {
return;
}
bool isLock = true;
if (infoList.at(2) == "") {
isLock = false;
} else {
isLock = true;
}
2022-06-17 18:48:59 +08:00
addOneWlanFrame(frame, devName, infoList.at(0), infoList.at(1), infoList.at(3), isLock, true, WIRELESS_TYPE, infoList.at(4), infoList.at(5).toInt());
2022-06-02 16:27:45 +08:00
}
//处理列表 未连接
void WlanConnect::addCustomItem(ItemFrame *frame, QString devName, QStringList infoList)
{
if (frame == nullptr) {
return;
}
bool isLock = true;
if (infoList.at(2) == "") {
isLock = false;
} else {
isLock = true;
}
2022-06-17 18:48:59 +08:00
addOneWlanFrame(frame, devName, infoList.at(0), infoList.at(1), "", isLock, false, WIRELESS_TYPE, infoList.at(3), infoList.at(4).toInt());
2022-06-02 16:27:45 +08:00
}
//增加设备
void WlanConnect::addDeviceFrame(QString devName)
{
qDebug() << "[WlanConnect]addDeviceFrame " << devName;
ItemFrame *itemFrame = new ItemFrame(devName, pluginWidget);
ui->availableLayout->addWidget(itemFrame);
itemFrame->deviceFrame->deviceLabel->setText(tr("card")+/*QString("%1").arg(count)+*/""+devName);
deviceFrameMap.insert(devName, itemFrame);
2022-06-17 18:48:59 +08:00
connect(itemFrame->addWlanWidget, &AddNetBtn::clicked, this, [=](){
2023-03-03 15:49:48 +08:00
if (m_interfaceUi!= nullptr && m_interfaceUi->isValid()) {
2022-06-17 18:48:59 +08:00
qDebug() << "[NetConnect]call showAddOtherWlanWidget" << devName << __LINE__;
2023-03-03 15:49:48 +08:00
m_interfaceUi->call(QStringLiteral("showAddOtherWlanWidget"), devName);
qDebug() << "[NetConnect]call showAddOtherWlanWidget Respond" << __LINE__;
2022-06-17 18:48:59 +08:00
}
});
2022-06-02 16:27:45 +08:00
}
//减少设备
void WlanConnect::removeDeviceFrame(QString devName)
{
qDebug() << "[WlanConnect]removeDeviceFrame " << devName;
if (deviceFrameMap.contains(devName)) {
ItemFrame *item = deviceFrameMap[devName];
if (item->lanItemFrame->layout() != NULL) {
QLayoutItem* layoutItem;
while ((layoutItem = item->lanItemFrame->layout()->takeAt(0)) != NULL) {
delete layoutItem->widget();
delete layoutItem;
item = nullptr;
}
item->itemMap.clear();
}
delete item;
item = nullptr;
deviceFrameMap.remove(devName);
}
}
//增加ap
2022-06-17 18:48:59 +08:00
void WlanConnect::addOneWlanFrame(ItemFrame *frame, QString deviceName, QString name, QString signal, QString uuid, bool isLock, bool status, int type, QString isApConnection, int category)
2022-06-02 16:27:45 +08:00
{
if (nullptr == frame) {
return;
}
bool bApConnection = (isApConnection == IsApConnection);
if (frame->itemMap.contains(name)) {
qDebug() << "[WlanConnect]Already exist a wifi " << name << " in " << deviceName;
return;
}
//设置单项的信息
int sign = setSignal(signal);
WlanItem * wlanItem = new WlanItem(status, isLock, pluginWidget);
QString iconamePath;
if (bApConnection) {
iconamePath = KApSymbolic;
} else {
2022-06-17 18:48:59 +08:00
iconamePath = wifiIcon(isLock, sign, category);
2022-06-02 16:27:45 +08:00
}
if (iconamePath != KLanSymbolic && iconamePath != NoNetSymbolic) {
wlanItem->iconLabel->setProperty("useIconHighlightEffect", 0x10);
}
QIcon searchIcon = QIcon::fromTheme(iconamePath);
2022-06-17 18:48:59 +08:00
wlanItem->iconLabel->setPixmap(searchIcon.pixmap(searchIcon.actualSize(QSize(ICON_SIZE))));
2022-06-02 16:27:45 +08:00
wlanItem->titileLabel->setText(name);
if (status) {
wlanItem->statusLabel->setText(tr("connected"));
frame->uuid = uuid;
wlanItem->uuid = uuid;
} else {
wlanItem->statusLabel->setText("");
}
connect(wlanItem->infoLabel, &InfoButton::clicked, this, [=]{
// open detail page
2023-03-03 15:49:48 +08:00
if (!m_interfaceUi->isValid()) {
2022-06-02 16:27:45 +08:00
return;
}
qDebug() << "[WlanConnect]call showPropertyWidget" << __LINE__;
2023-03-03 15:49:48 +08:00
m_interfaceUi->call(QStringLiteral("showPropertyWidget"), deviceName, name);
2022-06-02 16:27:45 +08:00
qDebug() << "[WlanConnect]call showPropertyWidget respond" << __LINE__;
});
connect(wlanItem, &QPushButton::clicked, this, [=] {
if (wlanItem->isAcitve) {
deActiveConnect(name, deviceName, type);
} else {
activeConnect(name, deviceName, type);
}
});
//记录到deviceFrame的itemMap中
deviceFrameMap[deviceName]->itemMap.insert(name, wlanItem);
int index;
if (status) {
index = 0;
} else {
index = sortWlanNet(deviceName, name, signal);
}
qDebug()<<"insert " << name << " to " << deviceName << " list, postion " << index;
frame->lanItemLayout->insertWidget(index, wlanItem);
frame->filletStyleChange();
}
//减少ap
void WlanConnect::removeOneWlanFrame(ItemFrame *frame, QString deviceName, QString ssid)
{
if (nullptr == frame) {
return;
}
if (frame->itemMap.contains(ssid)) {
qDebug() << "[WlanConnect]removeOneWlanFrame " << deviceName << ssid;
frame->lanItemLayout->removeWidget(frame->itemMap[ssid]);
delete frame->itemMap[ssid];
frame->itemMap.remove(ssid);
frame->filletStyleChange();
}
}
void WlanConnect::itemActiveConnectionStatusChanged(WlanItem *item, int status)
{
if (status == ACTIVATING) {
item->startLoading();
} else if (status == ACTIVATED) {
item->stopLoading();
item->statusLabel->clear();
item->statusLabel->setMinimumSize(36,36);
item->statusLabel->setMaximumSize(16777215,16777215);
item->statusLabel->setText(tr("connected"));
item->isAcitve = true;
} else if (status == DEACTIVATING) {
item->startLoading();
} else if (status == DEACTIVATED) {
item->stopLoading();
item->statusLabel->clear();
item->statusLabel->setMinimumSize(36,36);
item->statusLabel->setMaximumSize(16777215,16777215);
item->isAcitve = false;
}
}