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

IJHack / QtPass / 24616528398

18 Apr 2026 11:43PM UTC coverage: 22.607%. Remained the same
24616528398

Pull #1048

github

web-flow
Merge bba99ecdb into 5ac2b13c5
Pull Request #1048: Add QVERIFY checks for file operations in settings tests

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

10 existing lines in 2 files now uncovered.

1304 of 5768 relevant lines covered (22.61%)

8.52 hits per line

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

51.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 <QDebug>
8
#include <QFileSystemModel>
9
#include <QMessageBox>
10
#include <QMimeData>
11
#include <QRegularExpression>
12
#include <QtGlobal>
13
#include <utility>
14

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

25
auto operator>>(QDataStream &in,
×
26
                dragAndDropInfoPasswordStore &dragAndDropInfoPasswordStore)
27
    -> QDataStream & {
28
  in >> dragAndDropInfoPasswordStore.isDir >>
×
29
      dragAndDropInfoPasswordStore.isFile >> dragAndDropInfoPasswordStore.path;
×
30
  return in;
×
31
}
32

33
/**
34
 * @brief StoreModel::StoreModel
35
 * SubClass of QSortFilterProxyModel via
36
 * http://www.qtcentre.org/threads/46471-QTreeView-Filter
37
 */
38
StoreModel::StoreModel() { fs = nullptr; }
19✔
39

40
/**
41
 * @brief StoreModel::filterAcceptsRow should row be shown, wrapper for
42
 * StoreModel::showThis method.
43
 * @param sourceRow
44
 * @param sourceParent
45
 * @return
46
 */
47
auto StoreModel::filterAcceptsRow(int sourceRow,
19✔
48
                                  const QModelIndex &sourceParent) const
49
    -> bool {
50
  QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
19✔
51
  return showThis(index);
19✔
52
}
53

54
/**
55
 * @brief StoreModel::showThis should a row be shown, based on our search
56
 * criteria.
57
 * @param index
58
 * @return
59
 */
60
auto StoreModel::showThis(const QModelIndex &index) const -> bool {
44✔
61
  bool retVal = false;
62
  if (fs == nullptr) {
44✔
63
    return retVal;
64
  }
65
  // Gives you the info for number of childs with a parent
66
  if (sourceModel()->rowCount(index) > 0) {
43✔
67
    for (int nChild = 0; nChild < sourceModel()->rowCount(index); ++nChild) {
24✔
68
      QModelIndex childIndex = sourceModel()->index(nChild, 0, index);
24✔
69
      if (!childIndex.isValid()) {
70
        break;
71
      }
72
      retVal = showThis(childIndex);
24✔
73
      if (retVal) {
24✔
74
        break;
75
      }
76
    }
77
  } else {
78
    QModelIndex useIndex = sourceModel()->index(index.row(), 0, index.parent());
19✔
79
    QString path = fs->filePath(useIndex);
19✔
80
    path = QDir(store).relativeFilePath(path);
38✔
81
    if (path.startsWith(".git")) {
38✔
82
      return false;
83
    }
84
    path.replace(Util::endsWithGpg(), "");
18✔
85
    retVal = path.contains(filterRegularExpression());
18✔
86
  }
87
  return retVal;
88
}
89

90
/**
91
 * @brief StoreModel::setModelAndStore update the source model and store.
92
 * @param sourceModel
93
 * @param passStore
94
 */
95
void StoreModel::setModelAndStore(QFileSystemModel *sourceModel,
14✔
96
                                  QString passStore) {
97
  setSourceModel(sourceModel);
14✔
98
  fs = sourceModel;
14✔
99
  store = std::move(passStore);
100
}
14✔
101

102
void StoreModel::setStore(const QString &passStore) {
×
103
  store = passStore;
×
NEW
104
  invalidate();
×
UNCOV
105
}
×
106

107
/**
108
 * @brief StoreModel::data don't show the .gpg at the end of a file.
109
 * @param index
110
 * @param role
111
 * @return
112
 */
113
auto StoreModel::data(const QModelIndex &index, int role) const -> QVariant {
2✔
114
  if (!index.isValid()) {
115
    return {};
116
  }
117

118
  QVariant initial_value;
119
  initial_value = QSortFilterProxyModel::data(index, role);
1✔
120

121
  if (role == Qt::DisplayRole) {
1✔
122
    QString name = initial_value.toString();
1✔
123
    name.replace(Util::endsWithGpg(), "");
1✔
124
    initial_value.setValue(name);
1✔
125
  }
126

127
  return initial_value;
1✔
128
}
1✔
129

