File indexing completed on 2024-04-21 16:11:32

0001 /**
0002  * SPDX-FileCopyrightText: 2020 Nicolas Fella <nicolas.fella@gmx.de>
0003  * SPDX-FileCopyrightText: 2021 Nate Graham <nate@kde.org>
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "bluetooth.h"
0008 
0009 #include <QDBusConnection>
0010 #include <QDBusMessage>
0011 #include <QDBusPendingCallWatcher>
0012 #include <QDBusPendingReply>
0013 
0014 #include <KAboutData>
0015 #include <KConfigGroup>
0016 #include <KIO/ApplicationLauncherJob>
0017 #include <KIO/CommandLauncherJob>
0018 #include <KLocalizedString>
0019 #include <KPluginFactory>
0020 #include <KSharedConfig>
0021 
0022 #include <BluezQt/Services>
0023 
0024 #include "filereceiversettings.h"
0025 
0026 K_PLUGIN_CLASS_WITH_JSON(Bluetooth, "kcm_bluetooth.json")
0027 
0028 Bluetooth::Bluetooth(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
0029     : KQuickAddons::ConfigModule(parent, data, args)
0030 {
0031     setButtons(Help);
0032 
0033     qmlRegisterAnonymousType<QAbstractItemModel>("org.kde.bluedevil.kcm", 1);
0034     qmlRegisterSingletonInstance("org.kde.bluedevil.kcm", 1, 0, "FileReceiverSettings", FileReceiverSettings::self());
0035 }
0036 
0037 void Bluetooth::runWizard()
0038 {
0039     auto *job = new KIO::ApplicationLauncherJob(KService::serviceByDesktopName(QStringLiteral("org.kde.bluedevilwizard")));
0040     connect(job, &KJob::finished, this, [this](KJob *job) {
0041         if (job->error()) {
0042             Q_EMIT errorOccured(job->errorString());
0043         }
0044     });
0045     job->start();
0046 }
0047 
0048 void Bluetooth::runSendFile(const QString &ubi)
0049 {
0050     auto *job = new KIO::CommandLauncherJob(QStringLiteral("bluedevil-sendfile"), {QStringLiteral("-u"), ubi});
0051     job->setDesktopName(QStringLiteral("org.kde.bluedevilsendfile"));
0052     connect(job, &KJob::finished, this, [this](KJob *job) {
0053         if (job->error()) {
0054             Q_EMIT errorOccured(job->errorString());
0055         }
0056     });
0057     job->start();
0058 }
0059 
0060 void Bluetooth::checkNetworkConnection(const QStringList &uuids, const QString &address)
0061 {
0062     if (uuids.contains(BluezQt::Services::Nap)) {
0063         checkNetworkInternal(QStringLiteral("nap"), address);
0064     }
0065 
0066     if (uuids.contains(BluezQt::Services::DialupNetworking)) {
0067         checkNetworkInternal(QStringLiteral("dun"), address);
0068     }
0069 }
0070 
0071 void Bluetooth::checkNetworkInternal(const QString &service, const QString &address)
0072 {
0073     QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.plasmanetworkmanagement"),
0074                                                       QStringLiteral("/org/kde/plasmanetworkmanagement"),
0075                                                       QStringLiteral("org.kde.plasmanetworkmanagement"),
0076                                                       QStringLiteral("bluetoothConnectionExists"));
0077 
0078     msg << address;
0079     msg << service;
0080 
0081     QDBusPendingCallWatcher *call = new QDBusPendingCallWatcher(QDBusConnection::sessionBus().asyncCall(msg));
0082     connect(call, &QDBusPendingCallWatcher::finished, this, [this, service, call]() {
0083         QDBusPendingReply<bool> reply = *call;
0084         if (reply.isError()) {
0085             return;
0086         }
0087 
0088         Q_EMIT networkAvailable(service, reply.value());
0089     });
0090 }
0091 
0092 void Bluetooth::setupNetworkConnection(const QString &service, const QString &address, const QString &deviceName)
0093 {
0094     QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.plasmanetworkmanagement"),
0095                                                       QStringLiteral("/org/kde/plasmanetworkmanagement"),
0096                                                       QStringLiteral("org.kde.plasmanetworkmanagement"),
0097                                                       QStringLiteral("addBluetoothConnection"));
0098 
0099     msg << address;
0100     msg << service;
0101     msg << i18nc("DeviceName Network (Service)", "%1 Network (%2)", deviceName, service);
0102 
0103     QDBusConnection::sessionBus().call(msg, QDBus::NoBlock);
0104 }
0105 
0106 QString Bluetooth::bluetoothStatusAtLogin() const
0107 {
0108     const auto config = KSharedConfig::openConfig(QStringLiteral("bluedevilglobalrc"));
0109     const KConfigGroup globalGroup = config->group("Global");
0110     return globalGroup.readEntry("launchState", "remember");
0111 }
0112 
0113 void Bluetooth::setBluetoothStatusAtLogin(const QString &newStatus)
0114 {
0115     auto config = KSharedConfig::openConfig(QStringLiteral("bluedevilglobalrc"));
0116     KConfigGroup globalGroup = config->group("Global");
0117     const QString currentValue = (globalGroup.readEntry("launchState", "remember"));
0118 
0119     if (newStatus == currentValue) {
0120         return;
0121     }
0122 
0123     if (newStatus == "remember") {
0124         // Default value
0125         globalGroup.deleteEntry("launchState");
0126     } else {
0127         globalGroup.writeEntry("launchState", newStatus);
0128     }
0129 
0130     Q_EMIT bluetoothStatusAtLoginChanged(newStatus);
0131 }
0132 
0133 #include "bluetooth.moc"