Branch data Line data Source code
1 : : // SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2 : : // SPDX-License-Identifier: GPL-3.0-or-later
3 : : /**
4 : : * @file
5 : : * @brief Implements StoreTree; setStore() is where the file system model,
6 : : * the proxy and the view are re-rooted together.
7 : : */
8 : : #include "storetree.h"
9 : : #include "util.h"
10 : : #include <QDir>
11 : : #include <QHeaderView>
12 : : #include <QScrollBar>
13 : : #include <QTreeView>
14 : : #include <algorithm>
15 : :
16 : 227 : StoreTree::StoreTree(QTreeView *view, QObject *parent)
17 : 227 : : QObject(parent), m_view(view), m_fs(makeModel()) {
18 : 227 : m_view->setModel(&m_proxy);
19 : 227 : m_view->setHeaderHidden(true);
20 : 227 : m_view->setIndentation(15);
21 : 227 : m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
22 : 227 : m_view->setContextMenuPolicy(Qt::CustomContextMenu);
23 : : // An entry picked after a swap ends a restore still waiting: a later
24 : : // listing must not undo that pick, or a deselect after it.
25 : 227 : connect(m_view->selectionModel(), &QItemSelectionModel::currentChanged, this,
26 : 390 : [this](const QModelIndex ¤t) {
27 : : if (current.isValid()) {
28 : 122 : m_restoreCurrent.clear();
29 : : }
30 : 163 : });
31 : :
32 : : // A burst of changes (a pull, a re-encryption) ends in one reload.
33 : 227 : m_reloadTimer.setSingleShot(true);
34 : 227 : m_reloadTimer.setInterval(300);
35 : 227 : connect(&m_watcher, &QFileSystemWatcher::directoryChanged, &m_reloadTimer,
36 : 227 : qOverload<>(&QTimer::start));
37 : 227 : connect(&m_reloadTimer, &QTimer::timeout, this, &StoreTree::reload);
38 : : // Also when a folder is expanded: the model does not always report a
39 : : // listing (one it made while looking up a path, say).
40 : 227 : connect(m_view, &QTreeView::expanded, this, [this](const QModelIndex &index) {
41 : 87 : const QFileInfo info = m_fs->fileInfo(m_proxy.mapToSource(index));
42 [ + + - ]: 87 : if (info.isDir()) {
43 : 174 : watchDir(info.absoluteFilePath());
44 : : }
45 : 87 : });
46 : 227 : m_freshDeadline.setSingleShot(true);
47 : 227 : m_freshDeadline.setInterval(3000);
48 : 227 : connect(&m_freshDeadline, &QTimer::timeout, this, &StoreTree::swapInFresh);
49 : 227 : }
50 : :
51 : 272 : auto StoreTree::makeModel() -> std::unique_ptr<QFileSystemModel> {
52 : 272 : auto model = std::make_unique<QFileSystemModel>();
53 : 544 : model->setNameFilters(QStringList() << QStringLiteral("*.gpg"));
54 : 272 : model->setNameFilterDisables(false);
55 : 272 : model->setOption(QFileSystemModel::DontWatchForChanges, true);
56 : : QFileSystemModel *raw = model.get();
57 : 272 : connect(raw, &QFileSystemModel::directoryLoaded, this,
58 : 272 : [this, raw](const QString &path) {
59 [ + + ]: 254 : if (raw == m_fs.get()) {
60 : 210 : shownListed(path);
61 [ + + ]: 44 : } else if (raw == m_fresh.get()) {
62 : 42 : m_freshPending.remove(QDir::cleanPath(path));
63 [ + - ]: 42 : if (m_freshPending.isEmpty()) {
64 : : // Out of the model's own signal: the swap deletes models.
65 : 27 : QTimer::singleShot(0, this, &StoreTree::swapInFresh);
66 : : }
67 : : }
68 : 254 : });
69 : 272 : return model;
70 : 0 : }
71 : :
72 : 272 : void StoreTree::setUpColumns() {
73 : : // Columns exist only once a source model is attached: hiding the size,
74 : : // type and date ones is a no-op before that point, and addressing header
75 : : // section 0 without any sections crashes on Qt 6.8.
76 : 272 : m_view->setColumnHidden(1, true);
77 : 272 : m_view->setColumnHidden(2, true);
78 : 272 : m_view->setColumnHidden(3, true);
79 : 272 : m_view->header()->setSectionResizeMode(0, QHeaderView::Stretch);
80 : 272 : m_view->sortByColumn(0, Qt::AscendingOrder);
81 : 272 : }
82 : :
83 : 569 : void StoreTree::watchDir(const QString &dir) {
84 : 569 : const QString clean = QDir::cleanPath(dir);
85 [ + + + ]: 1138 : if (!m_watcher.directories().contains(clean)) {
86 : 324 : m_watcher.addPath(clean);
87 : : }
88 : 569 : }
89 : :
90 : 210 : void StoreTree::shownListed(const QString &path) {
91 : 210 : const QString clean = QDir::cleanPath(path);
92 : 210 : watchDir(clean);
93 : : // Listed during a build: the fresh model lists it too, or a search after
94 : : // the swap would judge it by its name alone.
95 [ + + ]: 210 : if (m_fresh != nullptr) {
96 : 1 : const QModelIndex fresh = m_fresh->index(clean);
97 [ + + - ]: 1 : if (fresh.isValid() && m_fresh->canFetchMore(fresh)) {
98 : 1 : m_freshPending.insert(clean);
99 : 1 : m_fresh->fetchMore(fresh);
100 : : }
101 : : }
102 : : // The last listing a swap did not wait for: filter again, as folders it
103 : : // judged before their rows came may match now.
104 : : const bool lastLate =
105 [ + + ]: 210 : m_stillListing.remove(clean) && m_stillListing.isEmpty();
106 [ + + + - : 212 : if (lastLate && !m_proxy.filterRegularExpression().pattern().isEmpty()) {
+ + + + -
- ]
107 : 2 : m_proxy.invalidate();
108 : 2 : m_view->setRootIndex(m_proxy.rootIndexFor(m_store));
109 : : }
110 : 210 : restoreCurrent();
111 [ + + ]: 210 : if (lastLate) {
112 : : // The wait ends with the late listings: a folder that does not show by
113 : : // then is not made current later, steps after the user left it.
114 : 2 : m_restoreCurrent.clear();
115 : : }
116 : 210 : emit directoryLoaded(path);
117 : 210 : }
118 : :
119 : 331 : void StoreTree::restoreCurrent() {
120 [ + + ]: 331 : if (m_restoreCurrent.isEmpty()) {
121 : 329 : return;
122 : : }
123 [ + + ]: 9 : if (!QFileInfo::exists(m_restoreCurrent)) {
124 : 2 : m_restoreCurrent.clear();
125 : 2 : return;
126 : : }
127 : 7 : const QModelIndex shown = indexFor(m_restoreCurrent);
128 : : if (!shown.isValid()) {
129 : : return;
130 : : }
131 : : // Unless something else is current (a pick also ends the wait).
132 : 2 : if (!m_view->currentIndex().isValid()) {
133 : 2 : m_view->setCurrentIndex(shown);
134 : : }
135 : 2 : m_restoreCurrent.clear();
136 : : }
137 : :
138 : 29 : void StoreTree::reload() {
139 [ + - ]: 29 : if (m_store.isEmpty()) {
140 : 1 : return;
141 : : }
142 [ + + ]: 29 : if (m_fresh != nullptr) {
143 : : // One build at a time. What changed meanwhile may be missing from it:
144 : : // list once more after the swap.
145 : 1 : m_reloadAgain = true;
146 : 1 : return;
147 : : }
148 : 28 : m_reloadTimer.stop();
149 : 56 : m_fresh = makeModel();
150 : : m_freshPending.clear();
151 : 28 : const QString root = QDir::cleanPath(m_store);
152 : 28 : m_fresh->setRootPath(root);
153 : 28 : m_freshPending.insert(root);
154 : 28 : m_fresh->fetchMore(m_fresh->index(root));
155 : : // Every folder the shown model listed and that still exists, expanded or
156 : : // not: a search judges a folder that was never listed by its name alone,
157 : : // and cannot expand one it hides, so a widened search would miss what is
158 : : // inside. The swap can also show the expanded ones expanded at once.
159 [ + + + + ]: 99 : for (const QString &dir : m_watcher.directories()) {
160 [ + + + + : 58 : if (dir == root || !QFileInfo(dir).isDir()) {
+ - + + -
- ]
161 : 28 : continue;
162 : : }
163 : 15 : m_freshPending.insert(dir);
164 : 15 : m_fresh->fetchMore(m_fresh->index(dir));
165 : : }
166 : 28 : m_freshDeadline.start();
167 : : }
168 : :
169 : 28 : auto StoreTree::expandedDirs() const -> QStringList {
170 : 28 : QStringList expanded;
171 [ + + + + ]: 128 : for (const QString &dir : m_watcher.directories()) {
172 : 44 : const QModelIndex shown = m_proxy.mapFromSource(m_fs->index(dir));
173 [ + + + ]: 39 : if (shown.isValid() && m_view->isExpanded(shown)) {
174 : : expanded << dir;
175 : : }
176 : : }
177 : : // Parents first, so each expand finds its row.
178 : 28 : std::sort(expanded.begin(), expanded.end(),
179 : 0 : [](const QString &a, const QString &b) {
180 : 0 : return a.count(QLatin1Char('/')) < b.count(QLatin1Char('/'));
181 : : });
182 : 28 : return expanded;
183 : : }
184 : :
185 : 28 : void StoreTree::unwatchGoneDirs() {
186 : 28 : const QStringList watched = m_watcher.directories();
187 : 28 : QStringList gone;
188 [ + + ]: 72 : for (const QString &dir : watched) {
189 [ + + - + ]: 44 : if (!QFileInfo(dir).isDir()) {
190 : : gone << dir;
191 : : }
192 : : }
193 [ - + ]: 28 : if (!gone.isEmpty()) {
194 : 0 : m_watcher.removePaths(gone);
195 : : }
196 : 28 : }
197 : :
198 : 28 : void StoreTree::swapInFresh() {
199 : 28 : m_freshDeadline.stop();
200 [ - + ]: 28 : if (m_fresh == nullptr) {
201 : 0 : return;
202 : : }
203 : : // What the view shows now, by path.
204 : 28 : const QStringList expanded = expandedDirs();
205 : : const QString current =
206 : 28 : m_fs->filePath(m_proxy.mapToSource(m_view->currentIndex()));
207 : 28 : const int scroll = m_view->verticalScrollBar()->value();
208 : :
209 : : m_retired = std::move(m_fs);
210 : : m_fs = std::move(m_fresh);
211 : : // Swapped at the deadline, the listings still under way finish in it.
212 : : m_stillListing = m_freshPending;
213 : : m_freshPending.clear();
214 : 28 : m_restoreCurrent.clear();
215 : 28 : m_proxy.setModelAndStore(m_fs.get(), m_store);
216 : 28 : setUpColumns();
217 : 28 : m_view->setRootIndex(m_proxy.rootIndexFor(m_store));
218 : : // The folders reload() had listed stay watched, as long as they exist; a
219 : : // folder the new model lists later is watched then.
220 : 28 : unwatchGoneDirs();
221 : 28 : watchDir(m_store);
222 [ + + ]: 39 : for (const QString &dir : std::as_const(expanded)) {
223 : 11 : const QModelIndex shown = m_proxy.mapFromSource(m_fs->index(dir));
224 : : if (shown.isValid()) {
225 : 9 : m_view->expand(shown);
226 : : }
227 : : }
228 : : bool currentLost = false;
229 [ + + ]: 28 : if (!current.isEmpty()) {
230 : 10 : const QModelIndex shown = m_proxy.mapFromSource(m_fs->index(current));
231 : : if (shown.isValid()) {
232 : 6 : m_view->setCurrentIndex(shown);
233 : : } else {
234 : : // Gone, or not listed yet when the swap came at the deadline: then
235 : : // it is made current again if the late listings show it.
236 : : currentLost = true;
237 : 4 : m_restoreCurrent = current;
238 : : }
239 : : }
240 : 28 : m_view->verticalScrollBar()->setValue(scroll);
241 [ + + ]: 28 : if (currentLost) {
242 : 4 : emit currentEntryLost(current);
243 : : }
244 : 28 : restoreCurrent();
245 : : if (m_stillListing.isEmpty()) {
246 : : // Every listing is in: what does not show now is not waited for.
247 : 27 : m_restoreCurrent.clear();
248 : : }
249 : 28 : emit directoryLoaded(m_store);
250 [ + + ]: 28 : if (m_reloadAgain) {
251 : 1 : m_reloadAgain = false;
252 : 1 : m_reloadTimer.start();
253 : : }
254 : : }
255 : :
256 : 244 : void StoreTree::setStore(const QString &path) {
257 : 244 : m_store = path;
258 : : // A build for the previous store is dropped; the folders watched were
259 : : // that store's.
260 : 244 : m_freshDeadline.stop();
261 : : m_fresh.reset();
262 : : m_freshPending.clear();
263 : : m_stillListing.clear();
264 : 244 : m_restoreCurrent.clear();
265 : 244 : m_reloadAgain = false;
266 [ + + ]: 244 : if (const QStringList watched = m_watcher.directories(); !watched.isEmpty()) {
267 : 34 : m_watcher.removePaths(watched);
268 : : }
269 : : // A fresh model for every store: one that listed this store before keeps
270 : : // its folders cached and would show them as they were, unwatched while
271 : : // another store was shown.
272 [ + + ]: 244 : if (m_proxy.sourceModel() != nullptr) {
273 : : m_retired = std::move(m_fs);
274 : 34 : m_fs = makeModel();
275 : : }
276 : 244 : const QModelIndex rootDir = m_fs->setRootPath(path);
277 : 244 : m_fs->fetchMore(rootDir);
278 : 244 : m_proxy.setModelAndStore(m_fs.get(), path);
279 : 244 : setUpColumns();
280 : 244 : m_proxy.setFilterRegularExpression(QRegularExpression());
281 : 244 : m_view->setRootIndex(m_proxy.rootIndexFor(path));
282 : : // Not left to directoryLoaded alone: the root may be listed before then.
283 [ + + - ]: 244 : if (QFileInfo(path).isDir()) {
284 : 244 : watchDir(path);
285 : : }
286 : 244 : }
287 : :
288 : 211 : void StoreTree::setPass(Pass *pass) { m_proxy.setPass(pass); }
289 : :
290 : 93 : void StoreTree::setFilter(const QRegularExpression &pattern) {
291 : 93 : m_proxy.setFilterRegularExpression(pattern);
292 : : // Filtering re-derives the mapping; the root must be looked up again.
293 : 93 : m_view->setRootIndex(m_proxy.rootIndexFor(m_store));
294 : 93 : restoreCurrent();
295 : 93 : }
296 : :
297 : 60 : auto StoreTree::dirFor(const QModelIndex &index, bool forPass) const
298 : : -> QString {
299 : 120 : const QString abspath = QDir(m_store).absolutePath() + QDir::separator();
300 : : if (!index.isValid()) {
301 [ + + ]: 21 : return forPass ? QString() : abspath;
302 : : }
303 : 39 : const QFileInfo info = m_fs->fileInfo(m_proxy.mapToSource(index));
304 : : QString filePath =
305 [ + + + + : 39 : info.isFile() ? info.absolutePath() : info.absoluteFilePath();
+ ]
306 [ + + ]: 39 : if (forPass) {
307 : 24 : filePath = QDir(abspath).relativeFilePath(filePath);
308 : : }
309 : 39 : filePath += QDir::separator();
310 : : return filePath;
311 : 39 : }
312 : :
313 : 53 : auto StoreTree::currentDir(bool forPass) const -> QString {
314 : 53 : return dirFor(m_view->currentIndex(), forPass);
315 : : }
316 : :
317 : 74 : auto StoreTree::fileFor(const QModelIndex &index, bool forPass) const
318 : : -> QString {
319 [ + + ]: 71 : if (!index.isValid() ||
320 [ + + + ]: 145 : !m_fs->fileInfo(m_proxy.mapToSource(index)).isFile()) {
321 : 4 : return {};
322 : : }
323 : 70 : QString filePath = m_fs->filePath(m_proxy.mapToSource(index));
324 [ + + ]: 70 : if (forPass) {
325 : 126 : filePath = QDir(m_store).relativeFilePath(filePath);
326 : 126 : filePath.replace(Util::endsWithGpg(), "");
327 : : }
328 : : return filePath;
329 : : }
330 : :
331 : 1 : auto StoreTree::currentFile(bool forPass) const -> QString {
332 : 1 : return fileFor(m_view->currentIndex(), forPass);
333 : : }
334 : :
335 : 99 : auto StoreTree::fileInfo(const QModelIndex &index) const -> QFileInfo {
336 : 99 : return m_fs->fileInfo(m_proxy.mapToSource(index));
337 : : }
338 : :
339 : 181 : auto StoreTree::indexFor(const QString &path) const -> QModelIndex {
340 : 181 : const QModelIndex src = m_fs->index(path);
341 : 181 : return src.isValid() ? m_proxy.mapFromSource(src) : QModelIndex();
342 : : }
343 : :
344 : 20 : auto StoreTree::rootIndex() -> QModelIndex {
345 : 20 : return m_proxy.rootIndexFor(m_store);
346 : : }
347 : :
348 : 0 : auto StoreTree::currentIndex() const -> QModelIndex {
349 : 0 : return m_view->currentIndex();
350 : : }
|