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

IJHack / QtPass / 24616233361

18 Apr 2026 11:24PM UTC coverage: 22.607% (-0.02%) from 22.627%
24616233361

push

github

web-flow
Merge pull request #1046 from IJHack/fix/code-quality-storemodel-settings

fix: pass QModelIndex by const ref, reorder MIME check, fix camelCase typo

5 of 12 new or added lines in 2 files covered. (41.67%)

13 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;
×
104
  QT_WARNING_PUSH
105
  QT_WARNING_DISABLE_DEPRECATED
UNCOV
106
  invalidateFilter();
×
107
  QT_WARNING_POP
108
}
×
109

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

121
  QVariant initial_value;
122
  initial_value = QSortFilterProxyModel::data(index, role);
1✔
123

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

130
  return initial_value;
1✔
131
}
1✔
132

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

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

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

157
  if (index.isValid()) {
158
    return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
159
  }
160
  return Qt::ItemIsDropEnabled | defaultFlags;
1✔
161
}
162

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

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

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

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

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

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

NEW
219
  if (data == nullptr ||
×
NEW
220
      !data->hasFormat("application/vnd+qtpass.dragAndDropInfoPasswordStore")) {
×
221
    return false;
222
  }
223

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

236
  QModelIndex useIndex =
NEW
237
      this->index(parent.row(), parent.column(), parent.parent());
×
238

UNCOV
239
  if (column > 0) {
×
240
    return false;
241
  }
242

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

256
  return false;
257
}
×
258

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

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

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

345
    if (leftD ^ rightD) {
2✔
346
      return leftD;
347
    }
348
  }
349
#endif
350

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