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 StoreModel, the filtering/sorting proxy over the store's
6 : * QFileSystemModel, and StoreDragItem, the payload of a tree drag.
7 : */
8 : #ifndef SRC_STOREMODEL_H_
9 : #define SRC_STOREMODEL_H_
10 :
11 : #include <QFileInfo>
12 : #include <QRegularExpression>
13 : #include <QSortFilterProxyModel>
14 : #include <optional>
15 :
16 : class Pass;
17 : class QFileSystemModel;
18 :
19 : /**
20 : * @struct StoreDragItem
21 : * @ingroup store
22 : * @brief What a drag from the store tree carries: the kind of the dragged
23 : * item and its path.
24 : *
25 : * Travels as the `kStoreDragMimeType` payload
26 : * (`application/vnd.qtpass.store-item`), serialised by operator<<() and read
27 : * back by operator>>().
28 : */
29 73 : struct StoreDragItem {
30 : /**
31 : * @brief Type of the dragged item.
32 : */
33 : enum class ItemKind {
34 : Unknown, /**< Item type has not been determined yet */
35 : Directory, /**< Dragged item is a directory */
36 : File /**< Dragged item is a file */
37 : };
38 :
39 : ItemKind kind{ItemKind::Unknown}; /**< Type of dragged item */
40 : QString path; /**< Full path to the dragged item */
41 : };
42 :
43 : class QDataStream;
44 :
45 : /**
46 : * @brief Serialize a StoreDragItem for the QtPass MIME type.
47 : * @param out Stream to write to.
48 : * @param info Drag/drop info to serialize.
49 : * @return The same stream, for chaining.
50 : */
51 : auto operator<<(QDataStream &out, const StoreDragItem &info) -> QDataStream &;
52 :
53 : /**
54 : * @brief Deserialize a StoreDragItem from the QtPass MIME type.
55 : *
56 : * Unrecognised kind bytes resolve to ItemKind::Unknown rather than being
57 : * rejected, so a future protocol bump remains decode-safe.
58 : *
59 : * @param in Stream to read from.
60 : * @param info Drag/drop info to populate.
61 : * @return The same stream, for chaining.
62 : */
63 : auto operator>>(QDataStream &in, StoreDragItem &info) -> QDataStream &;
64 :
65 : /// @cond DOXYGEN_MISPARSES_CONSTEXPR_INIT
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.
69 : inline constexpr QLatin1StringView
70 : kStoreDragMimeType("application/vnd.qtpass.store-item");
71 : /// @endcond
72 :
73 : /**
74 : * @class StoreModel
75 : * @ingroup store
76 : * @brief QSortFilterProxyModel for filtering and displaying password store.
77 : *
78 : * StoreModel wraps QFileSystemModel to provide password store-specific
79 : * filtering and display functionality. It handles:
80 : * - Hiding the `.git` directory from display
81 : * - Removing .gpg extension from file names
82 : * - Supporting drag and drop operations
83 : * - Custom sorting (directories before files)
84 : */
85 : class StoreModel : public QSortFilterProxyModel {
86 0 : Q_OBJECT
87 :
88 : private:
89 : /**
90 : * @brief The wrapped file system model, set by setModelAndStore(); null
91 : * until then, which makes showThis() hide everything.
92 : */
93 : QFileSystemModel *fs;
94 : /**
95 : * @brief Store root used to make paths relative for filtering and for the
96 : * drop boundary check.
97 : */
98 : QString store;
99 : /**
100 : * @brief Backend that performs drag-and-drop Move/Copy; not owned, null
101 : * until setPass(), in which case drops are refused.
102 : */
103 : Pass *m_pass{nullptr};
104 :
105 : /**
106 : * @brief Decode the drag payload from @p data.
107 : * @param data MIME data of the drop; may be null.
108 : * @return The payload, or nothing when @p data is null, carries no payload
109 : * of ours, or the payload does not deserialise cleanly.
110 : */
111 : static auto parseDropData(const QMimeData *data)
112 : -> std::optional<StoreDragItem>;
113 :
114 : /**
115 : * @brief Resolve a validated drop into a Move or Copy.
116 : *
117 : * Both endpoints must resolve inside the store after symlink resolution
118 : * (the payload could be crafted). A directory is dropped into the target
119 : * folder; a file onto a folder goes into it, a file onto a file asks the
120 : * user before overwriting.
121 : * @param info The decoded drag payload.
122 : * @param action Qt::MoveAction or Qt::CopyAction.
123 : * @param parent Proxy index of the drop target.
124 : * @return true once the operation has been dispatched to the backend;
125 : * false for an escaping path, an unknown kind or a bad target.
126 : */
127 : auto executeDropAction(const StoreDragItem &info, Qt::DropAction action,
128 : const QModelIndex &parent) -> bool;
129 : /**
130 : * @brief Perform the actual Move/Copy for a validated drop.
131 : * @param cleanedSrc Canonical source path (inside the store).
132 : * @param cleanedDest Canonical destination path (inside the store).
133 : * @param action Qt::MoveAction or Qt::CopyAction.
134 : * @param force Overwrite an existing destination.
135 : * @return true once the operation has been dispatched.
136 : */
137 : auto performDrop(const QString &cleanedSrc, const QString &cleanedDest,
138 : Qt::DropAction action, bool force) -> bool;
139 : /**
140 : * @brief Whether a proxy index is a link (or junction) inside the store,
141 : * the store root excluded.
142 : * @param proxyIndex Index in this model.
143 : * @return true for a link inside the store.
144 : */
145 : [[nodiscard]] auto isLinkInStore(const QModelIndex &proxyIndex) const -> bool;
146 : /**
147 : * @brief The path of a file system model index, as the node keeps it.
148 : *
149 : * Not QFileSystemModel::filePath(): that rebuilds the path through
150 : * parent(), which looks each folder up among all its siblings, and on
151 : * Windows it gives a resolved link's target instead of the link.
152 : * @param sourceIndex Index in the file system model; fs must be set.
153 : * @return The absolute path.
154 : */
155 : [[nodiscard]] auto pathOf(const QModelIndex &sourceIndex) const -> QString;
156 :
157 : /**
158 : * @brief showThis() for a folder with rows: shown when any of them is,
159 : * or, in a search, when its own name matches.
160 : * @param index The folder's index in the source model.
161 : * @param path Its path relative to the store.
162 : * @param inside Whether it lies inside the store, below its root.
163 : * @param outside Whether it lies outside the store.
164 : * @return Whether the folder is shown.
165 : */
166 : [[nodiscard]] auto showsFolder(const QModelIndex &index, const QString &path,
167 : bool inside, bool outside) const -> bool;
168 :
169 : public:
170 : /**
171 : * @brief Construct a StoreModel.
172 : */
173 : StoreModel();
174 :
175 : /**
176 : * @brief Inject the Pass backend for drag-and-drop Move/Copy operations.
177 : * @param pass Pass backend instance; must outlive this StoreModel.
178 : */
179 : void setPass(Pass *pass);
180 :
181 : /**
182 : * @brief Filter whether a row should be displayed.
183 : * @param source_row Row index in source model.
184 : * @param source_parent Parent index in source model.
185 : * @return true if row should be displayed.
186 : */
187 : [[nodiscard]] auto filterAcceptsRow(int source_row,
188 : const QModelIndex &source_parent) const
189 : -> bool override;
190 :
191 : /**
192 : * @brief Check if a specific index should be shown.
193 : * @param index Model index to check.
194 : * @return true if index should be visible.
195 : */
196 : [[nodiscard]] auto showThis(const QModelIndex &index) const -> bool;
197 :
198 : /**
199 : * @brief No children for a link inside the store: it is listed by name
200 : * but never entered, so the view shows no expand arrow.
201 : * @param parent Proxy index.
202 : * @return false for a link inside the store, otherwise as the base class.
203 : */
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)
209 : /**
210 : * @brief Nothing to fetch under a link inside the store, so the file
211 : * system model never lists what the link points to.
212 : * @param parent Proxy index.
213 : * @return false for a link inside the store, otherwise as the base class.
214 : */
215 : [[nodiscard]] auto canFetchMore(const QModelIndex &parent) const
216 : -> bool override;
217 : /**
218 : * @brief Fetch more rows, except under a link inside the store.
219 : * @param parent Proxy index.
220 : */
221 : void fetchMore(const QModelIndex &parent) override;
222 :
223 : /**
224 : * @brief Initialize model with source model and store path.
225 : * @param sourceModel Filesystem model to wrap.
226 : * @param passStore Root path of password store.
227 : */
228 : void setModelAndStore(QFileSystemModel *sourceModel,
229 : const QString &passStore);
230 :
231 : /**
232 : * @brief Set the source filesystem root and map it to a proxy index.
233 : *
234 : * Wraps the recurring `mapFromSource(fs->setRootPath(path))` dance used to
235 : * point the tree view at a directory, so callers don't reach through to the
236 : * wrapped QFileSystemModel.
237 : * @param path Directory to make the source root.
238 : * @return Proxy index for `path`, or an invalid index if no source model is
239 : * set.
240 : */
241 : auto rootIndexFor(const QString &path) -> QModelIndex;
242 :
243 : /**
244 : * @brief The tree filter for what is typed in the search box.
245 : *
246 : * The words, split at spaces, are matched literally and in order, with
247 : * anything in between: "work vpn" finds work/acme/vpn. Each word is taken
248 : * at its first place after the one before, which is where an in-order
249 : * match can always continue, and the pattern is anchored with atomic
250 : * groups, so a path is searched once: a greedy `.*` join backtracked
251 : * through every repeat of a word in a long path.
252 : * @param query The search box text, with slashes as separators.
253 : * @return A case-insensitive pattern; empty for no words.
254 : */
255 : [[nodiscard]] static auto wordsFilter(const QString &query)
256 : -> QRegularExpression;
257 :
258 : /**
259 : * @brief Update the store path used for filtering without changing the source
260 : * model.
261 : * @param passStore New root path of password store.
262 : */
263 : void setStore(const QString &passStore);
264 :
265 : /**
266 : * @brief Get display data for index.
267 : * @param index Model index.
268 : * @param role Data role (e.g., Qt::DisplayRole).
269 : * @return Display data for the role.
270 : */
271 : [[nodiscard]] auto data(const QModelIndex &index, int role) const
272 : -> QVariant override;
273 :
274 : /**
275 : * @brief Compare two indices for sorting.
276 : * @param source_left Left index to compare.
277 : * @param source_right Right index to compare.
278 : * @return true if left < right.
279 : */
280 : [[nodiscard]] auto lessThan(const QModelIndex &source_left,
281 : const QModelIndex &source_right) const
282 : -> bool override;
283 :
284 : /**
285 : * @brief Get the password store root path.
286 : * @return Store path.
287 : */
288 : [[nodiscard]] auto getStore() const -> QString { return store; }
289 :
290 : // QAbstractItemModel interface
291 : public:
292 : /**
293 : * @brief Get supported drop actions.
294 : * @return Supported Qt::DropActions.
295 : */
296 : [[nodiscard]] auto supportedDropActions() const -> Qt::DropActions override;
297 :
298 : /**
299 : * @brief Get supported drag actions.
300 : * @return Supported Qt::DropActions.
301 : */
302 : [[nodiscard]] auto supportedDragActions() const -> Qt::DropActions override;
303 :
304 : /**
305 : * @brief Get item flags for index.
306 : * @param index Model index.
307 : * @return Item flags.
308 : */
309 : [[nodiscard]] auto flags(const QModelIndex &index) const
310 : -> Qt::ItemFlags override;
311 :
312 : /**
313 : * @brief Get supported MIME types for drag/drop.
314 : * @return List of MIME type strings.
315 : */
316 : [[nodiscard]] auto mimeTypes() const -> QStringList override;
317 :
318 : /**
319 : * @brief Create MIME data from indexes.
320 : * @param indexes List of selected indexes.
321 : * @return MIME data object.
322 : */
323 : [[nodiscard]] auto mimeData(const QModelIndexList &indexes) const
324 : -> QMimeData * override;
325 :
326 : /**
327 : * @brief Check if drop is possible.
328 : * @param data MIME data to drop.
329 : * @param action Drop action.
330 : * @param row Target row.
331 : * @param column Target column.
332 : * @param parent Target parent index.
333 : * @return true if drop is possible.
334 : */
335 : auto canDropMimeData(const QMimeData *data, Qt::DropAction action, int row,
336 : int column, const QModelIndex &parent) const
337 : -> bool override;
338 :
339 : /**
340 : * @brief Handle dropped MIME data.
341 : * @param data MIME data to drop.
342 : * @param action Drop action.
343 : * @param row Target row.
344 : * @param column Target column.
345 : * @param parent Target parent index.
346 : * @return true if drop was handled.
347 : */
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_
|