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

IJHack / QtPass / 24614290642

18 Apr 2026 09:28PM UTC coverage: 22.627%. Remained the same
24614290642

push

github

annejan
fix: use beginFilterChange/endFilterChange on Qt 6.9+

invalidateFilter() is deprecated since Qt 6.9; use the replacement
begin/endFilterChange() pair instead. Older Qt versions still use the
unqualified invalidateFilter().

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

1304 of 5763 relevant lines covered (22.63%)

8.53 hits per line

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

53.78
/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
#if QT_VERSION >= QT_VERSION_CHECK(6, 9, 0)
105
  beginFilterChange();
106
  endFilterChange();
107
#else
108
  invalidateFilter();
×
109
#endif
110
}
×
111

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

123
  QVariant initial_value;
124
  initial_value = QSortFilterProxyModel::data(index, role);
1✔
125

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

132
  return initial_value;
1✔
133
}
1✔
134

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

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

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

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

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

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

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

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

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

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

221
  QModelIndex useIndex =
222
      this->index(parent.row(), parent.column(), parent.parent());
×
223
  QByteArray encodedData =
224
      data->data("application/vnd+qtpass.dragAndDropInfoPasswordStore");
×
225
  QDataStream stream(&encodedData, QIODevice::ReadOnly);
×
226
  dragAndDropInfoPasswordStore info;
227
  stream >> info;
×
228
  if (!data->hasFormat("application/vnd+qtpass.dragAndDropInfoPasswordStore")) {
×
229
    return false;
230
  }
231

232
  if (column > 0) {
×
233
    return false;
234
  }
235

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

249
  return false;
250
}
×
251

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

268
  if (action == Qt::IgnoreAction) {
×
269
    return true;
270
  }
271
  QByteArray encodedData =
272
      data->data("application/vnd+qtpass.dragAndDropInfoPasswordStore");
×
273

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

319
/**
320
 * @brief StoreModel::lessThan
321
 * @param source_left
322
 * @param source_right
323
 * @return
324
 */
325
auto StoreModel::lessThan(const QModelIndex &source_left,
2✔
326
                          const QModelIndex &source_right) const -> bool {
327
/* matches logic in QFileSystemModelSorter::compareNodes() */
328
#ifndef Q_OS_MAC
329
  if (fs && (source_left.column() == 0 || source_left.column() == 1)) {
2✔
330
    bool leftD = fs->isDir(source_left);
2✔
331
    bool rightD = fs->isDir(source_right);
2✔
332

333
    if (leftD ^ rightD) {
2✔
334
      return leftD;
335
    }
336
  }
337
#endif
338

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