QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
storemodel.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2014 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
8#ifndef SRC_STOREMODEL_H_
9#define SRC_STOREMODEL_H_
10
11#include <QFileInfo>
12#include <QRegularExpression>
13#include <QSortFilterProxyModel>
14#include <optional>
15
16class Pass;
17class QFileSystemModel;
18
33 enum class ItemKind {
35 Directory,
36 File
37 };
38
40 QString path;
41};
42
43class QDataStream;
44
51auto operator<<(QDataStream &out, const StoreDragItem &info) -> QDataStream &;
52
63auto operator>>(QDataStream &in, StoreDragItem &info) -> QDataStream &;
64
66// The one MIME type the store tree drags: a serialised StoreDragItem (see the
67// struct's documentation). Hidden from Doxygen, which reads the
68// constructor-style initialiser as a function taking a string literal.
69inline constexpr QLatin1StringView
70 kStoreDragMimeType("application/vnd.qtpass.store-item");
72
85class StoreModel : public QSortFilterProxyModel {
86 Q_OBJECT
87
88private:
93 QFileSystemModel *fs;
98 QString store;
103 Pass *m_pass{nullptr};
104
111 static auto parseDropData(const QMimeData *data)
112 -> std::optional<StoreDragItem>;
113
127 auto executeDropAction(const StoreDragItem &info, Qt::DropAction action,
128 const QModelIndex &parent) -> bool;
137 auto performDrop(const QString &cleanedSrc, const QString &cleanedDest,
138 Qt::DropAction action, bool force) -> bool;
145 [[nodiscard]] auto isLinkInStore(const QModelIndex &proxyIndex) const -> bool;
155 [[nodiscard]] auto pathOf(const QModelIndex &sourceIndex) const -> QString;
156
166 [[nodiscard]] auto showsFolder(const QModelIndex &index, const QString &path,
167 bool inside, bool outside) const -> bool;
168
169public:
173 StoreModel();
174
179 void setPass(Pass *pass);
180
187 [[nodiscard]] auto filterAcceptsRow(int source_row,
188 const QModelIndex &source_parent) const
189 -> bool override;
190
196 [[nodiscard]] auto showThis(const QModelIndex &index) const -> bool;
197
204 // The default argument is QAbstractItemModel's own, kept as declared there.
205 // NOLINTBEGIN(google-default-arguments)
206 [[nodiscard]] auto
207 hasChildren(const QModelIndex &parent = QModelIndex()) const -> bool override;
208 // NOLINTEND(google-default-arguments)
215 [[nodiscard]] auto canFetchMore(const QModelIndex &parent) const
216 -> bool override;
221 void fetchMore(const QModelIndex &parent) override;
222
228 void setModelAndStore(QFileSystemModel *sourceModel,
229 const QString &passStore);
230
241 auto rootIndexFor(const QString &path) -> QModelIndex;
242
255 [[nodiscard]] static auto wordsFilter(const QString &query)
256 -> QRegularExpression;
257
263 void setStore(const QString &passStore);
264
271 [[nodiscard]] auto data(const QModelIndex &index, int role) const
272 -> QVariant override;
273
280 [[nodiscard]] auto lessThan(const QModelIndex &source_left,
281 const QModelIndex &source_right) const
282 -> bool override;
283
288 [[nodiscard]] auto getStore() const -> QString { return store; }
289
290 // QAbstractItemModel interface
291public:
296 [[nodiscard]] auto supportedDropActions() const -> Qt::DropActions override;
297
302 [[nodiscard]] auto supportedDragActions() const -> Qt::DropActions override;
303
309 [[nodiscard]] auto flags(const QModelIndex &index) const
310 -> Qt::ItemFlags override;
311
316 [[nodiscard]] auto mimeTypes() const -> QStringList override;
317
323 [[nodiscard]] auto mimeData(const QModelIndexList &indexes) const
324 -> QMimeData * override;
325
335 auto canDropMimeData(const QMimeData *data, Qt::DropAction action, int row,
336 int column, const QModelIndex &parent) const
337 -> bool override;
338
348 auto dropMimeData(const QMimeData *data, Qt::DropAction action, int row,
349 int column, const QModelIndex &parent) -> bool override;
350};
351
352#endif // SRC_STOREMODEL_H_
Abstract base class for password store operations.
Definition pass.h:77
auto filterAcceptsRow(int source_row, const QModelIndex &source_parent) const -> bool override
Filter whether a row should be displayed.
auto getStore() const -> QString
Get the password store root path.
Definition storemodel.h:288
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...
Definition storemodel.h:103
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.
Definition storemodel.h:98
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...
Definition storemodel.h:93
auto supportedDropActions() const -> Qt::DropActions override
Get supported drop actions.
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.
What a drag from the store tree carries: the kind of the dragged item and its path.
Definition storemodel.h:29
QString path
Definition storemodel.h:40
ItemKind
Type of the dragged item.
Definition storemodel.h:33
ItemKind kind
Definition storemodel.h:39