File indexing completed on 2024-04-14 09:06:57

0001 /*
0002     SPDX-FileCopyrightText: 2013 Jan Grulich <jgrulich@redhat.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "availabledevices.h"
0008 
0009 #include <NetworkManagerQt/Manager>
0010 
0011 AvailableDevices::AvailableDevices(QObject *parent)
0012     : QObject(parent)
0013 {
0014     for (const NetworkManager::Device::Ptr &device : NetworkManager::networkInterfaces()) {
0015         if (device->type() == NetworkManager::Device::Modem) {
0016             m_modemDeviceAvailable = true;
0017         } else if (device->type() == NetworkManager::Device::Wifi) {
0018             m_wirelessDeviceAvailable = true;
0019         } else if (device->type() == NetworkManager::Device::Ethernet) {
0020             m_wiredDeviceAvailable = true;
0021         } else if (device->type() == NetworkManager::Device::Bluetooth) {
0022             m_bluetoothDeviceAvailable = true;
0023         }
0024     }
0025 
0026     connect(NetworkManager::notifier(), &NetworkManager::Notifier::deviceAdded, this, &AvailableDevices::deviceAdded);
0027     connect(NetworkManager::notifier(), &NetworkManager::Notifier::deviceRemoved, this, &AvailableDevices::deviceRemoved);
0028 }
0029 
0030 AvailableDevices::~AvailableDevices() = default;
0031 
0032 bool AvailableDevices::isWiredDeviceAvailable() const
0033 {
0034     return m_wiredDeviceAvailable;
0035 }
0036 
0037 bool AvailableDevices::isWirelessDeviceAvailable() const
0038 {
0039     return m_wirelessDeviceAvailable;
0040 }
0041 
0042 bool AvailableDevices::isModemDeviceAvailable() const
0043 {
0044     return m_modemDeviceAvailable;
0045 }
0046 
0047 bool AvailableDevices::isBluetoothDeviceAvailable() const
0048 {
0049     return m_bluetoothDeviceAvailable;
0050 }
0051 
0052 void AvailableDevices::deviceAdded(const QString &dev)
0053 {
0054     NetworkManager::Device::Ptr device = NetworkManager::findNetworkInterface(dev);
0055 
0056     if (device) {
0057         if (device->type() == NetworkManager::Device::Modem && !m_modemDeviceAvailable) {
0058             m_modemDeviceAvailable = true;
0059             Q_EMIT modemDeviceAvailableChanged(true);
0060         } else if (device->type() == NetworkManager::Device::Wifi && !m_wirelessDeviceAvailable) {
0061             m_wirelessDeviceAvailable = true;
0062             Q_EMIT wirelessDeviceAvailableChanged(true);
0063         } else if (device->type() == NetworkManager::Device::Ethernet && !m_wiredDeviceAvailable) {
0064             m_wiredDeviceAvailable = true;
0065             Q_EMIT wiredDeviceAvailableChanged(true);
0066         } else if (device->type() == NetworkManager::Device::Bluetooth && !m_bluetoothDeviceAvailable) {
0067             m_bluetoothDeviceAvailable = true;
0068             Q_EMIT bluetoothDeviceAvailableChanged(true);
0069         }
0070     }
0071 }
0072 
0073 void AvailableDevices::deviceRemoved()
0074 {
0075     bool wired = false;
0076     bool wireless = false;
0077     bool modem = false;
0078     bool bluetooth = false;
0079 
0080     for (const NetworkManager::Device::Ptr &device : NetworkManager::networkInterfaces()) {
0081         if (device->type() == NetworkManager::Device::Modem) {
0082             modem = true;
0083         } else if (device->type() == NetworkManager::Device::Wifi) {
0084             wireless = true;
0085         } else if (device->type() == NetworkManager::Device::Ethernet) {
0086             wired = true;
0087         } else if (device->type() == NetworkManager::Device::Bluetooth) {
0088             bluetooth = true;
0089         }
0090     }
0091 
0092     if (!wired && m_wiredDeviceAvailable) {
0093         m_wiredDeviceAvailable = false;
0094         Q_EMIT wiredDeviceAvailableChanged(false);
0095     }
0096 
0097     if (!wireless && m_wirelessDeviceAvailable) {
0098         m_wirelessDeviceAvailable = false;
0099         Q_EMIT wirelessDeviceAvailableChanged(false);
0100     }
0101 
0102     if (!modem && m_modemDeviceAvailable) {
0103         m_modemDeviceAvailable = false;
0104         Q_EMIT modemDeviceAvailableChanged(false);
0105     }
0106 
0107     if (!bluetooth && m_bluetoothDeviceAvailable) {
0108         m_bluetoothDeviceAvailable = false;
0109         Q_EMIT bluetoothDeviceAvailableChanged(false);
0110     }
0111 }
0112 
0113 #include "moc_availabledevices.cpp"