19 m_view->setHeaderHidden(
true);
20 m_view->setIndentation(15);
21 m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
22 m_view->setContextMenuPolicy(Qt::CustomContextMenu);
25 connect(
m_view->selectionModel(), &QItemSelectionModel::currentChanged,
this,
26 [
this](
const QModelIndex ¤t) {
27 if (current.isValid()) {
28 m_restoreCurrent.clear();
33 m_reloadTimer.setSingleShot(
true);
34 m_reloadTimer.setInterval(300);
35 connect(&m_watcher, &QFileSystemWatcher::directoryChanged, &m_reloadTimer,
36 qOverload<>(&QTimer::start));
40 connect(m_view, &QTreeView::expanded,
this, [
this](
const QModelIndex &index) {
41 const QFileInfo info = m_fs->fileInfo(m_proxy.mapToSource(index));
43 watchDir(info.absoluteFilePath());
46 m_freshDeadline.setSingleShot(
true);
47 m_freshDeadline.setInterval(3000);
52 auto model = std::make_unique<QFileSystemModel>();
53 model->setNameFilters(QStringList() << QStringLiteral(
"*.gpg"));
54 model->setNameFilterDisables(
false);
55 model->setOption(QFileSystemModel::DontWatchForChanges,
true);
56 QFileSystemModel *raw = model.get();
57 connect(raw, &QFileSystemModel::directoryLoaded,
this,
58 [
this, raw](
const QString &path) {
59 if (raw ==
m_fs.get()) {
61 }
else if (raw ==
m_fresh.get()) {
76 m_view->setColumnHidden(1,
true);
77 m_view->setColumnHidden(2,
true);
78 m_view->setColumnHidden(3,
true);
79 m_view->header()->setSectionResizeMode(0, QHeaderView::Stretch);
80 m_view->sortByColumn(0, Qt::AscendingOrder);
84 const QString clean = QDir::cleanPath(dir);
85 if (!
m_watcher.directories().contains(clean)) {
91 const QString clean = QDir::cleanPath(path);
96 const QModelIndex fresh =
m_fresh->index(clean);
97 if (fresh.isValid() &&
m_fresh->canFetchMore(fresh)) {
104 const bool lastLate =
106 if (lastLate && !
m_proxy.filterRegularExpression().pattern().isEmpty()) {
128 if (!shown.isValid()) {
132 if (!
m_view->currentIndex().isValid()) {
133 m_view->setCurrentIndex(shown);
151 const QString root = QDir::cleanPath(
m_store);
159 for (
const QString &dir :
m_watcher.directories()) {
160 if (dir == root || !QFileInfo(dir).isDir()) {
170 QStringList expanded;
171 for (
const QString &dir :
m_watcher.directories()) {
172 const QModelIndex shown =
m_proxy.mapFromSource(
m_fs->index(dir));
173 if (shown.isValid() &&
m_view->isExpanded(shown)) {
178 std::sort(expanded.begin(), expanded.end(),
179 [](
const QString &a,
const QString &b) {
180 return a.count(QLatin1Char(
'/')) < b.count(QLatin1Char(
'/'));
186 const QStringList watched =
m_watcher.directories();
188 for (
const QString &dir : watched) {
189 if (!QFileInfo(dir).isDir()) {
193 if (!gone.isEmpty()) {
205 const QString current =
207 const int scroll =
m_view->verticalScrollBar()->value();
222 for (
const QString &dir : std::as_const(expanded)) {
223 const QModelIndex shown =
m_proxy.mapFromSource(
m_fs->index(dir));
224 if (shown.isValid()) {
228 bool currentLost =
false;
229 if (!current.isEmpty()) {
230 const QModelIndex shown =
m_proxy.mapFromSource(
m_fs->index(current));
231 if (shown.isValid()) {
232 m_view->setCurrentIndex(shown);
240 m_view->verticalScrollBar()->setValue(scroll);
266 if (
const QStringList watched =
m_watcher.directories(); !watched.isEmpty()) {
272 if (
m_proxy.sourceModel() !=
nullptr) {
276 const QModelIndex rootDir =
m_fs->setRootPath(path);
277 m_fs->fetchMore(rootDir);
280 m_proxy.setFilterRegularExpression(QRegularExpression());
283 if (QFileInfo(path).isDir()) {
291 m_proxy.setFilterRegularExpression(pattern);
299 const QString abspath = QDir(
m_store).absolutePath() + QDir::separator();
300 if (!index.isValid()) {
301 return forPass ? QString() : abspath;
303 const QFileInfo info =
m_fs->fileInfo(
m_proxy.mapToSource(index));
305 info.isFile() ? info.absolutePath() : info.absoluteFilePath();
307 filePath = QDir(abspath).relativeFilePath(filePath);
309 filePath += QDir::separator();
319 if (!index.isValid() ||
320 !
m_fs->fileInfo(
m_proxy.mapToSource(index)).isFile()) {
323 QString filePath =
m_fs->filePath(
m_proxy.mapToSource(index));
325 filePath = QDir(
m_store).relativeFilePath(filePath);
340 const QModelIndex src =
m_fs->index(path);
341 return src.isValid() ?
m_proxy.mapFromSource(src) : QModelIndex();
349 return m_view->currentIndex();
Abstract base class for password store operations.
void setModelAndStore(QFileSystemModel *sourceModel, const QString &passStore)
Initialize model with source model and store path.
void setPass(Pass *pass)
Inject the Pass backend for drag-and-drop Move/Copy operations.
auto rootIndexFor(const QString &path) -> QModelIndex
Set the source filesystem root and map it to a proxy index.
QString m_store
Absolute store directory last passed to setStore().
auto dirFor(const QModelIndex &index, bool forPass) const -> QString
The folder an index stands for.
std::unique_ptr< QFileSystemModel > m_fs
Source model over the store directory, filtered to *.gpg; replaced by setStore() and reload().
void shownListed(const QString &path)
The shown model listed path: watch it, list it in a model being built too, and filter again once the ...
auto fileInfo(const QModelIndex &index) const -> QFileInfo
File system information for a view index.
auto rootIndex() -> QModelIndex
The proxy's root index for the current store.
void swapInFresh()
Swap in the model reload() built, restoring the view.
QString m_restoreCurrent
The current entry a swap could not show yet.
auto currentIndex() const -> QModelIndex
The view's current index.
QTimer m_freshDeadline
Ends the wait for m_fresh: it is swapped in as it is.
auto currentFile(bool forPass) const -> QString
fileFor() applied to the view's current index.
QTreeView * m_view
The tree view this object drives; owned by the main window.
auto fileFor(const QModelIndex &index, bool forPass) const -> QString
The entry an index stands for.
void setFilter(const QRegularExpression &pattern)
Filter the visible entries.
std::unique_ptr< QFileSystemModel > m_retired
The model reload() replaced, kept until the next one: a caller still holding it reads stale rows inst...
void directoryLoaded(const QString &path)
The file system model the view shows finished listing a folder; also once for the store root after re...
void restoreCurrent()
Make the entry a swap could not show current again, once it shows. The wait ends when the listings th...
void unwatchGoneDirs()
Stop watching the folders that no longer exist.
StoreTree(QTreeView *view, QObject *parent=nullptr)
Attach the proxy model to the view and set its look: hidden header, indentation, scroll bars,...
auto view() const -> QTreeView *
void setStore(const QString &path)
Point everything at path: file system root, proxy store and view root index. Also drops a search filt...
QSet< QString > m_freshPending
Folders m_fresh still has to list before the swap.
StoreModel m_proxy
Proxy the view shows: hides .git, strips .gpg, applies the search filter and handles drag and drop.
void watchDir(const QString &dir)
Watch dir from now on, as the shown model has listed it.
auto makeModel() -> std::unique_ptr< QFileSystemModel >
A file system model as the tree uses it: *.gpg only, and not watching (see the class description).
auto expandedDirs() const -> QStringList
The watched folders the view shows expanded, for swapInFresh().
QTimer m_reloadTimer
Waits for the watched folders to be quiet, then reload().
void reload()
List the store again in a fresh file system model and swap it in once the store root and every folder...
bool m_reloadAgain
A reload() came while m_fresh was built: run one after it.
void setUpColumns()
Hide the size, type and date columns and sort by name, once a source model is attached.
auto indexFor(const QString &path) const -> QModelIndex
The view index for an absolute path, or invalid when the file system model does not (yet) know it.
void currentEntryLost(const QString &path)
After a reload() the current entry or folder could not be shown again: it was removed or renamed,...
std::unique_ptr< QFileSystemModel > m_fresh
The model reload() is building, until it is swapped in.
QSet< QString > m_stillListing
Folders still being listed when m_fresh was swapped in.
void setPass(Pass *pass)
Hand the proxy the backend it drops onto.
auto currentDir(bool forPass) const -> QString
dirFor() applied to the view's current index.
QFileSystemWatcher m_watcher
The folders the shown model has listed; reload() lists all of them again.
static auto endsWithGpg() -> const QRegularExpression &
Returns a regex to match .gpg file extensions.
StoreTree: owns the QFileSystemModel and StoreModel behind the password tree view and maps view index...
Util: static helpers for the process environment, binary discovery, store paths, URL safety and the l...