Simplify apps model

The current code iterates over each service group separately and adds its apps. This leads to duplicate entries when an app is in multiple groups.

Since we are not actually interested in the group info, only in a flat list of all apps with some filters, we can simplify this.
This commit is contained in:
Nicolas Fella 2021-06-10 23:17:52 +02:00
parent 0fc51bfb1e
commit 992950085d

View file

@ -16,13 +16,12 @@
#include <QQuickWindow> #include <QQuickWindow>
// KDE // KDE
#include <KApplicationTrader>
#include <KIO/ApplicationLauncherJob> #include <KIO/ApplicationLauncherJob>
#include <KNotificationJobUiDelegate> #include <KNotificationJobUiDelegate>
#include <KService> #include <KService>
#include <KServiceGroup>
#include <KSharedConfig> #include <KSharedConfig>
#include <KSycoca> #include <KSycoca>
#include <KSycocaEntry>
#include <KWayland/Client/connection_thread.h> #include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/plasmawindowmanagement.h> #include <KWayland/Client/plasmawindowmanagement.h>
@ -158,71 +157,55 @@ void ApplicationListModel::loadApplications()
auto cfg = KSharedConfig::openConfig(QStringLiteral("applications-blacklistrc")); auto cfg = KSharedConfig::openConfig(QStringLiteral("applications-blacklistrc"));
auto blgroup = KConfigGroup(cfg, QStringLiteral("Applications")); auto blgroup = KConfigGroup(cfg, QStringLiteral("Applications"));
QStringList blacklist = blgroup.readEntry("blacklist", QStringList()); const QStringList blacklist = blgroup.readEntry("blacklist", QStringList());
beginResetModel(); beginResetModel();
m_applicationList.clear(); m_applicationList.clear();
KServiceGroup::Ptr group = KServiceGroup::root();
if (!group || !group->isValid()) return;
KServiceGroup::List subGroupList = group->entries(true);
QMap<int, ApplicationData> orderedList; QMap<int, ApplicationData> orderedList;
QList<ApplicationData> unorderedList; QList<ApplicationData> unorderedList;
QSet<QString> foundFavorites; QSet<QString> foundFavorites;
// Iterate over all entries in the group auto filter = [blacklist](const KService::Ptr &service) -> bool {
while (!subGroupList.isEmpty()) { if (service->noDisplay()) {
KSycocaEntry::Ptr groupEntry = subGroupList.first(); return false;
subGroupList.pop_front(); }
if (groupEntry->isType(KST_KServiceGroup)) { if (!service->showOnCurrentPlatform()) {
KServiceGroup::Ptr serviceGroup(static_cast<KServiceGroup *>(groupEntry.data())); return false;
}
if (!serviceGroup->noDisplay()) { if (blacklist.contains(service->desktopEntryName())) {
KServiceGroup::List entryGroupList = serviceGroup->entries(true); return false;
}
for(KServiceGroup::List::ConstIterator it = entryGroupList.constBegin(); it != entryGroupList.constEnd(); it++) { return true;
KSycocaEntry::Ptr entry = (*it); };
if (entry->isType(KST_KServiceGroup)) { const KService::List apps = KApplicationTrader::query(filter);
KServiceGroup::Ptr serviceGroup(static_cast<KServiceGroup *>(entry.data()));
subGroupList << serviceGroup;
} else if (entry->property(QStringLiteral("Exec")).isValid()) { for (const KService::Ptr &service : apps) {
KService::Ptr service(static_cast<KService *>(entry.data())); ApplicationData data;
data.name = service->name();
data.icon = service->icon();
data.storageId = service->storageId();
data.uniqueId = service->storageId();
data.entryPath = service->exec();
data.startupNotify = service->property(QStringLiteral("StartupNotify")).toBool();
if (service->isApplication() && if (m_favorites.contains(data.uniqueId)) {
!blacklist.contains(service->desktopEntryName()) && data.location = Favorites;
service->showOnCurrentPlatform() && foundFavorites.insert(data.uniqueId);
!service->property(QStringLiteral("Terminal"), QVariant::Bool).toBool()) { } else if (m_desktopItems.contains(data.uniqueId)) {
data.location = Desktop;
}
ApplicationData data; auto it = m_appPositions.constFind(data.uniqueId);
data.name = service->name(); if (it != m_appPositions.constEnd()) {
data.icon = service->icon(); orderedList[*it] = data;
data.storageId = service->storageId(); } else {
data.uniqueId = service->storageId(); unorderedList << data;
data.entryPath = service->exec();
data.startupNotify = service->property(QStringLiteral("StartupNotify")).toBool();
if (m_favorites.contains(data.uniqueId)) {
data.location = Favorites;
foundFavorites.insert(data.uniqueId);
} else if (m_desktopItems.contains(data.uniqueId)) {
data.location = Desktop;
}
auto it = m_appPositions.constFind(data.uniqueId);
if (it != m_appPositions.constEnd()) {
orderedList[*it] = data;
} else {
unorderedList << data;
}
}
}
}
}
} }
} }