Branch data Line data Source code
1 : : // SPDX-FileCopyrightText: 2014 Anne Jan Brouwer
2 : : // SPDX-License-Identifier: GPL-3.0-or-later
3 : : /**
4 : : * @file
5 : : * @brief Implements StoreModel and the StoreDragItem stream operators; the
6 : : * drop validation lives in executeDropAction().
7 : : */
8 : : #include "storemodel.h"
9 : : #include "pass.h"
10 : : #include "pathvalidator.h"
11 : : #include "plainmessagebox.h"
12 : : #include "qtpasslogging.h"
13 : : #include "util.h"
14 : : #include <QApplication>
15 : : #include <QDebug>
16 : : #include <QFileInfo>
17 : : #include <QFileSystemModel>
18 : : #include <QMessageBox>
19 : : #include <QMimeData>
20 : : #include <QRegularExpression>
21 : : #include <QtGlobal>
22 : :
23 : 18 : auto operator<<(QDataStream &out, const StoreDragItem &item) -> QDataStream & {
24 : 18 : out << static_cast<quint8>(item.kind) << item.path;
25 : 18 : return out;
26 : : }
27 : :
28 : 27 : auto operator>>(QDataStream &in, StoreDragItem &item) -> QDataStream & {
29 : : quint8 k;
30 : 27 : in >> k >> item.path;
31 [ + + + ]: 27 : switch (k) {
32 : 5 : case static_cast<quint8>(StoreDragItem::ItemKind::Directory):
33 : 5 : item.kind = StoreDragItem::ItemKind::Directory;
34 : 5 : break;
35 : 21 : case static_cast<quint8>(StoreDragItem::ItemKind::File):
36 : 21 : item.kind = StoreDragItem::ItemKind::File;
37 : 21 : break;
38 : 1 : default:
39 : 1 : item.kind = StoreDragItem::ItemKind::Unknown;
40 : 1 : break;
41 : : }
42 : 27 : return in;
43 : : }
44 : :
45 : : // Filter approach via http://www.qtcentre.org/threads/46471-QTreeView-Filter
46 : 269 : StoreModel::StoreModel() { fs = nullptr; }
47 : :
48 : 216 : void StoreModel::setPass(Pass *pass) { m_pass = pass; }
49 : :
50 : 113687 : auto StoreModel::filterAcceptsRow(int sourceRow,
51 : : const QModelIndex &sourceParent) const
52 : : -> bool {
53 : 113687 : QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
54 : 113687 : return showThis(index);
55 : : }
56 : :
57 : 144162 : auto StoreModel::showThis(const QModelIndex &index) const -> bool {
58 [ + + ]: 144162 : if (fs == nullptr) {
59 : : return false;
60 : : }
61 : : // Through filePath() a filter pass cost entries times sibling folders,
62 : : // again on every key typed in the search.
63 : 143934 : const QString filePath = pathOf(index);
64 : 143934 : QString path = QDir(store).relativeFilePath(filePath);
65 : : // Only what lies in the current store. The file system model keeps the
66 : : // rows of a store shown earlier (a profile switch only re-roots it), and
67 : : // a search would otherwise match those, open one and save it encrypted
68 : : // to this store's recipients. Folders above the store still lead there.
69 [ + - + + ]: 287868 : const bool outside = path == QLatin1String("..") ||
70 [ + - + - : 287868 : path.startsWith(QLatin1String("../")) ||
+ ]
71 : 141827 : Util::isAbsoluteFsPath(path);
72 : : // A link inside the store is neither an entry nor a folder (SECURITY.md):
73 : : // it is listed by its name, so it can be deleted, but never entered. A
74 : : // planted `x -> .` was an endlessly expandable folder, and the search's
75 : : // expandAll() loaded it level after level until memory ran out.
76 [ + + ]: 143934 : const bool inside = !outside && path != QLatin1String(".");
77 [ + + + ]: 140010 : if (inside && Util::isUnderLink(filePath, store, false)) {
78 : : return false;
79 : : }
80 [ + + + + : 143927 : const bool link = inside && Util::isLinkedFolder(filePath);
+ ]
81 [ + + + + ]: 143826 : if (!link && sourceModel()->rowCount(index) > 0) {
82 : 3676 : return showsFolder(index, path, inside, outside);
83 : : }
84 [ + + + + : 280481 : if (outside || path.startsWith(".git")) {
+ + ]
85 : : return false;
86 : : }
87 : : // As data() shows it: ".gpg" is dropped from an entry's name only.
88 : : // The model knows a row's kind without a stat.
89 [ + - + - : 140229 : if (!(fs != nullptr ? fs->isDir(index) : QFileInfo(filePath).isDir())) {
- + + ]
90 : 273944 : path.replace(Util::endsWithGpg(), "");
91 : : }
92 : 140229 : return path.contains(filterRegularExpression());
93 : : }
94 : :
95 : 3676 : auto StoreModel::showsFolder(const QModelIndex &index, const QString &path,
96 : : bool inside, bool outside) const -> bool {
97 [ + + ]: 30898 : for (int nChild = 0; nChild < sourceModel()->rowCount(index); ++nChild) {
98 : 30468 : const QModelIndex childIndex = sourceModel()->index(nChild, 0, index);
99 : : if (!childIndex.isValid()) {
100 : : break;
101 : : }
102 [ + + ]: 30468 : if (showThis(childIndex)) {
103 : 3246 : return true;
104 : : }
105 : : }
106 : : // Or, in a search, by its own name as data() shows it: `admins.gpg` is
107 : : // found by that name even when none of its entries is. Without a search
108 : : // a folder still shows only for what it holds, so .git and folders of
109 : : // other files stay out.
110 [ + + - + : 422 : return inside && !outside && !path.startsWith(QLatin1String(".git")) &&
- ]
111 [ + + + + : 1071 : !filterRegularExpression().pattern().isEmpty() &&
+ + - - ]
112 : 430 : path.contains(filterRegularExpression());
113 : : }
114 : :
115 : 61 : auto StoreModel::wordsFilter(const QString &query) -> QRegularExpression {
116 : : // Regular expressions belong to the content search; here a typed "[" or
117 : : // "(" must not switch filtering off, so each word is escaped.
118 : 61 : QString pattern;
119 : 61 : const QStringList words = query.split(QLatin1Char(' '), Qt::SkipEmptyParts);
120 [ + + ]: 130 : for (const QString &word : words) {
121 : 138 : pattern += QStringLiteral("(?>.*?") + QRegularExpression::escape(word) +
122 [ + + ]: 207 : QLatin1Char(')');
123 : : }
124 [ + + ]: 61 : if (pattern.isEmpty()) {
125 : 3 : return {};
126 : : }
127 : 58 : return QRegularExpression(QLatin1Char('^') + pattern,
128 : : QRegularExpression::CaseInsensitiveOption |
129 [ + + ]: 58 : QRegularExpression::DotMatchesEverythingOption);
130 : : }
131 : :
132 : 3563 : auto StoreModel::isLinkInStore(const QModelIndex &proxyIndex) const -> bool {
133 [ + + ]: 3563 : if (fs == nullptr || !proxyIndex.isValid()) {
134 : : return false;
135 : : }
136 : : // The same path showThis() lists the row by, or a link it lists would
137 : : // still get an expand arrow on Windows.
138 : 3015 : const QString filePath = pathOf(mapToSource(proxyIndex));
139 : 3015 : const QString path = QDir(store).relativeFilePath(filePath);
140 [ + + + - : 4520 : if (path == QLatin1String(".") || path == QLatin1String("..") ||
+ + ]
141 [ + + + + : 6021 : path.startsWith(QLatin1String("../")) || Util::isAbsoluteFsPath(path)) {
+ - ]
142 : 1514 : return false;
143 : : }
144 : 1501 : return Util::isLinkedFolder(filePath);
145 : : }
146 : :
147 : 146949 : auto StoreModel::pathOf(const QModelIndex &sourceIndex) const -> QString {
148 : : // filePath() only for a node that has no file information yet.
149 : 146949 : const QFileInfo info = fs->fileInfo(sourceIndex);
150 [ + - + ]: 146949 : return info.filePath().isEmpty() ? fs->filePath(sourceIndex)
151 : 293898 : : info.absoluteFilePath();
152 : 146949 : }
153 : :
154 : : // NOLINTNEXTLINE(google-default-arguments): see the declaration.
155 : 2551 : auto StoreModel::hasChildren(const QModelIndex &parent) const -> bool {
156 [ + + + + ]: 2551 : return !isLinkInStore(parent) && QSortFilterProxyModel::hasChildren(parent);
157 : : }
158 : :
159 : 925 : auto StoreModel::canFetchMore(const QModelIndex &parent) const -> bool {
160 [ + + + + ]: 925 : return !isLinkInStore(parent) && QSortFilterProxyModel::canFetchMore(parent);
161 : : }
162 : :
163 : 87 : void StoreModel::fetchMore(const QModelIndex &parent) {
164 [ + + ]: 87 : if (!isLinkInStore(parent)) {
165 : 86 : QSortFilterProxyModel::fetchMore(parent);
166 : : }
167 : 87 : }
168 : :
169 : 308 : void StoreModel::setModelAndStore(QFileSystemModel *sourceModel,
170 : : const QString &passStore) {
171 : 308 : setSourceModel(sourceModel);
172 : 308 : fs = sourceModel;
173 : 308 : store = passStore;
174 : 308 : }
175 : :
176 : 389 : auto StoreModel::rootIndexFor(const QString &path) -> QModelIndex {
177 [ + + ]: 389 : if (fs == nullptr) {
178 : 1 : return {};
179 : : }
180 : 776 : return mapFromSource(fs->setRootPath(QDir::cleanPath(path)));
181 : : }
182 : :
183 : 1 : void StoreModel::setStore(const QString &passStore) {
184 : : #if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
185 : : // The Direction-scoped endFilterChange() overload needs Qt 6.10.
186 : : beginFilterChange();
187 : : store = passStore;
188 : : endFilterChange(QSortFilterProxyModel::Direction::Rows);
189 : : #else
190 : 1 : store = passStore;
191 : 1 : invalidateFilter();
192 : : #endif
193 : 1 : }
194 : :
195 : 57861 : auto StoreModel::data(const QModelIndex &index, int role) const -> QVariant {
196 : : if (!index.isValid()) {
197 : : return {};
198 : : }
199 : :
200 : : QVariant initial_value;
201 : 57860 : initial_value = QSortFilterProxyModel::data(index, role);
202 : :
203 : : // ".gpg" ends an entry's name, not a folder's: a folder renamed to
204 : : // "admins.gpg" is shown as that, not as the "admins" it may stand in for.
205 [ + + ]: 57860 : if (role == Qt::DisplayRole &&
206 [ + - + + : 57860 : !(fs != nullptr && fs->isDir(mapToSource(index)))) {
+ + ]
207 : 6472 : QString name = initial_value.toString();
208 : 6472 : name.replace(Util::endsWithGpg(), "");
209 : 6472 : initial_value.setValue(name);
210 : : }
211 : :
212 : : return initial_value;
213 : 57860 : }
214 : :
215 : 1 : auto StoreModel::supportedDropActions() const -> Qt::DropActions {
216 : 1 : return Qt::CopyAction | Qt::MoveAction;
217 : : }
218 : :
219 : 1 : auto StoreModel::supportedDragActions() const -> Qt::DropActions {
220 : 1 : return Qt::CopyAction | Qt::MoveAction;
221 : : }
222 : :
223 : 97218 : auto StoreModel::flags(const QModelIndex &index) const -> Qt::ItemFlags {
224 : 97218 : Qt::ItemFlags defaultFlags = QSortFilterProxyModel::flags(index);
225 : :
226 : : if (index.isValid()) {
227 : : return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
228 : : }
229 : 1 : return Qt::ItemIsDropEnabled | defaultFlags;
230 : : }
231 : :
232 : 1 : auto StoreModel::mimeTypes() const -> QStringList {
233 : 1 : QStringList types;
234 : 0 : types << kStoreDragMimeType;
235 : 1 : return types;
236 : : }
237 : :
238 : 1 : auto StoreModel::mimeData(const QModelIndexList &indexes) const -> QMimeData * {
239 : 1 : StoreDragItem info;
240 : :
241 [ + - ]: 1 : if (indexes.isEmpty())
242 : : return nullptr;
243 : 1 : QByteArray encodedData;
244 : : // only use the first, otherwise we should enable multiselection
245 : 1 : QModelIndex index = indexes.at(0);
246 : : if (index.isValid()) {
247 : 1 : QModelIndex useIndex = mapToSource(index);
248 : 1 : const QFileInfo fileInfo = fs->fileInfo(useIndex);
249 : :
250 [ + - + ]: 1 : if (fileInfo.isDir()) {
251 : 0 : info.kind = StoreDragItem::ItemKind::Directory;
252 [ + + - ]: 1 : } else if (fileInfo.isFile()) {
253 : 1 : info.kind = StoreDragItem::ItemKind::File;
254 : : }
255 : 1 : info.path = fileInfo.absoluteFilePath();
256 : 1 : QDataStream stream(&encodedData, QIODevice::WriteOnly);
257 : 1 : stream << info;
258 : 1 : }
259 : :
260 : 1 : auto *mimeData = new QMimeData();
261 : 2 : mimeData->setData(kStoreDragMimeType, encodedData);
262 : : return mimeData;
263 : : }
264 : :
265 : 18 : auto StoreModel::canDropMimeData(const QMimeData *data, Qt::DropAction action,
266 : : int row, int column,
267 : : const QModelIndex &parent) const -> bool {
268 : 18 : qCDebug(lcQtPass) << "canDropMimeData" << action << row;
269 : :
270 : 18 : const auto parsed = parseDropData(data);
271 [ + + ]: 18 : if (!parsed) {
272 : : return false;
273 : : }
274 : : const StoreDragItem &info = *parsed;
275 : :
276 : : QModelIndex useIndex =
277 : 15 : this->index(parent.row(), parent.column(), parent.parent());
278 : :
279 [ + + ]: 15 : if (column > 0) {
280 : : return false;
281 : : }
282 : :
283 : : using IK = StoreDragItem::ItemKind;
284 : : // you can drop a folder on a folder
285 [ + + + + : 28 : if (fs->fileInfo(mapToSource(useIndex)).isDir() &&
+ + + ]
286 [ + + ]: 12 : info.kind == IK::Directory) {
287 : : return true;
288 : : }
289 : : // you can drop a file on a folder
290 [ + + + + : 14 : if (fs->fileInfo(mapToSource(useIndex)).isDir() && info.kind == IK::File) {
+ - + +
+ ]
291 : : return true;
292 : : }
293 : : // you can drop a file on a file
294 [ + + + + : 3 : if (fs->fileInfo(mapToSource(useIndex)).isFile() && info.kind == IK::File) {
- + + +
+ ]
295 : : return true;
296 : : }
297 : :
298 : : return false;
299 : : }
300 : :
301 : 10 : auto StoreModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
302 : : int row, int column, const QModelIndex &parent)
303 : : -> bool {
304 [ + - ]: 10 : if (!canDropMimeData(data, action, row, column, parent)) {
305 : : return false;
306 : : }
307 : :
308 [ + + ]: 10 : if (action == Qt::IgnoreAction) {
309 : : return true;
310 : : }
311 : :
312 [ + - ]: 9 : if (action != Qt::MoveAction && action != Qt::CopyAction) {
313 : : return false;
314 : : }
315 : :
316 : 9 : const auto info = parseDropData(data);
317 [ + - ]: 9 : if (!info) {
318 : : return false;
319 : : }
320 : :
321 : 9 : return executeDropAction(*info, action, parent);
322 : : }
323 : :
324 : 27 : auto StoreModel::parseDropData(const QMimeData *data)
325 : : -> std::optional<StoreDragItem> {
326 [ + + + + : 53 : if (data == nullptr || !data->hasFormat(kStoreDragMimeType)) {
+ + + ]
327 : 2 : return std::nullopt;
328 : : }
329 : 50 : QByteArray encodedData = data->data(kStoreDragMimeType);
330 [ + + ]: 25 : if (encodedData.isEmpty()) {
331 : 1 : return std::nullopt;
332 : : }
333 : 24 : QDataStream stream(&encodedData, QIODevice::ReadOnly);
334 : 24 : StoreDragItem info;
335 : 24 : stream >> info;
336 [ - + ]: 24 : if (stream.status() != QDataStream::Ok) {
337 : 0 : return std::nullopt;
338 : : }
339 : : return info;
340 : 24 : }
341 : :
342 : 9 : auto StoreModel::executeDropAction(const StoreDragItem &info,
343 : : Qt::DropAction action,
344 : : const QModelIndex &parent) -> bool {
345 : : QModelIndex destIndex =
346 : 9 : this->index(parent.row(), parent.column(), parent.parent());
347 : 9 : QFileInfo destFileinfo = fs->fileInfo(mapToSource(destIndex));
348 : 9 : QFileInfo srcFileInfo = QFileInfo(info.path);
349 : :
350 : 9 : QString cleanedSrc = QDir::cleanPath(srcFileInfo.absoluteFilePath());
351 : 9 : QString cleanedDest = QDir::cleanPath(destFileinfo.absoluteFilePath());
352 : :
353 : : // Drop data could be crafted: both endpoints must resolve inside the store
354 : : // after symlink resolution (e.g. no in-store symlink pointing at /etc).
355 [ + + + - : 15 : if (!PathValidator::isPathInStore(store, cleanedSrc) ||
+ ]
356 : 6 : !PathValidator::isPathInStore(store, cleanedDest)) {
357 : 6 : qCWarning(lcQtPass)
358 : 3 : << "executeDropAction: rejecting drop that escapes the store"
359 : 3 : << "(src=" << cleanedSrc << "dest=" << cleanedDest << ")";
360 : 3 : return false;
361 : : }
362 : :
363 [ + + - ]: 6 : switch (info.kind) {
364 : 1 : case StoreDragItem::ItemKind::Directory: {
365 : : // Dropping a folder onto a folder: move/copy it *into* the target.
366 [ + + - ]: 1 : if (!destFileinfo.isDir()) {
367 : : return false;
368 : : }
369 : : const QString destDir =
370 : 1 : QDir::cleanPath(Util::joinPath(cleanedDest, srcFileInfo.fileName()));
371 : 1 : return performDrop(cleanedSrc, destDir, action, false);
372 : : }
373 : 5 : case StoreDragItem::ItemKind::File:
374 : : // File onto a folder drops into it (no clash); file onto an existing
375 : : // file asks before overwriting.
376 [ + + - ]: 5 : if (destFileinfo.isDir()) {
377 : 5 : return performDrop(cleanedSrc, cleanedDest, action, false);
378 : : }
379 : 0 : return performDrop(cleanedSrc, cleanedDest, action,
380 : 0 : PlainMessageBox::askYes(
381 : : qobject_cast<QWidget *>(QObject::parent()),
382 : 0 : tr("Force overwrite?"),
383 : 0 : tr("overwrite %1 with %2?")
384 [ # # ]: 0 : .arg(Util::displayable(cleanedDest, false),
385 : 0 : Util::displayable(cleanedSrc, false))));
386 : 0 : default:
387 : 0 : qCWarning(lcQtPass)
388 : 0 : << "executeDropAction: unexpected ItemKind, ignoring drop";
389 : 0 : return false;
390 : : }
391 : 9 : }
392 : :
393 : 6 : auto StoreModel::performDrop(const QString &cleanedSrc,
394 : : const QString &cleanedDest, Qt::DropAction action,
395 : : bool force) -> bool {
396 [ + + ]: 6 : if (!m_pass) {
397 : : return false;
398 : : }
399 : : // The pass backend re-encrypts the whole destination folder: asked first
400 : : // when an entry already there would gain a reader it does not have.
401 [ + + ]: 5 : if (!PlainMessageBox::confirmPassRun(QApplication::activeWindow(), store,
402 : 10 : [this, &cleanedSrc, &cleanedDest] {
403 : 5 : return m_pass->entriesMoveWouldWiden(
404 : 5 : cleanedSrc, cleanedDest);
405 : : })) {
406 : : return false;
407 : : }
408 [ + + ]: 4 : if (action == Qt::MoveAction) {
409 : 9 : m_pass->Move(cleanedSrc, cleanedDest, force);
410 [ - + ]: 1 : } else if (action == Qt::CopyAction) {
411 : 3 : m_pass->Copy(cleanedSrc, cleanedDest, force);
412 : : }
413 : : return true;
414 : : }
415 : :
416 : 626021 : auto StoreModel::lessThan(const QModelIndex &source_left,
417 : : const QModelIndex &source_right) const -> bool {
418 : : /* matches logic in QFileSystemModelSorter::compareNodes() */
419 : : #ifndef Q_OS_MAC
420 [ + - - + : 626021 : if (fs && (source_left.column() == 0 || source_left.column() == 1)) {
- - ]
421 : 626021 : bool leftD = fs->isDir(source_left);
422 : 626021 : bool rightD = fs->isDir(source_right);
423 : :
424 [ + + ]: 626021 : if (leftD ^ rightD) {
425 : : return leftD;
426 : : }
427 : : }
428 : : #endif
429 : :
430 : 621787 : return QSortFilterProxyModel::lessThan(source_left, source_right);
431 : : }
|