• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IJHack / QtPass / 46651294917

01 Dec 2026 12:57PM UTC coverage: 94.542% (+0.03%) from 94.513%
46651294917

push

github

web-flow
Split Pass::skippedGpgId and StoreTree::swapInFresh (#2437)

Part of #2157. Behaviour is unchanged; comments moved with their code.

- Pass::skippedGpgId 15 -> 10: what the walk concludes at one folder
  moves to the private static member gpgIdVerdictIn().
- StoreTree::swapInFresh 15 -> 9: collecting the expanded folders moves
  to expandedDirs(), dropping the watches of folders that are gone to
  unwatchGoneDirs().

Signed-off-by: Anne Jan Brouwer <brouwer@annejan.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

31 of 33 new or added lines in 2 files covered. (93.94%)

11467 of 12129 relevant lines covered (94.54%)

23850.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.46
/src/storetree.cpp
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
StoreTree::StoreTree(QTreeView *view, QObject *parent)
227 ✔
17
    : QObject(parent), m_view(view), m_fs(makeModel()) {
227 ✔
18
  m_view->setModel(&m_proxy);
227 ✔
19
  m_view->setHeaderHidden(true);
227 ✔
20
  m_view->setIndentation(15);
227 ✔
21
  m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
227 ✔
22
  m_view->setContextMenuPolicy(Qt::CustomContextMenu);
227 ✔
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
  connect(m_view->selectionModel(), &QItemSelectionModel::currentChanged, this,
227 ✔
26
          [this](const QModelIndex &current) {
393 ✔
27
            if (current.isValid()) {
28
              m_restoreCurrent.clear();
125 ✔
29
            }
30
          });
166 ✔
31

32
  // A burst of changes (a pull, a re-encryption) ends in one reload.
33
  m_reloadTimer.setSingleShot(true);
227 ✔
34
  m_reloadTimer.setInterval(300);
227 ✔
35
  connect(&m_watcher, &QFileSystemWatcher::directoryChanged, &m_reloadTimer,
227 ✔
36
          qOverload<>(&QTimer::start));
227 ✔
37
  connect(&m_reloadTimer, &QTimer::timeout, this, &StoreTree::reload);
227 ✔
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
  connect(m_view, &QTreeView::expanded, this, [this](const QModelIndex &index) {
227 ✔
41
    const QFileInfo info = m_fs->fileInfo(m_proxy.mapToSource(index));
87 ✔
42
    if (info.isDir()) {
87 ✔
43
      watchDir(info.absoluteFilePath());
174 ✔
44
    }
45
  });
87 ✔
46
  m_freshDeadline.setSingleShot(true);
227 ✔
47
  m_freshDeadline.setInterval(3000);
227 ✔
48
  connect(&m_freshDeadline, &QTimer::timeout, this, &StoreTree::swapInFresh);
227 ✔
49
}
227 ✔
50

51
auto StoreTree::makeModel() -> std::unique_ptr<QFileSystemModel> {
272 ✔
52
  auto model = std::make_unique<QFileSystemModel>();
272 ✔
53
  model->setNameFilters(QStringList() << QStringLiteral("*.gpg"));
544 ✔
54
  model->setNameFilterDisables(false);
272 ✔
55
  model->setOption(QFileSystemModel::DontWatchForChanges, true);
272 ✔
56
  QFileSystemModel *raw = model.get();
57
  connect(raw, &QFileSystemModel::directoryLoaded, this,
272 ✔
58
          [this, raw](const QString &path) {
272 ✔
59
            if (raw == m_fs.get()) {
254 ✔
60
              shownListed(path);
210 ✔
61
            } else if (raw == m_fresh.get()) {
44 ✔
62
              m_freshPending.remove(QDir::cleanPath(path));
42 ✔
63
              if (m_freshPending.isEmpty()) {
42 ✔
64
                // Out of the model's own signal: the swap deletes models.
65
                QTimer::singleShot(0, this, &StoreTree::swapInFresh);
27 ✔
66
              }
67
            }
68
          });
254 ✔
69
  return model;
272 ✔
70
}
×
71

72
void StoreTree::setUpColumns() {
272 ✔
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
  m_view->setColumnHidden(1, true);
272 ✔
77
  m_view->setColumnHidden(2, true);
272 ✔
78
  m_view->setColumnHidden(3, true);
272 ✔
79
  m_view->header()->setSectionResizeMode(0, QHeaderView::Stretch);
272 ✔
80
  m_view->sortByColumn(0, Qt::AscendingOrder);
272 ✔
81
}
272 ✔
82

83
void StoreTree::watchDir(const QString &dir) {
569 ✔
84
  const QString clean = QDir::cleanPath(dir);
569 ✔
85
  if (!m_watcher.directories().contains(clean)) {
1,138 ✔
86
    m_watcher.addPath(clean);
324 ✔
87
  }
88
}
569 ✔
89

90
void StoreTree::shownListed(const QString &path) {
210 ✔
91
  const QString clean = QDir::cleanPath(path);
210 ✔
92
  watchDir(clean);
210 ✔
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
  if (m_fresh != nullptr) {
210 ✔
96
    const QModelIndex fresh = m_fresh->index(clean);
1 ✔
97
    if (fresh.isValid() && m_fresh->canFetchMore(fresh)) {
1 ✔
98
      m_freshPending.insert(clean);
1 ✔
99
      m_fresh->fetchMore(fresh);
1 ✔
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
      m_stillListing.remove(clean) && m_stillListing.isEmpty();
210 ✔
106
  if (lastLate && !m_proxy.filterRegularExpression().pattern().isEmpty()) {
212 ✔
107
    m_proxy.invalidate();
2 ✔
108
    m_view->setRootIndex(m_proxy.rootIndexFor(m_store));
2 ✔
109
  }
110
  restoreCurrent();
210 ✔
111
  if (lastLate) {
210 ✔
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
    m_restoreCurrent.clear();
2 ✔
115
  }
116
  emit directoryLoaded(path);
210 ✔
117
}
210 ✔
118

119
void StoreTree::restoreCurrent() {
331 ✔
120
  if (m_restoreCurrent.isEmpty()) {
331 ✔
121
    return;
329 ✔
122
  }
123
  if (!QFileInfo::exists(m_restoreCurrent)) {
9 ✔
124
    m_restoreCurrent.clear();
2 ✔
125
    return;
2 ✔
126
  }
127
  const QModelIndex shown = indexFor(m_restoreCurrent);
7 ✔
128
  if (!shown.isValid()) {
129
    return;
130
  }
131
  // Unless something else is current (a pick also ends the wait).
132
  if (!m_view->currentIndex().isValid()) {
2 ✔
133
    m_view->setCurrentIndex(shown);
2 ✔
134
  }
135
  m_restoreCurrent.clear();
2 ✔
136
}
137

138
void StoreTree::reload() {
29 ✔
139
  if (m_store.isEmpty()) {
29 ✔
140
    return;
1 ✔
141
  }
142
  if (m_fresh != nullptr) {
29 ✔
143
    // One build at a time. What changed meanwhile may be missing from it:
144
    // list once more after the swap.
145
    m_reloadAgain = true;
1 ✔
146
    return;
1 ✔
147
  }
148
  m_reloadTimer.stop();
28 ✔
149
  m_fresh = makeModel();
56 ✔
150
  m_freshPending.clear();
151
  const QString root = QDir::cleanPath(m_store);
28 ✔
152
  m_fresh->setRootPath(root);
28 ✔
153
  m_freshPending.insert(root);
28 ✔
154
  m_fresh->fetchMore(m_fresh->index(root));
28 ✔
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
  for (const QString &dir : m_watcher.directories()) {
99 ✔
160
    if (dir == root || !QFileInfo(dir).isDir()) {
58 ✔
161
      continue;
28 ✔
162
    }
163
    m_freshPending.insert(dir);
15 ✔
164
    m_fresh->fetchMore(m_fresh->index(dir));
15 ✔
165
  }
166
  m_freshDeadline.start();
28 ✔
167
}
168

169
auto StoreTree::expandedDirs() const -> QStringList {
28 ✔
170
  QStringList expanded;
28 ✔
171
  for (const QString &dir : m_watcher.directories()) {
128 ✔
172
    const QModelIndex shown = m_proxy.mapFromSource(m_fs->index(dir));
44 ✔
173
    if (shown.isValid() && m_view->isExpanded(shown)) {
39 ✔
174
      expanded << dir;
175
    }
176
  }
177
  // Parents first, so each expand finds its row.
178
  std::sort(expanded.begin(), expanded.end(),
28 ✔
179
            [](const QString &a, const QString &b) {
×
180
              return a.count(QLatin1Char('/')) < b.count(QLatin1Char('/'));
×
181
            });
182
  return expanded;
28 ✔
183
}
184

185
void StoreTree::unwatchGoneDirs() {
28 ✔
186
  const QStringList watched = m_watcher.directories();
28 ✔
187
  QStringList gone;
28 ✔
188
  for (const QString &dir : watched) {
72 ✔
189
    if (!QFileInfo(dir).isDir()) {
44 ✔
190
      gone << dir;
191
    }
192
  }
193
  if (!gone.isEmpty()) {
28 ✔
NEW
194
    m_watcher.removePaths(gone);
×
195
  }
196
}
28 ✔
197

198
void StoreTree::swapInFresh() {
28 ✔
199
  m_freshDeadline.stop();
28 ✔
200
  if (m_fresh == nullptr) {
28 ✔
NEW
201
    return;
×
202
  }
203
  // What the view shows now, by path.
204
  const QStringList expanded = expandedDirs();
28 ✔
205
  const QString current =
206
      m_fs->filePath(m_proxy.mapToSource(m_view->currentIndex()));
28 ✔
207
  const int scroll = m_view->verticalScrollBar()->value();
28 ✔
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
  m_restoreCurrent.clear();
28 ✔
215
  m_proxy.setModelAndStore(m_fs.get(), m_store);
28 ✔
216
  setUpColumns();
28 ✔
217
  m_view->setRootIndex(m_proxy.rootIndexFor(m_store));
28 ✔
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
  unwatchGoneDirs();
28 ✔
221
  watchDir(m_store);
28 ✔
222
  for (const QString &dir : std::as_const(expanded)) {
39 ✔
223
    const QModelIndex shown = m_proxy.mapFromSource(m_fs->index(dir));
11 ✔
224
    if (shown.isValid()) {
225
      m_view->expand(shown);
9 ✔
226
    }
227
  }
228
  bool currentLost = false;
229
  if (!current.isEmpty()) {
28 ✔
230
    const QModelIndex shown = m_proxy.mapFromSource(m_fs->index(current));
10 ✔
231
    if (shown.isValid()) {
232
      m_view->setCurrentIndex(shown);
6 ✔
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
      m_restoreCurrent = current;
4 ✔
238
    }
239
  }
240
  m_view->verticalScrollBar()->setValue(scroll);
28 ✔
241
  if (currentLost) {
28 ✔
242
    emit currentEntryLost(current);
4 ✔
243
  }
244
  restoreCurrent();
28 ✔
245
  if (m_stillListing.isEmpty()) {
246
    // Every listing is in: what does not show now is not waited for.
247
    m_restoreCurrent.clear();
27 ✔
248
  }
249
  emit directoryLoaded(m_store);
28 ✔
250
  if (m_reloadAgain) {
28 ✔
251
    m_reloadAgain = false;
1 ✔
252
    m_reloadTimer.start();
1 ✔
253
  }
254
}
255

256
void StoreTree::setStore(const QString &path) {
244 ✔
257
  m_store = path;
244 ✔
258
  // A build for the previous store is dropped; the folders watched were
259
  // that store's.
260
  m_freshDeadline.stop();
244 ✔
261
  m_fresh.reset();
262
  m_freshPending.clear();
263
  m_stillListing.clear();
264
  m_restoreCurrent.clear();
244 ✔
265
  m_reloadAgain = false;
244 ✔
266
  if (const QStringList watched = m_watcher.directories(); !watched.isEmpty()) {
244 ✔
267
    m_watcher.removePaths(watched);
34 ✔
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
  if (m_proxy.sourceModel() != nullptr) {
244 ✔
273
    m_retired = std::move(m_fs);
274
    m_fs = makeModel();
34 ✔
275
  }
276
  const QModelIndex rootDir = m_fs->setRootPath(path);
244 ✔
277
  m_fs->fetchMore(rootDir);
244 ✔
278
  m_proxy.setModelAndStore(m_fs.get(), path);
244 ✔
279
  setUpColumns();
244 ✔
280
  m_proxy.setFilterRegularExpression(QRegularExpression());
244 ✔
281
  m_view->setRootIndex(m_proxy.rootIndexFor(path));
244 ✔
282
  // Not left to directoryLoaded alone: the root may be listed before then.
283
  if (QFileInfo(path).isDir()) {
244 ✔
284
    watchDir(path);
244 ✔
285
  }
286
}
244 ✔
287

288
void StoreTree::setPass(Pass *pass) { m_proxy.setPass(pass); }
211 ✔
289

290
void StoreTree::setFilter(const QRegularExpression &pattern) {
93 ✔
291
  m_proxy.setFilterRegularExpression(pattern);
93 ✔
292
  // Filtering re-derives the mapping; the root must be looked up again.
293
  m_view->setRootIndex(m_proxy.rootIndexFor(m_store));
93 ✔
294
  restoreCurrent();
93 ✔
295
}
93 ✔
296

297
auto StoreTree::dirFor(const QModelIndex &index, bool forPass) const
60 ✔
298
    -> QString {
299
  const QString abspath = QDir(m_store).absolutePath() + QDir::separator();
120 ✔
300
  if (!index.isValid()) {
301
    return forPass ? QString() : abspath;
21 ✔
302
  }
303
  const QFileInfo info = m_fs->fileInfo(m_proxy.mapToSource(index));
39 ✔
304
  QString filePath =
305
      info.isFile() ? info.absolutePath() : info.absoluteFilePath();
39 ✔
306
  if (forPass) {
39 ✔
307
    filePath = QDir(abspath).relativeFilePath(filePath);
24 ✔
308
  }
309
  filePath += QDir::separator();
39 ✔
310
  return filePath;
311
}
39 ✔
312

313
auto StoreTree::currentDir(bool forPass) const -> QString {
53 ✔
314
  return dirFor(m_view->currentIndex(), forPass);
53 ✔
315
}
316

317
auto StoreTree::fileFor(const QModelIndex &index, bool forPass) const
74 ✔
318
    -> QString {
319
  if (!index.isValid() ||
71 ✔
320
      !m_fs->fileInfo(m_proxy.mapToSource(index)).isFile()) {
145 ✔
321
    return {};
4 ✔
322
  }
323
  QString filePath = m_fs->filePath(m_proxy.mapToSource(index));
70 ✔
324
  if (forPass) {
70 ✔
325
    filePath = QDir(m_store).relativeFilePath(filePath);
126 ✔
326
    filePath.replace(Util::endsWithGpg(), "");
126 ✔
327
  }
328
  return filePath;
329
}
330

331
auto StoreTree::currentFile(bool forPass) const -> QString {
1 ✔
332
  return fileFor(m_view->currentIndex(), forPass);
1 ✔
333
}
334

335
auto StoreTree::fileInfo(const QModelIndex &index) const -> QFileInfo {
99 ✔
336
  return m_fs->fileInfo(m_proxy.mapToSource(index));
99 ✔
337
}
338

339
auto StoreTree::indexFor(const QString &path) const -> QModelIndex {
180 ✔
340
  const QModelIndex src = m_fs->index(path);
180 ✔
341
  return src.isValid() ? m_proxy.mapFromSource(src) : QModelIndex();
180 ✔
342
}
343

344
auto StoreTree::rootIndex() -> QModelIndex {
20 ✔
345
  return m_proxy.rootIndexFor(m_store);
20 ✔
346
}
347

348
auto StoreTree::currentIndex() const -> QModelIndex {
×
349
  return m_view->currentIndex();
×
350
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc