Phosphor
Qt6 / Wayland library suite for window-management tools
 
Loading...
Searching...
No Matches
WatchedDirectorySet.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 fuddlesworth
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
6#include <PhosphorFsLoader/phosphorfsloader_export.h>
7
8#include <QtCore/QHash>
9#include <QtCore/QObject>
10#include <QtCore/QSet>
11#include <QtCore/QString>
12#include <QtCore/QStringList>
13#include <QtCore/QTimer>
14
15QT_BEGIN_NAMESPACE
16class QFileSystemWatcher;
17QT_END_NAMESPACE
18
19namespace PhosphorFsLoader {
20
21class IScanStrategy;
22
34enum class LiveReload : quint8 {
35 Off,
36 On,
37};
38
70
101class PHOSPHORFSLOADER_EXPORT WatchedDirectorySet : public QObject
102{
103 Q_OBJECT
104
105public:
113 explicit WatchedDirectorySet(IScanStrategy& strategy, QObject* parent = nullptr);
115
118
152 int registerDirectory(const QString& directory, LiveReload liveReload = LiveReload::Off);
153
166 int registerDirectories(const QStringList& directories, LiveReload liveReload = LiveReload::Off,
167 RegistrationOrder order = RegistrationOrder::LowestPriorityFirst);
168
199 int setDirectories(const QStringList& directories, LiveReload liveReload = LiveReload::Off,
200 RegistrationOrder order = RegistrationOrder::LowestPriorityFirst);
201
217
226 void rescanNow();
227
238 QStringList directories() const;
239
251 static QStringList filterNewSearchPaths(const QStringList& candidates, const QStringList& alreadyRegistered);
252
259
264 QString watchedAncestorForTest(const QString& target) const;
265
268 bool hasParentWatchForTest(const QString& path) const;
269
270Q_SIGNALS:
279
280private Q_SLOTS:
281 void rescanAll();
282
283private:
284 void installWatcherIfNeeded();
285 void attachWatcherForDir(const QString& directory);
286 void releaseAncestorWatchFor(const QString& targetKey);
287 void onWatchedPathChanged();
288 void syncFileWatches(const QStringList& desiredPaths);
289
290 IScanStrategy* m_strategy = nullptr;
291 QStringList m_directories;
296 QFileSystemWatcher* m_watcher = nullptr;
297 QSet<QString> m_watchedParents;
308 QHash<QString, QString> m_parentWatchFor;
311 QSet<QString> m_watchedFiles;
312 QTimer m_debounceTimer;
319 int m_rescanDepth = 0;
320 bool m_rescanRequestedWhileRunning = false;
321};
322
323} // namespace PhosphorFsLoader
Pluggable enumeration / parse / commit policy for WatchedDirectorySet.
Definition IScanStrategy.h:63
Watcher + debounce + rescan scaffolding for filesystem-backed loaders.
Definition WatchedDirectorySet.h:102
WatchedDirectorySet(const WatchedDirectorySet &)=delete
int setDirectories(const QStringList &directories, LiveReload liveReload=LiveReload::Off, RegistrationOrder order=RegistrationOrder::LowestPriorityFirst)
Replace the registered directory set with directories.
void requestRescan()
Trigger a debounced rescan of every registered directory.
int registerDirectory(const QString &directory, LiveReload liveReload=LiveReload::Off)
Register a directory for scanning + (optionally) watching.
void rescanCompleted()
Fired after every rescan, coalesced by the 50 ms debounce.
static QStringList filterNewSearchPaths(const QStringList &candidates, const QStringList &alreadyRegistered)
Helper for consumers building search-path APIs on top of the set: canonicalises (QDir::cleanPath) eve...
void rescanNow()
Run a rescan synchronously on the calling stack.
bool hasParentWatchForTest(const QString &path) const
Test-only: returns true if path is currently in the internal parent-watch set.
WatchedDirectorySet & operator=(const WatchedDirectorySet &)=delete
QStringList directories() const
Currently-registered directories in registration order.
WatchedDirectorySet(IScanStrategy &strategy, QObject *parent=nullptr)
Construct with a borrowed strategy.
QString watchedAncestorForTest(const QString &target) const
Test-only: introspection of the ancestor-watch bookkeeping used by attachWatcherForDir when a target ...
int registerDirectories(const QStringList &directories, LiveReload liveReload=LiveReload::Off, RegistrationOrder order=RegistrationOrder::LowestPriorityFirst)
Register multiple directories in caller-declared priority order.
void setDebounceIntervalForTest(int ms)
Test-only: override the debounce interval (default 50 ms).
Definition DirectoryLoader.h:18
RegistrationOrder
Caller's declared priority direction for registerDirectories / setDirectories input.
Definition WatchedDirectorySet.h:58
@ HighestPriorityFirst
[user, sys-highest, ..., sys-lowest] — the natural output of QStandardPaths::locateAll(GenericDataLoc...
@ LowestPriorityFirst
[sys-lowest, ..., sys-highest, user] — the canonical strategy view.
LiveReload
Opt-in policy for directory watching.
Definition WatchedDirectorySet.h:34