Simplify backend by only storing the list of disabled link providers

Previously the list of enabled and disabled providers was too easy to
get out of sync. Only having one list simplify the code and avoids issue
with for example the default state which should contains no disabled
backends.
This commit is contained in:
Carl Schwan 2024-11-03 22:10:51 +01:00
parent e2e36e698c
commit 5b10d9632c
No known key found for this signature in database
GPG key ID: 02325448204E452A
5 changed files with 61 additions and 49 deletions

View file

@ -73,20 +73,21 @@ void Daemon::init()
qCDebug(KDECONNECT_CORE) << "DBus registration complete"; qCDebug(KDECONNECT_CORE) << "DBus registration complete";
auto configInstance = KdeConnectConfig::instance(); auto configInstance = KdeConnectConfig::instance();
auto status = configInstance.linkProviderStatus(); const auto disabledLinkProviders = configInstance.disabledLinkProviders();
// Load backends // Load backends
if (d->m_testMode) { if (d->m_testMode) {
d->m_linkProviders.insert(new LoopbackLinkProvider()); d->m_linkProviders.insert(new LoopbackLinkProvider());
} else { } 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 #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 #endif
#ifdef KDECONNECT_LOOPBACK #ifdef KDECONNECT_LOOPBACK
d->m_linkProviders.insert(new LoopbackLinkProvider()); d->m_linkProviders.insert(new LoopbackLinkProvider());
#endif #endif
} }
Q_EMIT linkProvidersChanged(linkProviders());
qCDebug(KDECONNECT_CORE) << "Backends loaded"; qCDebug(KDECONNECT_CORE) << "Backends loaded";
@ -153,16 +154,16 @@ QSet<LinkProvider *> Daemon::getLinkProviders() const
QStringList Daemon::linkProviders() const QStringList Daemon::linkProviders() const
{ {
auto configInstance = KdeConnectConfig::instance(); auto configInstance = KdeConnectConfig::instance();
auto status = configInstance.linkProviderStatus(); const auto disabledLinkProviders = configInstance.disabledLinkProviders();
QStringList returnValue; QStringList returnValue;
for (LinkProvider *a : std::as_const(d->m_linkProviders)) { for (LinkProvider *a : std::as_const(d->m_linkProviders)) {
QString line(a->name()); QString line(a->name());
if (status[QStringLiteral("enabled")].contains(a->name())) { if (disabledLinkProviders.contains(a->name())) {
line += QStringLiteral("|enabled");
} else {
line += QStringLiteral("|disabled"); line += QStringLiteral("|disabled");
} else {
line += QStringLiteral("|enabled");
} }
returnValue.append(line); returnValue.append(line);
@ -170,37 +171,33 @@ QStringList Daemon::linkProviders() const
return returnValue; 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(); KdeConnectConfig configInstance = KdeConnectConfig::instance();
QStringList enabledProviders; configInstance.setDisabledLinkProviders(disabledLinkProviders);
QStringList disabledProviders; Q_EMIT linkProvidersChanged(linkProviders());
}
for (const auto &i : providerStatus) { void Daemon::setLinkProviderState(const QString &linkProvider, bool state)
auto components = i.split(QStringLiteral("|")); {
QString providerName = components.at(0); qCDebug(KDECONNECT_CORE) << "setLinkProviderState called" << linkProvider << state;
QString providerStatus = components.at(1);
const auto linkProviders = this->getLinkProviders(); KdeConnectConfig configInstance = KdeConnectConfig::instance();
for (LinkProvider *provider : linkProviders) {
if (provider->name() == providerName) { auto disabledLinkProviders = configInstance.disabledLinkProviders();
if (providerStatus == QStringLiteral("enabled")) { if (!state && !disabledLinkProviders.contains(linkProvider)) {
qCDebug(KDECONNECT_CORE) << "enabling " << providerName; disabledLinkProviders.append(linkProvider);
provider->enable(); } else if (state && disabledLinkProviders.contains(linkProvider)) {
enabledProviders.append(providerName); disabledLinkProviders.removeAll(linkProvider);
} else { } else {
qCDebug(KDECONNECT_CORE) << "disabling" << providerName; return; // no change
provider->disable();
disabledProviders.append(providerName);
} }
break;
} configInstance.setDisabledLinkProviders(disabledLinkProviders);
} Q_EMIT linkProvidersChanged(linkProviders());
}
configInstance.setLinkProviderStatus(enabledProviders, disabledProviders);
} }
QStringList Daemon::devices(bool onlyReachable, bool onlyTrusted) const QStringList Daemon::devices(bool onlyReachable, bool onlyTrusted) const

View file

@ -65,8 +65,14 @@ public Q_SLOTS:
Q_SCRIPTABLE QString deviceIdByName(const QString &name) const; 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 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; 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 deviceListChanged(); // Emitted when any of deviceAdded, deviceRemoved or deviceVisibilityChanged is emitted
Q_SCRIPTABLE void announcedNameChanged(const QString &announcedName); Q_SCRIPTABLE void announcedNameChanged(const QString &announcedName);
Q_SCRIPTABLE void pairingRequestsChanged(); Q_SCRIPTABLE void pairingRequestsChanged();
Q_SCRIPTABLE void linkProvidersChanged(const QStringList &linkProviders);
Q_SCRIPTABLE void customDevicesChanged(const QStringList &customDevices); Q_SCRIPTABLE void customDevicesChanged(const QStringList &customDevices);
private Q_SLOTS: private Q_SLOTS:

View file

@ -76,19 +76,15 @@ void KdeConnectConfig::setName(const QString &name)
d->m_config->sync(); 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->setValue(QStringLiteral("disabled_providers"), disabledProviders);
d->m_config->sync(); d->m_config->sync();
} }
QMap<QString, QStringList> KdeConnectConfig::linkProviderStatus() const QStringList KdeConnectConfig::disabledLinkProviders() const
{ {
return { return d->m_config->value(QStringLiteral("disabled_providers")).toStringList();
{QStringLiteral("enabled"), d->m_config->value(QStringLiteral("enabled_providers")).toStringList()},
{QStringLiteral("disabled"), d->m_config->value(QStringLiteral("disabled_providers")).toStringList()},
};
} }
DeviceType KdeConnectConfig::deviceType() DeviceType KdeConnectConfig::deviceType()

