14#include <QApplication>
17#include <QFileSystemModel>
20#include <QRegularExpression>
24 out << static_cast<quint8>(item.kind) << item.path;
51 const QModelIndex &sourceParent)
const
53 QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
63 const QString filePath =
pathOf(index);
64 QString path = QDir(
store).relativeFilePath(filePath);
69 const bool outside = path == QLatin1String(
"..") ||
70 path.startsWith(QLatin1String(
"../")) ||
76 const bool inside = !outside && path != QLatin1String(
".");
81 if (!link && sourceModel()->rowCount(index) > 0) {
84 if (outside || path.startsWith(
".git")) {
89 if (!(
fs !=
nullptr ?
fs->isDir(index) : QFileInfo(filePath).isDir())) {
92 return path.contains(filterRegularExpression());
96 bool inside,
bool outside)
const ->
bool {
97 for (
int nChild = 0; nChild < sourceModel()->rowCount(index); ++nChild) {
98 const QModelIndex childIndex = sourceModel()->index(nChild, 0, index);
99 if (!childIndex.isValid()) {
110 return inside && !outside && !path.startsWith(QLatin1String(
".git")) &&
111 !filterRegularExpression().pattern().isEmpty() &&
112 path.contains(filterRegularExpression());
119 const QStringList words = query.split(QLatin1Char(
' '), Qt::SkipEmptyParts);
120 for (
const QString &word : words) {
121 pattern += QStringLiteral(
"(?>.*?") + QRegularExpression::escape(word) +
124 if (pattern.isEmpty()) {
127 return QRegularExpression(QLatin1Char(
'^') + pattern,
128 QRegularExpression::CaseInsensitiveOption |
129 QRegularExpression::DotMatchesEverythingOption);
133 if (
fs ==
nullptr || !proxyIndex.isValid()) {
138 const QString filePath =
pathOf(mapToSource(proxyIndex));
139 const QString path = QDir(
store).relativeFilePath(filePath);
140 if (path == QLatin1String(
".") || path == QLatin1String(
"..") ||
149 const QFileInfo info =
fs->fileInfo(sourceIndex);
150 return info.filePath().isEmpty() ?
fs->filePath(sourceIndex)
151 : info.absoluteFilePath();
156 return !
isLinkInStore(parent) && QSortFilterProxyModel::hasChildren(parent);
160 return !
isLinkInStore(parent) && QSortFilterProxyModel::canFetchMore(parent);
165 QSortFilterProxyModel::fetchMore(parent);
170 const QString &passStore) {
171 setSourceModel(sourceModel);
180 return mapFromSource(
fs->setRootPath(QDir::cleanPath(path)));
184#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
188 endFilterChange(QSortFilterProxyModel::Direction::Rows);
196 if (!index.isValid()) {
200 QVariant initial_value;
201 initial_value = QSortFilterProxyModel::data(index, role);
205 if (role == Qt::DisplayRole &&
206 !(
fs !=
nullptr &&
fs->isDir(mapToSource(index)))) {
207 QString name = initial_value.toString();
209 initial_value.setValue(name);
212 return initial_value;
216 return Qt::CopyAction | Qt::MoveAction;
220 return Qt::CopyAction | Qt::MoveAction;
224 Qt::ItemFlags defaultFlags = QSortFilterProxyModel::flags(index);
226 if (index.isValid()) {
227 return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
229 return Qt::ItemIsDropEnabled | defaultFlags;
234 types << kStoreDragMimeType;
241 if (indexes.isEmpty())
243 QByteArray encodedData;
245 QModelIndex index = indexes.at(0);
246 if (index.isValid()) {
247 QModelIndex useIndex = mapToSource(index);
248 const QFileInfo fileInfo =
fs->fileInfo(useIndex);
250 if (fileInfo.isDir()) {
252 }
else if (fileInfo.isFile()) {
255 info.
path = fileInfo.absoluteFilePath();
256 QDataStream stream(&encodedData, QIODevice::WriteOnly);
261 mimeData->setData(kStoreDragMimeType, encodedData);
267 const QModelIndex &parent)
const ->
bool {
268 qCDebug(lcQtPass) <<
"canDropMimeData" << action << row;
276 QModelIndex useIndex =
277 this->index(parent.row(), parent.column(), parent.parent());
285 if (
fs->fileInfo(mapToSource(useIndex)).isDir() &&
286 info.
kind == IK::Directory) {
290 if (
fs->fileInfo(mapToSource(useIndex)).isDir() && info.
kind == IK::File) {
294 if (
fs->fileInfo(mapToSource(useIndex)).isFile() && info.
kind == IK::File) {
302 int row,
int column,
const QModelIndex &parent)
308 if (action == Qt::IgnoreAction) {
312 if (action != Qt::MoveAction && action != Qt::CopyAction) {
325 -> std::optional<StoreDragItem> {
326 if (
data ==
nullptr || !
data->hasFormat(kStoreDragMimeType)) {
329 QByteArray encodedData =
data->data(kStoreDragMimeType);
330 if (encodedData.isEmpty()) {
333 QDataStream stream(&encodedData, QIODevice::ReadOnly);
336 if (stream.status() != QDataStream::Ok) {
343 Qt::DropAction action,
344 const QModelIndex &parent) ->
bool {
345 QModelIndex destIndex =
346 this->index(parent.row(), parent.column(), parent.parent());
347 QFileInfo destFileinfo =
fs->fileInfo(mapToSource(destIndex));
348 QFileInfo srcFileInfo = QFileInfo(info.path);
350 QString cleanedSrc = QDir::cleanPath(srcFileInfo.absoluteFilePath());
351 QString cleanedDest = QDir::cleanPath(destFileinfo.absoluteFilePath());
358 <<
"executeDropAction: rejecting drop that escapes the store"
359 <<
"(src=" << cleanedSrc <<
"dest=" << cleanedDest <<
")";
366 if (!destFileinfo.isDir()) {
369 const QString destDir =
370 QDir::cleanPath(
Util::joinPath(cleanedDest, srcFileInfo.fileName()));
371 return performDrop(cleanedSrc, destDir, action,
false);
376 if (destFileinfo.isDir()) {
377 return performDrop(cleanedSrc, cleanedDest, action,
false);
379 return performDrop(cleanedSrc, cleanedDest, action,
381 qobject_cast<QWidget *>(QObject::parent()),
382 tr(
"Force overwrite?"),
383 tr(
"overwrite %1 with %2?")
388 <<
"executeDropAction: unexpected ItemKind, ignoring drop";
394 const QString &cleanedDest, Qt::DropAction action,
395 bool force) ->
bool {
402 [
this, &cleanedSrc, &cleanedDest] {
403 return m_pass->entriesMoveWouldWiden(
404 cleanedSrc, cleanedDest);
408 if (action == Qt::MoveAction) {
409 m_pass->Move(cleanedSrc, cleanedDest, force);
410 }
else if (action == Qt::CopyAction) {
411 m_pass->Copy(cleanedSrc, cleanedDest, force);
417 const QModelIndex &source_right)
const ->
bool {
420 if (
fs && (source_left.column() == 0 || source_left.column() == 1)) {
421 bool leftD =
fs->isDir(source_left);
422 bool rightD =
fs->isDir(source_right);
424 if (leftD ^ rightD) {
430 return QSortFilterProxyModel::lessThan(source_left, source_right);
Abstract base class for password store operations.
static auto isPathInStore(const QString &storeRoot, const QString &candidate) -> bool
Check that a candidate path resolves inside the password store root, after following symlinks and res...
auto filterAcceptsRow(int source_row, const QModelIndex &source_parent) const -> bool override
Filter whether a row should be displayed.
auto data(const QModelIndex &index, int role) const -> QVariant override
Get display data for index.
StoreModel()
Construct a StoreModel.
auto showsFolder(const QModelIndex &index, const QString &path, bool inside, bool outside) const -> bool
showThis() for a folder with rows: shown when any of them is, or, in a search, when its own name matc...
static auto parseDropData(const QMimeData *data) -> std::optional< StoreDragItem >
Decode the drag payload from data.
auto showThis(const QModelIndex &index) const -> bool
Check if a specific index should be shown.
auto mimeTypes() const -> QStringList override
Get supported MIME types for drag/drop.
static auto wordsFilter(const QString &query) -> QRegularExpression
The tree filter for what is typed in the search box.
auto pathOf(const QModelIndex &sourceIndex) const -> QString
The path of a file system model index, as the node keeps it.
Pass * m_pass
Backend that performs drag-and-drop Move/Copy; not owned, null until setPass(), in which case drops a...
void setModelAndStore(QFileSystemModel *sourceModel, const QString &passStore)
Initialize model with source model and store path.
auto dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) -> bool override
Handle dropped MIME data.
void setStore(const QString &passStore)
Update the store path used for filtering without changing the source model.
auto mimeData(const QModelIndexList &indexes) const -> QMimeData *override
Create MIME data from indexes.
auto executeDropAction(const StoreDragItem &info, Qt::DropAction action, const QModelIndex &parent) -> bool
Resolve a validated drop into a Move or Copy.
void setPass(Pass *pass)
Inject the Pass backend for drag-and-drop Move/Copy operations.
auto rootIndexFor(const QString &path) -> QModelIndex
Set the source filesystem root and map it to a proxy index.
QString store
Store root used to make paths relative for filtering and for the drop boundary check.
auto supportedDragActions() const -> Qt::DropActions override
Get supported drag actions.
auto hasChildren(const QModelIndex &parent=QModelIndex()) const -> bool override
No children for a link inside the store: it is listed by name but never entered, so the view shows no...
void fetchMore(const QModelIndex &parent) override
Fetch more rows, except under a link inside the store.
auto isLinkInStore(const QModelIndex &proxyIndex) const -> bool
Whether a proxy index is a link (or junction) inside the store, the store root excluded.
auto flags(const QModelIndex &index) const -> Qt::ItemFlags override
Get item flags for index.
auto performDrop(const QString &cleanedSrc, const QString &cleanedDest, Qt::DropAction action, bool force) -> bool
Perform the actual Move/Copy for a validated drop.
auto canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const -> bool override
Check if drop is possible.
auto lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const -> bool override
Compare two indices for sorting.
auto canFetchMore(const QModelIndex &parent) const -> bool override
Nothing to fetch under a link inside the store, so the file system model never lists what the link po...
QFileSystemModel * fs
The wrapped file system model, set by setModelAndStore(); null until then, which makes showThis() hid...
auto supportedDropActions() const -> Qt::DropActions override
Get supported drop actions.
static auto endsWithGpg() -> const QRegularExpression &
Returns a regex to match .gpg file extensions.
static auto isAbsoluteFsPath(const QString &path) -> bool
Whether path is absolute to the file system: as QDir::isAbsolutePath(), except that a name starting w...
static auto isUnderLink(const QString &path, const QString &storeRoot, bool includeSelf=true) -> bool
Whether the way from storeRoot down to path passes through a symbolic link or NTFS junction: path its...
static auto displayable(const QString &text, bool lineBreaks=true) -> QString
text with the characters that change how the text around them reads replaced by U+FFFD,...
static auto isLinkedFolder(const QString &path) -> bool
Whether path names a symbolic link or NTFS junction, with or without a trailing separator (which woul...
static auto joinPath(const QString &dir, const QString &name) -> QString
name in dir, as the file system means it.
auto confirmPassRun(QWidget *parent, const QString &store, const std::function< QStringList()> &check) -> bool
Run check (which lists the entries a pass run would give new readers, one gpg call each) under a wait...
auto askYes(QWidget *parent, const QString &title, const QString &text) -> bool
Ask a yes/no question in plain text.
Pass, the abstract password-store backend that RealPass and ImitatePass implement,...
PathValidator: is a path inside the password store, and how is it spelled relative to the store root.
QMessageBox helpers that force Qt::PlainText so store-controlled names can never be read as rich-text...
The "qtpass" logging category.
auto operator>>(QDataStream &in, StoreDragItem &item) -> QDataStream &
Deserialize a StoreDragItem from the QtPass MIME type.
auto operator<<(QDataStream &out, const StoreDragItem &item) -> QDataStream &
Serialize a StoreDragItem for the QtPass MIME type.
StoreModel, the filtering/sorting proxy over the store's QFileSystemModel, and StoreDragItem,...
What a drag from the store tree carries: the kind of the dragged item and its path.
ItemKind
Type of the dragged item.
Util: static helpers for the process environment, binary discovery, store paths, URL safety and the l...