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

IJHack / QtPass / 24637037799

19 Apr 2026 07:17PM UTC coverage: 27.338%. Remained the same
24637037799

Pull #1078

github

web-flow
Merge 54d48b5ae into cf114abdf
Pull Request #1078: docs: Comments on Qt6.10 specials

0 of 1 new or added line in 1 file covered. (0.0%)

45 existing lines in 1 file now uncovered.

1593 of 5827 relevant lines covered (27.34%)

16.46 hits per line

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

41.61
/src/storemodel.cpp
1
// SPDX-FileCopyrightText: 2014 Anne Jan Brouwer
2
// SPDX-License-Identifier: GPL-3.0-or-later
3
#include "storemodel.h"
4
#include "qtpasssettings.h"
5

6
#include "util.h"
7
#include <QApplication>
8
#include <QDebug>
9
#include <QFileSystemModel>
10
#include <QMessageBox>
11
#include <QMimeData>
12
#include <QRegularExpression>
13
#include <QtGlobal>
14

15
auto operator<<(
1✔
16
    QDataStream &out,
17
    const dragAndDropInfoPasswordStore &dragAndDropInfoPasswordStore)
18
    -> QDataStream & {
19
  out << static_cast<quint8>(dragAndDropInfoPasswordStore.kind)
1✔
20
      << dragAndDropInfoPasswordStore.path;
1✔
21
  return out;
1✔
22
}
23

24
auto operator>>(QDataStream &in,
×
25
                dragAndDropInfoPasswordStore &dragAndDropInfoPasswordStore)
26
    -> QDataStream & {
27
  quint8 k;
28
  in >> k >> dragAndDropInfoPasswordStore.path;
×
29
  switch (k) {
×
30
  case static_cast<quint8>(dragAndDropInfoPasswordStore::ItemKind::Directory):
×
31
    dragAndDropInfoPasswordStore.kind =
×
32
        dragAndDropInfoPasswordStore::ItemKind::Directory;
33
    break;
×
34
  case static_cast<quint8>(dragAndDropInfoPasswordStore::ItemKind::File):
×
35
    dragAndDropInfoPasswordStore.kind =
×
36
        dragAndDropInfoPasswordStore::ItemKind::File;
37
    break;
×
38
  default:
×
39
    dragAndDropInfoPasswordStore.kind =
×
40
        dragAndDropInfoPasswordStore::ItemKind::Unknown;
41
    break;
×
42
  }
43
  return in;
×
44
}
45

46
/**
47
 * @brief StoreModel::StoreModel
48
 * SubClass of QSortFilterProxyModel via
49
 * http://www.qtcentre.org/threads/46471-QTreeView-Filter
50
 */
51
StoreModel::StoreModel() { fs = nullptr; }
19✔
52

53
/**
54
 * @brief StoreModel::filterAcceptsRow should row be shown, wrapper for
55
 * StoreModel::showThis method.
56
 * @param sourceRow
57
 * @param sourceParent
58
 * @return
59
 */
60
auto StoreModel::filterAcceptsRow(int sourceRow,
19✔
61
                                  const QModelIndex &sourceParent) const
62
    -> bool {
63
  QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
19✔
64
  return showThis(index);
19✔
65
}
66

67
/**
68
 * @brief StoreModel::showThis should a row be shown, based on our search
69
 * criteria.
70
 * @param index
71
 * @return
72
 */
73
auto StoreModel::showThis(const QModelIndex &index) const -> bool {
44✔
74
  bool retVal = false;
75
  if (fs == nullptr) {
44✔
76
    return retVal;
77
  }
78
  // Gives you the info for number of children with a parent
79
  if (sourceModel()->rowCount(index) > 0) {
43✔
80
    for (int nChild = 0; nChild < sourceModel()->rowCount(index); ++nChild) {
24✔
81
      QModelIndex childIndex = sourceModel()->index(nChild, 0, index);
24✔
82
      if (!childIndex.isValid()) {
83
        break;
84
      }
85
      retVal = showThis(childIndex);
24✔
86
      if (retVal) {
24✔
87
        break;
88
      }
89
    }
90
  } else {
91
    QModelIndex useIndex = sourceModel()->index(index.row(), 0, index.parent());
19✔
92
    QString path = fs->filePath(useIndex);
19✔
93
    path = QDir(store).relativeFilePath(path);
38✔
94
    if (path.startsWith(".git")) {
38✔
95
      return false;
96
    }
97
    path.replace(Util::endsWithGpg(), "");
18✔
98
    retVal = path.contains(filterRegularExpression());
18✔
99
  }
100
  return retVal;
101
}
102