130
/**
131
 * @brief StoreModel::supportedDropActions enable drop.
132
 * @return
133
 */
134
auto StoreModel::supportedDropActions() const -> Qt::DropActions {
1✔
135
  return Qt::CopyAction | Qt::MoveAction;
1✔
136
}
137

138
/**
139
 * @brief StoreModel::supportedDragActions enable drag.
140
 * @return
141
 */
142
auto StoreModel::supportedDragActions() const -> Qt::DropActions {
1✔
143
  return Qt::CopyAction | Qt::MoveAction;
1✔
144
}
145

146
/**
147
 * @brief StoreModel::flags
148
 * @param index
149
 * @return
150
 */
151
auto StoreModel::flags(const QModelIndex &index) const -> Qt::ItemFlags {
2✔
152
  Qt::ItemFlags defaultFlags = QSortFilterProxyModel::flags(index);
2✔
153

154
  if (index.isValid()) {
155
    return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
156
  }
157
  return Qt::ItemIsDropEnabled | defaultFlags;
1✔
158
}
159

160
/**
161
 * @brief StoreModel::mimeTypes
162
 * @return
163
 */
164
auto StoreModel::mimeTypes() const -> QStringList {
1✔
165
  QStringList types;
1✔
166
  types << "application/vnd+qtpass.dragAndDropInfoPasswordStore";
1✔
167
  return types;
1✔
168
}
169

170
/**
171
 * @brief StoreModel::mimeData
172
 * @param indexes
173
 * @return
174
 */
175
auto StoreModel::mimeData(const QModelIndexList &indexes) const -> QMimeData * {
1✔
176
  dragAndDropInfoPasswordStore info;
177

178
  QByteArray encodedData;
1✔
179
  // only use the first, otherwise we should enable multiselection
180
  QModelIndex index = indexes.at(0);
1✔
181
  if (index.isValid()) {
182
    QModelIndex useIndex = mapToSource(index);
1✔
183

184
    info.isDir = fs->fileInfo(useIndex).isDir();
1✔
185
    info.isFile = fs->fileInfo(useIndex).isFile();
1✔
186
    info.path = fs->fileInfo(useIndex).absoluteFilePath();
2✔
187
    QDataStream stream(&encodedData, QIODevice::WriteOnly);
1✔
188
    stream << info;
1✔
189
  }
1✔
190

191
  auto *mimeData = new QMimeData();
1✔
192
  mimeData->setData("application/vnd+qtpass.dragAndDropInfoPasswordStore",
2✔
193
                    encodedData);
194
  return mimeData;
1✔
195
}
196

197
/**
198
 * @brief StoreModel::canDropMimeData
199
 * @param data
200
 * @param action
201
 * @param row
202
 * @param column
203
 * @param parent
204
 * @return
205
 */
206
auto StoreModel::canDropMimeData(const QMimeData *data, Qt::DropAction action,
×
207
                                 int row, int column,
208
                                 const QModelIndex &parent) const -> bool {
209
#ifdef QT_DEBUG
210
  qDebug() << action << row;
211
#else
212
  Q_UNUSED(action)
213
  Q_UNUSED(row)
214
#endif
215

216
  if (data == nullptr ||
×
217
      !data->hasFormat("application/vnd+qtpass.dragAndDropInfoPasswordStore")) {
×
218
    return false;
219
  }
220

221
  QByteArray encodedData =
222
      data->data("application/vnd+qtpass.dragAndDropInfoPasswordStore");
×
223
  if (encodedData.isEmpty()) {
×
224
    return false;
225
  }
226
  QDataStream stream(&encodedData, QIODevice::ReadOnly);
×
227
  dragAndDropInfoPasswordStore info;
228
  stream >> info;
×
229
  if (stream.status() != QDataStream::Ok) {
×
230
    return false;
231
  }
232

233
  QModelIndex useIndex =
234
      this->index(parent.row(), parent.column(), parent.parent());
×
235

236
  if (column > 0) {
×
237
    return false;
238
  }
239

240
  // you can drop a folder on a folder
241
  if (fs->fileInfo(mapToSource(useIndex)).isDir() && info.isDir) {
×
242
    return true;
243
  }
244
  // you can drop a file on a folder
245
  if (fs->fileInfo(mapToSource(useIndex)).isDir() && info.isFile) {
×
246
    return true;
247
  }
248
  // you can drop a file on a file
249
  if (fs->fileInfo(mapToSource(useIndex)).isFile() && info.isFile) {
×
250
    return true;
251
  }
252

253
  return false;
254
}
×
255

