QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
storetree.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
8#include "storetree.h"
9#include "util.h"
10#include <QDir>
11#include <QHeaderView>
12#include <QTreeView>
13
14StoreTree::StoreTree(QTreeView *view, QObject *parent)
15 : QObject(parent), m_view(view) {
16 m_fs.setNameFilters(QStringList() << QStringLiteral("*.gpg"));
17 m_fs.setNameFilterDisables(false);
18
19 m_view->setModel(&m_proxy);
20 m_view->setHeaderHidden(true);
21 m_view->setIndentation(15);
22 m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
23 m_view->setContextMenuPolicy(Qt::CustomContextMenu);
24}
25
26void StoreTree::setStore(const QString &path) {
27 m_store = path;
28 const QModelIndex rootDir = m_fs.setRootPath(path);
29 m_fs.fetchMore(rootDir);
30 if (m_proxy.sourceModel() == nullptr) {
32 // Columns exist only once a source model is attached: hiding the size,
33 // type and date ones is a no-op before that point, and addressing header
34 // section 0 without any sections crashes on Qt 6.8.
35 m_view->setColumnHidden(1, true);
36 m_view->setColumnHidden(2, true);
37 m_view->setColumnHidden(3, true);
38 m_view->header()->setSectionResizeMode(0, QHeaderView::Stretch);
39 m_view->sortByColumn(0, Qt::AscendingOrder);
40 } else {
41 m_proxy.setStore(path);
42 }
43 m_proxy.setFilterRegularExpression(QRegularExpression());
44 m_view->setRootIndex(m_proxy.rootIndexFor(path));
45}
46
47void StoreTree::setPass(Pass *pass) { m_proxy.setPass(pass); }
48
49void StoreTree::setFilter(const QRegularExpression &pattern) {
50 m_proxy.setFilterRegularExpression(pattern);
51 // Filtering re-derives the mapping; the root must be looked up again.
52 m_view->setRootIndex(m_proxy.rootIndexFor(m_store));
53}
54
55auto StoreTree::dirFor(const QModelIndex &index, bool forPass) const
56 -> QString {
57 const QString abspath = QDir(m_store).absolutePath() + QDir::separator();
58 if (!index.isValid()) {
59 return forPass ? QString() : abspath;
60 }
61 const QFileInfo info = m_fs.fileInfo(m_proxy.mapToSource(index));
62 QString filePath =
63 info.isFile() ? info.absolutePath() : info.absoluteFilePath();
64 if (forPass) {
65 filePath = QDir(abspath).relativeFilePath(filePath);
66 }
67 filePath += QDir::separator();
68 return filePath;
69}
70
71auto StoreTree::currentDir(bool forPass) const -> QString {
72 return dirFor(m_view->currentIndex(), forPass);
73}
74
75auto StoreTree::fileFor(const QModelIndex &index, bool forPass) const
76 -> QString {
77 if (!index.isValid() || !m_fs.fileInfo(m_proxy.mapToSource(index)).isFile()) {
78 return {};
79 }
80 QString filePath = m_fs.filePath(m_proxy.mapToSource(index));
81 if (forPass) {
82 filePath = QDir(m_store).relativeFilePath(filePath);
83 filePath.replace(Util::endsWithGpg(), "");
84 }
85 return filePath;
86}
87
88auto StoreTree::currentFile(bool forPass) const -> QString {
89 return fileFor(m_view->currentIndex(), forPass);
90}
91
92auto StoreTree::fileInfo(const QModelIndex &index) const -> QFileInfo {
93 return m_fs.fileInfo(m_proxy.mapToSource(index));
94}
95
96auto StoreTree::indexFor(const QString &path) const -> QModelIndex {
97 const QModelIndex src = m_fs.index(path);
98 return src.isValid() ? m_proxy.mapFromSource(src) : QModelIndex();
99}
100
101auto StoreTree::rootIndex() -> QModelIndex {
102 return m_proxy.rootIndexFor(m_store);
103}
104
105auto StoreTree::currentIndex() const -> QModelIndex {
106 return m_view->currentIndex();
107}
Abstract base class for password store operations.
Definition pass.h:75
void setModelAndStore(QFileSystemModel *sourceModel, const QString &passStore)
Initialize model with source model and store path.
void setStore(const QString &passStore)
Update the store path used for filtering without changing the source model.
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().
Definition storetree.h:152
auto dirFor(const QModelIndex &index, bool forPass) const -> QString
The folder an index stands for.
Definition storetree.cpp:55
auto fileInfo(const QModelIndex &index) const -> QFileInfo
File system information for a view index.
Definition storetree.cpp:92
auto rootIndex() -> QModelIndex
The proxy's root index for the current store.
auto currentIndex() const -> QModelIndex
The view's current index.
auto currentFile(bool forPass) const -> QString
fileFor() applied to the view's current index.
Definition storetree.cpp:88
QTreeView * m_view
The tree view this object drives; owned by the main window.
Definition storetree.h:140
auto fileFor(const QModelIndex &index, bool forPass) const -> QString
The entry an index stands for.
Definition storetree.cpp:75
void setFilter(const QRegularExpression &pattern)
Filter the visible entries.
Definition storetree.cpp:49
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:14
auto view() const -> QTreeView *
Definition storetree.h:136
QFileSystemModel m_fs
Source model over the store directory, filtered to *.gpg; re-rooted by setStore().
Definition storetree.h:145
void setStore(const QString &path)
Point everything at path: file system root, proxy store and view root index. Also drops a search filt...
Definition storetree.cpp:26
StoreModel m_proxy
Proxy the view shows: hides .git, strips .gpg, applies the search filter and handles drag and drop.
Definition storetree.h:150
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.
Definition storetree.cpp:96
void setPass(Pass *pass)
Hand the proxy the backend it drops onto.
Definition storetree.cpp:47
auto currentDir(bool forPass) const -> QString
dirFor() applied to the view's current index.
Definition storetree.cpp:71
static auto endsWithGpg() -> const QRegularExpression &
Returns a regex to match .gpg file extensions.
Definition util.cpp:582
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...