// SPDX-FileCopyrightText: 2026 Anne Jan Brouwer // SPDX-License-Identifier: GPL-3.0-or-later /** * @file * @brief Implements StoreTree; setStore() is where the file system model, * the proxy and the view are re-rooted together. */ #include "storetree.h" #include "util.h" #include #include #include #include #include StoreTree::StoreTree(QTreeView *view, QObject *parent) : QObject(parent), m_view(view), m_fs(makeModel()) { m_view->setModel(&m_proxy); m_view->setHeaderHidden(true); m_view->setIndentation(15); m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); m_view->setContextMenuPolicy(Qt::CustomContextMenu); // An entry picked after a swap ends a restore still waiting: a later // listing must not undo that pick, or a deselect after it. connect(m_view->selectionModel(), &QItemSelectionModel::currentChanged, this, [this](const QModelIndex ¤t) { if (current.isValid()) { m_restoreCurrent.clear(); } }); // A burst of changes (a pull, a re-encryption) ends in one reload. m_reloadTimer.setSingleShot(true); m_reloadTimer.setInterval(300); connect(&m_watcher, &QFileSystemWatcher::directoryChanged, &m_reloadTimer, qOverload<>(&QTimer::start)); connect(&m_reloadTimer, &QTimer::timeout, this, &StoreTree::reload); // Also when a folder is expanded: the model does not always report a // listing (one it made while looking up a path, say). connect(m_view, &QTreeView::expanded, this, [this](const QModelIndex &index) { const QFileInfo info = m_fs->fileInfo(m_proxy.mapToSource(index)); if (info.isDir()) { watchDir(info.absoluteFilePath()); } }); m_freshDeadline.setSingleShot(true); m_freshDeadline.setInterval(3000); connect(&m_freshDeadline, &QTimer::timeout, this, &StoreTree::swapInFresh); } auto StoreTree::makeModel() -> std::unique_ptr { auto model = std::make_unique(); model->setNameFilters(QStringList() << QStringLiteral("*.gpg")); model->setNameFilterDisables(false); model->setOption(QFileSystemModel::DontWatchForChanges, true); QFileSystemModel *raw = model.get(); connect(raw, &QFileSystemModel::directoryLoaded, this, [this, raw](const QString &path) { if (raw == m_fs.get()) { shownListed(path); } else if (raw == m_fresh.get()) { m_freshPending.remove(QDir::cleanPath(path)); if (m_freshPending.isEmpty()) { // Out of the model's own signal: the swap deletes models. QTimer::singleShot(0, this, &StoreTree::swapInFresh); } } }); return model; } void StoreTree::setUpColumns() { // Columns exist only once a source model is attached: hiding the size, // type and date ones is a no-op before that point, and addressing header // section 0 without any sections crashes on Qt 6.8. m_view->setColumnHidden(1, true); m_view->setColumnHidden(2, true); m_view->setColumnHidden(3, true); m_view->header()->setSectionResizeMode(0, QHeaderView::Stretch); m_view->sortByColumn(0, Qt::AscendingOrder); } void StoreTree::watchDir(const QString &dir) { const QString clean = QDir::cleanPath(dir); if (!m_watcher.directories().contains(clean)) { m_watcher.addPath(clean); } } void StoreTree::shownListed(const QString &path) { const QString clean = QDir::cleanPath(path); watchDir(clean); // Listed during a build: the fresh model lists it too, or a search after // the swap would judge it by its name alone. if (m_fresh != nullptr) { const QModelIndex fresh = m_fresh->index(clean); if (fresh.isValid() && m_fresh->canFetchMore(fresh)) { m_freshPending.insert(clean); m_fresh->fetchMore(fresh); } } // The last listing a swap did not wait for: filter again, as folders it // judged before their rows came may match now. const bool lastLate = m_stillListing.remove(clean) && m_stillListing.isEmpty(); if (lastLate && !m_proxy.filterRegularExpression().pattern().isEmpty()) { m_proxy.invalidate(); m_view->setRootIndex(m_proxy.rootIndexFor(m_store)); } restoreCurrent(); if (lastLate) { // The wait ends with the late listings: a folder that does not show by // then is not made current later, steps after the user left it. m_restoreCurrent.clear(); } emit directoryLoaded(path); } void StoreTree::restoreCurrent() { if (m_restoreCurrent.isEmpty()) { return; } if (!QFileInfo::exists(m_restoreCurrent)) { m_restoreCurrent.clear(); return; } const QModelIndex shown = indexFor(m_restoreCurrent); if (!shown.isValid()) { return; } // Unless something else is current (a pick also ends the wait). if (!m_view->currentIndex().isValid()) { m_view->setCurrentIndex(shown); } m_restoreCurrent.clear(); } void StoreTree::reload() { if (m_store.isEmpty()) { return; } if (m_fresh != nullptr) { // One build at a time. What changed meanwhile may be missing from it: // list once more after the swap. m_reloadAgain = true; return; } m_reloadTimer.stop(); m_fresh = makeModel(); m_freshPending.clear(); const QString root = QDir::cleanPath(m_store); m_fresh->setRootPath(root); m_freshPending.insert(root); m_fresh->fetchMore(m_fresh->index(root)); // Every folder the shown model listed and that still exists, expanded or // not: a search judges a folder that was never listed by its name alone, // and cannot expand one it hides, so a widened search would miss what is // inside. The swap can also show the expanded ones expanded at once. for (const QString &dir : m_watcher.directories()) { if (dir == root || !QFileInfo(dir).isDir()) { continue; } m_freshPending.insert(dir); m_fresh->fetchMore(m_fresh->index(dir)); } m_freshDeadline.start(); } void StoreTree::swapInFresh() { m_freshDeadline.stop(); if (m_fresh == nullptr) { return; } // What the view shows now, by path. QStringList expanded; for (const QString &dir : m_watcher.directories()) { const QModelIndex shown = m_proxy.mapFromSource(m_fs->index(dir)); if (shown.isValid() && m_view->isExpanded(shown)) { expanded << dir; } } // Parents first, so each expand finds its row. std::sort(expanded.begin(), expanded.end(), [](const QString &a, const QString &b) { return a.count(QLatin1Char('/')) < b.count(QLatin1Char('/')); }); const QString current = m_fs->filePath(m_proxy.mapToSource(m_view->currentIndex())); const int scroll = m_view->verticalScrollBar()->value(); m_retired = std::move(m_fs); m_fs = std::move(m_fresh); // Swapped at the deadline, the listings still under way finish in it. m_stillListing = m_freshPending; m_freshPending.clear(); m_restoreCurrent.clear(); m_proxy.setModelAndStore(m_fs.get(), m_store); setUpColumns(); m_view->setRootIndex(m_proxy.rootIndexFor(m_store)); // The folders reload() had listed stay watched, as long as they exist; a // folder the new model lists later is watched then. const QStringList watched = m_watcher.directories(); QStringList gone; for (const QString &dir : watched) { if (!QFileInfo(dir).isDir()) { gone << dir; } } if (!gone.isEmpty()) { m_watcher.removePaths(gone); } watchDir(m_store); for (const QString &dir : std::as_const(expanded)) { const QModelIndex shown = m_proxy.mapFromSource(m_fs->index(dir)); if (shown.isValid()) { m_view->expand(shown); } } bool currentLost = false; if (!current.isEmpty()) { const QModelIndex shown = m_proxy.mapFromSource(m_fs->index(current)); if (shown.isValid()) { m_view->setCurrentIndex(shown); } else { // Gone, or not listed yet when the swap came at the deadline: then // it is made current again if the late listings show it. currentLost = true; m_restoreCurrent = current; } } m_view->verticalScrollBar()->setValue(scroll); if (currentLost) { emit currentEntryLost(current); } restoreCurrent(); if (m_stillListing.isEmpty()) { // Every listing is in: what does not show now is not waited for. m_restoreCurrent.clear(); } emit directoryLoaded(m_store); if (m_reloadAgain) { m_reloadAgain = false; m_reloadTimer.start(); } } void StoreTree::setStore(const QString &path) { m_store = path; // A build for the previous store is dropped; the folders watched were // that store's. m_freshDeadline.stop(); m_fresh.reset(); m_freshPending.clear(); m_stillListing.clear(); m_restoreCurrent.clear(); m_reloadAgain = false; if (const QStringList watched = m_watcher.directories(); !watched.isEmpty()) { m_watcher.removePaths(watched); } // A fresh model for every store: one that listed this store before keeps // its folders cached and would show them as they were, unwatched while // another store was shown. if (m_proxy.sourceModel() != nullptr) { m_retired = std::move(m_fs); m_fs = makeModel(); } const QModelIndex rootDir = m_fs->setRootPath(path); m_fs->fetchMore(rootDir); m_proxy.setModelAndStore(m_fs.get(), path); setUpColumns(); m_proxy.setFilterRegularExpression(QRegularExpression()); m_view->setRootIndex(m_proxy.rootIndexFor(path)); // Not left to directoryLoaded alone: the root may be listed before then. if (QFileInfo(path).isDir()) { watchDir(path); } } void StoreTree::setPass(Pass *pass) { m_proxy.setPass(pass); } void StoreTree::setFilter(const QRegularExpression &pattern) { m_proxy.setFilterRegularExpression(pattern); // Filtering re-derives the mapping; the root must be looked up again. m_view->setRootIndex(m_proxy.rootIndexFor(m_store)); restoreCurrent(); } auto StoreTree::dirFor(const QModelIndex &index, bool forPass) const -> QString { const QString abspath = QDir(m_store).absolutePath() + QDir::separator(); if (!index.isValid()) { return forPass ? QString() : abspath; } const QFileInfo info = m_fs->fileInfo(m_proxy.mapToSource(index)); QString filePath = info.isFile() ? info.absolutePath() : info.absoluteFilePath(); if (forPass) { filePath = QDir(abspath).relativeFilePath(filePath); } filePath += QDir::separator(); return filePath; } auto StoreTree::currentDir(bool forPass) const -> QString { return dirFor(m_view->currentIndex(), forPass); } auto StoreTree::fileFor(const QModelIndex &index, bool forPass) const -> QString { if (!index.isValid() || !m_fs->fileInfo(m_proxy.mapToSource(index)).isFile()) { return {}; } QString filePath = m_fs->filePath(m_proxy.mapToSource(index)); if (forPass) { filePath = QDir(m_store).relativeFilePath(filePath); filePath.replace(Util::endsWithGpg(), ""); } return filePath; } auto StoreTree::currentFile(bool forPass) const -> QString { return fileFor(m_view->currentIndex(), forPass); } auto StoreTree::fileInfo(const QModelIndex &index) const -> QFileInfo { return m_fs->fileInfo(m_proxy.mapToSource(index)); } auto StoreTree::indexFor(const QString &path) const -> QModelIndex { const QModelIndex src = m_fs->index(path); return src.isValid() ? m_proxy.mapFromSource(src) : QModelIndex(); } auto StoreTree::rootIndex() -> QModelIndex { return m_proxy.rootIndexFor(m_store); } auto StoreTree::currentIndex() const -> QModelIndex { return m_view->currentIndex(); }