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

IJHack / QtPass / 46376622100

30 Nov 2026 07:48AM UTC coverage: 94.369% (-0.1%) from 94.493%
46376622100

push

github

web-flow
The store tree follows changes without Qt's quadratic watch (#2409)

QFileSystemModel watched the store and, for every changed file, found its
row by a linear search, so a pull that rewrote N entries of one folder froze
the window for on the order of N² comparisons (about half a minute for
20000 entries). Scan finding ANT-2026-OJ33F28Z.

The tree's model no longer watches (DontWatchForChanges). StoreTree watches
the folders the shown model has listed with its own QFileSystemWatcher and,
once they have been quiet for 300 ms, builds a fresh model in the
background, listing the store root and every folder listed before, and
swaps it in with the expanded folders, the current entry and the scroll
position kept. Building a fresh model is linear; the worst stall for 20000
entries went from about 23 s to under a second.

Around the swap:
- one build at a time; a change during a build runs one more after it;
- a folder listed while a build runs is listed in the fresh model too;
- the swap waits up to 3 s; listings still under way finish in the shown
  model, after which a search is applied again;
- a folder expanded later is watched then; a store shown again gets a
  fresh model;
- a current entry the swap cannot show is reported (currentEntryLost); an
  open tree menu closes on it rather than acting on the store root, and a
  current entry that still exists comes back once it shows, unless the
  user picked another one meanwhile.

MainWindow's pending-open logic follows StoreTree::directoryLoaded instead
of the model's signal.

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

145 of 152 new or added lines in 2 files covered. (95.39%)

15 existing lines in 1 file now uncovered.

11313 of 11988 relevant lines covered (94.37%)

23974.39 hits per line

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

96.3
/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)
222 ✔
17
    : QObject(parent), m_view(view), m_fs(makeModel()) {
222 ✔
18
  m_view->setModel(&m_proxy);
222 ✔
19
  m_view->setHeaderHidden(true);
222 ✔
20
  m_view->setIndentation(15);
222 ✔
21
  m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
222 ✔
22
  m_view->setContextMenuPolicy(Qt::CustomContextMenu);
222 ✔
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,
222 ✔
26
          [this](const QModelIndex &current) {
380 ✔
27
            if (current.isValid()) {
28
              m_restoreCurrent.clear();
118 ✔
29
            }
30
          });
158 ✔
31

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

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

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

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

90
void StoreTree::shownListed(const QString &path) {
195 ✔
91
  const QString clean = QDir::cleanPath(path);
195 ✔
92
  watchDir(clean);
195 ✔
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) {
195 ✔
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
  if (m_stillListing.remove(clean) && m_stillListing.isEmpty() &&
196 ✔
105
      !m_proxy.filterRegularExpression().pattern().isEmpty()) {
196 ✔
106
    m_proxy.invalidate();
1 ✔
107
    m_view->setRootIndex(m_proxy.rootIndexFor(m_store));
1 ✔
108
  }
109
  restoreCurrent();
195 ✔
110
  emit directoryLoaded(path);
195 ✔
111
}
195 ✔
112

113
void StoreTree::restoreCurrent() {
298 ✔
114
  if (m_restoreCurrent.isEmpty()) {
298 ✔
115
    return;
296 ✔
116
  }
117
  if (!QFileInfo::exists(m_restoreCurrent)) {
6 ✔
118
    m_restoreCurrent.clear();
2 ✔
119
    return;
2 ✔
120
  }
121
  const QModelIndex shown = indexFor(m_restoreCurrent);
4 ✔
122
  if (!shown.isValid()) {
123
    return;
124
  }
125
  // Unless something else is current (a pick also ends the wait).
126
  if (!m_view->currentIndex().isValid()) {
2 ✔
127
    m_view->setCurrentIndex(shown);
2 ✔
128
  }
129
  m_restoreCurrent.clear();
2 ✔
130
}
131

