File indexing completed on 2024-04-14 15:39:56

0001 /*
0002     SPDX-FileCopyrightText: 2011 Lamarque Souza <lamarque@kde.org>
0003     SPDX-FileCopyrightText: 2013 Lukas Tinkl <ltinkl@redhat.com>
0004     SPDX-FileCopyrightText: 2013-2014 Jan Grulich <jgrulich@redhat.com>
0005     SPDX-FileCopyrightText: 2015 David Rosca <nowrep@gmail.com>
0006 
0007     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0008 */
0009 
0010 #include "bluetoothmonitor.h"
0011 #include "plasma_nm_kded.h"
0012 
0013 #include <KLocalizedString>
0014 #include <KMessageBox>
0015 
0016 #include <NetworkManagerQt/BluetoothSetting>
0017 #include <NetworkManagerQt/ConnectionSettings>
0018 #include <NetworkManagerQt/Manager>
0019 #include <NetworkManagerQt/Settings>
0020 #include <NetworkManagerQt/Utils>
0021 
0022 BluetoothMonitor::BluetoothMonitor(QObject *parent)
0023     : QObject(parent)
0024 {
0025 }
0026 
0027 BluetoothMonitor::~BluetoothMonitor() = default;
0028 
0029 bool BluetoothMonitor::bluetoothConnectionExists(const QString &bdAddr, const QString &service)
0030 {
0031     if (bdAddr.isEmpty() || service.isEmpty()) {
0032         return false;
0033     }
0034 
0035     NetworkManager::BluetoothSetting::ProfileType profile;
0036 
0037     if (service == QLatin1String("dun")) {
0038         profile = NetworkManager::BluetoothSetting::Dun;
0039     } else if (service == QLatin1String("nap")) {
0040         profile = NetworkManager::BluetoothSetting::Panu;
0041     } else {
0042         return false;
0043     }
0044 
0045     for (const NetworkManager::Connection::Ptr &con : NetworkManager::listConnections()) {
0046         if (con && con->settings() && con->settings()->connectionType() == NetworkManager::ConnectionSettings::Bluetooth) {
0047             NetworkManager::BluetoothSetting::Ptr btSetting =
0048                 con->settings()->setting(NetworkManager::Setting::Bluetooth).staticCast<NetworkManager::BluetoothSetting>();
0049             if (btSetting->profileType() == profile && btSetting->bluetoothAddress() == NetworkManager::macAddressFromString(bdAddr)) {
0050                 return true;
0051             }
0052         }
0053     }
0054 
0055     return false;
0056 }
0057 
0058 void BluetoothMonitor::addBluetoothConnection(const QString &bdAddr, const QString &service, const QString &connectionName)
0059 {
0060     qCDebug(PLASMA_NM_KDED_LOG) << "Adding BT connection:" << bdAddr << service;
0061 
0062     if (bdAddr.isEmpty() || service.isEmpty() || connectionName.isEmpty()) {
0063         return;
0064     }
0065 
0066     if (service != QLatin1String("dun") && service != QLatin1String("nap")) {
0067         KMessageBox::error(nullptr, i18n("Only 'dun' and 'nap' services are supported."));
0068         return;
0069     }
0070 
0071     qCDebug(PLASMA_NM_KDED_LOG) << "Bdaddr == " << bdAddr;
0072 
0073     if (bluetoothConnectionExists(bdAddr, service)) {
0074         return;
0075     }
0076 
0077     if (service == QLatin1String("nap")) {
0078         NetworkManager::ConnectionSettings connectionSettings(NetworkManager::ConnectionSettings::Bluetooth, NM_BT_CAPABILITY_NAP);
0079         connectionSettings.setUuid(NetworkManager::ConnectionSettings::createNewUuid());
0080         connectionSettings.setId(connectionName);
0081         NetworkManager::BluetoothSetting::Ptr btSetting =
0082             connectionSettings.setting(NetworkManager::Setting::Bluetooth).staticCast<NetworkManager::BluetoothSetting>();
0083         btSetting->setBluetoothAddress(NetworkManager::macAddressFromString(bdAddr));
0084         btSetting->setProfileType(NetworkManager::BluetoothSetting::Panu);
0085         btSetting->setInitialized(true);
0086 
0087         NetworkManager::addConnection(connectionSettings.toMap());
0088     } else if (service == QLatin1String("dun")) {
0089         QPointer<MobileConnectionWizard> mobileConnectionWizard = new MobileConnectionWizard(NetworkManager::ConnectionSettings::Bluetooth);
0090         mobileConnectionWizard->setAttribute(Qt::WA_DeleteOnClose);
0091         connect(mobileConnectionWizard.data(), &MobileConnectionWizard::accepted, [bdAddr, connectionName, mobileConnectionWizard]() {
0092             if (mobileConnectionWizard->getError() == MobileProviders::Success) {
0093                 qCDebug(PLASMA_NM_KDED_LOG) << "Mobile broadband wizard finished:" << mobileConnectionWizard->type() << mobileConnectionWizard->args();
0094                 if (mobileConnectionWizard->args().count() == 2) { // GSM or CDMA
0095                     qCDebug(PLASMA_NM_KDED_LOG) << "Creating new DUN connection for BT device:" << bdAddr;
0096 
0097                     auto tmp = qdbus_cast<QVariantMap>(mobileConnectionWizard->args().value(1));
0098                     NetworkManager::ConnectionSettings connectionSettings(NetworkManager::ConnectionSettings::Bluetooth, NM_BT_CAPABILITY_DUN);
0099                     connectionSettings.setUuid(NetworkManager::ConnectionSettings::createNewUuid());
0100                     connectionSettings.setId(connectionName);
0101                     NetworkManager::BluetoothSetting::Ptr btSetting =
0102                         connectionSettings.setting(NetworkManager::Setting::Bluetooth).staticCast<NetworkManager::BluetoothSetting>();
0103                     btSetting->setBluetoothAddress(NetworkManager::macAddressFromString(bdAddr));
0104                     btSetting->setProfileType(NetworkManager::BluetoothSetting::Dun);
0105                     btSetting->setInitialized(true);
0106 
0107                     if (mobileConnectionWizard->type() == NetworkManager::ConnectionSettings::Gsm) {
0108                         connectionSettings.setting(NetworkManager::Setting::Gsm)->fromMap(tmp);
0109                         connectionSettings.setting(NetworkManager::Setting::Gsm)->setInitialized(true);
0110                     } else if (mobileConnectionWizard->type() == NetworkManager::ConnectionSettings::Cdma) {
0111                         connectionSettings.setting(NetworkManager::Setting::Cdma)->fromMap(tmp);
0112                         connectionSettings.setting(NetworkManager::Setting::Cdma)->setInitialized(true);
0113                     }
0114 
0115                     qCDebug(PLASMA_NM_KDED_LOG) << "Adding DUN connection" << connectionSettings;
0116 
0117                     NetworkManager::addConnection(connectionSettings.toMap());
0118                 }
0119             }
0120         });
0121         mobileConnectionWizard->setModal(true);
0122         mobileConnectionWizard->show();
0123     }
0124 }