diff --git a/core/daemon.cpp b/core/daemon.cpp index fe6c82e09..b79dce99c 100644 --- a/core/daemon.cpp +++ b/core/daemon.cpp @@ -73,20 +73,21 @@ void Daemon::init() qCDebug(KDECONNECT_CORE) << "DBus registration complete"; auto configInstance = KdeConnectConfig::instance(); - auto status = configInstance.linkProviderStatus(); + const auto disabledLinkProviders = configInstance.disabledLinkProviders(); // Load backends if (d->m_testMode) { d->m_linkProviders.insert(new LoopbackLinkProvider()); } else { - d->m_linkProviders.insert(new LanLinkProvider(false, status[QStringLiteral("disabled")].contains(QStringLiteral("LanLinkProvider")))); + d->m_linkProviders.insert(new LanLinkProvider(false, disabledLinkProviders.contains(QStringLiteral("LanLinkProvider")))); #ifdef KDECONNECT_BLUETOOTH - d->m_linkProviders.insert(new BluetoothLinkProvider(status[QStringLiteral("disabled")].contains(QStringLiteral("BluetoothLinkProvider")))); + d->m_linkProviders.insert(new BluetoothLinkProvider(disabledLinkProviders.contains(QStringLiteral("BluetoothLinkProvider")))); #endif #ifdef KDECONNECT_LOOPBACK d->m_linkProviders.insert(new LoopbackLinkProvider()); #endif } + Q_EMIT linkProvidersChanged(linkProviders()); qCDebug(KDECONNECT_CORE) << "Backends loaded"; @@ -153,16 +154,16 @@ QSet Daemon::getLinkProviders() const QStringList Daemon::linkProviders() const { auto configInstance = KdeConnectConfig::instance(); - auto status = configInstance.linkProviderStatus(); + const auto disabledLinkProviders = configInstance.disabledLinkProviders(); QStringList returnValue; for (LinkProvider *a : std::as_const(d->m_linkProviders)) { QString line(a->name()); - if (status[QStringLiteral("enabled")].contains(a->name())) { - line += QStringLiteral("|enabled"); - } else { + if (disabledLinkProviders.contains(a->name())) { line += QStringLiteral("|disabled"); + } else { + line += QStringLiteral("|enabled"); } returnValue.append(line); @@ -170,37 +171,33 @@ QStringList Daemon::linkProviders() const return returnValue; } -void Daemon::setProviderStatus(const QStringList &providerStatus) +void Daemon::setDisabledLinkProviders(const QStringList &disabledLinkProviders) { - qCDebug(KDECONNECT_CORE) << "setProviderStatus called" << providerStatus; + qCDebug(KDECONNECT_CORE) << "setDisabledLinkProviders called" << disabledLinkProviders; KdeConnectConfig configInstance = KdeConnectConfig::instance(); - QStringList enabledProviders; - QStringList disabledProviders; + configInstance.setDisabledLinkProviders(disabledLinkProviders); + Q_EMIT linkProvidersChanged(linkProviders()); +} - for (const auto &i : providerStatus) { - auto components = i.split(QStringLiteral("|")); - QString providerName = components.at(0); - QString providerStatus = components.at(1); +void Daemon::setLinkProviderState(const QString &linkProvider, bool state) +{ + qCDebug(KDECONNECT_CORE) << "setLinkProviderState called" << linkProvider << state; - const auto linkProviders = this->getLinkProviders(); - for (LinkProvider *provider : linkProviders) { - if (provider->name() == providerName) { - if (providerStatus == QStringLiteral("enabled")) { - qCDebug(KDECONNECT_CORE) << "enabling " << providerName; - provider->enable(); - enabledProviders.append(providerName); - } else { - qCDebug(KDECONNECT_CORE) << "disabling" << providerName; - provider->disable(); - disabledProviders.append(providerName); - } - break; - } - } + KdeConnectConfig configInstance = KdeConnectConfig::instance(); + + auto disabledLinkProviders = configInstance.disabledLinkProviders(); + if (!state && !disabledLinkProviders.contains(linkProvider)) { + disabledLinkProviders.append(linkProvider); + } else if (state && disabledLinkProviders.contains(linkProvider)) { + disabledLinkProviders.removeAll(linkProvider); + } else { + return; // no change } - configInstance.setLinkProviderStatus(enabledProviders, disabledProviders); + + configInstance.setDisabledLinkProviders(disabledLinkProviders); + Q_EMIT linkProvidersChanged(linkProviders()); } QStringList Daemon::devices(bool onlyReachable, bool onlyTrusted) const diff --git a/core/daemon.h b/core/daemon.h index d84d04a9f..06244d858 100644 --- a/core/daemon.h +++ b/core/daemon.h @@ -65,8 +65,14 @@ public Q_SLOTS: Q_SCRIPTABLE QString deviceIdByName(const QString &name) const; + /// Return the list of link providers with their state e.g. "BluetoothLinkProvider|enabled" Q_SCRIPTABLE QStringList linkProviders() const; - Q_SCRIPTABLE void setProviderStatus(const QStringList &providerStatus); + + /// Set the list of disabled link providers. + Q_SCRIPTABLE void setDisabledLinkProviders(const QStringList &disabledLinkProviders); + + /// Set the state of a link provider. + Q_SCRIPTABLE void setLinkProviderState(const QString &linkProvider, bool state); Q_SCRIPTABLE virtual void sendSimpleNotification(const QString &eventId, const QString &title, const QString &text, const QString &iconName) = 0; @@ -77,6 +83,7 @@ Q_SIGNALS: Q_SCRIPTABLE void deviceListChanged(); // Emitted when any of deviceAdded, deviceRemoved or deviceVisibilityChanged is emitted Q_SCRIPTABLE void announcedNameChanged(const QString &announcedName); Q_SCRIPTABLE void pairingRequestsChanged(); + Q_SCRIPTABLE void linkProvidersChanged(const QStringList &linkProviders); Q_SCRIPTABLE void customDevicesChanged(const QStringList &customDevices); private Q_SLOTS: diff --git a/core/kdeconnectconfig.cpp b/core/kdeconnectconfig.cpp index b222ba909..7286afe33 100644 --- a/core/kdeconnectconfig.cpp +++ b/core/kdeconnectconfig.cpp @@ -76,19 +76,15 @@ void KdeConnectConfig::setName(const QString &name) d->m_config->sync(); } -void KdeConnectConfig::setLinkProviderStatus(const QStringList enabledProviders, const QStringList disabledProviders) +void KdeConnectConfig::setDisabledLinkProviders(const QStringList disabledProviders) { - d->m_config->setValue(QStringLiteral("enabled_providers"), enabledProviders); d->m_config->setValue(QStringLiteral("disabled_providers"), disabledProviders); d->m_config->sync(); } -QMap KdeConnectConfig::linkProviderStatus() const +QStringList KdeConnectConfig::disabledLinkProviders() const { - return { - {QStringLiteral("enabled"), d->m_config->value(QStringLiteral("enabled_providers")).toStringList()}, - {QStringLiteral("disabled"), d->m_config->value(QStringLiteral("disabled_providers")).toStringList()}, - }; + return d->m_config->value(QStringLiteral("disabled_providers")).toStringList(); } DeviceType KdeConnectConfig::deviceType() diff --git a/core/kdeconnectconfig.h b/core/kdeconnectconfig.h index 917e11316..c15fd807e 100644 --- a/core/kdeconnectconfig.h +++ b/core/kdeconnectconfig.h @@ -35,8 +35,8 @@ public: QString certificatePath(); void setName(const QString &name); - void setLinkProviderStatus(const QStringList enabledProviders, const QStringList disabledProviders); - QMap linkProviderStatus() const; + void setDisabledLinkProviders(const QStringList disabledProviders); + QStringList disabledLinkProviders() const; /* * Trusted devices diff --git a/kcm/kcm.cpp b/kcm/kcm.cpp index 42facfa9d..d74c3735c 100644 --- a/kcm/kcm.cpp +++ b/kcm/kcm.cpp @@ -100,11 +100,23 @@ KdeConnectKcm::KdeConnectKcm(QObject *parent, const KPluginMetaData &md, const Q [this](bool error, const QStringList &linkProviders) { kcmUi.linkProviders_list->clear(); for (int i = 0; i < linkProviders.size(); ++i) { - QStringList linkProvider = linkProviders.at(i).split(QStringLiteral("|")); - QString providerName = linkProvider.at(0); + const QStringList linkProvider = linkProviders.at(i).split(QStringLiteral("|")); + const QString providerId = linkProvider.at(0); + QString displayName; + if (providerId == QLatin1StringView("BluetoothLinkProvider")) { + displayName = i18nc("@info KDE Connect provider name", "Bluetooth"); + } else if (providerId == QLatin1StringView("LoopbackLinkProvider")) { + displayName = i18nc("@info KDE Connect provider name", "Loopback"); + } else if (providerId == QLatin1StringView("LanLinkProvider")) { + displayName = i18nc("@info KDE Connect provider name", "WiFi Network"); + } else { + Q_ASSERT_X(false, Q_FUNC_INFO, "Unknow provider given"); + displayName = i18nc("@info KDE Connect provider name", "Unknown"); + } QString providerStatus = linkProvider.at(1); - QListWidgetItem *linkProviderItem = new QListWidgetItem(providerName, kcmUi.linkProviders_list); + QListWidgetItem *linkProviderItem = new QListWidgetItem(displayName, kcmUi.linkProviders_list); + linkProviderItem->setData(Qt::UserRole, providerId); if (providerStatus.compare(QStringLiteral("enabled")) == 0) { linkProviderItem->setCheckState(Qt::Checked); @@ -190,15 +202,15 @@ void KdeConnectKcm::refresh() void KdeConnectKcm::saveBackends() { - QStringList providerStatusToSend; + QStringList disabledLinkProviders; for (int i = 0; i < kcmUi.linkProviders_list->count(); ++i) { QListWidgetItem *item = kcmUi.linkProviders_list->item(i); - QString providerIsEnabled = item->checkState() == Qt::Checked ? QStringLiteral("enabled") : QStringLiteral("disabled"); - QString line = item->text() + QStringLiteral("|") + providerIsEnabled; - providerStatusToSend.append(line); + if (item->checkState() == Qt::Unchecked) { + disabledLinkProviders << item->data(Qt::UserRole).toString(); + } } - daemon->setProviderStatus(providerStatusToSend); + daemon->setDisabledLinkProviders(disabledLinkProviders); } void KdeConnectKcm::deviceSelected(const QString &deviceId)