File indexing completed on 2024-03-24 05:28:57

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