103
/**
104
 * @brief StoreModel::setModelAndStore update the source model and store.
105
 * @param sourceModel
106
 * @param passStore
107
 */
108
void StoreModel::setModelAndStore(QFileSystemModel *sourceModel,
14✔
109
                                  const QString &passStore) {
110
  setSourceModel(sourceModel);
14✔
111
  fs = sourceModel;
14✔
112
  store = passStore;
14✔
113
}
14✔
114

115
void StoreModel::setStore(const QString &passStore) {
×
116
#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
117
  // Qt 6.10+ provides beginFilterChange()/endFilterChange(), which is the
118
  // preferred API for scoped and more efficient filter updates.
119
  beginFilterChange();
120
  store = passStore;
121
  endFilterChange(QSortFilterProxyModel::Direction::Rows);
122
#else
123
  // Older Qt versions do not provide the begin/end filter change API, so we
124
  // update the store and manually invalidate the filter as a compatibility path.
NEW
125
  store = passStore;
×
126
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
UNCOV
127
  QSortFilterProxyModel::invalidateFilter();
×
128
#else
129
  invalidateFilter();
130
#endif
131
#endif
UNCOV
132
}
×
133

134
/**
135
 * @brief StoreModel::data don't show the .gpg at the end of a file.
136
 * @param index
137
 * @param role
138
 * @return
139
 */
140
auto StoreModel::data(const QModelIndex &index, int role) const -> QVariant {
2✔
141
  if (!index.isValid()) {
142
    return {};
143
  }
144

145
  QVariant initial_value;
146
  initial_value = QSortFilterProxyModel::data(index, role);
1✔
147

148
  if (role == Qt::DisplayRole) {
1✔
149
    QString name = initial_value.toString();
1✔
150
    name.replace(Util::endsWithGpg(), "");
1✔
151
    initial_value.setValue(name);
1✔
152
  }
153

154
  return initial_value;
1✔
155
}
1✔
156

157
/**
158
 * @brief StoreModel::supportedDropActions enable drop.
159
 * @return
160
 */
161
auto StoreModel::supportedDropActions() const -> Qt::DropActions {
1✔
162
  return Qt::CopyAction | Qt::MoveAction;
1✔
163
}
164

165
/**
166
 * @brief StoreModel::supportedDragActions enable drag.
167
 * @return
168
 */
169
auto StoreModel::supportedDragActions() const -> Qt::DropActions {
1✔
170
  return Qt::CopyAction | Qt::MoveAction;
1✔
171
}
172

173
/**
174
 * @brief StoreModel::flags
175
 * @param index
176
 * @return
177
 */
178
auto StoreModel::flags(const QModelIndex &index) const -> Qt::ItemFlags {
2✔
179
  Qt::ItemFlags defaultFlags = QSortFilterProxyModel::flags(index);
2✔
180

181
  if (index.isValid()) {
182
    return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
183
  }
184
  return Qt::ItemIsDropEnabled | defaultFlags;
1✔
185
}
186

187
/**
188
 * @brief StoreModel::mimeTypes
189
 * @return
190
 */
191
auto StoreModel::mimeTypes() const -> QStringList {
1✔
192
  QStringList types;
1✔
193
  types << "application/vnd+qtpass.dragAndDropInfoPasswordStore";
1✔
194
  return types;
1✔
195
}
196

197
/**
198
 * @brief StoreModel::mimeData
199
 * @param indexes
200
 * @return
201
 */
202
auto StoreModel::mimeData(const QModelIndexList &indexes) const -> QMimeData * {
1✔
203
  dragAndDropInfoPasswordStore info;
1✔
204

205
  QByteArray encodedData;
1✔
206
  // only use the first, otherwise we should enable multiselection
207
  QModelIndex index = indexes.at(0);
1✔
208
  if (index.isValid()) {
209
    QModelIndex useIndex = mapToSource(index);
1✔
210
    const QFileInfo fileInfo = fs->fileInfo(useIndex);
1✔
211

212
    if (fileInfo.isDir()) {
1✔
UNCOV
213
      info.kind = dragAndDropInfoPasswordStore::ItemKind::Directory;
×
214
    } else if (fileInfo.isFile()) {
1✔
215
      info.kind = dragAndDropInfoPasswordStore::ItemKind::File;
1✔
216
    }
217
    info.path = fileInfo.absoluteFilePath();
2✔
218
    QDataStream stream(&encodedData, QIODevice::WriteOnly);
1✔
219
    stream << info;
1✔
220
  }
1✔
221

222
  auto *mimeData = new QMimeData();
1✔
223
  mimeData->setData("application/vnd+qtpass.dragAndDropInfoPasswordStore",
2✔
224
                    encodedData);
225
  return mimeData;
1✔
226
}
227

