From e294adf96adcc5c5897c74b3af270ff9242bde08 Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Wed, 24 Sep 2025 12:17:26 +1000 Subject: plugin/fsm: fix crash Fixes #663 Fixes #681 Fixes #647 also build dev plugin with RelWithDebInfo --- plugin/src/Caelestia/Models/filesystemmodel.cpp | 36 +++++++++---------------- plugin/src/Caelestia/Models/filesystemmodel.hpp | 3 --- plugin/src/Caelestia/appdb.cpp | 4 +-- 3 files changed, 13 insertions(+), 30 deletions(-) (limited to 'plugin') diff --git a/plugin/src/Caelestia/Models/filesystemmodel.cpp b/plugin/src/Caelestia/Models/filesystemmodel.cpp index 0eadebb..81c07d9 100644 --- a/plugin/src/Caelestia/Models/filesystemmodel.cpp +++ b/plugin/src/Caelestia/Models/filesystemmodel.cpp @@ -266,14 +266,11 @@ void FileSystemModel::updateWatcher() { void FileSystemModel::updateEntries() { if (m_path.isEmpty()) { if (!m_entries.isEmpty()) { - auto toDelete = m_entries; - beginResetModel(); + qDeleteAll(m_entries); m_entries.clear(); endResetModel(); emit entriesChanged(); - - qDeleteAll(toDelete); } return; @@ -396,8 +393,7 @@ void FileSystemModel::applyChanges(const QSet& removedPaths, const QSet } std::sort(removedIndices.begin(), removedIndices.end(), std::greater()); - QList toDelete; - + // Batch remove old entries int start = -1; int end = -1; for (int idx : std::as_const(removedIndices)) { @@ -409,8 +405,7 @@ void FileSystemModel::applyChanges(const QSet& removedPaths, const QSet } else { beginRemoveRows(QModelIndex(), end, start); for (int i = start; i >= end; --i) { - emit removed(m_entries[i]->path()); - toDelete << m_entries.takeAt(i); + m_entries.takeAt(i)->deleteLater(); } endRemoveRows(); @@ -421,12 +416,12 @@ void FileSystemModel::applyChanges(const QSet& removedPaths, const QSet if (start != -1) { beginRemoveRows(QModelIndex(), end, start); for (int i = start; i >= end; --i) { - emit removed(m_entries[i]->path()); - toDelete << m_entries.takeAt(i); + m_entries.takeAt(i)->deleteLater(); } endRemoveRows(); } + // Create new entries QList newEntries; for (const auto& path : addedPaths) { newEntries << new FileSystemEntry(path, m_dir.relativeFilePath(path), this); @@ -435,57 +430,50 @@ void FileSystemModel::applyChanges(const QSet& removedPaths, const QSet return compareEntries(a, b); }); + // Batch insert new entries int insertStart = -1; - int prevRow = -1; QList batchItems; for (const auto& entry : std::as_const(newEntries)) { const auto it = std::lower_bound( m_entries.begin(), m_entries.end(), entry, [this](const FileSystemEntry* a, const FileSystemEntry* b) { return compareEntries(a, b); }); - int row = static_cast(it - m_entries.begin()); + const auto row = static_cast(it - m_entries.begin()); if (insertStart == -1) { insertStart = row; - prevRow = row; - batchItems.clear(); batchItems << entry; - } else if (row == prevRow + 1) { - prevRow = row; + } else if (row == insertStart + batchItems.size()) { batchItems << entry; } else { - beginInsertRows(QModelIndex(), insertStart, static_cast(insertStart + batchItems.size() - 1)); + beginInsertRows(QModelIndex(), insertStart, insertStart + static_cast(batchItems.size()) - 1); for (int i = 0; i < batchItems.size(); ++i) { m_entries.insert(insertStart + i, batchItems[i]); - emit added(batchItems[i]); } endInsertRows(); insertStart = row; - prevRow = row; batchItems.clear(); batchItems << entry; } - prevRow = static_cast(m_entries.indexOf(entry)); } if (!batchItems.isEmpty()) { - beginInsertRows(QModelIndex(), insertStart, static_cast(insertStart + batchItems.size() - 1)); + beginInsertRows(QModelIndex(), insertStart, insertStart + static_cast(batchItems.size()) - 1); for (int i = 0; i < batchItems.size(); ++i) { m_entries.insert(insertStart + i, batchItems[i]); - emit added(batchItems[i]); } endInsertRows(); } emit entriesChanged(); - qDeleteAll(toDelete); } bool FileSystemModel::compareEntries(const FileSystemEntry* a, const FileSystemEntry* b) const { if (a->isDir() != b->isDir()) { return m_sortReverse ^ a->isDir(); } - return m_sortReverse ^ (a->relativePath().localeAwareCompare(b->relativePath()) < 0); + const auto cmp = a->relativePath().localeAwareCompare(b->relativePath()); + return m_sortReverse ? cmp > 0 : cmp < 0; } } // namespace caelestia diff --git a/plugin/src/Caelestia/Models/filesystemmodel.hpp b/plugin/src/Caelestia/Models/filesystemmodel.hpp index 822babd..9178e0c 100644 --- a/plugin/src/Caelestia/Models/filesystemmodel.hpp +++ b/plugin/src/Caelestia/Models/filesystemmodel.hpp @@ -121,9 +121,6 @@ signals: void nameFiltersChanged(); void entriesChanged(); - void added(const caelestia::FileSystemEntry* entry); - void removed(const QString& path); - private: QDir m_dir; QFileSystemWatcher m_watcher; diff --git a/plugin/src/Caelestia/appdb.cpp b/plugin/src/Caelestia/appdb.cpp index 1fa2d77..72b346f 100644 --- a/plugin/src/Caelestia/appdb.cpp +++ b/plugin/src/Caelestia/appdb.cpp @@ -207,18 +207,16 @@ void AppDb::updateApps() { newIds.insert(entry->property("id").toString()); } - QList toDelete; for (auto it = m_apps.keyBegin(); it != m_apps.keyEnd(); ++it) { const auto& id = *it; if (!newIds.contains(id)) { dirty = true; - toDelete << m_apps.take(id); + m_apps.take(id)->deleteLater(); } } if (dirty) { emit appsChanged(); - qDeleteAll(toDelete); } } -- cgit v1.2.3-freya