kylin-nm/plugins/mobilehotspot/mobilehotspotwidget.cpp

997 lines
34 KiB
C++
Raw Normal View History

2022-07-05 10:13:48 +08:00
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
2024-01-26 11:07:17 +08:00
* Copyright (C) 2023, KylinSoft Co., Ltd.
2022-07-05 10:13:48 +08:00
*
* 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
2023-04-21 10:03:57 +08:00
* the Free Software Foundation; either version 3 of the License, or
2022-07-05 10:13:48 +08:00
* (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.
*
*/
2022-06-02 16:27:45 +08:00
#include "mobilehotspotwidget.h"
#include <QDebug>
#include <QFormLayout>
2022-06-02 16:27:45 +08:00
#define LABEL_RECT 17, 0, 105, 23
#define CONTENTS_MARGINS 0, 0, 0, 0
2022-06-17 18:48:59 +08:00
#define ITEM_MARGINS 16, 0, 16, 0
2022-06-02 16:27:45 +08:00
#define FRAME_MIN_SIZE 550, 60
#define FRAME_MAX_SIZE 16777215, 16777215
#define CONTECT_FRAME_MAX_SIZE 16777215, 60
2023-04-11 15:34:43 +08:00
#define HINT_TEXT_MARGINS 8, 0, 0, 0
#define FRAME_MIN_SIZE 550, 60
2023-04-11 15:34:43 +08:00
2022-06-17 18:48:59 +08:00
#define LABLE_MIN_WIDTH 188
2022-06-02 16:27:45 +08:00
#define COMBOBOX_MIN_WIDTH 200
#define LINE_MAX_SIZE 16777215, 1
#define LINE_MIN_SIZE 0, 1
#define ICON_SIZE 24,24
#define PASSWORD_FRAME_MIN_HIGHT 60
#define PASSWORD_FRAME_FIX_HIGHT 90
2022-06-30 14:58:47 +08:00
#define PASSWORD_FRAME_MIN_SIZE 550, 60
#define PASSWORD_FRAME_MAX_SIZE 16777215, 86
#define PASSWORD_ITEM_MARGINS 16, 10, 16, 10
2021-10-27 12:28:16 +08:00
2022-06-02 16:27:45 +08:00
#define WIRELESS 1
#define AP_NAME_MAX_LENGTH 32
#define REFRESH_MSEC 20*1000
#define LOG_HEAD "[MobileHotspotWidget]"
2022-06-02 16:27:45 +08:00
const QByteArray GSETTINGS_SCHEMA = "org.ukui.kylin-nm.switch";
const QString WIRELESS_SWITCH = "wirelessswitch";
void MobileHotspotWidget::showDesktopNotify(const QString &message)
{
QDBusInterface iface("org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
"org.freedesktop.Notifications",
QDBusConnection::sessionBus());
QList<QVariant> args;
2023-06-02 13:58:06 +08:00
args<<(tr("Settings"))
2022-06-02 16:27:45 +08:00
<<((unsigned int) 0)
2022-10-12 15:25:17 +08:00
<<QString("ukui-control-center")
2023-06-02 13:58:06 +08:00
<<tr("Settings desktop message") //显示的是什么类型的信息
2022-06-02 16:27:45 +08:00
<<message //显示的具体信息
<<QStringList()
<<QVariantMap()
<<(int)-1;
iface.callWithArgumentList(QDBus::AutoDetect,"Notify",args);
}
#define HOTSPOT_CONTROL
2022-06-02 16:27:45 +08:00
MobileHotspotWidget::MobileHotspotWidget(QWidget *parent) : QWidget(parent)
{
m_Vlayout = new QVBoxLayout(this);
m_Vlayout->setContentsMargins(CONTENTS_MARGINS);
m_Vlayout->setSpacing(0);
2022-06-02 16:27:45 +08:00
qDBusRegisterMetaType<QMap<QString, bool> >();
qDBusRegisterMetaType<QMap<QString, int> >();
qDBusRegisterMetaType<QVector<QStringList> >();
qDBusRegisterMetaType<QMap<QString, QVector<QStringList> >>();
initUI();
#ifdef HOTSPOT_CONTROL
initConnectDevPage();
initBlackListPage();
#endif
2022-06-02 16:27:45 +08:00
m_switchBtn->installEventFilter(this);
m_interface = new QDBusInterface("com.kylin.network", "/com/kylin/network",
"com.kylin.network",
QDBusConnection::sessionBus());
if(!m_interface->isValid()) {
qDebug() << LOG_HEAD << "dbus interface com.kylin.network is invaild";
2022-06-02 16:27:45 +08:00
m_switchBtn->setChecked(false);
setUiEnabled(false);
}
m_hostName = getHostName();
initDbusConnect();
initInterfaceInfo();
getApInfo();
#ifdef HOTSPOT_CONTROL
initNmDbus();
#endif
this->setLayout(m_Vlayout);
m_Vlayout->addStretch();
2022-06-17 18:48:59 +08:00
connect(m_switchBtn, &KSwitchButton::stateChanged, this, &MobileHotspotWidget::setUiEnabled);
2023-04-21 14:39:22 +08:00
connect(m_interfaceComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MobileHotspotWidget::onInterfaceChanged);
2022-06-02 16:27:45 +08:00
connect(m_interfaceComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=]() {
UkccCommon::buriedSettings("MobileHotspot", "Net card", QString("select"), m_interfaceComboBox->currentText());
2022-06-02 16:27:45 +08:00
m_interfaceName = m_interfaceComboBox->currentText();
updateBandCombox();
});
connect(m_freqBandComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), [=](){
if (m_isUserSelect && !m_freqBandComboBox->currentText().isEmpty()) {
UkccCommon::buriedSettings("MobileHotspot", "Frequency band", QString("select"), m_freqBandComboBox->currentText());
}
});
2023-04-21 14:39:22 +08:00
onInterfaceChanged();
#ifdef HOTSPOT_CONTROL
m_connectDevPage->refreshStalist();
m_blacklistPage->refreshBlacklist();
#endif
this->update();
2022-06-02 16:27:45 +08:00
}
MobileHotspotWidget::~MobileHotspotWidget()
{
deleteActivePathInterface();
deleteSettingPathInterface();
2022-06-02 16:27:45 +08:00
delete m_interface;
}
bool MobileHotspotWidget::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() == QEvent::MouseButtonDblClick && watched == m_switchBtn) {
return true;
}
if (watched == m_switchBtn) {
if (event->type() == QEvent::MouseButtonRelease) {
2022-06-02 16:27:45 +08:00
if (!m_interface->isValid()) {
return true;
}
2022-06-17 18:48:59 +08:00
if (!m_switchBtn->isCheckable()) {
2022-06-02 16:27:45 +08:00
showDesktopNotify(tr("wirless switch is close or no wireless device"));
return true;
}
2023-06-26 16:52:45 +08:00
UkccCommon::buriedSettings("MobileHotspot", "Open", QString("settings"), !m_switchBtn->isChecked() ? "true":"false");
2022-06-02 16:27:45 +08:00
if (m_switchBtn->isChecked()) {
2022-09-26 14:10:08 +08:00
// showDesktopNotify(tr("start to close hotspot"));
2022-06-02 16:27:45 +08:00
QDBusReply<void> reply = m_interface->call("deactiveWirelessAp", m_apNameLine->text(), m_uuid);
if (!reply.isValid()) {
qDebug() << LOG_HEAD << "call deactiveWirelessAp failed ";
2022-06-02 16:27:45 +08:00
return true;
}
#ifdef HOTSPOT_CONTROL
deleteActivePathInterface();
m_connectDevPage->setInterface(nullptr);
m_connectDevPage->refreshStalist();
m_blacklistPage->refreshBlacklist();
#endif
this->update();
2022-06-02 16:27:45 +08:00
} else {
if (m_apNameLine->text().isEmpty() || m_interfaceName.isEmpty())
{
showDesktopNotify(tr("hotpots name or device is invalid"));
return true;
}
if (m_pwdNameLine->text().length() < 8) {
2022-06-30 14:58:47 +08:00
// showDesktopNotify(tr("can not create hotspot with password length less than eight!"));
2022-06-02 16:27:45 +08:00
return true;
}
2022-09-26 14:10:08 +08:00
// showDesktopNotify(tr("start to open hotspot ") + m_apNameLine->text());
2022-06-02 16:27:45 +08:00
QDBusReply<void> reply = m_interface->call("activeWirelessAp",
m_apNameLine->text(),
m_pwdNameLine->text(),
m_freqBandComboBox->currentText(),
m_interfaceComboBox->currentText());
2022-06-02 16:27:45 +08:00
if (!reply.isValid()) {
qDebug() << LOG_HEAD << "call activeWirelessAp failed ";
2022-06-02 16:27:45 +08:00
return true;
}
}
return true;
}
}
return QWidget::eventFilter(watched, event);
}
void MobileHotspotWidget::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
}
2022-07-07 09:20:34 +08:00
void MobileHotspotWidget::resetFrameSize()
{
int height = 0;
for (int i = 0; i < m_hotspotFrame->layout()->count(); i ++) {
QWidget *w = m_hotspotFrame->layout()->itemAt(i)->widget();
if (w != nullptr && !w->isHidden()) {
height += w->height();
}
}
m_hotspotFrame->setFixedHeight(height);
}
2022-06-02 16:27:45 +08:00
void MobileHotspotWidget::initUI()
{
m_hotspotFrame = new QFrame(this);
m_hotspotFrame->setMinimumSize(FRAME_MIN_SIZE);
m_hotspotFrame->setMaximumSize(FRAME_MAX_SIZE);
m_hotspotFrame->setFrameShape(QFrame::Box);
2022-06-02 16:27:45 +08:00
QVBoxLayout *hotspotLyt = new QVBoxLayout(this);
2022-06-02 16:27:45 +08:00
hotspotLyt->setContentsMargins(0, 0, 0, 0);
m_hotspotFrame->setLayout(hotspotLyt);
2022-06-02 16:27:45 +08:00
m_hotspotTitleLabel = new TitleLabel(this);
m_hotspotTitleLabel->setText(tr("Hotspot"));
setSwitchFrame();
setApNameFrame();
setPasswordFrame();
setFreqBandFrame();
setInterFaceFrame();
switchAndApNameLine = myLine();
apNameAndPwdLine = myLine();
pwdAndfreqBandLine = myLine();
freqBandAndInterfaceLine = myLine();
hotspotLyt->addWidget(m_switchFrame);
hotspotLyt->addWidget(switchAndApNameLine);
hotspotLyt->addWidget(m_ApNameFrame);
hotspotLyt->addWidget(apNameAndPwdLine);
hotspotLyt->addWidget(m_passwordFrame);
hotspotLyt->addWidget(pwdAndfreqBandLine);
hotspotLyt->addWidget(m_freqBandFrame);
hotspotLyt->addWidget(freqBandAndInterfaceLine);
hotspotLyt->addWidget(m_interfaceFrame);
hotspotLyt->setSpacing(0);
2022-07-11 13:59:56 +08:00
resetFrameSize();
m_Vlayout->addWidget(m_hotspotTitleLabel);
m_Vlayout->addSpacing(8);
m_Vlayout->addWidget(m_hotspotFrame);
2022-06-02 16:27:45 +08:00
}
void MobileHotspotWidget::initDbusConnect()
{
if(m_interface->isValid()) {
connect(m_interface,SIGNAL(activateFailed(QString)), this, SLOT(onActivateFailed(QString)), Qt::QueuedConnection);
connect(m_interface,SIGNAL(deactivateFailed(QString)), this, SLOT(onDeactivateFailed(QString)), Qt::QueuedConnection);
connect(m_interface,SIGNAL(wirelessDeviceStatusChanged()), this, SLOT(onDeviceStatusChanged()), Qt::QueuedConnection);
2022-06-02 16:27:45 +08:00
connect(m_interface,SIGNAL(deviceNameChanged(QString, QString, int)), this, SLOT(onDeviceNameChanged(QString, QString, int)), Qt::QueuedConnection);
connect(m_interface,SIGNAL(hotspotDeactivated(QString, QString)), this, SLOT(onHotspotDeactivated(QString, QString)), Qt::QueuedConnection);
2023-04-11 15:34:43 +08:00
connect(m_interface,SIGNAL(hotspotActivated(QString, QString, QString, QString, QString)), this, SLOT(onHotspotActivated(QString, QString, QString, QString, QString)), Qt::QueuedConnection);
2022-06-02 16:27:45 +08:00
connect(m_interface, SIGNAL(wlanactiveConnectionStateChanged(QString, QString, QString, int)), this, SLOT(onActiveConnectionChanged(QString, QString, QString, int)), Qt::QueuedConnection);
2023-04-06 14:09:59 +08:00
connect(m_interface, SIGNAL(wirelessSwitchBtnChanged(bool)), this, SLOT(onWirelessBtnChanged(bool)), Qt::QueuedConnection);
2022-06-02 16:27:45 +08:00
}
connect(m_apNameLine, &QLineEdit::textEdited, this, &MobileHotspotWidget::onApLineEditTextEdit);
#ifdef HOTSPOT_CONTROL
connect(m_connectDevPage, SIGNAL(setStaIntoBlacklist(QString, QString)), m_blacklistPage, SLOT(onsetStaIntoBlacklist(QString, QString)));
#endif
2022-06-30 14:58:47 +08:00
connect(m_pwdNameLine, SIGNAL(textChanged(QString)), this, SLOT(onPwdTextChanged()));
2022-06-02 16:27:45 +08:00
}
void MobileHotspotWidget::onApLineEditTextEdit(QString text)
{
int count = 0;
int i = 0;
for ( ; i < text.length(); ++i) {
count += text.mid(i,1).toLocal8Bit().length();
if (count > AP_NAME_MAX_LENGTH) {
m_apNameLine->setText(text.left(i));
return;
2022-06-02 16:27:45 +08:00
}
}
}
2022-06-30 14:58:47 +08:00
void MobileHotspotWidget::onPwdTextChanged()
{
if (m_pwdNameLine->text().length() < 8) {
m_passwordFrame->setFixedHeight(PASSWORD_FRAME_FIX_HIGHT);
2022-06-30 14:58:47 +08:00
m_pwdHintLabel->show();
} else {
m_passwordFrame->setFixedHeight(PASSWORD_FRAME_MIN_HIGHT);
2022-06-30 14:58:47 +08:00
m_pwdHintLabel->hide();
}
2022-07-11 13:59:56 +08:00
resetFrameSize();
this->update();
2022-06-30 14:58:47 +08:00
}
2022-06-02 16:27:45 +08:00
2023-04-21 14:39:22 +08:00
void MobileHotspotWidget::onInterfaceChanged()
{
m_interfaceName = m_interfaceComboBox->currentText();
if(m_interface->isValid()) {
QDBusMessage result = m_interface->call(QStringLiteral("getWirelessList"));
if(result.type() == QDBusMessage::ErrorMessage)
{
qWarning() << "getWirelessList error:" << result.errorMessage();
return;
}
bool flag = false;
auto dbusArg = result.arguments().at(0).value<QDBusArgument>();
QMap<QString, QVector<QStringList>> variantList;
dbusArg >> variantList;
if (variantList.size() != 0) {
QMap<QString, QVector<QStringList>>::iterator iter;
for (iter = variantList.begin(); iter != variantList.end(); iter++) {
if (m_interfaceName == iter.key()) {
QVector<QStringList> wlanListInfo = iter.value();
if (!wlanListInfo.isEmpty() && wlanListInfo.at(0).size() > 1) {
flag = true;
}
break;
}
}
}
if (flag) {
m_interfaceWarnLabel->setText(tr("use ") + m_interfaceName +
tr(" share network, will interrupt local wireless connection"));
m_interfaceFrame->setFixedHeight(PASSWORD_FRAME_FIX_HIGHT);
m_warnWidget->show();
} else {
m_interfaceFrame->setFixedHeight(PASSWORD_FRAME_MIN_HIGHT);
m_warnWidget->hide();
}
resetFrameSize();
}
updateBandCombox();
}
2022-06-02 16:27:45 +08:00
void MobileHotspotWidget::onActiveConnectionChanged(QString deviceName, QString ssid, QString uuid, int status)
{
if(m_uuid == uuid && status == 4) {
showDesktopNotify(tr("hotspot already close"));
m_switchBtn->setChecked(false);
setUiEnabled(false);
m_uuid.clear();
}
2023-08-25 17:34:33 +08:00
if (deviceName != m_interfaceName) {
return;
}
if (m_interfaceComboBox && status == 2) {
2023-04-21 14:39:22 +08:00
onInterfaceChanged();
}
2022-06-02 16:27:45 +08:00
}
2023-04-06 14:09:59 +08:00
void MobileHotspotWidget::onWirelessBtnChanged(bool state)
2021-10-27 12:28:16 +08:00
{
2023-04-21 14:39:22 +08:00
if (!state) {
2023-04-06 14:09:59 +08:00
m_switchBtn->setChecked(state);
m_uuid.clear();
m_switchBtn->setCheckable(false);
} else {
m_switchBtn->setCheckable(true);
2022-06-02 16:27:45 +08:00
}
}
void MobileHotspotWidget::initInterfaceInfo()
{
if(!m_interface->isValid()) {
return;
}
if (m_interfaceComboBox->isVisible()) {
m_interfaceComboBox->hidePopup();
}
2022-06-02 16:27:45 +08:00
m_interfaceComboBox->clear();
QDBusReply<QMap<QString, bool> > reply = m_interface->call("getDeviceListAndEnabled",WIRELESS);
if (!reply.isValid()) {
qDebug() << LOG_HEAD <<"execute dbus method 'getDeviceListAndEnabled' is invalid in func initInterfaceInfo()";
2022-06-02 16:27:45 +08:00
setWidgetHidden(true);
return;
}
QMap<QString, bool> devMap = reply.value();
QDBusReply<QMap<QString, int> > capReply = m_interface->call("getWirelessDeviceCap");
if (!capReply.isValid()) {
qDebug() << LOG_HEAD <<"execute dbus method 'getWirelessDeviceCap' is invalid in func initInterfaceInfo()" <<capReply.error().type() ;
2022-06-02 16:27:45 +08:00
setWidgetHidden(true);
return;
}
QMap<QString, int> devCapMap = capReply.value();
if (devMap.isEmpty()) {
qDebug() << LOG_HEAD << "no wireless device";
2022-06-02 16:27:45 +08:00
setWidgetHidden(true);
2022-06-17 18:48:59 +08:00
m_switchBtn->setCheckable(false);
2022-06-02 16:27:45 +08:00
} else {
QMap<QString, bool>::Iterator iter = devMap.begin();
while (iter != devMap.end()) {
QString interfaceName = iter.key();
if (!(devCapMap[interfaceName] & 0x01)) {
2022-06-02 16:27:45 +08:00
m_interfaceComboBox->addItem(interfaceName);
}
iter++;
}
if (m_interfaceComboBox->count() > 0) {
setWidgetHidden(false);
m_interfaceName = m_interfaceComboBox->currentText();
if (m_interfaceComboBox->count() == 1) {
updateBandCombox();
}
} else {
qDebug() << LOG_HEAD << "no useable wireless device";
2022-06-02 16:27:45 +08:00
setWidgetHidden(true);
}
}
}
void MobileHotspotWidget::getApInfo()
{
if (!m_interface->isValid()) {
return;
}
if (m_interfaceComboBox->count() <= 0) {
2023-04-11 15:34:43 +08:00
m_switchBtn->setChecked(false);
setWidgetHidden(true);
qWarning() << LOG_HEAD << "getApInfo but interface is empty";
return;
}
2022-06-02 16:27:45 +08:00
QDBusReply<QStringList> reply = m_interface->call("getStoredApInfo");
if (!reply.isValid()) {
qDebug() << LOG_HEAD <<"execute dbus method 'getStoredApInfo' is invalid in func getObjectPath()";
2022-06-02 16:27:45 +08:00
}
QStringList apInfo = reply.value();
if (apInfo.isEmpty()) {
qDebug() << LOG_HEAD << "no stored hotspot info";
2022-06-02 16:27:45 +08:00
m_apNameLine->setText(m_hostName);
m_pwdNameLine->setText("12345678");
return;
} else {
m_apNameLine->setText(apInfo.at(0));
m_pwdNameLine->setText(apInfo.at(1));
2022-06-02 16:27:45 +08:00
int index = m_interfaceComboBox->findText(apInfo.at(2));
if (index >= 0) {
m_interfaceComboBox->setCurrentIndex(index);
m_interfaceName = apInfo.at(2);
} else {
qDebug() << LOG_HEAD << "no such interface " << apInfo.at(2);
2022-06-02 16:27:45 +08:00
}
if (apInfo.at(3) == "true") {
m_switchBtn->setChecked(true);
setUiEnabled(true);
m_uuid = apInfo.at(4);
} else {
m_switchBtn->setChecked(false);
setUiEnabled(false);
m_uuid = apInfo.at(4);
}
int i = m_freqBandComboBox->findText(apInfo.at(5));
if (i >= 0) {
m_isUserSelect = false;
m_freqBandComboBox->setCurrentIndex(i);
m_isUserSelect = true;
}
2022-06-02 16:27:45 +08:00
}
}
void MobileHotspotWidget::setSwitchFrame()
{
/* Open */
m_switchFrame = new QFrame(this);
m_switchFrame->setFrameShape(QFrame::Shape::NoFrame);
m_switchFrame->setMinimumSize(FRAME_MIN_SIZE);
m_switchFrame->setMaximumSize(CONTECT_FRAME_MAX_SIZE);
QHBoxLayout *switchLayout = new QHBoxLayout(m_switchFrame);
2022-06-02 16:27:45 +08:00
m_switchLabel = new QLabel(tr("Open"), this);
m_switchLabel->setMinimumWidth(LABLE_MIN_WIDTH);
2022-06-17 18:48:59 +08:00
m_switchBtn = new KSwitchButton(this);
switchLayout->setContentsMargins(ITEM_MARGINS);
2022-06-02 16:27:45 +08:00
switchLayout->addWidget(m_switchLabel);
switchLayout->addStretch();
switchLayout->addWidget(m_switchBtn);
m_switchFrame->setLayout(switchLayout);
}
void MobileHotspotWidget::setApNameFrame()
{
/* ApName */
m_ApNameFrame = new QFrame(this);
m_ApNameFrame->setFrameShape(QFrame::Shape::NoFrame);
m_ApNameFrame->setMinimumSize(FRAME_MIN_SIZE);
m_ApNameFrame->setMaximumSize(CONTECT_FRAME_MAX_SIZE);
QHBoxLayout *apNameHLayout = new QHBoxLayout(m_ApNameFrame);
2022-06-02 16:27:45 +08:00
m_apNameLabel = new QLabel(tr("Wi-Fi Name"), this);
m_apNameLabel->setMinimumWidth(LABLE_MIN_WIDTH);
m_apNameLine = new QLineEdit(this);
m_apNameLine->setMinimumWidth(COMBOBOX_MIN_WIDTH);
m_apNameLine->setMaxLength(AP_NAME_MAX_LENGTH);
2022-06-17 18:48:59 +08:00
apNameHLayout->setContentsMargins(ITEM_MARGINS);
apNameHLayout->setSpacing(0);
2022-06-02 16:27:45 +08:00
apNameHLayout->addWidget(m_apNameLabel);
apNameHLayout->addWidget(m_apNameLine);
m_ApNameFrame->setLayout(apNameHLayout);
}
void MobileHotspotWidget::setPasswordFrame()
{
/* Password */
m_passwordFrame = new QFrame(this);
m_passwordFrame->setFrameShape(QFrame::Shape::NoFrame);
m_passwordFrame->setMinimumSize(PASSWORD_FRAME_MIN_SIZE);
m_passwordFrame->setMaximumSize(PASSWORD_FRAME_MAX_SIZE);
2022-06-02 16:27:45 +08:00
m_pwdLabel = new QLabel(tr("Password"), this);
m_pwdLabel->setMinimumWidth(LABLE_MIN_WIDTH);
2022-06-17 18:48:59 +08:00
m_pwdNameLine = new KPasswordEdit(this);
m_pwdNameLine->setClearButtonEnabled(false);//禁用ClearBtn按钮X
2022-06-02 16:27:45 +08:00
m_pwdNameLine->setMinimumWidth(COMBOBOX_MIN_WIDTH);
m_pwdHintLabel= new QLabel(this);
m_pwdHintLabel->setFixedHeight(20);
m_pwdHintLabel->setContentsMargins(HINT_TEXT_MARGINS);
QPalette hintTextColor;
hintTextColor.setColor(QPalette::WindowText, Qt::red);
m_pwdHintLabel->setPalette(hintTextColor);
m_pwdHintLabel->setText(tr("Contains at least 8 characters")); //至少包含8个字符
QWidget *pwdInputWidget = new QWidget(m_passwordFrame);
QVBoxLayout *pwdInputVLayout = new QVBoxLayout(pwdInputWidget);
pwdInputVLayout->setContentsMargins(CONTENTS_MARGINS);
pwdInputVLayout->setSpacing(0);
pwdInputVLayout->addWidget(m_pwdNameLine);
pwdInputVLayout->addWidget(m_pwdHintLabel);
QGridLayout *pwdLayout = new QGridLayout(m_passwordFrame);
pwdLayout->setContentsMargins(PASSWORD_ITEM_MARGINS);
pwdLayout->setSpacing(0);
pwdLayout->addWidget(m_pwdLabel, 0, 0);
pwdLayout->addWidget(m_pwdNameLine, 0, 1);
pwdLayout->addWidget(m_pwdHintLabel, 1, 1);
m_passwordFrame->setLayout(pwdLayout);
m_pwdNameLine->installEventFilter(this);
2022-06-02 16:27:45 +08:00
}
void MobileHotspotWidget::setFreqBandFrame()
{
/* frequency band */
m_freqBandFrame = new QFrame(this);
m_freqBandFrame->setFrameShape(QFrame::Shape::NoFrame);
m_freqBandFrame->setMinimumSize(FRAME_MIN_SIZE);
m_freqBandFrame->setMaximumSize(CONTECT_FRAME_MAX_SIZE);
QHBoxLayout *freqBandHLayout = new QHBoxLayout(m_freqBandFrame);
2022-06-02 16:27:45 +08:00
m_freqBandLabel = new FixLabel(this);
m_freqBandLabel->setText(tr("Frequency band"));
m_freqBandLabel->setFixedWidth(LABLE_MIN_WIDTH - 8);
2022-06-02 16:27:45 +08:00
m_freqBandComboBox = new QComboBox(this);
m_freqBandComboBox->setInsertPolicy(QComboBox::NoInsert);
m_freqBandComboBox->setMinimumWidth(COMBOBOX_MIN_WIDTH);
m_freqBandComboBox->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
m_freqBandComboBox->addItem("2.4GHz");
m_freqBandComboBox->addItem("5GHz");
2022-06-17 18:48:59 +08:00
freqBandHLayout->setContentsMargins(ITEM_MARGINS);
freqBandHLayout->setSpacing(8);
2022-06-02 16:27:45 +08:00
freqBandHLayout->addWidget(m_freqBandLabel);
freqBandHLayout->addWidget(m_freqBandComboBox);
m_freqBandFrame->setLayout(freqBandHLayout);
}
void MobileHotspotWidget::setInterFaceFrame()
{
/* key tips */
m_interfaceFrame = new QFrame(this);
m_interfaceFrame->setFrameShape(QFrame::Shape::NoFrame);
2023-04-21 14:39:22 +08:00
m_interfaceFrame->setMinimumSize(PASSWORD_FRAME_MIN_SIZE);
m_interfaceFrame->setMaximumSize(PASSWORD_FRAME_MAX_SIZE);
2022-06-02 16:27:45 +08:00
m_interfaceLabel = new QLabel(tr("Net card"), this);
m_interfaceLabel->setFixedWidth(LABLE_MIN_WIDTH);
2022-06-02 16:27:45 +08:00
m_interfaceComboBox = new QComboBox(this);
m_interfaceComboBox->setInsertPolicy(QComboBox::NoInsert);
m_interfaceComboBox->setMinimumWidth(COMBOBOX_MIN_WIDTH);
2023-04-21 14:39:22 +08:00
m_warnWidget = new QWidget(this);
m_warnWidget->setFixedHeight(20);
m_warnWidget->setContentsMargins(8,0,0,0);
QHBoxLayout *warnTextHLayout = new QHBoxLayout(m_warnWidget);
2023-04-21 14:39:22 +08:00
QLabel* warnIcon = new QLabel(this);
warnIcon->setContentsMargins(0,0,0,0);
warnIcon->setPixmap(QIcon::fromTheme("dialog-warning").pixmap(16,16));
m_interfaceWarnLabel= new FixLabel(this);
2023-04-21 14:39:22 +08:00
m_interfaceWarnLabel->setFixedHeight(20);
QPalette hintTextColor;
hintTextColor.setColor(QPalette::WindowText, Qt::red);
m_interfaceWarnLabel->setPalette(hintTextColor);
warnTextHLayout->setSpacing(8);
2023-04-21 14:39:22 +08:00
warnTextHLayout->setContentsMargins(0,0,0,0);
warnTextHLayout->addWidget(warnIcon);
warnTextHLayout->addWidget(m_interfaceWarnLabel);
warnIcon->setFixedWidth(16);
2023-04-21 14:39:22 +08:00
m_warnWidget->setLayout(warnTextHLayout);
2022-12-08 14:00:51 +08:00
QGridLayout *interfaceFLayout = new QGridLayout(m_interfaceFrame);
2023-04-21 14:39:22 +08:00
interfaceFLayout->setContentsMargins(PASSWORD_ITEM_MARGINS);
interfaceFLayout->setSpacing(0);
interfaceFLayout->addWidget(m_interfaceLabel, 0, 0);
interfaceFLayout->addWidget(m_interfaceComboBox, 0, 1);
interfaceFLayout->addWidget(m_warnWidget, 1, 1);
2023-04-21 14:39:22 +08:00
m_interfaceFrame->setLayout(interfaceFLayout);
m_warnWidget->hide();
2022-06-02 16:27:45 +08:00
}
void MobileHotspotWidget::onActivateFailed(QString errorMessage)
{
if (errorMessage.indexOf("hotspot")) {
//todo
// showDesktopNotify(errorMessage);
}
}
void MobileHotspotWidget::onDeactivateFailed(QString errorMessage)
{
if (errorMessage.indexOf("hotspot")) {
// showDesktopNotify(errorMessage);
}
}
//设备插拔
void MobileHotspotWidget::onDeviceStatusChanged()
{
initInterfaceInfo();
getApInfo();
}
void MobileHotspotWidget::onDeviceNameChanged(QString oldName, QString newName, int type)
{
if (WIRELESS != type) {
return;
}
int index = m_interfaceComboBox->findText(oldName);
if (index >= 0) {
m_interfaceComboBox->setItemText(index, newName);
if (m_interfaceName == oldName) {
m_interfaceName = newName;
}
}
QTimer::singleShot(100, this, [=]() {
if (m_interfaceComboBox->currentText() == newName) {
updateBandCombox();
}
});
2022-06-02 16:27:45 +08:00
}
//热点断开
void MobileHotspotWidget::onHotspotDeactivated(QString devName, QString ssid)
{
if (!m_switchBtn->isChecked()) {
return;
}
if (devName == m_interfaceComboBox->currentText() && ssid == m_apNameLine->text()) {
m_switchBtn->setChecked(false);
m_uuid.clear();
// setUiEnabled(true);
showDesktopNotify(tr("hotspot already close"));
}
}
//热点连接
void MobileHotspotWidget::onHotspotActivated(QString devName, QString ssid, QString uuid, QString activePath, QString settingPath)
2022-06-02 16:27:45 +08:00
{
qDebug() << LOG_HEAD << "onHotspotActivated" <<devName << ssid << uuid;
2022-06-02 16:27:45 +08:00
if (m_switchBtn->isChecked()) {
return;
}
#ifdef HOTSPOT_CONTROL
if (activePath != nullptr) {
deleteActivePathInterface();
initActivePathInterface(activePath);
}
if (settingPath != nullptr) {
deleteSettingPathInterface();
initSettingPathInterface(settingPath);
}
m_connectDevPage->refreshStalist();
m_blacklistPage->refreshBlacklist();
#endif
this->update();
2022-06-02 16:27:45 +08:00
if (devName == m_interfaceComboBox->currentText() && ssid == m_apNameLine->text()) {
m_switchBtn->setChecked(true);
m_uuid = uuid;
// setUiEnabled(false);
showDesktopNotify(tr("hotspot already open"));
} else {
QStringList info;
if (!getApInfoBySsid(devName, ssid, info)) {
return;
}
int index = m_interfaceComboBox->findText(devName);
if (index >= 0) {
showDesktopNotify(tr("hotspot already open"));
m_apNameLine->setText(ssid);
m_interfaceComboBox->setCurrentIndex(index);
m_switchBtn->setChecked(true);
2022-06-17 18:48:59 +08:00
m_switchBtn->setCheckable(true);
2022-06-02 16:27:45 +08:00
m_pwdNameLine->setText(info.at(0));
m_interfaceName = devName;
updateBandCombox();
index = m_freqBandComboBox->findText(info.at(1));
if (index >= 0) {
m_isUserSelect = false;
2022-06-02 16:27:45 +08:00
m_freqBandComboBox->setCurrentIndex(index);
m_isUserSelect = true;
2022-06-02 16:27:45 +08:00
}
m_uuid = uuid;
} else {
qDebug() << "no such device in combo box";
}
}
}
bool MobileHotspotWidget::getApInfoBySsid(QString devName, QString ssid, QStringList &info)
{
info.clear();
if(!m_interface->isValid()) {
return false;
}
QDBusReply<QStringList> reply = m_interface->call("getApInfoBySsid", devName, ssid);
if (!reply.isValid()) {
qDebug()<<"execute dbus method 'getApInfoBySsid' is invalid in func getApInfoBySsid()";
}
info = reply.value();
if (info.size() != 2) {
return false;
} else {
return true;
}
}
void MobileHotspotWidget::setUiEnabled(bool enable)
{
qDebug() << "switch mode change to " << enable;
if (enable) {
m_pwdNameLine->setEnabled(false);
m_freqBandComboBox->setEnabled(false);
m_interfaceComboBox->setEnabled(false);
m_apNameLine->setEnabled(false);
} else {
m_pwdNameLine->setEnabled(true);
m_freqBandComboBox->setEnabled(true);
m_interfaceComboBox->setEnabled(true);
m_apNameLine->setEnabled(true);
}
}
void MobileHotspotWidget::setWidgetHidden(bool isHidden)
{
m_ApNameFrame->setHidden(isHidden);
m_passwordFrame->setHidden(isHidden);
m_freqBandFrame->setHidden(isHidden);
m_interfaceFrame->setHidden(isHidden);
switchAndApNameLine->setHidden(isHidden);
apNameAndPwdLine->setHidden(isHidden);
pwdAndfreqBandLine->setHidden(isHidden);
freqBandAndInterfaceLine->setHidden(isHidden);
if (isHidden) {
m_switchBtn->setChecked(false);
2022-06-17 18:48:59 +08:00
m_switchBtn->setCheckable(false);
2022-06-02 16:27:45 +08:00
m_interfaceName = "";
m_uuid = "";
} else {
2022-06-17 18:48:59 +08:00
m_switchBtn->setCheckable(true);
2023-04-06 14:09:59 +08:00
QDBusReply<bool> reply = m_interface->call("getWirelessSwitchBtnState");
bool state = reply.value();
onWirelessBtnChanged(state);
2022-06-02 16:27:45 +08:00
}
resetFrameSize();
2022-06-02 16:27:45 +08:00
}
void MobileHotspotWidget::updateBandCombox()
{
2023-05-30 13:47:28 +08:00
QString tmp = m_freqBandComboBox->currentText();
2022-06-02 16:27:45 +08:00
m_freqBandComboBox->clear();
QDBusReply<QMap<QString, int> > capReply = m_interface->call("getWirelessDeviceCap");
if (!capReply.isValid()) {
qDebug()<<"execute dbus method 'getWirelessDeviceCap' is invalid in func initInterfaceInfo()" << capReply.error().message();
setWidgetHidden(true);
return;
}
m_isUserSelect = false;
2022-06-02 16:27:45 +08:00
QMap<QString, int> devCapMap = capReply.value();
if (devCapMap[m_interfaceName] & 0x02) {
m_freqBandComboBox->addItem("2.4GHz");
2022-06-02 16:27:45 +08:00
}
if (devCapMap[m_interfaceName] & 0x04) {
m_freqBandComboBox->addItem("5GHz");
2022-06-02 16:27:45 +08:00
}
2023-05-30 13:47:28 +08:00
int index = m_freqBandComboBox->findText(tmp);
if (index >= 0) {
m_freqBandComboBox->setCurrentIndex(index);
}
m_isUserSelect = true;
2022-06-02 16:27:45 +08:00
}
QFrame* MobileHotspotWidget::myLine()
{
QFrame *line = new QFrame(this);
line->setMinimumSize(QSize(LINE_MIN_SIZE));
line->setMaximumSize(QSize(LINE_MAX_SIZE));
line->setLineWidth(0);
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);
return line;
}
QString MobileHotspotWidget::getHostName()
{
int count = 0;
while (count < 3) {
QDBusInterface hostInterface("org.freedesktop.hostname1",
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
QDBusConnection::systemBus());
if (hostInterface.isValid()) {
return hostInterface.property("Hostname").value<QString>();
} else {
count++;
}
}
return "default";
}
QString MobileHotspotWidget::getSettingPathByUuid()
{
if (!m_interface->isValid()) {
return nullptr;
}
QDBusReply<QString> reply = m_interface->call("getApConnectionPath", m_uuid);
if (!reply.isValid()) {
return nullptr;
}
return reply.value();
}
QString MobileHotspotWidget::getActivePathByUuid()
{
if (!m_interface->isValid()) {
return nullptr;
}
QDBusReply<QString> reply = m_interface->call("getActiveConnectionPath", m_uuid);
if (!reply.isValid()) {
return nullptr;
}
return reply.value();
}
void MobileHotspotWidget::initNmDbus()
{
QString activePath = getActivePathByUuid();
QString settingPath = getSettingPathByUuid();
if (activePath != nullptr) {
m_activePathInterface = new QDBusInterface("org.freedesktop.NetworkManager",
activePath,
"org.freedesktop.NetworkManager.Connection.Active",
QDBusConnection::systemBus());
if (m_activePathInterface->isValid()) {
m_connectDevPage->setInterface(m_activePathInterface);
connect(m_activePathInterface, SIGNAL(NewStaConnected(bool, QString, QString)), m_connectDevPage, SLOT(onStaDevAdded(bool, QString, QString)), Qt::QueuedConnection);
connect(m_activePathInterface, SIGNAL(StaRemoved(bool, QString, QString)), m_connectDevPage, SLOT(onStaDevRemoved(bool, QString, QString)), Qt::QueuedConnection);
}
}
if (settingPath != nullptr) {
m_settingPathInterface = new QDBusInterface("org.freedesktop.NetworkManager",
settingPath,
"org.freedesktop.NetworkManager.Settings.Connection",
QDBusConnection::systemBus());
if (m_settingPathInterface->isValid()) {
m_blacklistPage->setInterface(m_settingPathInterface);
}
}
}
void MobileHotspotWidget::initActivePathInterface(QString path)
{
if (path != nullptr){
m_activePathInterface = new QDBusInterface("org.freedesktop.NetworkManager",
path,
"org.freedesktop.NetworkManager.Connection.Active",
QDBusConnection::systemBus());
if (m_activePathInterface->isValid()) {
m_connectDevPage->setInterface(m_activePathInterface);
connect(m_activePathInterface, SIGNAL(NewStaConnected(bool, QString, QString)), m_connectDevPage, SLOT(onStaDevAdded(bool, QString, QString)), Qt::QueuedConnection);
connect(m_activePathInterface, SIGNAL(StaRemoved(bool, QString, QString)), m_connectDevPage, SLOT(onStaDevRemoved(bool, QString, QString)), Qt::QueuedConnection);
}
}
}
void MobileHotspotWidget::deleteActivePathInterface()
{
if (m_activePathInterface != nullptr){
disconnect(m_activePathInterface);
delete m_activePathInterface;
m_activePathInterface = nullptr;
}
}
void MobileHotspotWidget::initSettingPathInterface(QString path)
{
if (path != nullptr){
m_settingPathInterface = new QDBusInterface("org.freedesktop.NetworkManager",
path,
"org.freedesktop.NetworkManager.Settings.Connection",
QDBusConnection::systemBus());
if (m_settingPathInterface->isValid()) {
m_blacklistPage->setInterface(m_settingPathInterface);
}
}
}
void MobileHotspotWidget::deleteSettingPathInterface()
{
if (m_settingPathInterface != nullptr){
disconnect(m_settingPathInterface);
delete m_settingPathInterface;
m_settingPathInterface = nullptr;
}
}
void MobileHotspotWidget::initConnectDevPage()
{
m_connectDevPage = new ConnectdevPage(this);
m_Vlayout->addSpacing(32);
m_Vlayout->addWidget(m_connectDevPage);
}
void MobileHotspotWidget::initBlackListPage()
{
m_blacklistPage = new BlacklistPage(this);
m_Vlayout->addSpacing(32);
m_Vlayout->addWidget(m_blacklistPage);
}