From 7b0430d43ad88fbeda2037a4ca1b0abace7c9688 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sun, 15 Nov 2026 20:45:05 -0800 Subject: [PATCH 1/6] List other drives' trash in Trash What's deleted on another mounted drive goes to that drive's own trash (.Trash-$uid at its top), which Trash never listed: it couldn't be seen, restored or deleted, and with the home trash empty there was no Empty Trash to press either. Trash now lists every trash folder FileOps::trashDirs() finds, sorted together, and watches those with something in them. Restore, Delete and Empty Trash already worked on any of them. Fixes #137 Co-Authored-By: Claude Opus 5.5 --- src/foldermodel.cpp | 37 +++++++++++++++++++++++++++++++++++++ src/foldermodel.h | 6 ++++++ tests/folio_tests.cpp | 29 +++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/src/foldermodel.cpp b/src/foldermodel.cpp index a34caf2c..e64acd31 100644 --- a/src/foldermodel.cpp +++ b/src/foldermodel.cpp @@ -1,4 +1,5 @@ #include "foldermodel.h" +#include "fileops.h" #include #include @@ -278,6 +279,8 @@ QVector FolderModel::list(const QString &path, bool showHidd // Recents are always most recently used first; that's what they're for. return listRecent(recentFile(), showHidden, 200, stop); } + if (isTrash(path)) + return listTrash(FileOps::trashDirs(), showHidden, sort, stop, locale); QVector entries; QDir dir(path); if (!dir.exists()) { @@ -302,6 +305,28 @@ QVector FolderModel::list(const QString &path, bool showHidd return entries; } +bool FolderModel::isTrash(const QString &path) { + return path == QDir::cleanPath(FileOps::homeTrash() + "/files"); +} + +QVector FolderModel::listTrash(const QStringList &trashDirs, bool showHidden, + const QString &sort, const std::function &stop, + const QLocale &locale) { + // A drive's trash is there only once something on it has been trashed, + // and one that can't be read is left out rather than hiding the rest. + QVector entries; + QMimeDatabase mimes; + for (const QString &dir : trashDirs) { + for (QDirIterator it(dir + "/files", entryFilters(showHidden)); it.hasNext();) { + if (stop && stop()) + return {}; + entries.push_back(entryFor(it.nextFileInfo(), mimes)); + } + } + sortEntries(entries, sort, locale); + return entries; +} + FolderModel::Entry FolderModel::entryFor(const QFileInfo &info, const QMimeDatabase &mimes) { Entry e; e.name = info.fileName(); @@ -513,6 +538,18 @@ void FolderModel::reload() { emit selectionChanged(); } } + // The trash lists other drives' trash folders too: those with + // something in them are watched as well. + if (isTrash(m_path)) { + QStringList folders; + for (const Entry &e : m_entries) { + const QString folder = QFileInfo(e.path).absolutePath(); + if (folder != m_path && !folders.contains(folder) && !m_watcher.directories().contains(folder)) + folders << folder; + } + if (!folders.isEmpty()) + m_watcher.addPaths(folders); + } setError(error); setLoading(false); emit loaded(); diff --git a/src/foldermodel.h b/src/foldermodel.h index 3fbc149d..b29d7d05 100644 --- a/src/foldermodel.h +++ b/src/foldermodel.h @@ -137,6 +137,12 @@ class FolderModel : public QAbstractListModel { // whose names contain `query` (ignoring case), or all of them. static bool isPluginView(const QString &path) { return path.startsWith("plugin:") && !path.contains('/'); } static QVector listItems(const QVariantList &items, const QString &query = {}); + // The trash: the home trash's files folder, which lists what's in every + // trash, each mounted drive's own too (see FileOps::trashDirs()), so + // what was deleted on another drive can be seen, restored and emptied. + static bool isTrash(const QString &path); + static QVector listTrash(const QStringList &trashDirs, bool showHidden, const QString &sort, + const std::function &stop = {}, const QLocale &locale = QLocale()); // Recents and plugins' views: lists of their own, not folders. static bool isView(const QString &path) { return path == recentPath() || isPluginView(path); } static QString kindForMime(const QString &mime); diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 42458bd1..36ccf89a 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -1555,6 +1555,35 @@ private slots: QVERIFY(FolderModel::listRecent(dir.filePath("recently-used.xbel"), false, 200, [&] { return ++checks > 3; }).size() == 1); } + void trashListsEveryDrivesTrash() + { + // What was deleted on another drive is in that drive's own trash; + // Trash lists it with the home trash's items, sorted together + // (folders first, as anywhere). + QTemporaryDir data, drive; + const auto restore = dataHomeAt(data.path()); + const QString home = data.filePath("Trash"); + const QString other = drive.filePath(".Trash-" + QString::number(getuid())); + for (const QString &folder : {home + "/files", home + "/info", other + "/files", other + "/info"}) + QDir().mkpath(folder); + write(home + "/files/b.txt", "x"); + write(other + "/files/a.txt", "x"); + QDir().mkpath(other + "/files/c"); + write(other + "/info/a.txt.trashinfo", "[Trash Info]\nPath=/elsewhere/a.txt\n"); + QVERIFY(FolderModel::isTrash(home + "/files")); + QVERIFY(!FolderModel::isTrash(other + "/files")); + // A drive with no trash yet is passed over. + const auto entries = FolderModel::listTrash({home, other, drive.filePath(".Trash/1000")}, false, "name"); + QCOMPARE(entries.size(), 3); + QCOMPARE(entries.at(0).path, other + "/files/c"); + QCOMPARE(entries.at(1).path, other + "/files/a.txt"); + QCOMPARE(entries.at(2).path, home + "/files/b.txt"); + // Each is found where it is, with its note, so it can be restored. + QCOMPARE(FileOps::infoFileFor(entries.at(2).path), home + "/info/b.txt.trashinfo"); + // Left before it's done, it lists nothing. + QVERIFY(FolderModel::listTrash({home, other}, false, "name", [] { return true; }).isEmpty()); + } + void folderSortsFiveWays() { QVector e(4); From da078e7203c63cbfb131345eb54d5cb63cbef5db Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sun, 15 Nov 2026 20:57:45 -0800 Subject: [PATCH 2/6] Search and watch every drive's trash in Trash A search in Trash looked only in the home trash, missing what Trash lists from other drives. And another drive's trash was watched only once it had something in it, so what was trashed there into an empty trash, or a drive's first trash, didn't show until Trash was opened again. Trash's search now runs over every trash's files folder, sorted together and limited in all. Its listing, off the window's thread, also says what to watch: each trash's files folder, empty or not, and the top of each writable drive without a trash yet, where the first trash on it is made. A folder watched for the first time is read once more, for anything that arrived before it was watched. Co-Authored-By: Claude Opus 5.5 --- src/fileops.cpp | 16 ++++++++++ src/fileops.h | 4 +++ src/foldermodel.cpp | 74 ++++++++++++++++++++++++++++++++----------- src/foldermodel.h | 6 ++++ tests/folio_tests.cpp | 20 ++++++++++++ 5 files changed, 101 insertions(+), 19 deletions(-) diff --git a/src/fileops.cpp b/src/fileops.cpp index da94370e..88c72312 100644 --- a/src/fileops.cpp +++ b/src/fileops.cpp @@ -1375,6 +1375,22 @@ QStringList FileOps::trashDirs() { return dirs; } +QStringList FileOps::trashWatchPaths(const QStringList &trashDirs) { + QStringList paths; + for (const QString &dir : trashDirs) + paths << dir + "/files"; + const QString uid = QString::number(getuid()); + for (const QStorageInfo &volume : QStorageInfo::mountedVolumes()) { + const QString root = volume.rootPath(); + if (trashDirs.contains(root + "/.Trash-" + uid) || trashDirs.contains(root + "/.Trash/" + uid)) + continue; + // Only a drive that can be written to at the top gets a trash there. + if (QFileInfo(root).isWritable() && !paths.contains(root)) + paths << root; + } + return paths; +} + bool FileOps::inTrash(const QString &path) const { const QString clean = QDir::cleanPath(path); for (const QString &dir : trashDirs()) diff --git a/src/fileops.h b/src/fileops.h index 9a87ecd8..e9f16f8c 100644 --- a/src/fileops.h +++ b/src/fileops.h @@ -233,6 +233,10 @@ class FileOps : public QObject { static QString homeTrash(); // The trash folders: the home one, then each mounted drive's own. static QStringList trashDirs(); + // What Trash watches while it's open: each trash's files folder, and the + // top of each drive that could have a trash but has none yet, where the + // first thing trashed on it makes one. + static QStringList trashWatchPaths(const QStringList &trashDirs); // For an item directly in a trash's files folder: its note. Empty for // anything else, such as what's inside a trashed folder, which has none. static QString infoFileFor(const QString &trashedPath); diff --git a/src/foldermodel.cpp b/src/foldermodel.cpp index e64acd31..ea65ed66 100644 --- a/src/foldermodel.cpp +++ b/src/foldermodel.cpp @@ -327,6 +327,22 @@ QVector FolderModel::listTrash(const QStringList &trashDirs, return entries; } +QVector FolderModel::searchTrash(const QStringList &trashDirs, const QString &query, + bool showHidden, const QString &sort, + const std::function &stop, int limit, + const QLocale &locale, const QStringList &filter) { + QVector entries; + for (const QString &dir : trashDirs) { + if (entries.size() >= limit) + break; + entries += search(dir + "/files", query, showHidden, sort, stop, limit - entries.size(), locale, filter); + if (stop && stop()) + return {}; + } + sortEntries(entries, sort, locale); + return entries; +} + FolderModel::Entry FolderModel::entryFor(const QFileInfo &info, const QMimeDatabase &mimes) { Entry e; e.name = info.fileName(); @@ -501,6 +517,16 @@ int FolderModel::itemCount(const QString &folder, bool showHidden) { return count; } +namespace { +// What a background listing hands back: its entries, why there are none, +// and what else to watch for changes (for the trash, other drives'). +struct Listing { + QVector entries; + QString error; + QStringList watch; +}; +} + void FolderModel::reload() { if (m_path.isEmpty()) return; @@ -516,13 +542,15 @@ void FolderModel::reload() { const QStringList filter = m_filter; *m_latest = generation; const auto latest = m_latest; - auto *watcher = new QFutureWatcher, QString>>(this); + auto *watcher = new QFutureWatcher(this); connect(watcher, &QFutureWatcherBase::finished, this, [this, watcher, generation] { watcher->deleteLater(); if (generation != m_generation) return; // A newer listing replaced this one while it ran. m_staleClear.stop(); - const auto [entries, error] = watcher->result(); + const Listing listing = watcher->result(); + const QVector &entries = listing.entries; + const QString &error = listing.error; // Unchanged (the usual case after showing a remembered listing): // leave the items alone, and only fill in folder counts still missing. const bool same = error == m_error && !m_entries.isEmpty() && sameListing(m_entries, entries); @@ -538,18 +566,15 @@ void FolderModel::reload() { emit selectionChanged(); } } - // The trash lists other drives' trash folders too: those with - // something in them are watched as well. - if (isTrash(m_path)) { - QStringList folders; - for (const Entry &e : m_entries) { - const QString folder = QFileInfo(e.path).absolutePath(); - if (folder != m_path && !folders.contains(folder) && !m_watcher.directories().contains(folder)) - folders << folder; - } - if (!folders.isEmpty()) - m_watcher.addPaths(folders); - } + // The trash lists every drive's trash, so it watches each, empty or + // not, and the top of each drive that has none yet. Something may + // have arrived in one before it was watched: it's read once more. + QStringList unwatched; + for (const QString &folder : listing.watch) + if (folder != m_path && !m_watcher.directories().contains(folder)) + unwatched << folder; + if (!unwatched.isEmpty() && m_watcher.addPaths(unwatched).size() < unwatched.size()) + m_debounce.start(); setError(error); setLoading(false); emit loaded(); @@ -558,14 +583,25 @@ void FolderModel::reload() { countFolders(generation); }); watcher->setFuture(QtConcurrent::run([path, showHidden, sort, query, items, filter, latest, generation, locale] { - QString error; + Listing listing; + QString &error = listing.error; + auto &entries = listing.entries; const auto overtaken = [&] { return *latest != generation; }; - auto entries = isPluginView(path) ? listItems(items, query) - : query.isEmpty() || path == recentPath() ? list(path, showHidden, sort, &error, overtaken, locale) - : search(path, query, showHidden, sort, overtaken, 1000, locale, filter); + if (isTrash(path)) { + // Finding the drives' trash folders looks over every mount, so + // it's done here, off the window's thread. + const QStringList dirs = FileOps::trashDirs(); + entries = query.isEmpty() ? listTrash(dirs, showHidden, sort, overtaken, locale) + : searchTrash(dirs, query, showHidden, sort, overtaken, 1000, locale, filter); + listing.watch = FileOps::trashWatchPaths(dirs); + } else { + entries = isPluginView(path) ? listItems(items, query) + : query.isEmpty() || path == recentPath() ? list(path, showHidden, sort, &error, overtaken, locale) + : search(path, query, showHidden, sort, overtaken, 1000, locale, filter); + } if (!isPluginView(path)) entries.removeIf([&](const Entry &entry) { return !fits(filter, entry); }); - return qMakePair(entries, error); + return listing; })); } diff --git a/src/foldermodel.h b/src/foldermodel.h index b29d7d05..f42149dd 100644 --- a/src/foldermodel.h +++ b/src/foldermodel.h @@ -143,6 +143,12 @@ class FolderModel : public QAbstractListModel { static bool isTrash(const QString &path); static QVector listTrash(const QStringList &trashDirs, bool showHidden, const QString &sort, const std::function &stop = {}, const QLocale &locale = QLocale()); + // A search of the trash: of every trash's files folder, as search() of + // each, sorted together and stopped at `limit` matches in all. + static QVector searchTrash(const QStringList &trashDirs, const QString &query, bool showHidden, + const QString &sort, const std::function &stop = {}, + int limit = 1000, const QLocale &locale = QLocale(), + const QStringList &filter = {}); // Recents and plugins' views: lists of their own, not folders. static bool isView(const QString &path) { return path == recentPath() || isPluginView(path); } static QString kindForMime(const QString &mime); diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 36ccf89a..4ca52766 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -1582,6 +1582,26 @@ private slots: QCOMPARE(FileOps::infoFileFor(entries.at(2).path), home + "/info/b.txt.trashinfo"); // Left before it's done, it lists nothing. QVERIFY(FolderModel::listTrash({home, other}, false, "name", [] { return true; }).isEmpty()); + + // A search of the trash finds what's in every drive's, inside + // trashed folders too, up to the limit in all. + write(other + "/files/c/a inside.txt", "x"); + write(home + "/files/abc.txt", "x"); + const auto found = FolderModel::searchTrash({home, other}, "a", false, "name"); + QStringList paths; + for (const auto &e : found) + paths << e.path; + QStringList expected{other + "/files/a.txt", other + "/files/c/a inside.txt", home + "/files/abc.txt"}; + paths.sort(); + expected.sort(); + QCOMPARE(paths, expected); + QCOMPARE(FolderModel::searchTrash({home, other}, "a", false, "name", {}, 2).size(), 2); + + // Each trash's files folder is watched while Trash is open, the + // empty ones too, so what's trashed there later shows. + const QStringList watched = FileOps::trashWatchPaths({home, other}); + QVERIFY(watched.contains(home + "/files")); + QVERIFY(watched.contains(other + "/files")); } void folderSortsFiveWays() From 346b38de58d9a334053863a404a8cbf8ede4485f Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sun, 15 Nov 2026 21:11:44 -0800 Subject: [PATCH 3/6] Watch where a drive's first trash is being made, and only what's needed A drive's first trash is made a folder at a time: .Trash-, then its files folder. Trash watched the drive's top until the files folder was there, and a top folder isn't told what's made in its folders, so what was trashed after a listing that ran in between didn't show. Trash now watches the deepest folder of a drive's trash there is so far (and the drive's shared .Trash, or its folder for the user, when there's one). And once a drive has its trash, its top is no longer watched: Trash stops watching what its latest listing doesn't name, so changes there don't list every trash again. Co-Authored-By: Claude Opus 5.5 --- src/fileops.cpp | 23 +++++++++++++++++++---- src/fileops.h | 7 ++++--- src/foldermodel.cpp | 26 ++++++++++++++++++-------- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/fileops.cpp b/src/fileops.cpp index 88c72312..08c9987d 100644 --- a/src/fileops.cpp +++ b/src/fileops.cpp @@ -1379,14 +1379,29 @@ QStringList FileOps::trashWatchPaths(const QStringList &trashDirs) { QStringList paths; for (const QString &dir : trashDirs) paths << dir + "/files"; + // A drive's first trash is made a folder at a time (.Trash-, then + // its files), so on each drive without one the deepest folder of it + // there is so far is watched: what's made next is made in it. const QString uid = QString::number(getuid()); for (const QStorageInfo &volume : QStorageInfo::mountedVolumes()) { const QString root = volume.rootPath(); - if (trashDirs.contains(root + "/.Trash-" + uid) || trashDirs.contains(root + "/.Trash/" + uid)) + const QString own = root + "/.Trash-" + uid, shared = root + "/.Trash"; + if (trashDirs.contains(own) || trashDirs.contains(shared + "/" + uid)) continue; - // Only a drive that can be written to at the top gets a trash there. - if (QFileInfo(root).isWritable() && !paths.contains(root)) - paths << root; + QStringList deepest; + if (QFileInfo(own).isDir()) + deepest << own; + else if (QFileInfo(root).isWritable()) + deepest << root; + // The drive's shared trash, which only its administrator makes, has + // a folder for each user that uses it. + if (QFileInfo(shared + "/" + uid).isDir()) + deepest << shared + "/" + uid; + else if (QFileInfo(shared).isDir()) + deepest << shared; + for (const QString &path : deepest) + if (!paths.contains(path)) + paths << path; } return paths; } diff --git a/src/fileops.h b/src/fileops.h index e9f16f8c..57e5f0fb 100644 --- a/src/fileops.h +++ b/src/fileops.h @@ -233,9 +233,10 @@ class FileOps : public QObject { static QString homeTrash(); // The trash folders: the home one, then each mounted drive's own. static QStringList trashDirs(); - // What Trash watches while it's open: each trash's files folder, and the - // top of each drive that could have a trash but has none yet, where the - // first thing trashed on it makes one. + // What Trash watches while it's open: each trash's files folder, and on + // each drive that could have a trash but has none yet, the deepest part + // of one there is so far (the drive's top, if nothing), where the first + // thing trashed on it makes the rest. static QStringList trashWatchPaths(const QStringList &trashDirs); // For an item directly in a trash's files folder: its note. Empty for // anything else, such as what's inside a trashed folder, which has none. diff --git a/src/foldermodel.cpp b/src/foldermodel.cpp index ea65ed66..46066bff 100644 --- a/src/foldermodel.cpp +++ b/src/foldermodel.cpp @@ -567,14 +567,24 @@ void FolderModel::reload() { } } // The trash lists every drive's trash, so it watches each, empty or - // not, and the top of each drive that has none yet. Something may - // have arrived in one before it was watched: it's read once more. - QStringList unwatched; - for (const QString &folder : listing.watch) - if (folder != m_path && !m_watcher.directories().contains(folder)) - unwatched << folder; - if (!unwatched.isEmpty() && m_watcher.addPaths(unwatched).size() < unwatched.size()) - m_debounce.start(); + // not, and where on each drive without one a trash is made. What + // it no longer needs to watch (a drive's top, once the drive has a + // trash) it stops watching. Something may have arrived in a folder + // before it was watched: the trash is read once more. + if (isTrash(m_path)) { + const QStringList watched = m_watcher.directories(); + QStringList stale, unwatched; + for (const QString &folder : watched) + if (folder != m_path && !listing.watch.contains(folder)) + stale << folder; + for (const QString &folder : listing.watch) + if (folder != m_path && !watched.contains(folder)) + unwatched << folder; + if (!stale.isEmpty()) + m_watcher.removePaths(stale); + if (!unwatched.isEmpty() && m_watcher.addPaths(unwatched).size() < unwatched.size()) + m_debounce.start(); + } setError(error); setLoading(false); emit loaded(); From a321d556190ddd9bf4cdb7d192b534952aee070e Mon Sep 17 00:00:00 2001 From: omarchybot <317366263+omarchybot@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:23:31 +0100 Subject: [PATCH 4/6] List every drive's trash with less The maintainer asked for this to be as simple as it can be. Trash lists and searches every trash's files folder through one listTrash(), on the listing thread, and watches another drive's trash once it has shown something from it. Trash no longer watches the top of every writable drive for a first trash to appear. That watched /tmp and /run/user/, so ordinary activity there re-listed Trash, and each re-listing looked over every mount again. A drive's first trash, made while Trash is open, now shows the next time Trash is opened. Co-Authored-By: Claude Opus 5.5 --- src/fileops.cpp | 31 ------------- src/fileops.h | 5 -- src/foldermodel.cpp | 103 ++++++++++-------------------------------- src/foldermodel.h | 16 ++----- tests/folio_tests.cpp | 60 ++++++------------------ 5 files changed, 45 insertions(+), 170 deletions(-) diff --git a/src/fileops.cpp b/src/fileops.cpp index 08c9987d..da94370e 100644 --- a/src/fileops.cpp +++ b/src/fileops.cpp @@ -1375,37 +1375,6 @@ QStringList FileOps::trashDirs() { return dirs; } -QStringList FileOps::trashWatchPaths(const QStringList &trashDirs) { - QStringList paths; - for (const QString &dir : trashDirs) - paths << dir + "/files"; - // A drive's first trash is made a folder at a time (.Trash-, then - // its files), so on each drive without one the deepest folder of it - // there is so far is watched: what's made next is made in it. - const QString uid = QString::number(getuid()); - for (const QStorageInfo &volume : QStorageInfo::mountedVolumes()) { - const QString root = volume.rootPath(); - const QString own = root + "/.Trash-" + uid, shared = root + "/.Trash"; - if (trashDirs.contains(own) || trashDirs.contains(shared + "/" + uid)) - continue; - QStringList deepest; - if (QFileInfo(own).isDir()) - deepest << own; - else if (QFileInfo(root).isWritable()) - deepest << root; - // The drive's shared trash, which only its administrator makes, has - // a folder for each user that uses it. - if (QFileInfo(shared + "/" + uid).isDir()) - deepest << shared + "/" + uid; - else if (QFileInfo(shared).isDir()) - deepest << shared; - for (const QString &path : deepest) - if (!paths.contains(path)) - paths << path; - } - return paths; -} - bool FileOps::inTrash(const QString &path) const { const QString clean = QDir::cleanPath(path); for (const QString &dir : trashDirs()) diff --git a/src/fileops.h b/src/fileops.h index 57e5f0fb..9a87ecd8 100644 --- a/src/fileops.h +++ b/src/fileops.h @@ -233,11 +233,6 @@ class FileOps : public QObject { static QString homeTrash(); // The trash folders: the home one, then each mounted drive's own. static QStringList trashDirs(); - // What Trash watches while it's open: each trash's files folder, and on - // each drive that could have a trash but has none yet, the deepest part - // of one there is so far (the drive's top, if nothing), where the first - // thing trashed on it makes the rest. - static QStringList trashWatchPaths(const QStringList &trashDirs); // For an item directly in a trash's files folder: its note. Empty for // anything else, such as what's inside a trashed folder, which has none. static QString infoFileFor(const QString &trashedPath); diff --git a/src/foldermodel.cpp b/src/foldermodel.cpp index 46066bff..bb770c4e 100644 --- a/src/foldermodel.cpp +++ b/src/foldermodel.cpp @@ -279,8 +279,6 @@ QVector FolderModel::list(const QString &path, bool showHidd // Recents are always most recently used first; that's what they're for. return listRecent(recentFile(), showHidden, 200, stop); } - if (isTrash(path)) - return listTrash(FileOps::trashDirs(), showHidden, sort, stop, locale); QVector entries; QDir dir(path); if (!dir.exists()) { @@ -309,36 +307,13 @@ bool FolderModel::isTrash(const QString &path) { return path == QDir::cleanPath(FileOps::homeTrash() + "/files"); } -QVector FolderModel::listTrash(const QStringList &trashDirs, bool showHidden, +QVector FolderModel::listTrash(const QStringList &trashDirs, const QString &query, bool showHidden, const QString &sort, const std::function &stop, - const QLocale &locale) { - // A drive's trash is there only once something on it has been trashed, - // and one that can't be read is left out rather than hiding the rest. + const QLocale &locale, const QStringList &filter) { QVector entries; - QMimeDatabase mimes; - for (const QString &dir : trashDirs) { - for (QDirIterator it(dir + "/files", entryFilters(showHidden)); it.hasNext();) { - if (stop && stop()) - return {}; - entries.push_back(entryFor(it.nextFileInfo(), mimes)); - } - } - sortEntries(entries, sort, locale); - return entries; -} - -QVector FolderModel::searchTrash(const QStringList &trashDirs, const QString &query, - bool showHidden, const QString &sort, - const std::function &stop, int limit, - const QLocale &locale, const QStringList &filter) { - QVector entries; - for (const QString &dir : trashDirs) { - if (entries.size() >= limit) - break; - entries += search(dir + "/files", query, showHidden, sort, stop, limit - entries.size(), locale, filter); - if (stop && stop()) - return {}; - } + for (const QString &dir : trashDirs) + entries += query.isEmpty() ? list(dir + "/files", showHidden, sort, nullptr, stop, locale) + : search(dir + "/files", query, showHidden, sort, stop, 1000, locale, filter); sortEntries(entries, sort, locale); return entries; } @@ -517,16 +492,6 @@ int FolderModel::itemCount(const QString &folder, bool showHidden) { return count; } -namespace { -// What a background listing hands back: its entries, why there are none, -// and what else to watch for changes (for the trash, other drives'). -struct Listing { - QVector entries; - QString error; - QStringList watch; -}; -} - void FolderModel::reload() { if (m_path.isEmpty()) return; @@ -542,15 +507,13 @@ void FolderModel::reload() { const QStringList filter = m_filter; *m_latest = generation; const auto latest = m_latest; - auto *watcher = new QFutureWatcher(this); + auto *watcher = new QFutureWatcher, QString>>(this); connect(watcher, &QFutureWatcherBase::finished, this, [this, watcher, generation] { watcher->deleteLater(); if (generation != m_generation) return; // A newer listing replaced this one while it ran. m_staleClear.stop(); - const Listing listing = watcher->result(); - const QVector &entries = listing.entries; - const QString &error = listing.error; + const auto [entries, error] = watcher->result(); // Unchanged (the usual case after showing a remembered listing): // leave the items alone, and only fill in folder counts still missing. const bool same = error == m_error && !m_entries.isEmpty() && sameListing(m_entries, entries); @@ -566,24 +529,16 @@ void FolderModel::reload() { emit selectionChanged(); } } - // The trash lists every drive's trash, so it watches each, empty or - // not, and where on each drive without one a trash is made. What - // it no longer needs to watch (a drive's top, once the drive has a - // trash) it stops watching. Something may have arrived in a folder - // before it was watched: the trash is read once more. - if (isTrash(m_path)) { - const QStringList watched = m_watcher.directories(); - QStringList stale, unwatched; - for (const QString &folder : watched) - if (folder != m_path && !listing.watch.contains(folder)) - stale << folder; - for (const QString &folder : listing.watch) - if (folder != m_path && !watched.contains(folder)) - unwatched << folder; - if (!stale.isEmpty()) - m_watcher.removePaths(stale); - if (!unwatched.isEmpty() && m_watcher.addPaths(unwatched).size() < unwatched.size()) - m_debounce.start(); + // Another drive's trash is watched once something is seen in it. + if (isTrash(m_path) && m_query.isEmpty()) { + QStringList folders; + for (const Entry &e : entries) + if (const QString folder = QFileInfo(e.path).absolutePath(); !folders.contains(folder)) + folders << folder; + for (const QString &watched : m_watcher.directories()) + folders.removeAll(watched); + if (!folders.isEmpty()) + m_watcher.addPaths(folders); } setError(error); setLoading(false); @@ -593,25 +548,17 @@ void FolderModel::reload() { countFolders(generation); }); watcher->setFuture(QtConcurrent::run([path, showHidden, sort, query, items, filter, latest, generation, locale] { - Listing listing; - QString &error = listing.error; - auto &entries = listing.entries; + QString error; const auto overtaken = [&] { return *latest != generation; }; - if (isTrash(path)) { - // Finding the drives' trash folders looks over every mount, so - // it's done here, off the window's thread. - const QStringList dirs = FileOps::trashDirs(); - entries = query.isEmpty() ? listTrash(dirs, showHidden, sort, overtaken, locale) - : searchTrash(dirs, query, showHidden, sort, overtaken, 1000, locale, filter); - listing.watch = FileOps::trashWatchPaths(dirs); - } else { - entries = isPluginView(path) ? listItems(items, query) - : query.isEmpty() || path == recentPath() ? list(path, showHidden, sort, &error, overtaken, locale) - : search(path, query, showHidden, sort, overtaken, 1000, locale, filter); - } + // Finding the drives' trash looks over every mount, so it's done here, + // off the window's thread. + auto entries = isPluginView(path) ? listItems(items, query) + : isTrash(path) ? listTrash(FileOps::trashDirs(), query, showHidden, sort, overtaken, locale, filter) + : query.isEmpty() || path == recentPath() ? list(path, showHidden, sort, &error, overtaken, locale) + : search(path, query, showHidden, sort, overtaken, 1000, locale, filter); if (!isPluginView(path)) entries.removeIf([&](const Entry &entry) { return !fits(filter, entry); }); - return listing; + return qMakePair(entries, error); })); } diff --git a/src/foldermodel.h b/src/foldermodel.h index f42149dd..a0d6c2c8 100644 --- a/src/foldermodel.h +++ b/src/foldermodel.h @@ -137,18 +137,12 @@ class FolderModel : public QAbstractListModel { // whose names contain `query` (ignoring case), or all of them. static bool isPluginView(const QString &path) { return path.startsWith("plugin:") && !path.contains('/'); } static QVector listItems(const QVariantList &items, const QString &query = {}); - // The trash: the home trash's files folder, which lists what's in every - // trash, each mounted drive's own too (see FileOps::trashDirs()), so - // what was deleted on another drive can be seen, restored and emptied. + // The trash is the home trash's files folder, and it lists every trash's: + // what's deleted on another drive goes to that drive's own. static bool isTrash(const QString &path); - static QVector listTrash(const QStringList &trashDirs, bool showHidden, const QString &sort, - const std::function &stop = {}, const QLocale &locale = QLocale()); - // A search of the trash: of every trash's files folder, as search() of - // each, sorted together and stopped at `limit` matches in all. - static QVector searchTrash(const QStringList &trashDirs, const QString &query, bool showHidden, - const QString &sort, const std::function &stop = {}, - int limit = 1000, const QLocale &locale = QLocale(), - const QStringList &filter = {}); + static QVector listTrash(const QStringList &trashDirs, const QString &query, bool showHidden, + const QString &sort, const std::function &stop = {}, + const QLocale &locale = QLocale(), const QStringList &filter = {}); // Recents and plugins' views: lists of their own, not folders. static bool isView(const QString &path) { return path == recentPath() || isPluginView(path); } static QString kindForMime(const QString &mime); diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 4ca52766..77a12ccd 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -1557,51 +1557,21 @@ private slots: void trashListsEveryDrivesTrash() { - // What was deleted on another drive is in that drive's own trash; - // Trash lists it with the home trash's items, sorted together - // (folders first, as anywhere). - QTemporaryDir data, drive; - const auto restore = dataHomeAt(data.path()); - const QString home = data.filePath("Trash"); - const QString other = drive.filePath(".Trash-" + QString::number(getuid())); - for (const QString &folder : {home + "/files", home + "/info", other + "/files", other + "/info"}) - QDir().mkpath(folder); - write(home + "/files/b.txt", "x"); - write(other + "/files/a.txt", "x"); - QDir().mkpath(other + "/files/c"); - write(other + "/info/a.txt.trashinfo", "[Trash Info]\nPath=/elsewhere/a.txt\n"); - QVERIFY(FolderModel::isTrash(home + "/files")); - QVERIFY(!FolderModel::isTrash(other + "/files")); - // A drive with no trash yet is passed over. - const auto entries = FolderModel::listTrash({home, other, drive.filePath(".Trash/1000")}, false, "name"); - QCOMPARE(entries.size(), 3); - QCOMPARE(entries.at(0).path, other + "/files/c"); - QCOMPARE(entries.at(1).path, other + "/files/a.txt"); - QCOMPARE(entries.at(2).path, home + "/files/b.txt"); - // Each is found where it is, with its note, so it can be restored. - QCOMPARE(FileOps::infoFileFor(entries.at(2).path), home + "/info/b.txt.trashinfo"); - // Left before it's done, it lists nothing. - QVERIFY(FolderModel::listTrash({home, other}, false, "name", [] { return true; }).isEmpty()); - - // A search of the trash finds what's in every drive's, inside - // trashed folders too, up to the limit in all. - write(other + "/files/c/a inside.txt", "x"); - write(home + "/files/abc.txt", "x"); - const auto found = FolderModel::searchTrash({home, other}, "a", false, "name"); - QStringList paths; - for (const auto &e : found) - paths << e.path; - QStringList expected{other + "/files/a.txt", other + "/files/c/a inside.txt", home + "/files/abc.txt"}; - paths.sort(); - expected.sort(); - QCOMPARE(paths, expected); - QCOMPARE(FolderModel::searchTrash({home, other}, "a", false, "name", {}, 2).size(), 2); - - // Each trash's files folder is watched while Trash is open, the - // empty ones too, so what's trashed there later shows. - const QStringList watched = FileOps::trashWatchPaths({home, other}); - QVERIFY(watched.contains(home + "/files")); - QVERIFY(watched.contains(other + "/files")); + // Another drive's trash is listed and searched with the home one's, + // sorted together; a drive without one is passed over. + QTemporaryDir home, other; + QDir().mkpath(home.filePath("files")); + QDir().mkpath(other.filePath("files")); + write(home.filePath("files/b.txt"), "x"); + write(other.filePath("files/a.txt"), "x"); + const QStringList dirs{home.path(), other.path(), other.filePath("none")}; + const auto entries = FolderModel::listTrash(dirs, {}, false, "name"); + QCOMPARE(entries.size(), 2); + QCOMPARE(entries.at(0).path, other.filePath("files/a.txt")); + QCOMPARE(entries.at(1).path, home.filePath("files/b.txt")); + const auto found = FolderModel::listTrash(dirs, "a", false, "name"); + QCOMPARE(found.size(), 1); + QCOMPARE(found.at(0).path, other.filePath("files/a.txt")); } void folderSortsFiveWays() From a3abbcaedf5d8b0a385301c3d1cd5b5a5de6532b Mon Sep 17 00:00:00 2001 From: omarchybot <317366263+omarchybot@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:23:31 +0100 Subject: [PATCH 5/6] Watch every drive's trash while the trash is shown Watching only the trashes something was listed from missed an empty one: what another app, or another Folio pane, trashed onto that drive while the trash was open didn't show until it was opened again. The listing now hands back the trash folders it found, and each one's files folder is watched, as the home trash's is. Co-Authored-By: Claude Opus 5.5 Co-Authored-By: Codex Medium --- src/foldermodel.cpp | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/foldermodel.cpp b/src/foldermodel.cpp index bb770c4e..ae222cab 100644 --- a/src/foldermodel.cpp +++ b/src/foldermodel.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include FolderModel::FolderModel(QObject *parent) : QAbstractListModel(parent) { @@ -507,13 +508,13 @@ void FolderModel::reload() { const QStringList filter = m_filter; *m_latest = generation; const auto latest = m_latest; - auto *watcher = new QFutureWatcher, QString>>(this); + auto *watcher = new QFutureWatcher, QString, QStringList>>(this); connect(watcher, &QFutureWatcherBase::finished, this, [this, watcher, generation] { watcher->deleteLater(); if (generation != m_generation) return; // A newer listing replaced this one while it ran. m_staleClear.stop(); - const auto [entries, error] = watcher->result(); + const auto [entries, error, trashDirs] = watcher->result(); // Unchanged (the usual case after showing a remembered listing): // leave the items alone, and only fill in folder counts still missing. const bool same = error == m_error && !m_entries.isEmpty() && sameListing(m_entries, entries); @@ -529,17 +530,10 @@ void FolderModel::reload() { emit selectionChanged(); } } - // Another drive's trash is watched once something is seen in it. - if (isTrash(m_path) && m_query.isEmpty()) { - QStringList folders; - for (const Entry &e : entries) - if (const QString folder = QFileInfo(e.path).absolutePath(); !folders.contains(folder)) - folders << folder; - for (const QString &watched : m_watcher.directories()) - folders.removeAll(watched); - if (!folders.isEmpty()) - m_watcher.addPaths(folders); - } + // Every drive's trash is watched while the trash is shown, as the home one is. + for (const QString &dir : trashDirs.mid(1)) + if (!m_watcher.directories().contains(dir + "/files")) + m_watcher.addPath(dir + "/files"); setError(error); setLoading(false); emit loaded(); @@ -552,13 +546,14 @@ void FolderModel::reload() { const auto overtaken = [&] { return *latest != generation; }; // Finding the drives' trash looks over every mount, so it's done here, // off the window's thread. + const QStringList trashDirs = isTrash(path) ? FileOps::trashDirs() : QStringList(); auto entries = isPluginView(path) ? listItems(items, query) - : isTrash(path) ? listTrash(FileOps::trashDirs(), query, showHidden, sort, overtaken, locale, filter) + : isTrash(path) ? listTrash(trashDirs, query, showHidden, sort, overtaken, locale, filter) : query.isEmpty() || path == recentPath() ? list(path, showHidden, sort, &error, overtaken, locale) : search(path, query, showHidden, sort, overtaken, 1000, locale, filter); if (!isPluginView(path)) entries.removeIf([&](const Entry &entry) { return !fits(filter, entry); }); - return qMakePair(entries, error); + return std::make_tuple(entries, error, trashDirs); })); } From 8a78e709dfd16799c2f2d49d93e4212d4c5e0d14 Mon Sep 17 00:00:00 2001 From: omarchybot <317366263+omarchybot@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:15:57 +0100 Subject: [PATCH 6/6] Share a search's limit across the trashes, and stop between them Simplifying listTrash() dropped two things the original searchTrash() and listTrash() had. Each trash got a fresh 1000-match limit, so a search kept going through later drives after earlier ones had filled it. And nothing checked stop between trashes, so a listing left behind still went on to read each remaining drive. One limit now covers every trash, no further drive is searched once it is reached, and a listing that is stopped returns before the next drive. Co-Authored-By: Claude Opus 5.5 --- src/foldermodel.cpp | 15 +++++++++++---- src/foldermodel.h | 2 +- tests/folio_tests.cpp | 5 +++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/foldermodel.cpp b/src/foldermodel.cpp index ae222cab..7d3d6ee8 100644 --- a/src/foldermodel.cpp +++ b/src/foldermodel.cpp @@ -310,11 +310,18 @@ bool FolderModel::isTrash(const QString &path) { QVector FolderModel::listTrash(const QStringList &trashDirs, const QString &query, bool showHidden, const QString &sort, const std::function &stop, - const QLocale &locale, const QStringList &filter) { + int limit, const QLocale &locale, const QStringList &filter) { + // A search's limit is for every trash together, and a listing left + // behind stops before it reads another drive. QVector entries; - for (const QString &dir : trashDirs) + for (const QString &dir : trashDirs) { + if (stop && stop()) + return {}; + if (!query.isEmpty() && entries.size() >= limit) + break; entries += query.isEmpty() ? list(dir + "/files", showHidden, sort, nullptr, stop, locale) - : search(dir + "/files", query, showHidden, sort, stop, 1000, locale, filter); + : search(dir + "/files", query, showHidden, sort, stop, limit - entries.size(), locale, filter); + } sortEntries(entries, sort, locale); return entries; } @@ -548,7 +555,7 @@ void FolderModel::reload() { // off the window's thread. const QStringList trashDirs = isTrash(path) ? FileOps::trashDirs() : QStringList(); auto entries = isPluginView(path) ? listItems(items, query) - : isTrash(path) ? listTrash(trashDirs, query, showHidden, sort, overtaken, locale, filter) + : isTrash(path) ? listTrash(trashDirs, query, showHidden, sort, overtaken, 1000, locale, filter) : query.isEmpty() || path == recentPath() ? list(path, showHidden, sort, &error, overtaken, locale) : search(path, query, showHidden, sort, overtaken, 1000, locale, filter); if (!isPluginView(path)) diff --git a/src/foldermodel.h b/src/foldermodel.h index a0d6c2c8..16806b2b 100644 --- a/src/foldermodel.h +++ b/src/foldermodel.h @@ -141,7 +141,7 @@ class FolderModel : public QAbstractListModel { // what's deleted on another drive goes to that drive's own. static bool isTrash(const QString &path); static QVector listTrash(const QStringList &trashDirs, const QString &query, bool showHidden, - const QString &sort, const std::function &stop = {}, + const QString &sort, const std::function &stop = {}, int limit = 1000, const QLocale &locale = QLocale(), const QStringList &filter = {}); // Recents and plugins' views: lists of their own, not folders. static bool isView(const QString &path) { return path == recentPath() || isPluginView(path); } diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 77a12ccd..15b39ceb 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -1572,6 +1572,11 @@ private slots: const auto found = FolderModel::listTrash(dirs, "a", false, "name"); QCOMPARE(found.size(), 1); QCOMPARE(found.at(0).path, other.filePath("files/a.txt")); + // A search's limit is for every trash together, and a listing left + // once the first trash is read reads no other. + QCOMPARE(FolderModel::listTrash(dirs, "txt", false, "name", {}, 1).size(), 1); + int checks = 0; + QVERIFY(FolderModel::listTrash(dirs, {}, false, "name", [&] { return ++checks > 2; }).isEmpty()); } void folderSortsFiveWays()