KIO

kfileplacesmodel.h
1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2007 Kevin Ottens <ervin@kde.org>
4 SPDX-FileCopyrightText: 2007 David Faure <faure@kde.org>
5 SPDX-FileCopyrightText: 2023 Harald Sitter <sitter@kde.org>
6
7 SPDX-License-Identifier: LGPL-2.0-only
8*/
9
10#ifndef KFILEPLACESMODEL_H
11#define KFILEPLACESMODEL_H
12
13#include "kiofilewidgets_export.h"
14
15#include <KBookmark>
16#include <QAbstractItemModel>
17#include <QUrl>
18
19#include <solid/device.h>
20#include <solid/solidnamespace.h>
21
22#include <memory>
23
24class KFilePlacesModelPrivate;
26
27class QMimeData;
28class QAction;
29
30/**
31 * @class KFilePlacesModel kfileplacesmodel.h <KFilePlacesModel>
32 *
33 * This class is a list view model. Each entry represents a "place"
34 * where user can access files. Only relevant when
35 * used with QListView or QTableView.
36 * @note This class is since 6.0 re-entrant
37 */
38class KIOFILEWIDGETS_EXPORT KFilePlacesModel : public QAbstractItemModel
39{
40 Q_OBJECT
41
42 Q_PROPERTY(QStringList supportedSchemes READ supportedSchemes WRITE setSupportedSchemes NOTIFY supportedSchemesChanged)
43
44public:
45 // Note: run printf "0x%08X\n" $(($RANDOM*$RANDOM))
46 // to define additional roles.
48 /** roleName is "url". @see url() */
49 UrlRole = 0x069CD12B,
50
51 /** roleName is "isHidden". @see isHidden() */
52 HiddenRole = 0x0741CAAC,
53
54 /** roleName is "isSetupNeeded". @see setupNeeded() */
55 SetupNeededRole = 0x059A935D,
56
57 /**
58 * Whether the place is a fixed device (neither hotpluggable nor removable).
59 * roleName is "isFixedDevice".
60 */
61 FixedDeviceRole = 0x332896C1,
62
63 /**
64 * Whether the place should have its free space displayed in a capacity bar.
65 * roleName is "isCapacityBarRecommended".
66 */
67 CapacityBarRecommendedRole = 0x1548C5C4,
68
69 /**
70 * The name of the group, for example "Remote" or "Devices". roleName is "group".
71 * @since 5.40
72 */
73 GroupRole = 0x0a5b64ee,
74
75 /**
76 * roleName is "iconName".
77 * @see icon()
78 * @since 5.41
79 */
80 IconNameRole = 0x00a45c00,
81
82 /** roleName is "isGroupHidden".
83 * @see isGroupHidden()
84 * @since 5.42
85 */
86 GroupHiddenRole = 0x21a4b936,
87
88 /** roleName is "isTeardownAllowed".
89 * @see isTeardownAllowed().
90 * @since 5.91
91 */
92 TeardownAllowedRole = 0x02533364,
93
94 /** roleName is "isEjectAllowed".
95 * @since 5.94.
96 */
97 EjectAllowedRole = 0x0A16AC5B,
98
99 /**
100 * roleName is "isTeardownOverlayRecommended".
101 * @see isTeardownOverlayRecommended()
102 * @since 5.95
103 */
104 TeardownOverlayRecommendedRole = 0x032EDCCE,
105
106 /**
107 * roleName is "deviceAccessibility".
108 * @see deviceAccessibility()
109 * @since 5.99
110 */
111 DeviceAccessibilityRole = 0x023FFD93,
112 };
113
114 /**
115 * Describes the available group types used in this model.
116 * @since 5.42
117 */
119 PlacesType, ///< "Places" section
120 RemoteType, ///< "Remote" section
121 RecentlySavedType, ///< "Recent" section
122 SearchForType, ///< "Search for" section
123 DevicesType, ///< "Devices" section
124 RemovableDevicesType, ///< "Removable Devices" section
125 UnknownType, ///< Unknown GroupType
126 TagsType, ///< "Tags" section. @since 5.54
127 };
128 Q_ENUM(GroupType)
129
130 enum DeviceAccessibility {
131 SetupNeeded,
132 SetupInProgress,
133 Accessible,
134 TeardownInProgress
135 };
136 Q_ENUM(DeviceAccessibility)
137
138 explicit KFilePlacesModel(QObject *parent = nullptr);
139 ~KFilePlacesModel() override;
140
141 /**
142 * @return The URL of the place at index @p index.
143 */
144 Q_INVOKABLE QUrl url(const QModelIndex &index) const;
145
146 /**
147 * @return Whether the place at index @p index needs to be mounted before it can be used.
148 */
149 Q_INVOKABLE bool setupNeeded(const QModelIndex &index) const;
150
151 /**
152 * @return Whether the place is a device that can be unmounted, e.g. it is
153 * mounted but does not point at system Root or the user's Home directory.
154 *
155 * It does not indicate whether the teardown can succeed.
156 * @since 5.91
157 */
158 Q_INVOKABLE bool isTeardownAllowed(const QModelIndex &index) const;
159
160 /**
161 * @return Whether the place is a device that can be ejected, e.g. it is
162 * a CD, DVD, etc.
163 *
164 * It does not indicate whether the eject can succeed.
165 * @since 5.94
166 */
167 Q_INVOKABLE bool isEjectAllowed(const QModelIndex &index) const;
168
169 /**
170 * @return Whether showing an inline teardown button is recommended,
171 * e.g. when it is a removable drive.
172 *
173 * @since 5.95
174 **/
175 Q_INVOKABLE bool isTeardownOverlayRecommended(const QModelIndex &index) const;
176
177 /**
178 * @return Whether this device is currently accessible or being (un)mounted.
179 *
180 * @since 5.99
181 */
182 Q_INVOKABLE KFilePlacesModel::DeviceAccessibility deviceAccessibility(const QModelIndex &index) const;
183
184 /**
185 * @return The icon of the place at index @p index.
186 */
187 Q_INVOKABLE QIcon icon(const QModelIndex &index) const;
188
189 /**
190 * @return The user-visible text of the place at index @p index.
191 */
192 Q_INVOKABLE QString text(const QModelIndex &index) const;
193
194 /**
195 * @return Whether the place at index @p index is hidden or is inside an hidden group.
196 */
197 Q_INVOKABLE bool isHidden(const QModelIndex &index) const;
198
199 /**
200 * @return Whether the group type @p type is hidden.
201 * @since 5.42
202 */
203 Q_INVOKABLE bool isGroupHidden(const GroupType type) const;
204
205 /**
206 * @return Whether the group of the place at index @p index is hidden.
207 * @since 5.42
208 */
209 Q_INVOKABLE bool isGroupHidden(const QModelIndex &index) const;
210
211 /**
212 * @return Whether the place at index @p index is a device handled by Solid.
213 * @see deviceForIndex()
214 */
215 Q_INVOKABLE bool isDevice(const QModelIndex &index) const;
216
217 /**
218 * @return The solid device of the place at index @p index, if it is a device. Otherwise a default Solid::Device() instance is returned.
219 * @see isDevice()
220 */
221 Solid::Device deviceForIndex(const QModelIndex &index) const;
222
223 /**
224 * @return The KBookmark instance of the place at index @p index.
225 * If the index is not valid, a default KBookmark instance is returned.
226 */
227 KBookmark bookmarkForIndex(const QModelIndex &index) const;
228
229 /**
230 * @return The KBookmark instance of the place with url @p searchUrl.
231 * If the bookmark corresponding to searchUrl is not found, a default KBookmark instance is returned.
232 * @since 5.63
233 */
234 KBookmark bookmarkForUrl(const QUrl &searchUrl) const;
235
236 /**
237 * @return The group type of the place at index @p index.
238 * @since 5.42
239 */
240 Q_INVOKABLE GroupType groupType(const QModelIndex &index) const;
241
242 /**
243 * @return The list of model indexes that have @ type as their group type.
244 * @see groupType()
245 * @since 5.42
246 */
247 Q_INVOKABLE QModelIndexList groupIndexes(const GroupType type) const;
248
249 /**
250 * @return A QAction with a proper translated label that can be used to trigger the requestTeardown()
251 * method for the place at index @p index.
252 * @see requestTeardown()
253 */
254 Q_INVOKABLE QAction *teardownActionForIndex(const QModelIndex &index) const;
255
256 /**
257 * @return A QAction with a proper translated label that can be used to trigger the requestEject()
258 * method for the place at index @p index.
259 * @see requestEject()
260 */
261 Q_INVOKABLE QAction *ejectActionForIndex(const QModelIndex &index) const;
262
263 /**
264 * @return A QAction with a proper translated label that can be used to open a partitioning menu for the device. nullptr if not a device.
265 */
266 Q_INVOKABLE QAction *partitionActionForIndex(const QModelIndex &index) const;
267
268 /**
269 * Unmounts the place at index @p index by triggering the teardown functionality of its Solid device.
270 * @see deviceForIndex()
271 */
272 Q_INVOKABLE void requestTeardown(const QModelIndex &index);
273
274 /**
275 * Ejects the place at index @p index by triggering the eject functionality of its Solid device.
276 * @see deviceForIndex()
277 */
278 Q_INVOKABLE void requestEject(const QModelIndex &index);
279
280 /**
281 * Mounts the place at index @p index by triggering the setup functionality of its Solid device.
282 * @see deviceForIndex()
283 */
284 Q_INVOKABLE void requestSetup(const QModelIndex &index);
285
286 /**
287 * Adds a new place to the model.
288 * @param text The user-visible text for the place
289 * @param url The URL of the place. It will be stored in its QUrl::FullyEncoded string format.
290 * @param iconName The icon of the place
291 * @param appName If set as the value of QCoreApplication::applicationName(), will make the place visible only in this application.
292 */
293 Q_INVOKABLE void addPlace(const QString &text, const QUrl &url, const QString &iconName = QString(), const QString &appName = QString());
294
295 /**
296 * Adds a new place to the model.
297 * @param text The user-visible text for the place
298 * @param url The URL of the place. It will be stored in its QUrl::FullyEncoded string format.
299 * @param iconName The icon of the place
300 * @param appName If set as the value of QCoreApplication::applicationName(), will make the place visible only in this application.
301 * @param after The index after which the new place will be added.
302 */
303 Q_INVOKABLE void addPlace(const QString &text, const QUrl &url, const QString &iconName, const QString &appName, const QModelIndex &after);
304
305 /**
306 * Edits the place with index @p index.
307 * @param text The new user-visible text for the place
308 * @param url The new URL of the place
309 * @param iconName The new icon of the place
310 * @param appName The new application-local filter for the place (@see addPlace()).
311 */
312 Q_INVOKABLE void
313 editPlace(const QModelIndex &index, const QString &text, const QUrl &url, const QString &iconName = QString(), const QString &appName = QString());
314
315 /**
316 * Deletes the place with index @p index from the model.
317 */
318 Q_INVOKABLE void removePlace(const QModelIndex &index) const;
319
320 /**
321 * Changes the visibility of the place with index @p index, but only if the place is not inside an hidden group.
322 * @param hidden Whether the place should be hidden or visible.
323 * @see isGroupHidden()
324 */
325 Q_INVOKABLE void setPlaceHidden(const QModelIndex &index, bool hidden);
326
327 /**
328 * Changes the visibility of the group with type @p type.
329 * @param hidden Whether the group should be hidden or visible.
330 * @see isGroupHidden()
331 * @since 5.42
332 */
333 Q_INVOKABLE void setGroupHidden(const GroupType type, bool hidden);
334
335 /**
336 * @brief Move place at @p itemRow to a position before @p row
337 * @return Whether the place has been moved.
338 * @since 5.41
339 */
340 Q_INVOKABLE bool movePlace(int itemRow, int row);
341
342 /**
343 * @return The number of hidden places in the model.
344 * @see isHidden()
345 */
346 Q_INVOKABLE int hiddenCount() const;
347
348 /**
349 * @brief Get a visible data based on Qt role for the given index.
350 * Return the device information for the give index.
351 *
352 * @param index The QModelIndex which contains the row, column to fetch the data.
353 * @param role The Interview data role(ex: Qt::DisplayRole).
354 *
355 * @return the data for the given index and role.
356 */
357 QVariant data(const QModelIndex &index, int role) const override;
358
359 /**
360 * @brief Get the children model index for the given row and column.
361 */
362 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
363
364 /**
365 * @brief Get the parent QModelIndex for the given model child.
366 */
367 QModelIndex parent(const QModelIndex &child) const override;
368
369 /// Reimplemented from QAbstractItemModel.
370 /// @see AdditionalRoles
371 QHash<int, QByteArray> roleNames() const override;
372
373 /**
374 * @brief Get the number of rows for a model index.
375 */
376 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
377
378 /**
379 * @brief Get the number of columns for a model index.
380 */
381 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
382
383 /**
384 * Returns the closest item for the URL \a url.
385 * The closest item is defined as item which is equal to
386 * the URL or at least is a parent URL. If there are more than
387 * one possible parent URL candidates, the item which covers
388 * the bigger range of the URL is returned.
389 *
390 * Example: the url is '/home/peter/Documents/Music'.
391 * Available items are:
392 * - /home/peter
393 * - /home/peter/Documents
394 *
395 * The returned item will the one for '/home/peter/Documents'.
396 */
397 QModelIndex closestItem(const QUrl &url) const;
398
399 Qt::DropActions supportedDropActions() const override;
400 Qt::ItemFlags flags(const QModelIndex &index) const override;
401 QStringList mimeTypes() const override;
402 QMimeData *mimeData(const QModelIndexList &indexes) const override;
403 bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
404
405 /**
406 * @brief Reload bookmark information
407 * @since 5.41
408 */
409 Q_INVOKABLE void refresh() const;
410
411 /**
412 * @brief Converts the URL, which contains "virtual" URLs for system-items like
413 * "timeline:/lastmonth" into a Query-URL "timeline:/2017-10"
414 * that will be handled by the corresponding KIO worker.
415 * Virtual URLs for bookmarks are used to be independent from
416 * internal format changes.
417 * @param an url
418 * @return the converted URL, which can be handled by a KIO worker
419 * @since 5.41
420 */
421 static QUrl convertedUrl(const QUrl &url);
422
423 /**
424 * Set the URL schemes that the file widget should allow navigating to.
425 *
426 * If the returned list is empty, all schemes are supported. Examples for
427 * schemes are @c "file" or @c "ftp".
428 *
429 * @sa QFileDialog::setSupportedSchemes
430 * @since 5.43
431 */
432 void setSupportedSchemes(const QStringList &schemes);
433
434 /**
435 * Returns the URL schemes that the file widget should allow navigating to.
436 *
437 * If the returned list is empty, all schemes are supported.
438 *
439 * @sa QFileDialog::supportedSchemes
440 * @since 5.43
441 */
442 QStringList supportedSchemes() const;
443
445 /**
446 * @p message An error message explaining what went wrong.
447 */
448 void errorMessage(const QString &message);
449
450 /**
451 * Emitted after the Solid setup ends.
452 * @param success Whether the Solid setup has been successful.
453 * @see requestSetup()
454 */
455 void setupDone(const QModelIndex &index, bool success);
456
457 /**
458 * Emitted after the teardown of a device ends.
459 *
460 * @note In case of an error, the @p errorMessage signal
461 * will also be emitted with a message describing the error.
462 *
463 * @param error Type of error that occurred, if any.
464 * @param errorData More information about the error, if any.
465 * @since 5.100
466 */
467 void teardownDone(const QModelIndex &index, Solid::ErrorType error, const QVariant &errorData);
468
469 /**
470 * Emitted whenever the visibility of the group @p group changes.
471 * @param hidden The new visibility of the group.
472 * @see setGroupHidden()
473 * @since 5.42
474 */
476
477 /**
478 * Called once the model has been reloaded
479 *
480 * @since 5.71
481 */
482 void reloaded();
483
484 /**
485 * Emitted whenever the list of supported schemes has been changed
486 *
487 * @since 5.94
488 */
490
491private:
492 friend class KFilePlacesModelPrivate;
493 std::unique_ptr<KFilePlacesModelPrivate> d;
494};
495
496#endif
This class is a list view model.
void teardownDone(const QModelIndex &index, Solid::ErrorType error, const QVariant &errorData)
Emitted after the teardown of a device ends.
GroupType
Describes the available group types used in this model.
@ PlacesType
"Places" section
@ RemoteType
"Remote" section
@ RemovableDevicesType
"Removable Devices" section
@ RecentlySavedType
"Recent" section
@ DevicesType
"Devices" section
@ UnknownType
Unknown GroupType.
@ TagsType
"Tags" section.
@ SearchForType
"Search for" section
void reloaded()
Called once the model has been reloaded.
void setupDone(const QModelIndex &index, bool success)
Emitted after the Solid setup ends.
void supportedSchemesChanged()
Emitted whenever the list of supported schemes has been changed.
void groupHiddenChanged(KFilePlacesModel::GroupType group, bool hidden)
Emitted whenever the visibility of the group group changes.
void errorMessage(const QString &message)
message An error message explaining what went wrong.
virtual int columnCount(const QModelIndex &parent) const const=0
virtual QVariant data(const QModelIndex &index, int role) const const=0
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual QMimeData * mimeData(const QModelIndexList &indexes) const const
virtual QStringList mimeTypes() const const
virtual QHash< int, QByteArray > roleNames() const const
virtual int rowCount(const QModelIndex &parent) const const=0
virtual Qt::DropActions supportedDropActions() const const
Q_ENUM(...)
Q_PROPERTY(...)
Q_SIGNALSQ_SIGNALS
QObject * parent() const const
typedef DropActions
typedef ItemFlags
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:28 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.