256
/**
257
 * @brief StoreModel::dropMimeData
258
 * @param data
259
 * @param action
260
 * @param row
261
 * @param column
262
 * @param parent
263
 * @return
264
 */
265
auto StoreModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
×
266
                              int row, int column, const QModelIndex &parent)
267
    -> bool {
268
  if (!canDropMimeData(data, action, row, column, parent)) {
×
269
    return false;
270
  }
271

272
  if (action == Qt::IgnoreAction) {
×
273
    return true;
274
  }
275
  QByteArray encodedData =
276
      data->data("application/vnd+qtpass.dragAndDropInfoPasswordStore");
×
277
  if (encodedData.isEmpty()) {
×
278
    return false;
279
  }
280
  QDataStream stream(&encodedData, QIODevice::ReadOnly);
×
281
  dragAndDropInfoPasswordStore info;
282
  stream >> info;
×
283
  if (stream.status() != QDataStream::Ok) {
×
284
    return false;
285
  }
286
  QModelIndex destIndex =
287
      this->index(parent.row(), parent.column(), parent.parent());
×
288
  QFileInfo destFileinfo = fs->fileInfo(mapToSource(destIndex));
×
289
  QFileInfo srcFileInfo = QFileInfo(info.path);
×
290
  QString cleanedSrc = QDir::cleanPath(srcFileInfo.absoluteFilePath());
×
291
  QString cleanedDest = QDir::cleanPath(destFileinfo.absoluteFilePath());
×
292
  if (info.isDir) {
×
293
    // dropped dir onto dir
294
    if (destFileinfo.isDir()) {
×
295
      QDir destDir = QDir(cleanedDest).filePath(srcFileInfo.fileName());
×
296
      QString cleanedDestDir = QDir::cleanPath(destDir.absolutePath());
×
297
      if (action == Qt::MoveAction) {
×
298
        QtPassSettings::getPass()->Move(cleanedSrc, cleanedDestDir);
×
299
      } else if (action == Qt::CopyAction) {
×
300
        QtPassSettings::getPass()->Copy(cleanedSrc, cleanedDestDir);
×
301
      }
302
    }
×
303
  } else if (info.isFile) {
×
304
    // dropped file onto a directory
305
    if (destFileinfo.isDir()) {
×
306
      if (action == Qt::MoveAction) {
×
307
        QtPassSettings::getPass()->Move(cleanedSrc, cleanedDest);
×
308
      } else if (action == Qt::CopyAction) {
×
309
        QtPassSettings::getPass()->Copy(cleanedSrc, cleanedDest);
×
310
      }
311
    } else if (destFileinfo.isFile()) {
×
312
      // dropped file onto a file
313
      int answer = QMessageBox::question(
×
314
          nullptr, tr("force overwrite?"),
×
315
          tr("overwrite %1 with %2?").arg(cleanedDest, cleanedSrc),
×
316
          QMessageBox::Yes | QMessageBox::No);
317
      bool force = answer == QMessageBox::Yes;
×
318
      if (action == Qt::MoveAction) {
×
319
        QtPassSettings::getPass()->Move(cleanedSrc, cleanedDest, force);
×
320
      } else if (action == Qt::CopyAction) {
×
321
        QtPassSettings::getPass()->Copy(cleanedSrc, cleanedDest, force);
×
322
      }
323
    }
324
  }
325
  return true;
326
}
×
327

328
/**
329
 * @brief StoreModel::lessThan
330
 * @param source_left
331
 * @param source_right
332
 * @return
333
 */
334
auto StoreModel::lessThan(const QModelIndex &source_left,
2✔
335
                          const QModelIndex &source_right) const -> bool {
336
/* matches logic in QFileSystemModelSorter::compareNodes() */
337
#ifndef Q_OS_MAC
338
  if (fs && (source_left.column() == 0 || source_left.column() == 1)) {
2✔
339
    bool leftD = fs->isDir(source_left);
2✔
340
    bool rightD = fs->isDir(source_right);
2✔
341

342
    if (leftD ^ rightD) {
2✔
343
      return leftD;
344
    }
345
  }
346
#endif
347

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