228
/**
229
 * @brief StoreModel::canDropMimeData
230
 * @param data
231
 * @param action
232
 * @param row
233
 * @param column
234
 * @param parent
235
 * @return
236
 */
UNCOV
237
auto StoreModel::canDropMimeData(const QMimeData *data, Qt::DropAction action,
×
238
                                 int row, int column,
239
                                 const QModelIndex &parent) const -> bool {
240
#ifdef QT_DEBUG
241
  qDebug() << action << row;
242
#else
243
  Q_UNUSED(action)
244
  Q_UNUSED(row)
245
#endif
246

UNCOV
247
  if (data == nullptr ||
×
248
      !data->hasFormat("application/vnd+qtpass.dragAndDropInfoPasswordStore")) {
×
249
    return false;
250
  }
251

252
  QByteArray encodedData =
UNCOV
253
      data->data("application/vnd+qtpass.dragAndDropInfoPasswordStore");
×
254
  if (encodedData.isEmpty()) {
×
255
    return false;
256
  }
UNCOV
257
  QDataStream stream(&encodedData, QIODevice::ReadOnly);
×
258
  dragAndDropInfoPasswordStore info;
×
259
  stream >> info;
×
260
  if (stream.status() != QDataStream::Ok) {
×
261
    return false;
262
  }
263

264
  QModelIndex useIndex =
UNCOV
265
      this->index(parent.row(), parent.column(), parent.parent());
×
266

UNCOV
267
  if (column > 0) {
×
268
    return false;
269
  }
270

271
  using IK = dragAndDropInfoPasswordStore::ItemKind;
272
  // you can drop a folder on a folder
UNCOV
273
  if (fs->fileInfo(mapToSource(useIndex)).isDir() &&
×
274
      info.kind == IK::Directory) {
×
275
    return true;
276
  }
277
  // you can drop a file on a folder
UNCOV
278
  if (fs->fileInfo(mapToSource(useIndex)).isDir() && info.kind == IK::File) {
×
279
    return true;
280
  }
281
  // you can drop a file on a file
UNCOV
282
  if (fs->fileInfo(mapToSource(useIndex)).isFile() && info.kind == IK::File) {
×
283
    return true;
284
  }
285

286
  return false;
UNCOV
287
}
×
288

289
/**
290
 * @brief StoreModel::dropMimeData
291
 * @param data
292
 * @param action
293
 * @param row
294
 * @param column
295
 * @param parent
296
 * @return
297
 */
UNCOV
298
auto StoreModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
×
299
                              int row, int column, const QModelIndex &parent)
300
    -> bool {
UNCOV
301
  if (!canDropMimeData(data, action, row, column, parent)) {
×
302
    return false;
303
  }
304

UNCOV
305
  if (action == Qt::IgnoreAction) {
×
306
    return true;
307
  }
308

UNCOV
309
  if (action != Qt::MoveAction && action != Qt::CopyAction) {
×
310
    return false;
311
  }
312

UNCOV
313
  dragAndDropInfoPasswordStore info;
×
314
  if (!parseDropData(data, &info)) {
×
315
    return false;
316
  }
317

UNCOV
318
  return executeDropAction(info, action, parent);
×
319
}
320

UNCOV
321
auto StoreModel::parseDropData(const QMimeData *data,
×
322
                               dragAndDropInfoPasswordStore *outInfo) -> bool {
323
  QByteArray encodedData =
UNCOV
324
      data->data("application/vnd+qtpass.dragAndDropInfoPasswordStore");
×
325
  if (encodedData.isEmpty()) {
×
326
    return false;
327
  }
328

UNCOV
329
  QDataStream stream(&encodedData, QIODevice::ReadOnly);
×
330
  dragAndDropInfoPasswordStore info;
×
331
  stream >> info;
×
332
  if (stream.status() != QDataStream::Ok) {
×
333
    return false;
334
  }
335

336
  *outInfo = info;
UNCOV
337
  return true;
×
338
}
×
339

UNCOV
340
auto StoreModel::executeDropAction(const dragAndDropInfoPasswordStore &info,
×
341
                                   Qt::DropAction action,
342
                                   const QModelIndex &parent) -> bool {
343
  QModelIndex destIndex =
UNCOV
344
      this->index(parent.row(), parent.column(), parent.parent());
×
345
  QFileInfo destFileinfo = fs->fileInfo(mapToSource(destIndex));
×
346
  QFileInfo srcFileInfo = QFileInfo(info.path);
×
347

UNCOV
348
  QString cleanedSrc = QDir::cleanPath(srcFileInfo.absoluteFilePath());
×
349
  QString cleanedDest = QDir::cleanPath(destFileinfo.absoluteFilePath());
×
350

UNCOV
351
  switch (info.kind) {
×
352
  case dragAndDropInfoPasswordStore::ItemKind::Directory:
×
353
    return handleDirDrop(cleanedSrc, destFileinfo, srcFileInfo, action);
×
354
  case dragAndDropInfoPasswordStore::ItemKind::File:
×
355
    return handleFileDrop(cleanedSrc, cleanedDest, destFileinfo, action);
×
356
  default:
UNCOV
357
    qWarning() << "executeDropAction: unexpected ItemKind, ignoring drop";
×
358
    return false;
×
359
  }
UNCOV
360
}
×
361

