Skip to content

Commit

Permalink
fix: clean up invalid apps after app list changed
Browse files Browse the repository at this point in the history
应用列表变化后,检查不再存在的应用并移除这些应用。

Log:
  • Loading branch information
BLumia committed Jan 3, 2024
1 parent 2b6efb0 commit 18e84ab
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/models/appsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ AppsModel::AppsModel(QObject *parent)
connect(m_appInfoMonitor, &AppInfoMonitor::changed, this, [this](){
qDebug() << "changed";
QList<AppItem *> items(allAppInfosShouldBeShown());
cleanUpInvalidApps(items);
QList<AppItem *> duplicatedItems = updateItems(items);
for (AppItem * item : qAsConst(duplicatedItems)) {
delete item;
Expand Down Expand Up @@ -136,3 +137,18 @@ QList<AppItem *> AppsModel::allAppInfosShouldBeShown() const
}
return items;
}

// remove apps that are not in the \l knownExistedApps list
void AppsModel::cleanUpInvalidApps(const QList<AppItem *> knownExistedApps)
{
QSet<QString> 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);
}
}
}
1 change: 1 addition & 0 deletions src/models/appsmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class AppsModel : public QStandardItemModel
explicit AppsModel(QObject *parent = nullptr);

QList<AppItem *> allAppInfosShouldBeShown() const;
void cleanUpInvalidApps(const QList<AppItem *> knownExistedApps);

AppInfoMonitor * m_appInfoMonitor;

Expand Down

0 comments on commit 18e84ab

Please sign in to comment.