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

IJHack / QtPass / 24634426661

19 Apr 2026 05:03PM UTC coverage: 27.201% (+0.04%) from 27.166%
24634426661

push

github

web-flow
Merge pull request #1072 from IJHack/ai-findings-autofix/src-storemodel.cpp

fix: correct 'childs' typo and capitalise dialog title in storemodel

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

9 existing lines in 1 file now uncovered.

1582 of 5816 relevant lines covered (27.2%)

10.23 hits per line

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

41.25
/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
  beginFilterChange();
118
  store = passStore;
119
  endFilterChange(QSortFilterProxyModel::Direction::Rows);
120
#else
121
  store = passStore;
×
122
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
123
  QSortFilterProxyModel::invalidateFilter();
×
124
#else
125
  invalidateFilter();
126
#endif
127
#endif
128
}
×
129

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

141
  QVariant initial_value;
142
  initial_value = QSortFilterProxyModel::data(index, role);
1✔
143

144
  if (role == Qt::DisplayRole) {
1✔
145
    QString name = initial_value.toString();
1✔
146
    name.replace(Util::endsWithGpg(), "");
1✔
147
    initial_value.setValue(name);
1✔
148
  }
149

150
  return initial_value;
1✔
151
}
1✔
152

153
/**
154
 * @brief StoreModel::supportedDropActions enable drop.
155
 * @return
156
 */
157
auto StoreModel::supportedDropActions() const -> Qt::DropActions {
1✔
158
  return Qt::CopyAction | Qt::MoveAction;
1✔
159
}
160

161
/**
162
 * @brief StoreModel::supportedDragActions enable drag.
163
 * @return
164
 */
165
auto StoreModel::supportedDragActions() const -> Qt::DropActions {
1✔
166
  return Qt::CopyAction | Qt::MoveAction;
1✔
167
}
168

169
/**
170
 * @brief StoreModel::flags
171
 * @param index
172
 * @return
173
 */
174
auto StoreModel::flags(const QModelIndex &index) const -> Qt::ItemFlags {
2✔
175
  Qt::ItemFlags defaultFlags = QSortFilterProxyModel::flags(index);
2✔
176

177
  if (index.isValid()) {
178
    return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
179
  }
180
  return Qt::ItemIsDropEnabled | defaultFlags;
1✔
181
}
182

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

193
/**
194
 * @brief StoreModel::mimeData
195
 * @param indexes
196
 * @return
197
 */
198
auto StoreModel::mimeData(const QModelIndexList &indexes) const -> QMimeData * {
1✔
199
  dragAndDropInfoPasswordStore info;
1✔
200

201
  QByteArray encodedData;
1✔
202
  // only use the first, otherwise we should enable multiselection
203
  QModelIndex index = indexes.at(0);
1✔
204
  if (index.isValid()) {
205
    QModelIndex useIndex = mapToSource(index);
1✔
206

207
    if (fs->fileInfo(useIndex).isDir())
1✔
208
      info.kind = dragAndDropInfoPasswordStore::ItemKind::Directory;
×
209
    else if (fs->fileInfo(useIndex).isFile())
1✔
210
      info.kind = dragAndDropInfoPasswordStore::ItemKind::File;
1✔
211
    info.path = fs->fileInfo(useIndex).absoluteFilePath();
2✔
212
    QDataStream stream(&encodedData, QIODevice::WriteOnly);
1✔
213
    stream << info;
1✔
214
  }
1✔
215

216
  auto *mimeData = new QMimeData();
1✔
217
  mimeData->setData("application/vnd+qtpass.dragAndDropInfoPasswordStore",
2✔
218
                    encodedData);
219
  return mimeData;
1✔
220
}
221

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

241
  if (data == nullptr ||
×
242
      !data->hasFormat("application/vnd+qtpass.dragAndDropInfoPasswordStore")) {
×
243
    return false;
244
  }
245

246
  QByteArray encodedData =
247
      data->data("application/vnd+qtpass.dragAndDropInfoPasswordStore");
×
248
  if (encodedData.isEmpty()) {
×
249
    return false;
250
  }
251
  QDataStream stream(&encodedData, QIODevice::ReadOnly);
×
252
  dragAndDropInfoPasswordStore info;
×
253
  stream >> info;
×
254
  if (stream.status() != QDataStream::Ok) {
×
255
    return false;
256
  }
257

258
  QModelIndex useIndex =
259
      this->index(parent.row(), parent.column(), parent.parent());
×
260

261
  if (column > 0) {
×
262
    return false;
263
  }
264

265
  using IK = dragAndDropInfoPasswordStore::ItemKind;
266
  // you can drop a folder on a folder
267
  if (fs->fileInfo(mapToSource(useIndex)).isDir() &&
×
268
      info.kind == IK::Directory) {
×
269
    return true;
270
  }
271
  // you can drop a file on a folder
272
  if (fs->fileInfo(mapToSource(useIndex)).isDir() && info.kind == IK::File) {
×
273
    return true;
274
  }
275
  // you can drop a file on a file
276
  if (fs->fileInfo(mapToSource(useIndex)).isFile() && info.kind == IK::File) {
×
277
    return true;
278
  }
279

280
  return false;
281
}
×
282

283
/**
284
 * @brief StoreModel::dropMimeData
285
 * @param data
286
 * @param action
287
 * @param row
288
 * @param column
289
 * @param parent
290
 * @return
291
 */
292
auto StoreModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
×
293
                              int row, int column, const QModelIndex &parent)