UNCOV
362
auto StoreModel::handleDirDrop(const QString &cleanedSrc,
×
363
                               const QFileInfo &destFileinfo,
364
                               const QFileInfo &srcFileInfo,
365
                               Qt::DropAction action) -> bool {
UNCOV
366
  if (!destFileinfo.isDir()) {
×
367
    return false;
368
  }
369

UNCOV
370
  QDir destDir = QDir(QDir::cleanPath(destFileinfo.absoluteFilePath()))
×
371
                     .filePath(srcFileInfo.fileName());
×
372
  QString cleanedDestDir = QDir::cleanPath(destDir.absolutePath());
×
373

UNCOV
374
  if (action == Qt::MoveAction) {
×
375
    QtPassSettings::getPass()->Move(cleanedSrc, cleanedDestDir);
×
376
  } else if (action == Qt::CopyAction) {
×
377
    QtPassSettings::getPass()->Copy(cleanedSrc, cleanedDestDir);
×
378
  }
379
  return true;
UNCOV
380
}
×
381

UNCOV
382
auto StoreModel::handleFileDrop(const QString &cleanedSrc,
×
383
                                const QString &cleanedDest,
384
                                const QFileInfo &destFileinfo,
385
                                Qt::DropAction action) -> bool {
UNCOV
386
  if (destFileinfo.isDir()) {
×
387
    return handleFileToDirDrop(cleanedSrc, cleanedDest, action);
×
388
  }
UNCOV
389
  return handleFileToFileDrop(cleanedSrc, cleanedDest, action);
×
390
}
391

UNCOV
392
auto StoreModel::handleFileToDirDrop(const QString &cleanedSrc,
×
393
                                     const QString &cleanedDest,
394
                                     Qt::DropAction action) -> bool {
UNCOV
395
  if (action == Qt::MoveAction) {
×
396
    QtPassSettings::getPass()->Move(cleanedSrc, cleanedDest);
×
397
  } else if (action == Qt::CopyAction) {
×
398
    QtPassSettings::getPass()->Copy(cleanedSrc, cleanedDest);
×
399
  }
UNCOV
400
  return true;
×
401
}
402

UNCOV
403
auto StoreModel::handleFileToFileDrop(const QString &cleanedSrc,
×
404
                                      const QString &cleanedDest,
405
                                      Qt::DropAction action) -> bool {
406
  QWidget *parentWidget = qobject_cast<QWidget *>(parent());
UNCOV
407
  int answer = QMessageBox::question(
×
408
      parentWidget, tr("Force overwrite?"),
×
409
      tr("overwrite %1 with %2?").arg(cleanedDest, cleanedSrc),
×
410
      QMessageBox::Yes | QMessageBox::No);
UNCOV
411
  bool force = answer == QMessageBox::Yes;
×
412

UNCOV
413
  if (action == Qt::MoveAction) {
×
414
    QtPassSettings::getPass()->Move(cleanedSrc, cleanedDest, force);
×
415
  } else if (action == Qt::CopyAction) {
×
416
    QtPassSettings::getPass()->Copy(cleanedSrc, cleanedDest, force);
×
417
  }
UNCOV
418
  return true;
×
419
}
420

421
/**
422
 * @brief StoreModel::lessThan
423
 * @param source_left
424
 * @param source_right
425
 * @return
426
 */
427
auto StoreModel::lessThan(const QModelIndex &source_left,
2✔
428
                          const QModelIndex &source_right) const -> bool {
429
/* matches logic in QFileSystemModelSorter::compareNodes() */
430
#ifndef Q_OS_MAC
431
  if (fs && (source_left.column() == 0 || source_left.column() == 1)) {
2✔
432
    bool leftD = fs->isDir(source_left);
2✔
433
    bool rightD = fs->isDir(source_right);
2✔
434

435
    if (leftD ^ rightD) {
2✔
436
      return leftD;
437
    }
438
  }
439
#endif
440

441
  return QSortFilterProxyModel::lessThan(source_left, source_right);
1✔
442
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc