Skip to content

Commit 92f15a6

Browse files
committed
core/desktopentry: watch parents, simplify debounce logic
1 parent 21860b4 commit 92f15a6

File tree

4 files changed

+186
-118
lines changed

4 files changed

+186
-118
lines changed

src/core/desktopentry.cpp

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "desktopentry.hpp"
22
#include <algorithm>
3+
#include <utility>
34

45
#include <qcontainerfwd.h>
56
#include <qdebug.h>
@@ -192,6 +193,9 @@ ParsedDesktopEntryData DesktopEntry::parseText(const QString& id, const QString&
192193
}
193194

194195
void DesktopEntry::updateState(const ParsedDesktopEntryData& newState) {
196+
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
197+
QScopedPropertyUpdateGroup group;
198+
#endif
195199
this->bName = newState.name;
196200
this->bGenericName = newState.genericName;
197201
this->bStartupClass = newState.startupClass;
@@ -210,19 +214,33 @@ void DesktopEntry::updateState(const ParsedDesktopEntryData& newState) {
210214
}
211215

212216
void DesktopEntry::updateActions(const QHash<QString, DesktopActionData>& newActions) {
213-
for (auto* action: this->mActions.values()) {
214-
action->deleteLater();
217+
auto old = this->mActions;
218+
219+
for (auto it = newActions.cbegin(); it != newActions.cend(); ++it) {
220+
const auto& key = it.key();
221+
const auto& d = it.value();
222+
223+
DesktopAction* act = nullptr;
224+
if (auto found = old.find(key); found != old.end()) {
225+
act = found.value();
226+
old.erase(found);
227+
} else {
228+
act = new DesktopAction(d.id, this);
229+
this->mActions.insert(key, act);
230+
}
231+
232+
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
233+
QScopedPropertyUpdateGroup group;
234+
#endif
235+
act->bName = d.name;
236+
act->bIcon = d.icon;
237+
act->bExecString = d.execString;
238+
act->bCommand = d.command;
239+
act->mEntries = d.entries;
215240
}
216-
this->mActions.clear();
217-
218-
for (const auto& [actionName, actionData]: newActions.asKeyValueRange()) {
219-
auto* action = new DesktopAction(actionData.id, this);
220-
action->mEntries = actionData.entries;
221-
action->mName = actionData.name;
222-
action->mIcon = actionData.icon;
223-
action->mExecString = actionData.execString;
224-
action->mCommand = actionData.command;
225-
this->mActions.insert(actionName, action);
241+
242+
for (auto* leftover: std::as_const(old)) {
243+
leftover->deleteLater();
226244
}
227245
}
228246

@@ -306,7 +324,7 @@ void DesktopEntry::doExec(const QList<QString>& execString, const QString& worki
306324
}
307325

308326
void DesktopAction::execute() const {
309-
DesktopEntry::doExec(this->mCommand, this->entry->bWorkingDirectory.value());
327+
DesktopEntry::doExec(this->bCommand.value(), this->entry->bWorkingDirectory.value());
310328
}
311329

312330
DesktopEntryScanner::DesktopEntryScanner(DesktopEntryManager* manager): manager(manager) {
@@ -315,7 +333,7 @@ DesktopEntryScanner::DesktopEntryScanner(DesktopEntryManager* manager): manager(
315333

316334
void DesktopEntryScanner::run() {
317335
auto desktopPaths = DesktopEntryManager::desktopPaths();
318-
auto scanResults = DesktopEntryScanResults();
336+
auto scanResults = QList<ParsedDesktopEntryData>();
319337

320338
for (int i = desktopPaths.size() - 1; i >= 0; --i) {
321339
const auto& path = desktopPaths.at(i);
@@ -327,13 +345,12 @@ void DesktopEntryScanner::run() {
327345

328346
QMetaObject::invokeMethod(
329347
this->manager,
330-
&DesktopEntryManager::onScanCompleted,
331-
Qt::QueuedConnection,
332-
scanResults
348+
[mgr = this->manager, results = std::move(scanResults)]() { mgr->onScanCompleted(results); },
349+
Qt::QueuedConnection
333350
);
334351
}
335352

336-
void DesktopEntryScanner::scanDirectory(const QDir& dir, DesktopEntryScanResults& entries) {
353+
void DesktopEntryScanner::scanDirectory(const QDir& dir, QList<ParsedDesktopEntryData>& entries) {
337354
auto dirEntries = dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
338355

339356
for (auto& entry: dirEntries) {
@@ -362,9 +379,6 @@ void DesktopEntryScanner::scanDirectory(const QDir& dir, DesktopEntryScanResults
362379
}
363380

364381
DesktopEntryManager::DesktopEntryManager() {
365-
qRegisterMetaType<ParsedDesktopEntryData>("ParsedDesktopEntryData");
366-
qRegisterMetaType<DesktopEntryScanResults>("DesktopEntryScanResults");
367-
368382
this->monitor = new DesktopEntryMonitor(this);
369383
connect(
370384
this->monitor,
@@ -466,33 +480,23 @@ const QStringList& DesktopEntryManager::desktopPaths() {
466480
auto dataPaths = QStringList();
467481

468482
auto dataHome = qEnvironmentVariable("XDG_DATA_HOME");
469-
if (dataHome.isEmpty()) {
470-
if (qEnvironmentVariableIsSet("HOME")) {
471-
dataHome = qEnvironmentVariable("HOME") + "/.local/share";
472-
}
473-
}
474-
if (!dataHome.isEmpty()) {
475-
dataPaths.append(dataHome + "/applications");
476-
}
483+
if (dataHome.isEmpty() && qEnvironmentVariableIsSet("HOME"))
484+
dataHome = qEnvironmentVariable("HOME") + "/.local/share";
485+
if (!dataHome.isEmpty()) dataPaths.append(dataHome + "/applications");
477486

478487
auto dataDirs = qEnvironmentVariable("XDG_DATA_DIRS");
479-
if (dataDirs.isEmpty()) {
480-
dataDirs = "/usr/local/share:/usr/share";
481-
}
488+
if (dataDirs.isEmpty()) dataDirs = "/usr/local/share:/usr/share";
482489

483-
for (const auto& dir: dataDirs.split(':', Qt::SkipEmptyParts)) {
484-
if (!dir.isEmpty()) {
485-
dataPaths.append(dir + "/applications");
486-
}
487-
}
490+
for (const auto& dir: dataDirs.split(':', Qt::SkipEmptyParts))
491+
dataPaths.append(dir + "/applications");
488492

489493
return dataPaths;
490494
}();
491495

492496
return paths;
493497
}
494498

495-
void DesktopEntryManager::onScanCompleted(const DesktopEntryScanResults& scanResults) {
499+
void DesktopEntryManager::onScanCompleted(const QList<ParsedDesktopEntryData>& scanResults) {
496500
auto guard = qScopeGuard([this] { this->scanInProgress = false; });
497501

498502
auto oldEntries = this->desktopEntries;
@@ -522,14 +526,11 @@ void DesktopEntryManager::onScanCompleted(const DesktopEntryScanResults& scanRes
522526
qCDebug(logDesktopEntry) << "Found desktop entry" << data.id;
523527

524528
auto lowerId = data.id.toLower();
525-
526529
auto conflictingId = newEntries.contains(data.id);
527530

528531
if (conflictingId) {
529532
qCDebug(logDesktopEntry) << "Replacing old entry for" << data.id;
530-
if (auto* victim = newEntries.take(data.id)) {
531-
victim->deleteLater();
532-
}
533+
if (auto* victim = newEntries.take(data.id)) victim->deleteLater();
533534
newLowercaseEntries.remove(lowerId);
534535
}
535536

@@ -551,17 +552,12 @@ void DesktopEntryManager::onScanCompleted(const DesktopEntryScanResults& scanRes
551552
this->lowercaseDesktopEntries = newLowercaseEntries;
552553

553554
auto newApplications = QVector<DesktopEntry*>();
554-
for (auto& entry: this->desktopEntries.values()) {
555-
if (!entry->noDisplay()) {
556-
newApplications.append(entry);
557-
}
558-
}
555+
for (auto* entry: this->desktopEntries.values())
556+
if (!entry->noDisplay()) newApplications.append(entry);
559557

560558
this->mApplications.diffUpdate(newApplications);
561559

562-
for (auto* e: oldEntries) {
563-
e->deleteLater();
564-
}
560+
for (auto* e: oldEntries) e->deleteLater();
565561

566562
emit applicationsChanged();
567563
}

0 commit comments

Comments
 (0)