294
    -> bool {
295
  if (!canDropMimeData(data, action, row, column, parent)) {
×
296
    return false;
297
  }
298

299
  if (action == Qt::IgnoreAction) {
×
300
    return true;
301
  }
302

303
  if (action != Qt::MoveAction && action != Qt::CopyAction) {
×
304
    return false;
305
  }
306

307
  dragAndDropInfoPasswordStore info;
×
308
  if (!parseDropData(data, &info)) {
×
309
    return false;
310
  }
311

312
  return executeDropAction(info, action, parent);
×
313
}
314

315
auto StoreModel::parseDropData(const QMimeData *data,
×
316
                               dragAndDropInfoPasswordStore *outInfo) -> bool {
317
  QByteArray encodedData =
318
      data->data("application/vnd+qtpass.dragAndDropInfoPasswordStore");
×
319
  if (encodedData.isEmpty()) {
×
320
    return false;
321
  }
322

323
  QDataStream stream(&encodedData, QIODevice::ReadOnly);
×
324
  dragAndDropInfoPasswordStore info;
×
325
  stream >> info;
×
326
  if (stream.status() != QDataStream::Ok) {
×
327
    return false;
328
  }
329

330
  *outInfo = info;
331
  return true;
×
332
}
×
333

334
auto StoreModel::executeDropAction(const dragAndDropInfoPasswordStore &info,
×
335
                                   Qt::DropAction action,
336
                                   const QModelIndex &parent) -> bool {
337
  QModelIndex destIndex =
338
      this->index(parent.row(), parent.column(), parent.parent());
×
339
  QFileInfo destFileinfo = fs->fileInfo(mapToSource(destIndex));
×
340
  QFileInfo srcFileInfo = QFileInfo(info.path);
×
341

342
  QString cleanedSrc = QDir::cleanPath(srcFileInfo.absoluteFilePath());
×
343
  QString cleanedDest = QDir::cleanPath(destFileinfo.absoluteFilePath());
×
344

345
  switch (info.kind) {
×
346
  case dragAndDropInfoPasswordStore::ItemKind::Directory:
×
347
    return handleDirDrop(cleanedSrc, destFileinfo, srcFileInfo, action);
×
348
  case dragAndDropInfoPasswordStore::ItemKind::File:
×
349
    return handleFileDrop(cleanedSrc, cleanedDest, destFileinfo, action);
×
350
  default:
351
    qWarning() << "executeDropAction: unexpected ItemKind, ignoring drop";
×
352
    return false;
×
353
  }
354
}
×
355

356
auto StoreModel::handleDirDrop(const QString &cleanedSrc,
×
357
                               const QFileInfo &destFileinfo,
358
                               const QFileInfo &srcFileInfo,
359
                               Qt::DropAction action) -> bool {
360
  if (!destFileinfo.isDir()) {
×
361
    return false;
362
  }
363

364
  QDir destDir = QDir(QDir::cleanPath(destFileinfo.absoluteFilePath()))
×
365
                     .filePath(srcFileInfo.fileName());
×
366
  QString cleanedDestDir = QDir::cleanPath(destDir.absolutePath());
×
367

368
  if (action == Qt::MoveAction) {
×
369
    QtPassSettings::getPass()->Move(cleanedSrc, cleanedDestDir);
×
370
  } else if (action == Qt::CopyAction) {
×
371
    QtPassSettings::getPass()->Copy(cleanedSrc, cleanedDestDir);
×
372
  }
373
  return true;
374
}
×
375

376
auto StoreModel::handleFileDrop(const QString &cleanedSrc,
×
377
                                const QString &cleanedDest,
378
                                const QFileInfo &destFileinfo,
379
                                Qt::DropAction action) -> bool {
380
  if (destFileinfo.isDir()) {
×
381
    return handleFileToDirDrop(cleanedSrc, cleanedDest, action);
×
382
  }
383
  return handleFileToFileDrop(cleanedSrc, cleanedDest, action);
×
384
}
385

386
auto StoreModel::handleFileToDirDrop(const QString &cleanedSrc,
×
387
                                     const QString &cleanedDest,
388
                                     Qt::DropAction action) -> bool {
389
  if (action == Qt::MoveAction) {
×
390
    QtPassSettings::getPass()->Move(cleanedSrc, cleanedDest);
×
391
  } else if (action == Qt::CopyAction) {
×
392
    QtPassSettings::getPass()->Copy(cleanedSrc, cleanedDest);
×
393
  }
394
  return true;
×
395
}
396

397
auto StoreModel::handleFileToFileDrop(const QString &cleanedSrc,
×
398
                                      const QString &cleanedDest,
399
                                      Qt::DropAction action) -> bool {
400
  QWidget *parentWidget = qobject_cast<QWidget *>(parent());
401
  int answer = QMessageBox::question(
×
NEW
402
      parentWidget, tr("Force overwrite?"),
×
403
      tr("overwrite %1 with %2?").arg(cleanedDest, cleanedSrc),
×
404
      QMessageBox::Yes | QMessageBox::No);
405
  bool force = answer == QMessageBox::Yes;
×
406

407
  if (action == Qt::MoveAction) {
×
408
    QtPassSettings::getPass()->Move(cleanedSrc, cleanedDest, force);
×
409
  } else if (action == Qt::CopyAction) {
×
410
    QtPassSettings::getPass()->Copy(cleanedSrc, cleanedDest, force);
×
411
  }
412
  return true;
×
413
}
414

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

429
    if (leftD ^ rightD) {
2✔
430
      return leftD;
431
    }
432
  }
433
#endif
434

435
  return QSortFilterProxyModel::lessThan(source_left, source_right);
1✔
436
}
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