diff --git a/src/foldermodel.cpp b/src/foldermodel.cpp index a34caf2c..7d3d6ee8 100644 --- a/src/foldermodel.cpp +++ b/src/foldermodel.cpp @@ -1,4 +1,5 @@ #include "foldermodel.h" +#include "fileops.h" #include #include @@ -13,6 +14,7 @@ #include #include #include +#include #include FolderModel::FolderModel(QObject *parent) : QAbstractListModel(parent) { @@ -302,6 +304,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, const QString &query, bool showHidden, + const QString &sort, const std::function &stop, + 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) { + 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, limit - entries.size(), locale, filter); + } + sortEntries(entries, sort, locale); + return entries; +} + FolderModel::Entry FolderModel::entryFor(const QFileInfo &info, const QMimeDatabase &mimes) { Entry e; e.name = info.fileName(); @@ -491,13 +515,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); @@ -513,6 +537,10 @@ void FolderModel::reload() { emit selectionChanged(); } } + // 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(); @@ -523,12 +551,16 @@ void FolderModel::reload() { watcher->setFuture(QtConcurrent::run([path, showHidden, sort, query, items, filter, latest, generation, locale] { QString error; 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(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)) entries.removeIf([&](const Entry &entry) { return !fits(filter, entry); }); - return qMakePair(entries, error); + return std::make_tuple(entries, error, trashDirs); })); } diff --git a/src/foldermodel.h b/src/foldermodel.h index 3fbc149d..16806b2b 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 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, 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 42458bd1..15b39ceb 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -1555,6 +1555,30 @@ private slots: QVERIFY(FolderModel::listRecent(dir.filePath("recently-used.xbel"), false, 200, [&] { return ++checks > 3; }).size() == 1); } + void trashListsEveryDrivesTrash() + { + // 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")); + // 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() { QVector e(4);