QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
storetree.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
8#ifndef SRC_STORETREE_H_
9#define SRC_STORETREE_H_
10
11#include "storemodel.h"
12#include <QFileInfo>
13#include <QFileSystemModel>
14#include <QFileSystemWatcher>
15#include <QModelIndex>
16#include <QObject>
17#include <QSet>
18#include <QString>
19#include <QStringList>
20#include <QTimer>
21#include <memory>
22
23class Pass;
24class QTreeView;
25
46class StoreTree : public QObject {
47 Q_OBJECT
48
49public:
57 explicit StoreTree(QTreeView *view, QObject *parent = nullptr);
58
64 void setStore(const QString &path);
65
73 void reload();
74
79 void setPass(Pass *pass);
80
86 void setFilter(const QRegularExpression &pattern);
87
96 [[nodiscard]] auto dirFor(const QModelIndex &index, bool forPass) const
97 -> QString;
98
104 [[nodiscard]] auto currentDir(bool forPass) const -> QString;
105
114 [[nodiscard]] auto fileFor(const QModelIndex &index, bool forPass) const
115 -> QString;
116
122 [[nodiscard]] auto currentFile(bool forPass) const -> QString;
123
129 [[nodiscard]] auto fileInfo(const QModelIndex &index) const -> QFileInfo;
130
137 [[nodiscard]] auto indexFor(const QString &path) const -> QModelIndex;
138
143 [[nodiscard]] auto rootIndex() -> QModelIndex;
144
149 [[nodiscard]] auto currentIndex() const -> QModelIndex;
150
152 [[nodiscard]] auto store() const -> QString { return m_store; }
154 [[nodiscard]] auto proxy() -> StoreModel & { return m_proxy; }
156 [[nodiscard]] auto fileSystem() -> QFileSystemModel & { return *m_fs; }
158 [[nodiscard]] auto view() const -> QTreeView * { return m_view; }
159
160signals:
166 void directoryLoaded(const QString &path);
176 void currentEntryLost(const QString &path);
177
178private:
179 friend class tst_storetree;
180 friend class tst_mainwindow;
181
187 auto makeModel() -> std::unique_ptr<QFileSystemModel>;
192 void setUpColumns();
197 void watchDir(const QString &dir);
201 void swapInFresh();
206 [[nodiscard]] auto expandedDirs() const -> QStringList;
210 void unwatchGoneDirs();
217 void shownListed(const QString &path);
224 void restoreCurrent();
225
227 QTreeView *m_view;
232 std::unique_ptr<QFileSystemModel> m_fs;
237 std::unique_ptr<QFileSystemModel> m_retired;
239 std::unique_ptr<QFileSystemModel> m_fresh;
241 QSet<QString> m_freshPending;
243 QSet<QString> m_stillListing;
247 bool m_reloadAgain = false;
254 QFileSystemWatcher m_watcher;
263 QString m_store;
264};
265
266#endif // SRC_STORETREE_H_
Abstract base class for password store operations.
Definition pass.h:77
QSortFilterProxyModel for filtering and displaying password store.
Definition storemodel.h:85
QString m_store
Absolute store directory last passed to setStore().
Definition storetree.h:263
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().
Definition storetree.h:232
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 ...
Definition storetree.cpp:90
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.
Definition storetree.h:245
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.
Definition storetree.h:249
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.
Definition storetree.h:227
auto fileFor(const QModelIndex &index, bool forPass) const -> QString
The entry an index stands for.
auto store() const -> QString
Definition storetree.h:152
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...
Definition storetree.h:237
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,...
Definition storetree.cpp:16
auto view() const -> QTreeView *
Definition storetree.h:158
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.
Definition storetree.h:241
StoreModel m_proxy
Proxy the view shows: hides .git, strips .gpg, applies the search filter and handles drag and drop.
Definition storetree.h:261
auto fileSystem() -> QFileSystemModel &
Definition storetree.h:156
void watchDir(const QString &dir)
Watch dir from now on, as the shown model has listed it.
Definition storetree.cpp:83
auto makeModel() -> std::unique_ptr< QFileSystemModel >
A file system model as the tree uses it: *.gpg only, and not watching (see the class description).
Definition storetree.cpp:51
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().
Definition storetree.h:256
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.
Definition storetree.h:247
void setUpColumns()
Hide the size, type and date columns and sort by name, once a source model is attached.
Definition storetree.cpp:72
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.
Definition storetree.h:239
QSet< QString > m_stillListing
Folders still being listed when m_fresh was swapped in.
Definition storetree.h:243
auto proxy() -> StoreModel &
Definition storetree.h:154
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.
Definition storetree.h:254
StoreModel, the filtering/sorting proxy over the store's QFileSystemModel, and StoreDragItem,...