From 18e84ab3a63727e758fbdb45ef76c67b3bf51519 Mon Sep 17 00:00:00 2001 From: Wang Zichong Date: Wed, 3 Jan 2024 17:42:28 +0800 Subject: [PATCH] fix: clean up invalid apps after app list changed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 应用列表变化后,检查不再存在的应用并移除这些应用。 Log: --- src/models/appsmodel.cpp | 16 ++++++++++++++++ src/models/appsmodel.h | 1 + 2 files changed, 17 insertions(+) diff --git a/src/models/appsmodel.cpp b/src/models/appsmodel.cpp index 2f45583a..a1b1f391 100644 --- a/src/models/appsmodel.cpp +++ b/src/models/appsmodel.cpp @@ -31,6 +31,7 @@ AppsModel::AppsModel(QObject *parent) connect(m_appInfoMonitor, &AppInfoMonitor::changed, this, [this](){ qDebug() << "changed"; QList items(allAppInfosShouldBeShown()); + cleanUpInvalidApps(items); QList duplicatedItems = updateItems(items); for (AppItem * item : qAsConst(duplicatedItems)) { delete item; @@ -136,3 +137,18 @@ QList AppsModel::allAppInfosShouldBeShown() const } return items; } + +// remove apps that are not in the \l knownExistedApps list +void AppsModel::cleanUpInvalidApps(const QList knownExistedApps) +{ + QSet existedApps; + for (const AppItem * app : knownExistedApps) { + existedApps.insert(app->freedesktopId()); + } + for (int i = rowCount() - 1; i >= 0; i--) { + const QString & appId(data(index(i, 0), AppItem::DesktopIdRole).toString()); + if (!existedApps.contains(appId)) { + removeRow(i); + } + } +} diff --git a/src/models/appsmodel.h b/src/models/appsmodel.h index d1b3fb15..2ea7a22b 100644 --- a/src/models/appsmodel.h +++ b/src/models/appsmodel.h @@ -40,6 +40,7 @@ class AppsModel : public QStandardItemModel explicit AppsModel(QObject *parent = nullptr); QList allAppInfosShouldBeShown() const; + void cleanUpInvalidApps(const QList knownExistedApps); AppInfoMonitor * m_appInfoMonitor;