132
void StoreTree::reload() {
21 ✔
133
  if (m_store.isEmpty()) {
21 ✔
134
    return;
1 ✔
135
  }
136
  if (m_fresh != nullptr) {
21 ✔
137
    // One build at a time. What changed meanwhile may be missing from it:
138
    // list once more after the swap.
139
    m_reloadAgain = true;
1 ✔
140
    return;
1 ✔
141
  }
142
  m_reloadTimer.stop();
20 ✔
143
  m_fresh = makeModel();
40 ✔
144
  m_freshPending.clear();
145
  const QString root = QDir::cleanPath(m_store);
20 ✔
146
  m_fresh->setRootPath(root);
20 ✔
147
  m_freshPending.insert(root);
20 ✔
148
  m_fresh->fetchMore(m_fresh->index(root));
20 ✔
149
  // Every folder the shown model listed and that still exists, expanded or
150
  // not: a search judges a folder that was never listed by its name alone,
151
  // and cannot expand one it hides, so a widened search would miss what is
152
  // inside. The swap can also show the expanded ones expanded at once.
153
  for (const QString &dir : m_watcher.directories()) {
73 ✔
154
    if (dir == root || !QFileInfo(dir).isDir()) {
46 ✔
155
      continue;
20 ✔
156
    }
157
    m_freshPending.insert(dir);
13 ✔
158
    m_fresh->fetchMore(m_fresh->index(dir));
13 ✔
159
  }
160
  m_freshDeadline.start();
20 ✔
161
}
162

163
void StoreTree::swapInFresh() {
20 ✔
164
  m_freshDeadline.stop();
20 ✔
165
  if (m_fresh == nullptr) {
20 ✔
NEW
166
    return;
×
167
  }
168
  // What the view shows now, by path.
169
  QStringList expanded;
20 ✔
170
  for (const QString &dir : m_watcher.directories()) {
74 ✔
171
    const QModelIndex shown = m_proxy.mapFromSource(m_fs->index(dir));
34 ✔
172
    if (shown.isValid() && m_view->isExpanded(shown)) {
29 ✔
173
      expanded << dir;
174
    }
175
  }
176
  // Parents first, so each expand finds its row.
177
  std::sort(expanded.begin(), expanded.end(),
40 ✔
NEW
178
            [](const QString &a, const QString &b) {
×
NEW
179
              return a.count(QLatin1Char('/')) < b.count(QLatin1Char('/'));
×
180
            });
181
  const QString current =
182
      m_fs->filePath(m_proxy.mapToSource(m_view->currentIndex()));
20 ✔
183
  const int scroll = m_view->verticalScrollBar()->value();
20 ✔
184

185
  m_retired = std::move(m_fs);
186
  m_fs = std::move(m_fresh);
187
  // Swapped at the deadline, the listings still under way finish in it.
188
  m_stillListing = m_freshPending;
189
  m_freshPending.clear();
190
  m_restoreCurrent.clear();
20 ✔
191
  m_proxy.setModelAndStore(m_fs.get(), m_store);
20 ✔
192
  setUpColumns();
20 ✔
193
  m_view->setRootIndex(m_proxy.rootIndexFor(m_store));
20 ✔
194
  // The folders reload() had listed stay watched, as long as they exist; a
195
  // folder the new model lists later is watched then.
196
  const QStringList watched = m_watcher.directories();
20 ✔
197
  QStringList gone;
20 ✔
198
  for (const QString &dir : watched) {
54 ✔
199
    if (!QFileInfo(dir).isDir()) {
34 ✔
200
      gone << dir;
201
    }
202
  }
203
  if (!gone.isEmpty()) {
20 ✔
NEW
204
    m_watcher.removePaths(gone);
×
205
  }
206
  watchDir(m_store);
20 ✔
207
  for (const QString &dir : std::as_const(expanded)) {
29 ✔
208
    const QModelIndex shown = m_proxy.mapFromSource(m_fs->index(dir));
9 ✔
209
    if (shown.isValid()) {
210
      m_view->expand(shown);
8 ✔
211
    }
212
  }
213
  bool currentLost = false;
214
  if (!current.isEmpty()) {
20 ✔
215
    const QModelIndex shown = m_proxy.mapFromSource(m_fs->index(current));
8 ✔
216
    if (shown.isValid()) {
217
      m_view->setCurrentIndex(shown);
5 ✔
218
    } else {
219
      // Gone, or not listed yet when the swap came at the deadline: then
220
      // it is made current again once its listing shows it.
221
      currentLost = true;
222
      m_restoreCurrent = current;
3 ✔
223
    }
224
  }
225
  m_view->verticalScrollBar()->setValue(scroll);
20 ✔
226
  if (currentLost) {
20 ✔
227
    emit currentEntryLost(current);
3 ✔
228
  }
229
  restoreCurrent();
20 ✔
230
  emit directoryLoaded(m_store);
20 ✔
231
  if (m_reloadAgain) {
20 ✔
232
    m_reloadAgain = false;
1 ✔
233
    m_reloadTimer.start();
1 ✔
234
  }
235
}
236

237
void StoreTree::setStore(const QString &path) {
238 ✔
238
  m_store = path;
238 ✔
239
  // A build for the previous store is dropped; the folders watched were
240
  // that store's.
241
  m_freshDeadline.stop();
238 ✔
242
  m_fresh.reset();
243
  m_freshPending.clear();
244
  m_stillListing.clear();
245
  m_restoreCurrent.clear();
238 ✔
246
  m_reloadAgain = false;
238 ✔
247
  if (const QStringList watched = m_watcher.directories(); !watched.isEmpty()) {
238 ✔
248
    m_watcher.removePaths(watched);
32 ✔
249
  }
250
  // A fresh model for every store: one that listed this store before keeps
251
  // its folders cached and would show them as they were, unwatched while
252
  // another store was shown.
253
  if (m_proxy.sourceModel() != nullptr) {
238 ✔
254
    m_retired = std::move(m_fs);
255
    m_fs = makeModel();
32 ✔
256
  }
257
  const QModelIndex rootDir = m_fs->setRootPath(path);
238 ✔
258
  m_fs->fetchMore(rootDir);
238 ✔
259
  m_proxy.setModelAndStore(m_fs.get(), path);
238 ✔
260
  setUpColumns();
238 ✔
261
  m_proxy.setFilterRegularExpression(QRegularExpression());
238 ✔
262
  m_view->setRootIndex(m_proxy.rootIndexFor(path));
238 ✔
263
  // Not left to directoryLoaded alone: the root may be listed before then.
264
  if (QFileInfo(path).isDir()) {
238 ✔
265
    watchDir(path);
238 ✔
266
  }
267
}
238 ✔
268

269
void StoreTree::setPass(Pass *pass) { m_proxy.setPass(pass); }
208 ✔
270

271
void StoreTree::setFilter(const QRegularExpression &pattern) {
83 ✔
272
  m_proxy.setFilterRegularExpression(pattern);
83 ✔
273
  // Filtering re-derives the mapping; the root must be looked up again.
274
  m_view->setRootIndex(m_proxy.rootIndexFor(m_store));
83 ✔
275
  restoreCurrent();
83 ✔
276
}
83 ✔
277

278
auto StoreTree::dirFor(const QModelIndex &index, bool forPass) const
56 ✔
279
    -> QString {
280
  const QString abspath = QDir(m_store).absolutePath() + QDir::separator();
112 ✔
281
  if (!index.isValid()) {
282
    return forPass ? QString() : abspath;
21 ✔
283
  }
284
  const QFileInfo info = m_fs->fileInfo(m_proxy.mapToSource(index));
35 ✔
285
  QString filePath =
286
      info.isFile() ? info.absolutePath() : info.absoluteFilePath();
35 ✔
287
  if (forPass) {
35 ✔
288
    filePath = QDir(abspath).relativeFilePath(filePath);
22 ✔
289
  }
290
  filePath += QDir::separator();
35 ✔
291
  return filePath;
292
}
35 ✔
293

294
auto StoreTree::currentDir(bool forPass) const -> QString {
49 ✔
295
  return dirFor(m_view->currentIndex(), forPass);
49 ✔
296
}
297

298
auto StoreTree::fileFor(const QModelIndex &index, bool forPass) const
75 ✔
299
    -> QString {
300
  if (!index.isValid() ||
72 ✔
301
      !m_fs->fileInfo(m_proxy.mapToSource(index)).isFile()) {
147 ✔
302
    return {};
4 ✔
303
  }
304
  QString filePath = m_fs->filePath(m_proxy.mapToSource(index));
71 ✔
305
  if (forPass) {
71 ✔
306
    filePath = QDir(m_store).relativeFilePath(filePath);
128 ✔
307
    filePath.replace(Util::endsWithGpg(), "");
128 ✔
308
  }
309
  return filePath;
310
}
311

312
auto StoreTree::currentFile(bool forPass) const -> QString {
1 ✔
313
  return fileFor(m_view->currentIndex(), forPass);
1 ✔
314
}
315

316
auto StoreTree::fileInfo(const QModelIndex &index) const -> QFileInfo {
83 ✔
317
  return m_fs->fileInfo(m_proxy.mapToSource(index));
83 ✔
318
}
319

320
auto StoreTree::indexFor(const QString &path) const -> QModelIndex {
156 ✔
321
  const QModelIndex src = m_fs->index(path);
156 ✔
322
  return src.isValid() ? m_proxy.mapFromSource(src) : QModelIndex();
156 ✔
323
}
324

325
auto StoreTree::rootIndex() -> QModelIndex {
21 ✔
326
  return m_proxy.rootIndexFor(m_store);
21 ✔
327
}
328

329
auto StoreTree::currentIndex() const -> QModelIndex {
×
330
  return m_view->currentIndex();
×
331
}
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