diff --git a/src/backend/dbus-interface/kylinactiveconnectresource.cpp b/src/backend/dbus-interface/kylinactiveconnectresource.cpp index 7f1c604d..df31ff31 100644 --- a/src/backend/dbus-interface/kylinactiveconnectresource.cpp +++ b/src/backend/dbus-interface/kylinactiveconnectresource.cpp @@ -56,6 +56,31 @@ KyConnectItem *KyActiveConnectResourse::getActiveConnectionItem(NetworkManager:: return activeConnectItem; } +KyConnectItem *KyActiveConnectResourse::getActiveConnectionByUuid(QString connectUuid) +{ + NetworkManager::ActiveConnection::Ptr activeConnectPtr = + m_networkResourceInstance->getActiveConnect(connectUuid); + + if (nullptr == activeConnectPtr) { + qWarning()<< "[KyActiveConnectResourse]" <<"it can not find connect "<< connectUuid; + return nullptr; + } + + KyConnectItem *activeConnectItem = getActiveConnectionItem(activeConnectPtr); + if (nullptr == activeConnectItem) { + return nullptr; + } + + QStringList interfaces = activeConnectPtr->devices(); + QString ifaceUni = interfaces.at(0); + NetworkManager::Device:: Ptr devicePtr = + m_networkResourceInstance->findDeviceUni(ifaceUni); + activeConnectItem->m_ifaceName = devicePtr->interfaceName(); + activeConnectItem->m_itemType = activeConnectPtr->type(); + + return activeConnectItem; +} + KyConnectItem *KyActiveConnectResourse::getActiveConnectionByUuid(QString connectUuid, QString deviceName) { diff --git a/src/backend/dbus-interface/kylinactiveconnectresource.h b/src/backend/dbus-interface/kylinactiveconnectresource.h index b5d02054..93225d1b 100644 --- a/src/backend/dbus-interface/kylinactiveconnectresource.h +++ b/src/backend/dbus-interface/kylinactiveconnectresource.h @@ -17,6 +17,7 @@ public: ~KyActiveConnectResourse(); public: + KyConnectItem *getActiveConnectionByUuid(QString connectUuid); KyConnectItem *getActiveConnectionByUuid(QString connectUuid, QString deviceName); void getActiveConnectionList(QString deviceName, NetworkManager::ConnectionSettings::ConnectionType connectionType, diff --git a/src/backend/dbus-interface/kylinconnectitem.cpp b/src/backend/dbus-interface/kylinconnectitem.cpp index 5bc7857b..011dee63 100644 --- a/src/backend/dbus-interface/kylinconnectitem.cpp +++ b/src/backend/dbus-interface/kylinconnectitem.cpp @@ -18,6 +18,11 @@ KyConnectItem::~KyConnectItem() } +void KyConnectItem::setConnectUuid(QString uuid) +{ + m_connectUuid = uuid; +} + void KyConnectItem::dumpInfo() { qDebug()<<"wired connection item info:"; diff --git a/src/backend/dbus-interface/kylinconnectitem.h b/src/backend/dbus-interface/kylinconnectitem.h index d696b03e..4cdaa1ae 100644 --- a/src/backend/dbus-interface/kylinconnectitem.h +++ b/src/backend/dbus-interface/kylinconnectitem.h @@ -14,6 +14,8 @@ public: public: void dumpInfo(); + void setConnectUuid(QString uuid); + public: QString m_connectName; QString m_connectUuid; diff --git a/src/backend/dbus-interface/kylinconnectresource.cpp b/src/backend/dbus-interface/kylinconnectresource.cpp index dfec1ea8..a3b380ed 100644 --- a/src/backend/dbus-interface/kylinconnectresource.cpp +++ b/src/backend/dbus-interface/kylinconnectresource.cpp @@ -47,6 +47,30 @@ KyConnectItem *KyConnectResourse::getConnectionItem(NetworkManager::Connection:: return connectionItem; } +KyConnectItem * KyConnectResourse::getConnectionItemByUuid(QString connectUuid) +{ + NetworkManager::Connection::Ptr connectPtr = + m_networkResourceInstance->getConnect(connectUuid); + + if (nullptr == connectPtr) { + qWarning()<< "[KyConnectResourse]" <<"get connect failed, connect uuid"<isActiveConnection(connectPtr->uuid())) { + qDebug()<<"[KyConnectResourse]"<name()<<"is active connection"; + return nullptr; + } + + KyConnectItem *connectItem = getConnectionItem(connectPtr); + if (nullptr != connectItem) { + connectItem->dumpInfo(); + return connectItem; + } + + return nullptr; +} + KyConnectItem * KyConnectResourse::getConnectionItemByUuid(QString connectUuid, QString deviceName) { NetworkManager::Connection::Ptr connectPtr = @@ -552,3 +576,53 @@ void KyConnectResourse::getApConnections(QList &apConnectItem return; } + + +bool KyConnectResourse::isWiredConnection(QString uuid) +{ + NetworkManager::Connection::Ptr connectPtr = + m_networkResourceInstance->getConnect(uuid); + + if (connectPtr->isValid()) { + NetworkManager::ConnectionSettings::Ptr connectSettingPtr = connectPtr->settings(); + + if (connectSettingPtr.isNull()) { + qWarning()<<"[KyConnectResourse]"<<"get connect setting failed, connect uuid"<settings()->connectionType()) { + return true; + } + } + + return false; +} + +bool KyConnectResourse::isWirelessConnection(QString uuid) +{ + NetworkManager::Connection::Ptr connectPtr = + m_networkResourceInstance->getConnect(uuid); + + if (connectPtr->isValid()) { + NetworkManager::ConnectionSettings::Ptr connectSettingPtr = connectPtr->settings(); + + if (connectSettingPtr.isNull()) { + qWarning()<<"[KyConnectResourse]"<<"get connect setting failed, connect uuid"<settings()->connectionType()) { + return true; + } + } + + return false; +} + +bool KyConnectResourse::isActivatedConnection(QString uuid) +{ + return m_networkResourceInstance->isActiveConnection(uuid); +} diff --git a/src/backend/dbus-interface/kylinconnectresource.h b/src/backend/dbus-interface/kylinconnectresource.h index 2634c502..58798bb0 100644 --- a/src/backend/dbus-interface/kylinconnectresource.h +++ b/src/backend/dbus-interface/kylinconnectresource.h @@ -18,6 +18,7 @@ public: ~KyConnectResourse(); public: + KyConnectItem *getConnectionItemByUuid(QString connectUuid); KyConnectItem *getConnectionItemByUuid(QString connectUuid, QString deviceName); void getConnectionList(QString deviceName, NetworkManager::ConnectionSettings::ConnectionType connectionType, @@ -30,6 +31,10 @@ public: bool getInterfaceByUuid(QString &deviceName, NetworkManager::ConnectionSettings::ConnectionType &type, const QString connUuid); void getConnectivity(NetworkManager::Connectivity &connectivity); + bool isWiredConnection(QString uuid); + bool isWirelessConnection(QString uuid); + bool isActivatedConnection(QString uuid); + private: KyConnectItem *getConnectionItem(NetworkManager::Connection::Ptr connectPtr); void getConnectIp(NetworkManager::ConnectionSettings::Ptr settingPtr, diff --git a/src/backend/dbus-interface/kylinnetworkresourcemanager.cpp b/src/backend/dbus-interface/kylinnetworkresourcemanager.cpp index 76d7a71e..b1156588 100644 --- a/src/backend/dbus-interface/kylinnetworkresourcemanager.cpp +++ b/src/backend/dbus-interface/kylinnetworkresourcemanager.cpp @@ -166,7 +166,7 @@ void KyNetworkResourceManager::addActiveConnection(NetworkManager::ActiveConnect connect(conn.data(), &NetworkManager::ActiveConnection::typeChanged, this, &KyNetworkResourceManager::onActiveConnectionUpdated); connect(conn.data(), &NetworkManager::ActiveConnection::masterChanged, this, &KyNetworkResourceManager::onActiveConnectionUpdated); connect(conn.data(), &NetworkManager::ActiveConnection::specificObjectChanged, this, &KyNetworkResourceManager::onActiveConnectionUpdated); - connect(conn.data(), &NetworkManager::ActiveConnection::stateChangedReason, this, &KyNetworkResourceManager::onActiveConnectionChangedReason); + //connect(conn.data(), &NetworkManager::ActiveConnection::stateChangedReason, this, &KyNetworkResourceManager::onActiveConnectionChangedReason); connect(conn.data(), &NetworkManager::ActiveConnection::stateChanged, this, &KyNetworkResourceManager::onActiveConnectionChanged); connect(conn.data(), &NetworkManager::ActiveConnection::vpnChanged, this, &KyNetworkResourceManager::onActiveConnectionUpdated); connect(conn.data(), &NetworkManager::ActiveConnection::uuidChanged, this, &KyNetworkResourceManager::onActiveConnectionUpdated); @@ -556,6 +556,10 @@ void KyNetworkResourceManager::onActiveConnectionChanged( if (activeConnect->isValid()) { qDebug()<<"!New state change activate connect"<uuid(); qDebug()<<"!New the active connect state"<state() != state) { + qDebug()<<"connect real state"<state() <<"change state"<uuid(), state, NetworkManager::ActiveConnection::Reason::UknownReason); } else { diff --git a/src/frontend/list-items/lanlistitem.cpp b/src/frontend/list-items/lanlistitem.cpp index b5548eed..209ea67d 100644 --- a/src/frontend/list-items/lanlistitem.cpp +++ b/src/frontend/list-items/lanlistitem.cpp @@ -93,9 +93,11 @@ void LanListItem::onInfoButtonClicked() void LanListItem::onLanStatusChange(QString uuid, NetworkManager::ActiveConnection::State state, NetworkManager::ActiveConnection::Reason reason) { - qDebug() <<"[LanListItem]Connection State Change to:" << state; + qDebug() <<"[LanListItem] Connection State Change to:" << state << uuid; + if (m_data->m_connectUuid == uuid) { - if (state == NetworkManager::ActiveConnection::State::Activating || state == NetworkManager::ActiveConnection::State::Deactivating) { + if (state == NetworkManager::ActiveConnection::State::Activating + || state == NetworkManager::ActiveConnection::State::Deactivating) { qDebug() << "[LanListItem]Activating!Loading!" << state; m_netButton->startLoading(); } else { diff --git a/src/frontend/list-items/listitem.cpp b/src/frontend/list-items/listitem.cpp index c60d1afe..021402c0 100644 --- a/src/frontend/list-items/listitem.cpp +++ b/src/frontend/list-items/listitem.cpp @@ -21,11 +21,16 @@ ListItem::ListItem(QWidget *parent) : QFrame(parent) ListItem::~ListItem() { - delete m_netButton; - m_netButton = NULL; + if (nullptr != m_netButton) { + delete m_netButton; + m_netButton = nullptr; + } + + if (nullptr != m_infoButton) { + delete m_infoButton; + m_infoButton = nullptr; + } - delete m_infoButton; - m_infoButton = NULL; } void ListItem::setName(const QString &name) diff --git a/src/frontend/tab-pages/lanpage.cpp b/src/frontend/tab-pages/lanpage.cpp index ae0773e6..f69419fd 100644 --- a/src/frontend/tab-pages/lanpage.cpp +++ b/src/frontend/tab-pages/lanpage.cpp @@ -7,41 +7,45 @@ #define TITLE_LAYOUT_MARGINS 24,0,24,0 #define LAN_LIST_SPACING 2 #define TEXT_MARGINS 16,0,0,0 -//#define SCROLL_AREA_HEIGHT 200 #define SETTINGS_LAYOUT_MARGINS 24,16,24,16 #define TRANSPARENT_COLOR QColor(0,0,0,0) #define SWITCH_WIDTH 48 #define SWITCH_HEIGHT 24 #define ITEM_HEIGHT 48 +const QString INVALID_CONNECT_UUID = "emptyconnect"; + const QString WIRED_SWITCH = "wiredswitch"; LanPage::LanPage(QWidget *parent) : TabPage(parent) { m_activeResourse = new KyActiveConnectResourse(this); m_connectResourse = new KyConnectResourse(this); - m_device = new KyNetworkDeviceResourse(this); + m_deviceResource = new KyNetworkDeviceResourse(this); + + m_devList.clear(); + m_deviceResource->getNetworkDeviceList(NetworkManager::Device::Type::Ethernet, m_devList); initDeviceState(); - initUI(); initNetSwitch(); initDeviceCombox(); - initList(m_deviceName); + initLanArea(); - connect(m_activeResourse, &KyActiveConnectResourse::stateChangeReason, this, &LanPage::updateLanlist); + connect(m_activeResourse, &KyActiveConnectResourse::stateChangeReason, this, &LanPage::onUpdateLanlist); - connect(m_connectResourse, &KyConnectResourse::connectionAdd, this, &LanPage::addConnectionSlot); - connect(m_connectResourse, &KyConnectResourse::connectionRemove, this, &LanPage::removeConnectionSlot); - connect(m_connectResourse, &KyConnectResourse::connectionUpdate, this, &LanPage::connectionUpdateSlot); + connect(m_connectResourse, &KyConnectResourse::connectionAdd, this, &LanPage::onAddConnection); + connect(m_connectResourse, &KyConnectResourse::connectionRemove, this, &LanPage::onRemoveConnection); + connect(m_connectResourse, &KyConnectResourse::connectionUpdate, this, &LanPage::onUpdateConnection); - connect(m_device, &KyNetworkDeviceResourse::deviceAdd, this, &LanPage::onDeviceAdd); - connect(m_device, &KyNetworkDeviceResourse::deviceRemove, this, &LanPage::onDeviceRemove); - connect(m_device, &KyNetworkDeviceResourse::deviceNameUpdate, this, &LanPage::onDeviceNameUpdate); + connect(m_deviceResource, &KyNetworkDeviceResourse::deviceAdd, this, &LanPage::onDeviceAdd); + connect(m_deviceResource, &KyNetworkDeviceResourse::deviceRemove, this, &LanPage::onDeviceRemove); + connect(m_deviceResource, &KyNetworkDeviceResourse::deviceNameUpdate, this, &LanPage::onDeviceNameUpdate); connect(m_wiredConnectOperation, &KyWiredConnectOperation::activateConnectionError, this, &LanPage::activateFailed); connect(m_wiredConnectOperation, &KyWiredConnectOperation::deactivateConnectionError, this, &LanPage::deactivateFailed); + } LanPage::~LanPage() @@ -70,7 +74,8 @@ void LanPage::initNetSwitch() if (QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA)) { m_switchGsettings = new QGSettings(GSETTINGS_SCHEMA); if (m_switchGsettings->keys().contains(WIRED_SWITCH)) { - m_netSwitch->setSwitchStatus(m_switchGsettings->get(WIRED_SWITCH).toBool()); + m_wiredSwitch = m_switchGsettings->get(WIRED_SWITCH).toBool(); + m_netSwitch->setSwitchStatus(m_wiredSwitch); connect(m_switchGsettings, &QGSettings::changed, this, &LanPage::onSwithGsettingsChanged); } connect(m_netSwitch, &SwitchButton::clicked, this, &LanPage::onLanSwitchClicked); @@ -84,30 +89,37 @@ void LanPage::onSwithGsettingsChanged(const QString &key) if (key == WIRED_SWITCH) { m_netSwitch->blockSignals(true); - qDebug()<<"[LanPage] SwitchButton statue changed to:" - << m_switchGsettings->get(WIRED_SWITCH).toBool(); + m_wiredSwitch = m_switchGsettings->get(WIRED_SWITCH).toBool(); + qDebug()<<"[LanPage] SwitchButton statue changed to:" << m_wiredSwitch; KyWiredConnectOperation wiredOperation; + if (m_wiredSwitch) { + QStringList disabledDevices; + disabledDevices.clear(); + getDisabledDevices(disabledDevices); - QStringList deviceList; - m_device->getNetworkDeviceList(NetworkManager::Device::Type::Ethernet, deviceList); - - bool isOn = m_switchGsettings->get(WIRED_SWITCH).toBool(); - if (isOn) { - for (int index = 0; index < deviceList.size(); ++index) { - qDebug()<<"[LanPage] open wired device "<< deviceList.at(index); - wiredOperation.openWiredNetworkWithDevice(deviceList.at(index)); - saveDeviceEnableState(deviceList.at(index), true); + for (int index = 0; index < disabledDevices.size(); ++index) { + qDebug()<<"[LanPage] open wired device "<< disabledDevices.at(index); + wiredOperation.openWiredNetworkWithDevice(disabledDevices.at(index)); + saveDeviceEnableState(disabledDevices.at(index), true); } } else { - for (int index = 0; index < deviceList.size(); ++index) { - qDebug()<<"[LanPage] close wired device "<< deviceList.at(index); - wiredOperation.closeWiredNetworkWithDevice(deviceList.at(index)); - saveDeviceEnableState(deviceList.at(index), false); + QStringList enableDevices; + enableDevices.clear(); + getEnabledDevice(enableDevices); + + for (int index = 0; index < enableDevices.size(); ++index) { + qDebug()<<"[LanPage] close wired device "<< enableDevices.at(index); + wiredOperation.closeWiredNetworkWithDevice(enableDevices.at(index)); + saveDeviceEnableState(enableDevices.at(index), false); } } + initDeviceCombox(); - m_netSwitch->setSwitchStatus(isOn); + initLanArea(); + + m_netSwitch->setSwitchStatus(m_wiredSwitch); + m_netSwitch->blockSignals(false); } } @@ -123,148 +135,339 @@ void LanPage::onLanSwitchClicked() } } -void LanPage::removeConnectionSlot(QString path) //删除时后端会自动断开激活,将其从未激活列表中删除 +void LanPage::getEnabledDevice(QStringList &enableDeviceList) { - //for dbus - qDebug() << "[LanPage] emit lanRemove because removeConnectionSlot " << path; - emit lanRemove(path); + int index = 0; + QMap deviceMap; - qDebug()<<"[LanPage] Removing a connection, path:"<::iterator iters; - for (iters = m_deactiveMap.begin(); iters != m_deactiveMap.end(); ++iters) { - KyConnectItem *item = iters.key(); - if (item->m_connectPath == path) { - qDebug()<<"[LanPage] Remove a connection from inactive list"; - m_inactivatedLanListWidget->removeItemWidget(iters.value()); - delete(iters.value()); - m_deactiveMap.erase(iters); - isLan = true; + if (m_devList.isEmpty()) { + qDebug()<<"[LanPage] there is not wired device."; + return; + } - break; + getDeviceEnableState(WIRED, deviceMap); + for (index = 0; index < m_devList.size(); ++index) { + if (deviceMap.contains(m_devList.at(index))) { + if (deviceMap[m_devList.at(index)]) { + enableDeviceList << m_devList.at(index); + } + } else { + saveDeviceEnableState(m_devList.at(index), true); + enableDeviceList << m_devList.at(index); } } + + return; } -void LanPage::addConnectionSlot(QString uuid) //新增一个有线连接,将其加入到激活列表 +void LanPage::getDisabledDevices(QStringList &disableDeviceList) { - QString devName; - NetworkManager::ConnectionSettings::ConnectionType type; - if(m_connectResourse->getInterfaceByUuid(devName, type, uuid)) { - if (type != NetworkManager::ConnectionSettings::ConnectionType::Wired) { - qDebug() << "[LanPage] addConnectionSlot but type is not Wired"; - return; - } - } else { - qDebug() << "[LanPage] addConnectionSlot but uuid is invalid"; + int index = 0; + QMap deviceMap; + + if (m_devList.isEmpty()) { + qDebug()<<"[LanPage] there is not wired device."; + return; } - //for dbus - KyConnectItem *item = nullptr; - item = m_connectResourse->getConnectionItemByUuid(uuid, devName); - if (nullptr != item) { - QStringList info; - info << item->m_connectName << uuid << item->m_connectPath; - qDebug() << "[LanPage] emit lanAdd because addConnection " << devName; - emit lanAdd(devName, info); + getDeviceEnableState(WIRED, deviceMap); + for (index = 0; index < m_devList.size(); ++index) { + if (deviceMap.contains(m_devList.at(index))) { + if (!deviceMap[m_devList.at(index)]) { + disableDeviceList << m_devList.at(index); + } + } } - KyConnectItem * newItem = m_connectResourse->getConnectionItemByUuid(uuid, m_deviceName); - if (newItem != nullptr) { - if (newItem->m_itemType == NetworkManager::ConnectionSettings::ConnectionType::Wired) { - qDebug()<<"[LanPage] Add a new connection, name:"<m_connectName; - addNewItem(newItem, m_inactivatedLanListWidget); - m_deactiveMap.insert(newItem, m_listWidgetItem); - } - } else { - qDebug()<<"[LanPage] GetConnectionItemByUuid is empty when add a new!"; - } + return; } void LanPage::initDeviceCombox() { //TODO 获取设备列表,单设备时隐藏下拉框,多设备时添加到下拉框;m_devList记录插入的所有设备,deviceMap记录设备状态 - QMap deviceMap; - getDeviceEnableState(0,deviceMap); - m_deviceComboBox->clear(); - m_devList.clear(); - enableDevice.clear(); - if (!m_switchGsettings) { - qDebug() << "[LanPage]:m_switchGsettings is null" << Q_FUNC_INFO << __LINE__; - return; - } + if (m_wiredSwitch) { + QStringList enableDevices; + enableDevices.clear(); + getEnabledDevice(enableDevices); - m_deviceComboBox->clear(); - m_devList.clear(); - enableDevice.clear(); - - getDeviceEnableState(WIRED, deviceMap); - - bool isOn = m_switchGsettings->get(WIRED_SWITCH).toBool(); - if (!isOn) { - m_deviceFrame->hide(); - m_activatedNetFrame->hide(); - m_inactivatedNetFrame->hide(); - m_activatedNetDivider->hide(); - m_inactivatedNetDivider->hide(); - m_deviceName = ""; - } else { - m_device->getNetworkDeviceList(NetworkManager::Device::Type::Ethernet, m_devList); - for (int i=0; i 1) { + for (int index = 0; index < enableDeviceCount; ++index) { + m_deviceComboBox->addItem(enableDevices.at(index)); } - } - qDebug() << "[LanPage]device num:" << enableDevice.count(); - if (enableDevice.count() == 0) { - m_deviceFrame->show(); - m_deviceComboBox->hide(); - m_tipsLabel->show(); - m_activatedNetFrame->hide(); - m_inactivatedNetFrame->hide(); - m_activatedNetDivider->hide(); - m_inactivatedNetDivider->hide(); - m_deviceName = ""; - return; - } - - m_activatedNetFrame->show(); - m_inactivatedNetFrame->show(); - m_activatedNetDivider->show(); - m_inactivatedNetDivider->show(); - - if (enableDevice.count() == 1) { - m_deviceFrame->hide(); - m_deviceName = enableDevice.at(0); - qDebug() << "[LanPage]Current device:" << m_deviceName; - initList(m_deviceName); - return; - } else { m_deviceFrame->show(); m_tipsLabel->hide(); m_deviceComboBox->show(); - for (int j=0; jaddItem(enableDevice.at(j)); - } + m_currentDeviceName = m_deviceComboBox->currentText(); + } else if (enableDeviceCount == 1) { + m_deviceFrame->hide(); + m_currentDeviceName = enableDevices.at(0); + } else { + m_deviceFrame->show(); + m_deviceComboBox->hide(); + m_tipsLabel->show(); + m_currentDeviceName = ""; + } + } else { + m_deviceFrame->hide(); + m_currentDeviceName = ""; + } - qDebug() << "[LanPage]Current device:" << m_deviceComboBox->currentText(); - m_deviceName = m_deviceComboBox->currentText(); - initList(m_deviceName); + return; +} + +bool LanPage::connectionItemIsExist(QMap &connectMap, + QString uuid) +{ + QMap::iterator iter; + for (iter = connectMap.begin(); iter != connectMap.end(); ++iter) { //检查其是否已经在未激活列表中 + KyConnectItem *p_item = iter.key(); + if (p_item->m_connectUuid == uuid) { + return true; } } + return false; +} + +void LanPage::addEmptyConnectItem(QMap &connectMap, + QListWidget *lanListWidget) +{ + KyConnectItem *p_nullItem = new KyConnectItem(); + p_nullItem->setConnectUuid(INVALID_CONNECT_UUID); + QListWidgetItem *p_listWidgetItem = addNewItem(nullptr, lanListWidget); + connectMap.insert(p_nullItem, p_listWidgetItem); + + return; +} + + +void LanPage::deleteConnectionMapItem(QMap &connectMap, + QListWidget *lanListWidget, QString uuid) +{ + QMap::iterator iter; + + iter = connectMap.begin(); + while (iter != connectMap.end()) { + KyConnectItem *p_item = iter.key(); + if (p_item->m_connectUuid == uuid + || INVALID_CONNECT_UUID == p_item->m_connectUuid) { + qDebug()<<"[LanPage] delete connection map item" + << p_item->m_connectName << p_item->m_connectUuid; + QListWidgetItem *p_widgetItem = iter.value(); + LanListItem *p_lanItem = (LanListItem *)lanListWidget->itemWidget(p_widgetItem); + + lanListWidget->removeItemWidget(p_widgetItem); + + delete p_lanItem; + p_lanItem = nullptr; + + delete p_widgetItem; + p_widgetItem = nullptr; + + iter = connectMap.erase(iter); + + delete p_item; + p_item = nullptr; + + continue; + } + + iter++; + } + + return; +} + +void LanPage::clearConnectionMap(QMap &connectMap, + QListWidget *lanListWidget) +{ + QMap::iterator iter; + + iter = connectMap.begin(); + while (iter != connectMap.end()) { + KyConnectItem *p_connectItem = iter.key(); + QListWidgetItem *p_widgetItem = iter.value(); + + qDebug()<<"[LanPage] clear connection map item"<m_connectName; + + LanListItem *p_lanItem = (LanListItem *)lanListWidget->itemWidget(p_widgetItem); + lanListWidget->removeItemWidget(p_widgetItem); + + delete p_lanItem; + p_lanItem = nullptr; + + delete p_widgetItem; + p_widgetItem = nullptr; + + delete p_connectItem; + p_connectItem = nullptr; + + iter = connectMap.erase(iter); + } + + return; +} + +void LanPage::constructActiveConnectionArea() +{ + QList activedList; + + activedList.clear(); + clearConnectionMap(m_activeMap, m_activatedLanListWidget); + + m_activeResourse->getActiveConnectionList(m_currentDeviceName, + NetworkManager::ConnectionSettings::Wired, activedList); //激活列表的显示 + qDebug() << "[LanPage] construct active connection area get Active list size:" << activedList.size(); + if (!activedList.isEmpty()) { + for (int index = 0; index < activedList.size(); index++) { + KyConnectItem *activeItemData = activedList.at(index); + qDebug()<<"[LanPage] construct active connection area add active item"<m_connectName; + QListWidgetItem *p_listWidgetItem = addNewItem(activeItemData, m_activatedLanListWidget); + m_activeMap.insert(activeItemData, p_listWidgetItem); + } + } else { + qDebug()<<"[LanPage] there is not active"; + + addEmptyConnectItem(m_activeMap, m_activatedLanListWidget); + } + + return; +} + +void LanPage::constructConnectionArea() +{ + QList deactivedList; + + deactivedList.clear(); + clearConnectionMap(m_deactiveMap, m_inactivatedLanListWidget); + + m_connectResourse->getConnectionList(m_currentDeviceName, NetworkManager::ConnectionSettings::Wired, deactivedList); //未激活列表的显示 + qDebug() << "[LanPage]construct connection area get connection list size:" << deactivedList.size(); + if (!deactivedList.isEmpty()) { + for (int index = 0; index < deactivedList.size(); index++) { + KyConnectItem *deactiveItemData = deactivedList.at(index); + qDebug()<<"[LanPage] construct connection area add deactive item"<m_connectName; + QListWidgetItem *p_listWidgetItem = addNewItem(deactiveItemData, m_inactivatedLanListWidget); + m_deactiveMap.insert(deactiveItemData, p_listWidgetItem); + } + } + + return; +} + +void LanPage::initLanArea() +{ + if (!m_wiredSwitch || m_currentDeviceName.isEmpty()) { + m_activatedNetFrame->hide(); + m_inactivatedNetFrame->hide(); + m_activatedNetDivider->hide(); + m_inactivatedNetDivider->hide(); + } else { + m_activatedNetFrame->show(); + m_inactivatedNetFrame->show(); + m_activatedNetDivider->show(); + m_inactivatedNetDivider->show(); + constructActiveConnectionArea(); + constructConnectionArea(); + } + + return; +} + +void LanPage::onRemoveConnection(QString path) //删除时后端会自动断开激活,将其从未激活列表中删除 +{ + //for dbus + qDebug() << "[LanPage] emit lanRemove because onRemoveConnection " << path; + emit lanRemove(path); + + QEventLoop loop; + QTimer::singleShot(200, &loop, SLOT(quit())); + loop.exec(); + + QMap::iterator iters; + for (iters = m_deactiveMap.begin(); iters != m_deactiveMap.end(); ++iters) { + KyConnectItem *p_item = iters.key(); + if (p_item->m_connectPath == path) { + qDebug()<<"[LanPage] Remove a connection from inactive list"; + + LanListItem *p_lanItem = (LanListItem *)m_inactivatedLanListWidget->itemWidget(iters.value()); + + m_inactivatedLanListWidget->removeItemWidget(iters.value()); + + delete p_lanItem; + p_lanItem = nullptr; + + delete(iters.value()); + m_deactiveMap.erase(iters); + + delete p_item; + p_item = nullptr; + + break; + } + } + + return; +} + +void LanPage::onAddConnection(QString uuid) //新增一个有线连接,将其加入到激活列表 +{ + if (!m_connectResourse->isWiredConnection(uuid)) { + return; + } + + KyConnectItem *p_newItem = nullptr; + p_newItem = m_connectResourse->getConnectionItemByUuid(uuid); + if (nullptr != p_newItem) { + sendLanAddSignal(p_newItem); + } + + if (p_newItem->m_ifaceName == m_currentDeviceName || p_newItem->m_ifaceName == "") { + qDebug()<<"[LanPage] Add a new connection, name:"<m_connectName; + QListWidgetItem *p_listWidgetItem = addNewItem(p_newItem, m_inactivatedLanListWidget); + m_deactiveMap.insert(p_newItem, p_listWidgetItem); + } else { + delete p_newItem; + p_newItem = nullptr; + } + + return; +} + +void LanPage::addDeviceForCombox(QString deviceName) +{ + disconnect(m_deviceComboBox, QOverload::of(&QComboBox::currentIndexChanged), this, &LanPage::onDeviceComboxIndexChanged); + if (m_wiredSwitch) { + saveDeviceEnableState(deviceName, true); + if (m_currentDeviceName.isEmpty()) { + //1、从无到有添加第一块有线网卡 + //2、有多快网卡,但是没有使能 + m_deviceFrame->hide(); + m_currentDeviceName = deviceName; + } else if (m_deviceComboBox->count() == 0) { + //3、现在有且只有一块网卡,并已使能 + //4、有多快网卡,且使能了其中一块 + m_deviceComboBox->addItem(m_currentDeviceName); + m_deviceComboBox->addItem(deviceName); + + m_deviceFrame->show(); + m_tipsLabel->hide(); + m_deviceComboBox->show(); + } else if (m_deviceComboBox->count() > 0) { + //5、有多快网卡且使能了多块网卡 + m_deviceComboBox->addItem(deviceName); + } + } else { + saveDeviceEnableState(deviceName, false); + } + + connect(m_deviceComboBox, QOverload::of(&QComboBox::currentIndexChanged), + this, &LanPage::onDeviceComboxIndexChanged); return; } @@ -274,66 +477,131 @@ void LanPage::onDeviceAdd(QString deviceName, NetworkManager::Device::Type devic return; } + if (m_devList.contains(deviceName)) { + return; + } + qDebug() << "[LanPage] Begin add device:" << deviceName; m_devList << deviceName; - - if (getDefaultDevice().isEmpty()) { - updateDefaultDevice(deviceName); - setDefaultDevice(WIRED, deviceName); + addDeviceForCombox(deviceName); + if (m_currentDeviceName == deviceName) { + initLanArea(); } + emit deviceStatusChanged(); - initDeviceCombox(); + + return; +} + +void LanPage::deleteDeviceFromCombox(QString deviceName) +{ + disconnect(m_deviceComboBox, QOverload::of(&QComboBox::currentIndexChanged), + this, &LanPage::onDeviceComboxIndexChanged); + + if (m_wiredSwitch) { + if (m_currentDeviceName.isEmpty()) { + //1、没有使能任何网卡 + goto l_out; + } else if (m_deviceComboBox->count() == 0) { + //2、使能了一个网卡,且当前网卡是要删除的网卡 + if (m_currentDeviceName == deviceName) { + m_deviceFrame->show(); + m_deviceComboBox->hide(); + m_tipsLabel->show(); + m_currentDeviceName = ""; + } + } else if (m_deviceComboBox->count() == 2) { + //3、使能了两个网卡,且包括要删除的网卡,有可能是要删除的网卡 + if (m_deviceComboBox->findText(deviceName) != -1) { + for (int index = m_deviceComboBox->count() - 1; index >= 0; index--) { + if (m_currentDeviceName == deviceName + && m_deviceComboBox->itemText(index) != deviceName) { + m_currentDeviceName = m_deviceComboBox->itemText(index); + } + m_deviceComboBox->removeItem(index); + } + + m_tipsLabel->hide(); + m_deviceFrame->hide(); + m_deviceComboBox->hide(); + } + } else { + //4、使能了多个网卡,且包括要删除的网卡,有可能是正要删除的网卡 + int index = m_deviceComboBox->findText(deviceName); + if (index != -1) { + m_deviceComboBox->removeItem(index); + if (m_currentDeviceName == deviceName) { + m_currentDeviceName = m_deviceComboBox->currentText(); + } + } + } + } + +l_out: + connect(m_deviceComboBox, QOverload::of(&QComboBox::currentIndexChanged), + this, &LanPage::onDeviceComboxIndexChanged); + + deleteDeviceEnableState(deviceName); + + return; } void LanPage::onDeviceRemove(QString deviceName) { - qDebug() << "[LanPage] deviceRemove:" << deviceName; - if (getDefaultDevice() == deviceName) { - QStringList list; - QString newDefaultDevice = ""; - list.empty(); - m_device->getNetworkDeviceList(NetworkManager::Device::Type::Ethernet, list); - if (!list.isEmpty()) { - newDefaultDevice = list.at(0); - } - updateDefaultDevice(newDefaultDevice); - setDefaultDevice(WIRED, newDefaultDevice); + if (!m_devList.contains(deviceName)) { + return; } - if (m_devList.contains(deviceName)) { - m_devList.removeOne(deviceName); + qDebug() << "[LanPage] deviceRemove:" << deviceName; + + QString nowDevice = m_currentDeviceName; + + m_devList.removeOne(deviceName); + deleteDeviceFromCombox(deviceName); + + if (nowDevice == deviceName) { + initLanArea(); } emit deviceStatusChanged(); - initDeviceCombox(); + + return; +} + +void LanPage::updateDeviceCombox(QString oldDeviceName, QString newDeviceName) +{ + if (m_currentDeviceName == oldDeviceName) { + m_currentDeviceName = newDeviceName; + } + + int index = m_deviceComboBox->findText(oldDeviceName); + if (index != -1) { + m_deviceComboBox->setItemText(index, newDeviceName); + } + + return; } void LanPage::onDeviceNameUpdate(QString oldName, QString newName) { - if (getDefaultDevice() == oldName) { - updateDefaultDevice(newName); - setDefaultDevice(WIRED, newName); - } + if (m_devList.contains(oldName)) { + m_devList.removeOne(oldName); + m_devList.append(newName); + qDebug() << "[LanPage] emit deviceNameUpdate " << oldName << newName; - if (m_devList.contains(oldName)) { - m_devList.removeOne(oldName); - m_devList.append(newName); - qDebug() << "[LanPage] emit deviceNameUpdate " << oldName << newName; + updateDeviceCombox(oldName, newName); - emit deviceNameChanged(oldName, newName); - initDeviceCombox(); - } + emit deviceNameChanged(oldName, newName); + } } void LanPage::onDeviceComboxIndexChanged(int currentIndex) { //TODO 设备变更时更新设备和列表 - if (enableDevice.count() == m_deviceComboBox->count()) { - m_deviceName = m_deviceComboBox->currentText(); - qDebug() << "[LanPage]Current Device Changed to:" << m_deviceName; - initList(m_deviceName); - } + m_currentDeviceName = m_deviceComboBox->currentText(); + qDebug() << "[LanPage]Current Device Changed to:" << m_currentDeviceName; + initLanArea(); } void LanPage::initUI() @@ -370,206 +638,127 @@ void LanPage::initUI() // emit this->lanConnectChanged(); } -void LanPage::addNewItem(KyConnectItem *itemData, QListWidget *listWidget) +QListWidgetItem *LanPage::addNewItem(KyConnectItem *itemData, QListWidget *listWidget) { - m_listWidgetItem = new QListWidgetItem(listWidget); - m_listWidgetItem->setSizeHint(QSize(listWidget->width(),ITEM_HEIGHT)); - listWidget->addItem(m_listWidgetItem); + QListWidgetItem *p_listWidgetItem = new QListWidgetItem(listWidget); + p_listWidgetItem->setSizeHint(QSize(listWidget->width(),ITEM_HEIGHT)); + listWidget->addItem(p_listWidgetItem); LanListItem *p_lanItem = nullptr; if (itemData != nullptr) { - p_lanItem = new LanListItem(itemData, m_deviceName); - qDebug() << "[LanPage] addNewItem, connection: " << itemData->m_connectName << "deviceName: " << m_deviceName; + p_lanItem = new LanListItem(itemData, m_currentDeviceName); + qDebug() << "[LanPage] addNewItem, connection: " << itemData->m_connectName << "deviceName: " << m_currentDeviceName; } else { p_lanItem = new LanListItem(); qDebug() << "[LanPage] Add nullItem!"; } - listWidget->setItemWidget(m_listWidgetItem, p_lanItem); + + listWidget->setItemWidget(p_listWidgetItem, p_lanItem); + + return p_listWidgetItem; } -void LanPage::initList(QString m_deviceName) //程序拉起,初始化显示 +void LanPage::updateActivatedConnectionArea(QString uuid) { - m_activatedLanListWidget->clear(); - m_inactivatedLanListWidget->clear(); - m_activeMap.clear(); - m_deactiveMap.clear(); - m_activedList.clear(); - m_deactivedList.clear(); + KyConnectItem *p_newItem = nullptr; + p_newItem = m_activeResourse->getActiveConnectionByUuid(uuid); + if (nullptr == p_newItem) { + qWarning()<<"[LanPage] get active connection failed, connection uuid" << uuid; + return; + } - m_activeResourse->getActiveConnectionList(m_deviceName, - NetworkManager::ConnectionSettings::Wired, m_activedList); //激活列表的显示 - qDebug() << "[LanPage]init list! Active list:" << m_activedList.size() << "Deactive list:" << m_deactivedList.size(); - if (!m_activedList.isEmpty()) { - for (int i = 0; i < m_activedList.size(); i++) { - KyConnectItem *activeItemData = m_activedList.at(i); - addNewItem(activeItemData, m_activatedLanListWidget); - emit this->lanConnectChanged(); + //发送通知给控制面板 + emit lanActiveConnectionStateChanged(p_newItem->m_ifaceName, uuid, p_newItem->m_connectState); - m_activeMap.insert(activeItemData, m_listWidgetItem); + if (p_newItem->m_ifaceName == m_currentDeviceName) { + qDebug()<<"[LanPage] update active connection area delete connection item " + << p_newItem->m_connectName; + deleteConnectionMapItem(m_deactiveMap, m_inactivatedLanListWidget, uuid); + + deleteConnectionMapItem(m_activeMap, m_activatedLanListWidget, INVALID_CONNECT_UUID); + if (connectionItemIsExist(m_activeMap, uuid)) { + delete p_newItem; + p_newItem = nullptr; + return; } + + qDebug()<<"[LanPage]update active connection item"<m_connectName; + QListWidgetItem *p_listWidgetItem = addNewItem(p_newItem, m_activatedLanListWidget); + m_activeMap.insert(p_newItem, p_listWidgetItem); + } else { - m_nullItem = new QListWidgetItem(m_activatedLanListWidget); - m_nullItem->setSizeHint(QSize(m_activatedLanListWidget->width(),ITEM_HEIGHT)); - m_activatedLanListWidget->addItem(m_nullItem); - - m_nullLanItem = new LanListItem(); - m_activatedLanListWidget->setItemWidget(m_nullItem, m_nullLanItem); + //释放内存 + delete p_newItem; + p_newItem = nullptr; } - m_connectResourse->getConnectionList(m_deviceName, NetworkManager::ConnectionSettings::Wired, m_deactivedList); //未激活列表的显示 - if (!m_deactivedList.isEmpty()) { - for (int i = 0; i < m_deactivedList.size(); i++) { - KyConnectItem *deactiveItemData = m_deactivedList.at(i); - addNewItem(deactiveItemData, m_inactivatedLanListWidget); - - m_deactiveMap.insert(deactiveItemData, m_listWidgetItem); - } - } - - bool isCarriered = true; - if (!m_device->wiredDeviceCarriered(m_deviceName)) { - isCarriered = false; - } - - m_activeResourse->getActiveConnectionList(m_deviceName, NetworkManager::ConnectionSettings::Wired, m_activedList); //激活列表的显示 - qDebug() << "[LanPage]init list! Active list:" << m_activedList.size() << "Deactive list:" << m_deactivedList.size(); - if (!m_activedList.isEmpty()) { - for (int i = 0; i < m_activedList.size(); i++) { - KyConnectItem *activeItemData = m_activedList.at(i); - if (isCarriered) { - addNewItem(activeItemData, m_activatedLanListWidget); - m_activeMap.insert(activeItemData, m_listWidgetItem); - } else { - addNewItem(activeItemData, m_inactivatedLanListWidget); - m_deactiveMap.insert(activeItemData, m_listWidgetItem); - } - } - } else { - addNewItem(nullptr, m_activatedLanListWidget); //显示一个未激活任何连接的item - } - - + return; } -void LanPage::updateLanlist(QString uuid, NetworkManager::ActiveConnection::State state, NetworkManager::ActiveConnection::Reason reason) +void LanPage::updateConnectionArea(QString uuid) +{ + KyConnectItem *p_newItem = nullptr; + p_newItem = m_connectResourse->getConnectionItemByUuid(uuid); + if (nullptr == p_newItem) { + qWarning()<<"[LanPage] get active connection failed, connection uuid" << uuid; + return; + } + + emit lanActiveConnectionStateChanged(p_newItem->m_ifaceName, uuid, p_newItem->m_connectState); + + if (p_newItem->m_ifaceName == m_currentDeviceName || p_newItem->m_ifaceName == "") { + qDebug()<<"[LanPage] update connection area"<m_connectName; + + deleteConnectionMapItem(m_activeMap, m_activatedLanListWidget, uuid); + if (m_activeMap.count() <= 0) { + addEmptyConnectItem(m_activeMap, m_activatedLanListWidget); + } + + if (connectionItemIsExist(m_deactiveMap, uuid)) { + delete p_newItem; + p_newItem = nullptr; + return; + } + + qDebug()<<"[LanPage] update connection item"<m_connectName; + QListWidgetItem *p_listWidgetItem = addNewItem(p_newItem, m_inactivatedLanListWidget); + m_deactiveMap.insert(p_newItem, p_listWidgetItem); + + emit this->lanConnectChanged(); + } else { + delete p_newItem; + p_newItem = nullptr; + } + + return; +} + +void LanPage::onUpdateLanlist(QString uuid, + NetworkManager::ActiveConnection::State state, + NetworkManager::ActiveConnection::Reason reason) { //lanpage函数内持续监听连接状态的变化并记录供其他函数调用获取状态 - QString devName; - NetworkManager::ConnectionSettings::ConnectionType type; - if (m_connectResourse->getInterfaceByUuid(devName, type, uuid)) { - if (type != NetworkManager::ConnectionSettings::ConnectionType::Wired) { - return; - } - } - if (NetworkManager::ActiveConnection::State::Activated == state){ - m_isLanConnected = true; - qDebug() << "[lanpage]lanIsConnected status : " << m_isLanConnected << Q_FUNC_INFO << __LINE__ ; - } else { - m_isLanConnected = false; - qDebug() << "=[lanpage]lanIsConnected status : " << m_isLanConnected << Q_FUNC_INFO << __LINE__ ; - } - qDebug()<<"[LanPage] State change slot:"<getInterfaceByUuid(devName, type, uuid)) { - if (type != NetworkManager::ConnectionSettings::ConnectionType::Wired) { - qDebug() << "[LanPage] updateLanlist but type is not Wired"; - return; - } - } else { - qDebug() << "[LanPage] updateLanlist but uuid is invalid"; + if (!m_connectResourse->isWiredConnection(uuid)) { + return; } - QStringList devNameList; - if (m_activeResourse->isActiveConnection(uuid, devNameList)) { - for (int i = 0; i < devNameList.size(); ++i) { - KyConnectItem *item = nullptr; - item = m_activeResourse->getActiveConnectionByUuid(uuid, devNameList.at(i)); - if (nullptr != item) { - QStringList info; - info << item->m_connectName << uuid << item->m_connectPath; - emit lanActiveConnectionStateChanged(devNameList.at(i), uuid, state); - } - } - } else { - qDebug() << "emit lanActiveConnectionStateChanged" << devName << uuid << state; - emit lanActiveConnectionStateChanged(devName, uuid, state); + qDebug()<<"[LanPage] connection uuid"<< uuid + << "state change slot:"<< state; + + if (state == NetworkManager::ActiveConnection::State::Activated) { + updateActivatedConnectionArea(uuid); + } else if (state == NetworkManager::ActiveConnection::State::Deactivated) { + updateConnectionArea(uuid); } - qDebug() << "[LanPage] Update Device Name:" << m_deviceName; - - if (state == NetworkManager::ActiveConnection::State::Deactivated) { - qDebug()<<"Get a deactivate, begin to remove it from activeList"; - QMap::iterator i; - for (i = m_activeMap.begin(); i != m_activeMap.constEnd(); ++i) { //有新断开,若在激活列表里则删掉 - KyConnectItem *m_item = i.key(); - if (m_item->m_connectUuid == uuid) { - m_activatedLanListWidget->removeItemWidget(i.value()); - delete(i.value()); - emit this->lanConnectChanged(); - break; - } - } - - bool hasDeactiveNet = false; - QMap::iterator iter; - for (iter = m_deactiveMap.begin(); iter != m_deactiveMap.end(); ++iter) { //检查其是否已经在未激活列表中 - KyConnectItem *m_item = iter.key(); - if (m_item->m_connectUuid == uuid) { - hasDeactiveNet = true; - break; - } - } - qDebug()<<"The deactive has been in DeactiveNet:"<::iterator iters; - for (iters = m_activeMap.begin(); iters != m_activeMap.end(); ++iters) { //在未激活列表中增加一项连接 - KyConnectItem *m_item = iters.key(); - if (m_item->m_connectUuid == uuid && !hasDeactiveNet) { - qDebug()<<"Add a deactive connection to inactiveList:"<m_connectName; - m_item->m_connectState = state; - addNewItem(m_item, m_inactivatedLanListWidget); - - m_deactiveMap.insert(m_item, m_listWidgetItem); - m_activeMap.erase(iters); - break; - } - } - } else if (state == NetworkManager::ActiveConnection::State::Activated) { - qDebug()<<"Get an actived connection, begin to move it from deactive to avtive!"; - QMap::iterator iter; //在未激活列表里删除 - for (iter = m_deactiveMap.begin(); iter != m_deactiveMap.constEnd(); ++iter) { - KyConnectItem *m_item = iter.key(); - if (m_item->m_connectUuid == uuid) { - m_activatedLanListWidget->clear(); - qDebug()<<"Add an active connection to activeList:"<m_connectName; - m_item->m_connectState = state; - addNewItem(m_item, m_activatedLanListWidget); - - m_inactivatedLanListWidget->removeItemWidget(iter.value()); - delete(iter.value()); - - m_activeMap.insert(m_item, m_listWidgetItem); - m_deactiveMap.erase(iter); - break; - } - } - } else { - - } - - if (m_activeMap.count() <= 0) { - m_activatedLanListWidget->clear(); - addNewItem(nullptr, m_activatedLanListWidget); //显示一个未激活任何连接的item - } + return; } void LanPage::getWiredList(QMap > &map) { QStringList devlist; - m_device->getNetworkDeviceList(NetworkManager::Device::Type::Ethernet, devlist); + m_deviceResource->getNetworkDeviceList(NetworkManager::Device::Type::Ethernet, devlist); if (devlist.isEmpty()) { return; } @@ -596,105 +785,157 @@ void LanPage::getWiredList(QMap > &map) return; } -void LanPage::connectionUpdateSlot(QString uuid) +void LanPage::sendLanUpdateSignal(KyConnectItem *p_connectItem) { - QStringList devNameList; - bool isCurrentDevice = false; - KyConnectItem *item = nullptr; + QStringList info; + info << p_connectItem->m_connectName << p_connectItem->m_connectUuid << p_connectItem->m_connectPath; + emit lanUpdate(p_connectItem->m_ifaceName, info); - qDebug() << "[LanPage]:Connection Changed !" << Q_FUNC_INFO << __LINE__; + return; +} - if (m_activeResourse->isActiveConnection(uuid, devNameList)) { //从activeResourse里找 - for (int i = 0; i < devNameList.size(); ++i) { - item = m_activeResourse->getActiveConnectionByUuid(uuid, devNameList.at(i)); - if (nullptr != item) { - if (item->m_itemType != NetworkManager::ConnectionSettings::ConnectionType::Wired) { - return; - } - if (devNameList.at(i) == m_deviceName) { - qDebug() << "[LanPage]:Connection's device:" << devNameList.at(i) << Q_FUNC_INFO << __LINE__; - isCurrentDevice = true; - } - //for dbus - QStringList info; - info << item->m_connectName << uuid << item->m_connectPath; - emit lanUpdate(devNameList.at(i), info); - } - } - } else { - QString devName; - NetworkManager::ConnectionSettings::ConnectionType type; - if (!m_connectResourse->getInterfaceByUuid(devName, type, uuid)) { //从connectResourse里找 - return; - } - if (type != NetworkManager::ConnectionSettings::ConnectionType::Wired) { - return; - } - item = m_connectResourse->getConnectionItemByUuid(uuid, devName); - if (nullptr != item) { - if (devName == m_deviceName) { - qDebug() << "[LanPage]:Connection's device:" << devName << Q_FUNC_INFO << __LINE__; - isCurrentDevice = true; - } - //for dbus - QStringList info; - info << item->m_connectName << uuid << item->m_connectPath; - emit lanUpdate(devName, info); - } - } - qDebug() << "[LanPage]:Connection belong to current device:" << isCurrentDevice; +void LanPage::sendLanAddSignal(KyConnectItem *p_connectItem) +{ + QStringList info; + info << p_connectItem->m_connectName << p_connectItem->m_connectUuid << p_connectItem->m_connectPath; + qDebug() << "[LanPage] emit lanAdd because addConnection "; + emit lanAdd(p_connectItem->m_ifaceName, info); + + return; +} + +void LanPage::updateConnectionProperty(KyConnectItem *p_connectItem) +{ QMap::iterator iter; for (iter = m_deactiveMap.begin(); iter != m_deactiveMap.constEnd(); ++iter) { - KyConnectItem *m_item = iter.key(); - if (m_item->m_connectUuid == uuid) { - if (!isCurrentDevice) { //当前未激活连接的设备改变 + KyConnectItem *p_item = iter.key(); + if (p_item->m_connectUuid == p_connectItem->m_connectUuid) { + if (p_connectItem->m_ifaceName != "" + && m_currentDeviceName != p_connectItem->m_ifaceName) { //当前未激活连接的设备改变 + LanListItem *p_lanItem = (LanListItem *)m_inactivatedLanListWidget->itemWidget(iter.value()); + m_inactivatedLanListWidget->removeItemWidget(iter.value()); + + delete p_lanItem; + p_lanItem = nullptr; + delete iter.value(); m_deactiveMap.erase(iter); - qDebug() << "[LanPage]:Deactive Connection's device change, remove it" << Q_FUNC_INFO << __LINE__; - return; + + delete p_item; + p_item = nullptr; } else { - if (m_item->m_connectName != item->m_connectName || m_item->m_connectPath != item->m_connectPath) { //当前未激活连接的其他数据改变(除了激活状态外) - qDebug() << "[LanPage]:Deactive Connection's name or path changed." << Q_FUNC_INFO << __LINE__; - qDebug() << "new:" << item->m_connectUuid << item->m_connectName << item->m_connectPath << item->m_connectState << item->m_ifaceName; - qDebug() << "old:" << m_item->m_connectUuid << m_item->m_connectName << m_item->m_connectPath << m_item->m_connectState << m_item->m_ifaceName; + if (p_connectItem->m_connectName != p_item->m_connectName + || p_connectItem->m_connectPath != p_item->m_connectPath) { //当前未激活连接的其他数据改变(除了激活状态外) + LanListItem *p_lanItem = (LanListItem *)m_inactivatedLanListWidget->itemWidget(iter.value()); + m_inactivatedLanListWidget->removeItemWidget(iter.value()); + + delete p_lanItem; + p_lanItem = nullptr; + delete iter.value(); m_deactiveMap.erase(iter); - addNewItem(item, m_inactivatedLanListWidget); - m_deactiveMap.insert(item, m_listWidgetItem); - break; + delete p_item; + p_item = nullptr; + + QListWidgetItem *p_listWidgetItem = addNewItem(p_connectItem, m_inactivatedLanListWidget); + m_deactiveMap.insert(p_connectItem, p_listWidgetItem); + p_connectItem = nullptr; } } } + break; } + + return; +} + +void LanPage::updateActiveConnectionProperty(KyConnectItem *p_connectItem) +{ QMap::iterator iters; for (iters = m_activeMap.begin(); iters != m_activeMap.constEnd(); ++iters) { - KyConnectItem *m_item = iters.key(); - if (m_item->m_connectUuid == uuid) { - if (!isCurrentDevice) { //当前激活连接的设备改变 + KyConnectItem *p_item = iters.key(); + if (p_item->m_connectUuid == p_connectItem->m_connectUuid) { + if (m_currentDeviceName != p_connectItem->m_ifaceName) { //当前激活连接的设备改变 + LanListItem *p_lanItem = (LanListItem *)m_activatedLanListWidget->itemWidget(iters.value()); m_activatedLanListWidget->removeItemWidget(iters.value()); + + delete p_lanItem; + p_lanItem = nullptr; + delete iters.value(); m_activeMap.erase(iters); - qDebug() << "[LanPage]:Active Connection's device change, remove it" << Q_FUNC_INFO << __LINE__; - return; + + delete p_item; + p_item = nullptr; + } else { - if (m_item->m_connectName != item->m_connectName || m_item->m_connectPath != item->m_connectPath) { //当前激活连接的其他数据改变(除了激活状态外) - qDebug() << "[LanPage]:Active Connection's name or path changed." << Q_FUNC_INFO << __LINE__; - qDebug() << "new:" << item->m_connectUuid << item->m_connectName << item->m_connectPath << item->m_connectState << item->m_ifaceName; - qDebug() << "old:" << m_item->m_connectUuid << m_item->m_connectName << m_item->m_connectPath << m_item->m_connectState << m_item->m_ifaceName; + if (p_item->m_connectName != p_connectItem->m_connectName + || p_item->m_connectPath != p_connectItem->m_connectPath) { //当前激活连接的其他数据改变(除了激活状态外) + LanListItem *p_lanItem = (LanListItem *)m_activatedLanListWidget->itemWidget(iters.value()); m_activatedLanListWidget->removeItemWidget(iters.value()); + + delete p_lanItem; + p_lanItem = nullptr; + delete iters.value(); m_activeMap.erase(iters); - addNewItem(item, m_activatedLanListWidget); - m_activeMap.insert(item, m_listWidgetItem); - break; + delete p_item; + p_item = nullptr; + + QListWidgetItem *p_listWidgetItem = addNewItem(p_connectItem, m_activatedLanListWidget); + m_activeMap.insert(p_connectItem, p_listWidgetItem); + p_connectItem = nullptr; } } + break; } } + + return; +} + +void LanPage::onUpdateConnection(QString uuid) +{ + + if (!m_connectResourse->isWiredConnection(uuid)) { + return; + } + + qDebug() << "[LanPage]:Connection Changed !" << Q_FUNC_INFO << __LINE__; + + KyConnectItem *p_newItem = nullptr; + if (m_connectResourse->isActivatedConnection(uuid)) { + p_newItem = m_activeResourse->getActiveConnectionByUuid(uuid); + if (nullptr == p_newItem) { + qWarning()<<"[LanPage] get item failed, when update activate connection." + <<"connection uuid"<getConnectionItemByUuid(uuid); + if (nullptr == p_newItem) { + qWarning()<<"[LanPage] get item failed, when update connection." + <<"connection uuid"<activateConnection(connUuid, devName); } void LanPage::deactivateWired(const QString& devName, const QString& connUuid) { - qDebug() << "deactivateWired" << devName << connUuid; -// KyConnectItem *item = nullptr; -// item = m_activeResourse->getActiveConnectionByUuid(connUuid, devName); -// if (nullptr == item) { -// qDebug() << "not ActiveConnection"; -// item = m_connectResourse->getConnectionItemByUuid(connUuid, devName); -// if (nullptr == item) { -// QString errorMessage = tr("it can not find the activate connect" + tr("uuid") + connUuid; -// qWarning()<deactivateWiredConnection(name, connUuid); } void LanPage::showDetailPage(QString devName, QString uuid) { - KyConnectItem *item = nullptr; + KyConnectItem *p_item = nullptr; bool isActive = true; - item = m_activeResourse->getActiveConnectionByUuid(uuid, devName); - if (nullptr == item) { - item = m_connectResourse->getConnectionItemByUuid(uuid, devName); - if (nullptr == item) { - qWarning()<<"[LanPage] GetConnectionItemByUuid is empty when showDetailPage." - <<"device name"<isActivatedConnection(uuid)) { + p_item = m_activeResourse->getActiveConnectionByUuid(uuid); + isActive = true; + } else { + p_item = m_connectResourse->getConnectionItemByUuid(uuid); + isActive = false; } - NetDetail *netDetail = new NetDetail(devName, item->m_connectName, uuid, isActive, false, false, this); + if (nullptr == p_item) { + qWarning()<<"[LanPage] GetConnectionItemByUuid is empty when showDetailPage." + <<"device name"<m_connectName, uuid, isActive, false, false, this); netDetail->show(); - delete item; - item = nullptr; + delete p_item; + p_item = nullptr; } diff --git a/src/frontend/tab-pages/lanpage.h b/src/frontend/tab-pages/lanpage.h index 31e99f1d..0cf560ea 100644 --- a/src/frontend/tab-pages/lanpage.h +++ b/src/frontend/tab-pages/lanpage.h @@ -30,63 +30,84 @@ public: void showDetailPage(QString devName, QString uuid); void setWiredDeviceEnable(const QString& devName, bool enable); +private: + void initDeviceState(); + void initUI(); + void initLanArea(); + void initNetSwitch(); + + void initDeviceCombox(); + void updateDeviceCombox(QString oldDeviceName, QString newDeviceName); + void deleteDeviceFromCombox(QString deviceName); + void addDeviceForCombox(QString deviceName); + + QListWidgetItem *addNewItem(KyConnectItem *itemData, QListWidget *listWidget); + + void getEnabledDevice(QStringList &enableDeviceList); + void getDisabledDevices(QStringList &disableDeviceList); + + void constructConnectionArea(); + void constructActiveConnectionArea(); + + void updateConnectionArea(QString uuid); + void updateActivatedConnectionArea(QString uuid); + + void updateActiveConnectionProperty(KyConnectItem *p_connectItem); + void updateConnectionProperty(KyConnectItem *p_connectItem); + + void sendLanUpdateSignal(KyConnectItem *p_connectItem); + void sendLanAddSignal(KyConnectItem *p_connectItem); + + void addEmptyConnectItem(QMap &connectMap, + QListWidget *lanListWidget); + void clearConnectionMap(QMap &connectMap, + QListWidget *lanListWidget); + void deleteConnectionMapItem(QMap &connectMap, + QListWidget *lanListWidget, QString uuid); + bool connectionItemIsExist(QMap &connectMap, + QString uuid); + signals: void lanAdd(QString devName, QStringList info); void lanRemove(QString dbusPath); void lanUpdate(QString devName, QStringList info); + void lanActiveConnectionStateChanged(QString interface, QString uuid, int status); void lanConnectChanged(); -private: - void initDeviceState(); - void initDeviceCombox(); - void initUI(); - void initList(QString m_deviceName); - void addNewItem(KyConnectItem *itemData, QListWidget *listWidget); - void addNUllItem(QListWidget *listWidget); - void initNetSwitch(); +private slots: + void onUpdateLanlist(QString uuid, NetworkManager::ActiveConnection::State state, NetworkManager::ActiveConnection::Reason reason); + + void onAddConnection(QString uuid); + void onRemoveConnection(QString path); + void onUpdateConnection(QString uuid); + + void onSwithGsettingsChanged(const QString &key); + void onLanSwitchClicked(); + + void onDeviceAdd(QString deviceName, NetworkManager::Device::Type deviceType); + void onDeviceRemove(QString deviceName); + void onDeviceNameUpdate(QString oldName, QString newName); + + void onDeviceComboxIndexChanged(int currentIndex); private: QListWidget * m_activatedLanListWidget = nullptr; QListWidget * m_inactivatedLanListWidget = nullptr; - LanListItem * m_nullLanItem = nullptr; - QListWidgetItem *m_nullItem = nullptr; - QListWidgetItem *m_listWidgetItem = nullptr; - - KyNetworkDeviceResourse *m_device = nullptr; + KyNetworkDeviceResourse *m_deviceResource = nullptr; KyWiredConnectOperation *m_wiredConnectOperation = nullptr; KyActiveConnectResourse *m_activeResourse = nullptr; //激活的连接 - KyConnectResourse *m_connectResourse = nullptr; //未激活的连接 - - QList m_activedList; - QList m_deactivedList; - -// QMap > m_deviceMap; + KyConnectResourse *m_connectResourse = nullptr; //未激活的连接 QMap m_deactiveMap; QMap m_activeMap; -// QMap m_deactiveMap; -// QMap m_activeMap; - - QString m_deviceName; + QString m_currentDeviceName; QStringList m_devList; - QStringList enableDevice; - QGSettings * m_switchGsettings = nullptr; + QGSettings *m_switchGsettings = nullptr; -private slots: - void updateLanlist(QString uuid, NetworkManager::ActiveConnection::State state, NetworkManager::ActiveConnection::Reason reason); - void addConnectionSlot(QString uuid); - void removeConnectionSlot(QString path); - void connectionUpdateSlot(QString uuid); - void onSwithGsettingsChanged(const QString &key); - void onLanSwitchClicked(); - void onDeviceAdd(QString deviceName, NetworkManager::Device::Type deviceType); - void onDeviceRemove(QString deviceName); - void onDeviceNameUpdate(QString oldName, QString newName); - void onDeviceComboxIndexChanged(int currentIndex); -// void onLanDataChange(QString uuid); + bool m_wiredSwitch = true; }; #endif // LANPAGE_H diff --git a/src/frontend/tab-pages/tabpage.cpp b/src/frontend/tab-pages/tabpage.cpp index 7e5e9312..c35f3d71 100644 --- a/src/frontend/tab-pages/tabpage.cpp +++ b/src/frontend/tab-pages/tabpage.cpp @@ -187,6 +187,18 @@ void saveDeviceEnableState(QString deviceName, bool enable) return; } +void deleteDeviceEnableState(QString deviceName) +{ + QSettings * m_settings = new QSettings(CONFIG_FILE_PATH, QSettings::IniFormat); + m_settings->beginGroup("CARDEABLE"); + m_settings->remove(deviceName); + m_settings->endGroup(); + m_settings->sync(); + delete m_settings; + m_settings = nullptr; + return; +} + void getDeviceEnableState(int type, QMap &map) { map.clear(); diff --git a/src/frontend/tab-pages/tabpage.h b/src/frontend/tab-pages/tabpage.h index 7088fb8c..2d639906 100644 --- a/src/frontend/tab-pages/tabpage.h +++ b/src/frontend/tab-pages/tabpage.h @@ -42,6 +42,7 @@ const QString CONFIG_FILE_PATH = QDir::homePath() + "/.config/ukui/kylin-nm.c bool checkDeviceExist(KyDeviceType deviceType, QString deviceName); void setDefaultDevice(KyDeviceType deviceType, QString deviceName); void saveDeviceEnableState(QString deviceName, bool enable); +void deleteDeviceEnableState(QString deviceName); void getDeviceEnableState(int type, QMap &map); class TabPage : public QWidget