View file

@ -35,8 +35,8 @@ public:
QString certificatePath(); QString certificatePath();
void setName(const QString &name); void setName(const QString &name);
void setLinkProviderStatus(const QStringList enabledProviders, const QStringList disabledProviders); void setDisabledLinkProviders(const QStringList disabledProviders);
QMap<QString, QStringList> linkProviderStatus() const; QStringList disabledLinkProviders() const;
/* /*
* Trusted devices * Trusted devices

View file

@ -100,11 +100,23 @@ KdeConnectKcm::KdeConnectKcm(QObject *parent, const KPluginMetaData &md, const Q
[this](bool error, const QStringList &linkProviders) { [this](bool error, const QStringList &linkProviders) {
kcmUi.linkProviders_list->clear(); kcmUi.linkProviders_list->clear();
for (int i = 0; i < linkProviders.size(); ++i) { for (int i = 0; i < linkProviders.size(); ++i) {
QStringList linkProvider = linkProviders.at(i).split(QStringLiteral("|")); const QStringList linkProvider = linkProviders.at(i).split(QStringLiteral("|"));
QString providerName = linkProvider.at(0); 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); 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) { if (providerStatus.compare(QStringLiteral("enabled")) == 0) {
linkProviderItem->setCheckState(Qt::Checked); linkProviderItem->setCheckState(Qt::Checked);
@ -190,15 +202,15 @@ void KdeConnectKcm::refresh()
void KdeConnectKcm::saveBackends() void KdeConnectKcm::saveBackends()
{ {
QStringList providerStatusToSend; QStringList disabledLinkProviders;
for (int i = 0; i < kcmUi.linkProviders_list->count(); ++i) { for (int i = 0; i < kcmUi.linkProviders_list->count(); ++i) {
QListWidgetItem *item = kcmUi.linkProviders_list->item(i); QListWidgetItem *item = kcmUi.linkProviders_list->item(i);
QString providerIsEnabled = item->checkState() == Qt::Checked ? QStringLiteral("enabled") : QStringLiteral("disabled"); if (item->checkState() == Qt::Unchecked) {
QString line = item->text() + QStringLiteral("|") + providerIsEnabled; disabledLinkProviders << item->data(Qt::UserRole).toString();
providerStatusToSend.append(line); }
} }
daemon->setProviderStatus(providerStatusToSend); daemon->setDisabledLinkProviders(disabledLinkProviders);
} }
void KdeConnectKcm::deviceSelected(const QString &deviceId) void KdeConnectKcm::deviceSelected(const QString &deviceId)