Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src/foldermodel.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "foldermodel.h"
#include "fileops.h"

#include <QCache>
#include <QCollator>
Expand All @@ -13,6 +14,7 @@
#include <QMimeDatabase>
#include <QtConcurrent>
#include <algorithm>
#include <tuple>
#include <sys/stat.h>

FolderModel::FolderModel(QObject *parent) : QAbstractListModel(parent) {
Expand Down Expand Up @@ -302,6 +304,28 @@ QVector<FolderModel::Entry> FolderModel::list(const QString &path, bool showHidd
return entries;
}

bool FolderModel::isTrash(const QString &path) {
return path == QDir::cleanPath(FileOps::homeTrash() + "/files");
}

QVector<FolderModel::Entry> FolderModel::listTrash(const QStringList &trashDirs, const QString &query, bool showHidden,
const QString &sort, const std::function<bool()> &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<Entry> 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();
Expand Down Expand Up @@ -491,13 +515,13 @@ void FolderModel::reload() {
const QStringList filter = m_filter;
*m_latest = generation;
const auto latest = m_latest;
auto *watcher = new QFutureWatcher<QPair<QVector<Entry>, QString>>(this);
auto *watcher = new QFutureWatcher<std::tuple<QVector<Entry>, 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);
Expand All @@ -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();
Expand All @@ -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);
}));
}

Expand Down
6 changes: 6 additions & 0 deletions src/foldermodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entry> 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<Entry> listTrash(const QStringList &trashDirs, const QString &query, bool showHidden,
const QString &sort, const std::function<bool()> &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);
Expand Down
24 changes: 24 additions & 0 deletions tests/folio_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<FolderModel::Entry> e(4);
Expand Down
Loading