MauiKit File Browsing

fmlist.h
1/*
2 * <one line to give the program's name and a brief idea of what it does.>
3 * Copyright (C) 2018 Camilo Higuita <email>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include <QObject>
22#include <QImage>
23#include <QQmlEngine>
24
25#include <MauiKit4/Core/mauilist.h>
26
27#include "filebrowsing_export.h"
28
29#include "fmstatic.h"
30
31class FM;
32
33/**
34 * @brief Represents the status of a directory listing, be it non existence location, loading or empty.
35 *
36 * The status object is divided into different properties for convenience, such as error label, message, icon, code, etc.
37 */
38struct FILEBROWSING_EXPORT PathStatus
39{
40 Q_GADGET
41
42 /**
43 * The status code. More details are exposed with the other properties.
44 */
45 Q_PROPERTY(PathStatus::STATUS_CODE code MEMBER m_code)
46
47 /**
48 * The status title.
49 */
50 Q_PROPERTY(QString title MEMBER m_title)
51
52 /**
53 * The message details of the status.
54 */
55 Q_PROPERTY(QString message MEMBER m_message)
56
57 /**
58 * An associated icon name for the status.
59 */
60 Q_PROPERTY(QString icon MEMBER m_icon)
61
62 /**
63 * Whether the location is empty and there is nothing for listing.
64 */
65 Q_PROPERTY(bool empty MEMBER m_empty)
66
67 /**
68 * Whether the location can be accessed and exists.
69 */
70 Q_PROPERTY(bool exists MEMBER m_exists)
71
72public:
73 /**
74 * @brief The different status that can occur.
75 */
76 enum STATUS_CODE : int
77 {
78 /**
79 * The content is still loading its contents
80 */
82
83 /**
84 * The listing of the contents has failed. For knowing the reason check the other properties, such as `title`, `exists`, etc.
85 */
87
88 /**
89 * The listing has finished successfully
90 */
91 READY
92
93 }; Q_ENUM(STATUS_CODE)
94
95 STATUS_CODE m_code;
96 QString m_title;
97 QString m_message;
98 QString m_icon;
99 bool m_empty = false;
100 bool m_exists = false;
101};
102Q_DECLARE_METATYPE(PathStatus)
103
104/**
105 * @private
106 */
107struct FILEBROWSING_EXPORT NavHistory {
108 void appendPath(const QUrl &path)
109 {
110 this->prev_history.append(path);
111 }
112
113 QUrl getPosteriorPath()
114 {
115 if (this->post_history.isEmpty())
116 return QUrl();
117
118 return this->post_history.takeLast();
119 }
120
121 QUrl getPreviousPath()
122 {
123 if (this->prev_history.isEmpty())
124 return QUrl();
125
126 if (this->prev_history.length() < 2)
127 return this->prev_history.at(0);
128
129 this->post_history.append(this->prev_history.takeLast());
130 return this->prev_history.takeLast();
131 }
132
133private:
134 QVector<QUrl> prev_history;
135 QVector<QUrl> post_history;
136};
137
138/**
139 * @brief The FMList class
140 * Model for listing the file system files and directories and perform relevant actions upon it
141 */
142class FILEBROWSING_EXPORT FMList : public MauiList
143{
144 Q_OBJECT
145 QML_ELEMENT
146 Q_DISABLE_COPY(FMList)
147
148 // writable
149
150 /**
151 * Whether to auto load the content entries when the path property is modified. Otherwise explicitly call the load method.
152 * @see search
153 * @see fill
154 */
155 Q_PROPERTY(bool autoLoad READ getAutoLoad WRITE setAutoLoad NOTIFY autoLoadChanged)
156
157 /**
158 * The URL to location path to proceed listing all of its file entries.
159 * There is support for multiple type of location depending on the scheme, for example local file system uses `file://`, while you can browser networks using `ftp://` or `fish://`. Support for those locations depends on KIO and its slaves - to know more about it read the KIO slaves documentation.
160 */
161 Q_PROPERTY(QString path READ getPath WRITE setPath NOTIFY pathChanged)
162
163 /**
164 * Whether to list the hidden entries.
165 * By default this is set to `false`.
166 */
167 Q_PROPERTY(bool hidden READ getHidden WRITE setHidden NOTIFY hiddenChanged)
168
169 /**
170 * Whether only directories should be listed.
171 * By default this is set to `false`.
172 */
173 Q_PROPERTY(bool onlyDirs READ getOnlyDirs WRITE setOnlyDirs NOTIFY onlyDirsChanged)
174
175 /**
176 * Whether the folders should be sorted first and then the files.
177 * By default this is set to `true`.
178 */
179 Q_PROPERTY(bool foldersFirst READ getFoldersFirst WRITE setFoldersFirst NOTIFY foldersFirstChanged)
180
181 /**
182 * When the location if a remote cloud directory, this allows to define the depth of the levels for listing the contents.
183 * By default this is set to `1`, which will only lists the entries in the current location, a bigger depth will start listing sub-directories too.
184 * @deprecated
185 */
186 Q_PROPERTY(int cloudDepth READ getCloudDepth WRITE setCloudDepth NOTIFY cloudDepthChanged)
187
188 /**
189 * The list of string values to filter the listing. For example to only list PNG and JPG images: `filters: ["*.png", "*.jpg"]`.
190 * To reset or clear the filters you can set the property to `undefined`
191 */
192 Q_PROPERTY(QStringList filters READ getFilters WRITE setFilters NOTIFY filtersChanged RESET resetFilters)
193
194 /**
195 * A convenient way to filter the location contents by a file type (mimetype).
196 * By default this is set to `FILTER_TYPE::NONE`.
197 */
198 Q_PROPERTY(FMList::FILTER filterType READ getFilterType WRITE setFilterType NOTIFY filterTypeChanged RESET resetFilterType)
199
200 /**
201 * The sorting value.
202 * By default this is set to `SORTBY::MODIFIED`.
203 */
204 Q_PROPERTY(FMList::SORTBY sortBy READ getSortBy WRITE setSortBy NOTIFY sortByChanged)
205
206 /**
207 * Whether destructive actions or modifications can be done to the current location contents, such as deleting, renaming, pasting, adding, etc.
208 * This only protects the location contents if using this API action methods.
209 */
210 Q_PROPERTY(bool readOnly READ readOnly WRITE setReadOnly NOTIFY readOnlyChanged)
211
212 // readonly
213 /**
214 * The title name of the current location.
215 */
216 Q_PROPERTY(QString pathName READ getPathName NOTIFY pathNameChanged FINAL)
217
218 /**
219 * The known type of the current location.
220 */
221 Q_PROPERTY(FMList::PATHTYPE pathType READ getPathType NOTIFY pathTypeChanged FINAL)
222
223 /**
224 * The current status of the location contents listing. This is a group of properties.
225 */
226 Q_PROPERTY(PathStatus status READ getStatus NOTIFY statusChanged)
227
228 /**
229 * The location of the parent directory of this current location.
230 */
231 Q_PROPERTY(QUrl parentPath READ getParentPath NOTIFY pathChanged)
232
233 /**
234 * Merge the name filters and mimetype filters together for filtering the requested location contentent's.
235 * The `filters` and `filterType` are the properties to be merged. If this is set to false, then the `filters` property will have priority over the `filterType` one, unless it is empty.
236 */
237 Q_PROPERTY(bool mergeFilters READ mergeFilters WRITE setMergeFilters NOTIFY mergeFiltersChanged)
238
239public:
240 /**
241 * @brief The possible values to sort the location contents
242 */
243 enum SORTBY : uint_fast8_t {
244 /**
245 * The size of the file entry
246 */
247 SIZE = FMH::MODEL_KEY::SIZE,
248
249 /**
250 * The last modified date of the entry file
251 */
252 MODIFIED = FMH::MODEL_KEY::MODIFIED,
253
254 /**
255 * The creation date of the file entry
256 */
257 DATE = FMH::MODEL_KEY::DATE,
258
259 /**
260 * The name or title of the file entry
261 */
262 LABEL = FMH::MODEL_KEY::LABEL,
263
264 /**
265 * The file type of the entry. Deduced from its file suffix name
266 */
267 MIME = FMH::MODEL_KEY::MIME,
268
269 /**
270 * The date when the file entry was added
271 */
272 ADDDATE = FMH::MODEL_KEY::ADDDATE
273 };
274 Q_ENUM(SORTBY)
275
276 /**
277 * @brief The possible values to filter the a location content by a mime-type.
278 */
279 enum FILTER : uint_fast8_t {
280 /**
281 * Audio file types. Such as MP3, WAV, FLAC, MP4, etc.
282 */
283 AUDIO = FMStatic::FILTER_TYPE::AUDIO,
284
285 /**
286 * Video file types
287 */
288 VIDEO = FMStatic::FILTER_TYPE::VIDEO,
289
290 /**
291 * Plain text file types
292 */
293 TEXT = FMStatic::FILTER_TYPE::TEXT,
294
295 /**
296 * Image file types
297 */
298 IMAGE = FMStatic::FILTER_TYPE::IMAGE,
299
300 /**
301 * PDF, EBooks and comic books file types
302 */
303 DOCUMENT = FMStatic::FILTER_TYPE::DOCUMENT,
304
305 /**
306 * Compressed archives
307 */
308 COMPRESSED = FMStatic::FILTER_TYPE::COMPRESSED,
309
310 /**
311 * Font file types
312 */
313 FONT = FMStatic::FILTER_TYPE::FONT,
314
315 /**
316 * Any file type
317 */
318 NONE = FMStatic::FILTER_TYPE::NONE
319 };
320 Q_ENUM(FILTER)
321
322 /**
323 * @brief The different location or places types.
324 */
341 Q_ENUM(PATHTYPE)
342
343 /**
344 * @brief The possible view types for listing the entries in the FileBrowser visual control.
345 */
346 enum VIEW_TYPE : uint_fast8_t {
347 /**
348 * Display the file system entries in a grid view.
349 */
351
352 /**
353 * Display the file system entries in a list, with more information details visible.
354 */
355 LIST_VIEW
356 };
357 Q_ENUM(VIEW_TYPE)
358
359 /**
360 * @brief FMList
361 * @param parent
362 */
363 FMList(QObject *parent = nullptr);
364
365 /**
366 * @brief items
367 * @return
368 */
369 const FMH::MODEL_LIST &items() const final override;
370
371 FMList::SORTBY getSortBy() const;
372 void setSortBy(const FMList::SORTBY &key);
373
374 /**
375 * @private
376 */
377 void componentComplete() override final;
378
379 bool getAutoLoad() const;
380 void setAutoLoad(bool value);
381
382 QString getPath() const;
383 void setPath(const QString &path);
384
385 QString getPathName() const;
386
387 FMList::PATHTYPE getPathType() const;
388
389 QStringList getFilters() const;
390 void setFilters(const QStringList &filters);
391 void resetFilters();
392
393 FMList::FILTER getFilterType() const;
394 void setFilterType(const FMList::FILTER &type);
395 void resetFilterType();
396
397 bool getHidden() const;
398 void setHidden(const bool &state);
399
400 bool getOnlyDirs() const;
401 void setOnlyDirs(const bool &state);
402
403 const QUrl getParentPath();
404
405 bool getFoldersFirst() const;
406 void setFoldersFirst(const bool &value);
407
408 int getCloudDepth() const;
409 void setCloudDepth(const int &value);
410
411 PathStatus getStatus() const;
412
413 void setReadOnly(bool value);
414 bool readOnly() const;
415
416 void setMergeFilters(bool value);
417 bool mergeFilters() const;
418
419private:
420 FM *fm;
421 void clear();
422 void reset();
423 void setList();
424 void assignList(const FMH::MODEL_LIST &list);
425 void appendToList(const FMH::MODEL_LIST &list);
426 void sortList();
427 void filterContent(const QString &query, const QUrl &path);
428 void setStatus(const PathStatus &status);
429
430 bool saveImageFile(const QImage &image);
431 bool saveTextFile(const QString &data, const QString &format);
432 /**
433 * @brief Gets a model of the files associated with a tag
434 * @param tag The lookup tag
435 * @param filters Filters as regular expression
436 * @return Model of files associated
437 */
438 FMH::MODEL_LIST getTagContent(const QString &tag, const QStringList &filters = {});
439
440 FMH::MODEL_LIST list = {{}};
441
442 bool m_autoLoad = true;
443 QUrl path;
444 QString pathName = QString();
445 QStringList filters = {};
446
447 bool onlyDirs = false;
448 bool hidden = false;
449
450 bool foldersFirst = false;
451 int cloudDepth = 1;
452
453 PathStatus m_status;
454
457 FMList::PATHTYPE pathType = FMList::PATHTYPE::PLACES_PATH;
458
459 NavHistory m_navHistory;
460
461 bool m_readOnly = false;
462 bool m_mergeFilters = true;
463
464public Q_SLOTS:
465
466 /**
467 * @brief Refresh the model for new changes. h content listing ill be regenerated.
468 */
469 void refresh();
470
471 /**
472 * @brief Create a new directory within the current directory.
473 * @param name the name of the directory
474 */
475 void createDir(const QString &name);
476
477 /**
478 * @brief Create a new file.
479 * @note To create a custom file, please supply with the correct suffix.
480 * @param name the name of the new file, for example `new.txt`
481 */
482 void createFile(const QString &name);
483
484 /**
485 * @brief Rename a file with from a given URL to the a new name provided
486 * @param url the file URL to be renamed
487 * @param newName the new name for the file
488 */
489 void renameFile(const QString &url, const QString &newName);
490
491 /**
492 * @brief Create a symbolic link to the given URL in the current location.
493 * @param url the file URL to create the link from
494 */
495 void createSymlink(const QString &url);
496
497 /**
498 * @brief Completely remove the set of file URLs provided. This action can not be undone
499 * @param urls the list of file URLS to be removed
500 */
501 void removeFiles(const QStringList &urls);
502
503 /**
504 * @brief Remove and move to the trash the provided set of file URLs
505 * @param urls the list of file URLS to be removed
506 */
507 void moveToTrash(const QStringList &urls);
508
509 /**
510 * @brief Whether the clipboard has a supported type of content.
511 * @return whether the clipboard content is a supported file URL or a text or image raw data.
512 */
513 bool clipboardHasContent() const;
514
515 /**
516 * @brief Copy a list of file URLs into the current directory
517 * @param urls list of files
518 */
519 void copyInto(const QStringList &urls);
520
521 /**
522 * @brief Cut/move a list of file URLs to the current directory
523 * @param urls list of files
524 */
525 void cutInto(const QStringList &urls);
526
527 /**
528 * @brief Handle the paste action.
529 * This allows to quickly paste into the current location any file URL in the clipboard, and raw image data and text snippets into a new file.
530 */
531 void paste();
532
533 /**
534 * @brief Changes the icon of a directory by making use of the directory config file
535 * @param index the index position of the directory in the model
536 * @param iconName then name of the new icon
537 */
538 void setDirIcon(const int &index, const QString &iconName);
539
540 /**
541 * @brief Remove an item from the model, this does not remove the file from the file system
542 * @param index the index position of the file entry
543 */
544 void remove(const int &index);
545
546 /**
547 * @brief Start a search - starting from the current location - for a given name query.
548 * @param query the search query name
549 * @param recursive whether the search should be recursive and look into the subsequent sub-directories structure. By default this is set to `false`.
550 */
551 void search(const QString &query, bool recursive = true);
552
553 /**
554 * @brief The immediate previous path location that was navigated
555 * @return the path URL location
556 */
557 const QUrl previousPath();
558
559 /**
560 * @brief The immediate posterior path location that was navigated
561 * @return the path URL location
562 */
563 const QUrl posteriorPath();
564
565 /**
566 * @brief Given a file-name-query, check if it exists in the current location
567 * @param index the index of the found file entry otherwise `-1`
568 */
569 int indexOfName(const QString &query);
570 int indexOfFile(const QString &url);
571
572Q_SIGNALS:
573 void pathChanged();
574 void pathNameChanged();
575 void pathTypeChanged();
576 void filtersChanged();
577 void filterTypeChanged();
578 void hiddenChanged();
579 void onlyDirsChanged();
580 void sortByChanged();
581 void foldersFirstChanged();
582 void statusChanged();
583 void cloudDepthChanged();
584 void autoLoadChanged();
585 void readOnlyChanged();
586 void mergeFiltersChanged();
587
588 /**
589 * @brief Emitted when the listing process has any error message that needs to be notified.
590 * @param message the warning message text
591 */
592 void warning(QString message);
593
594 /**
595 * @brief Emitted while the file listing is still in progress.
596 * @param percent the loading progress - it goes from 0 to 100.
597 */
598 void progress(int percent);
599};
600
The FMList class Model for listing the file system files and directories and perform relevant actions...
Definition fmlist.h:143
PATHTYPE
The different location or places types.
Definition fmlist.h:325
SORTBY
The possible values to sort the location contents.
Definition fmlist.h:243
@ MODIFIED
The last modified date of the entry file.
Definition fmlist.h:252
FILTER
The possible values to filter the a location content by a mime-type.
Definition fmlist.h:279
@ NONE
Any file type.
Definition fmlist.h:318
void progress(int percent)
Emitted while the file listing is still in progress.
VIEW_TYPE
The possible view types for listing the entries in the FileBrowser visual control.
Definition fmlist.h:346
@ ICON_VIEW
Display the file system entries in a grid view.
Definition fmlist.h:350
void warning(QString message)
Emitted when the listing process has any error message that needs to be notified.
@ REMOTE_PATH
Remote locations, such as servers accessed via SSH or FTP.
Definition fmstatic.h:261
@ OTHER_PATH
Any other path.
Definition fmstatic.h:326
@ FISH_PATH
A remote SHH or FTP.
Definition fmstatic.h:306
@ APPS_PATH
The applications location.
Definition fmstatic.h:286
@ TAGS_PATH
A tag location.
Definition fmstatic.h:276
@ DRIVES_PATH
Hard drives locations.
Definition fmstatic.h:266
@ MTP_PATH
MTP path.
Definition fmstatic.h:311
@ CLOUD_PATH
A remote cloud server path.
Definition fmstatic.h:301
@ TRASH_PATH
The trash location.
Definition fmstatic.h:291
@ BOOKMARKS_PATH
A bookmarked location.
Definition fmstatic.h:321
@ REMOVABLE_PATH
Removable places, such as optic CDs, USB pen drives, etc.
Definition fmstatic.h:271
@ QUICK_PATH
The common standard paths.
Definition fmstatic.h:316
@ PLACES_PATH
Local paths, such as the Downloads, Pictures, etc.
Definition fmstatic.h:256
The FM class stands for File Management, and exposes methods for file listing, browsing and handling,...
Definition fm.h:104
Q_SCRIPTABLE CaptureState status()
bool remove(const QString &column, const QVariant &value)
QString path(const QString &relativePath)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
const QList< QKeySequence > & paste()
const QList< QKeySequence > & renameFile()
const QList< QKeySequence > & moveToTrash()
QString & append(QChar ch)
Represents the status of a directory listing, be it non existence location, loading or empty.
Definition fmlist.h:39
STATUS_CODE
The different status that can occur.
Definition fmlist.h:77
@ LOADING
The content is still loading its contents.
Definition fmlist.h:81
@ ERROR
The listing of the contents has failed.
Definition fmlist.h:86
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.