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";
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<LinkProvider *> 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);
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 {
qCDebug(KDECONNECT_CORE) << "disabling" << providerName;
provider->disable();
disabledProviders.append(providerName);
return; // no change
}
break;
}
}
}
configInstance.setLinkProviderStatus(enabledProviders, disabledProviders);
configInstance.setDisabledLinkProviders(disabledLinkProviders);
Q_EMIT linkProvidersChanged(linkProviders());
}
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;
/// 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:

View file

@ -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<QString, QStringList> 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()

View file

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

View file

@ -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)