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
233public:
234 /**
235 * @brief The possible values to sort the location contents
236 */
237 enum SORTBY : uint_fast8_t {
238 /**
239 * The size of the file entry
240 */
241 SIZE = FMH::MODEL_KEY::SIZE,
242
243 /**
244 * The last modified date of the entry file
245 */
246 MODIFIED = FMH::MODEL_KEY::MODIFIED,
247
248 /**
249 * The creation date of the file entry
250 */
251 DATE = FMH::MODEL_KEY::DATE,
252
253 /**
254 * The name or title of the file entry
255 */
256 LABEL = FMH::MODEL_KEY::LABEL,
257
258 /**
259 * The file type of the entry. Deduced from its file suffix name
260 */
261 MIME = FMH::MODEL_KEY::MIME,
262
263 /**
264 * The date when the file entry was added
265 */
266 ADDDATE = FMH::MODEL_KEY::ADDDATE
267 };
268 Q_ENUM(SORTBY)
269
270 /**
271 * @brief The possible values to filter the a location content by a mime-type.
272 */
273 enum FILTER : uint_fast8_t {
274 /**
275 * Audio file types. Such as MP3, WAV, FLAC, MP4, etc.
276 */
277 AUDIO = FMStatic::FILTER_TYPE::AUDIO,
278
279 /**
280 * Video file types
281 */
282 VIDEO = FMStatic::FILTER_TYPE::VIDEO,
283
284 /**
285 * Plain text file types
286 */
287 TEXT = FMStatic::FILTER_TYPE::TEXT,
288
289 /**
290 * Image file types
291 */
292 IMAGE = FMStatic::FILTER_TYPE::IMAGE,
293
294 /**
295 * PDF, EBooks and comic books file types
296 */
297 DOCUMENT = FMStatic::FILTER_TYPE::DOCUMENT,
298
299 /**
300 * Compressed archives
301 */
302 COMPRESSED = FMStatic::FILTER_TYPE::COMPRESSED,
303
304 /**
305 * Font file types
306 */
307 FONT = FMStatic::FILTER_TYPE::FONT,
308
309 /**
310 * Any file type
311 */
312 NONE = FMStatic::FILTER_TYPE::NONE
313 };
314 Q_ENUM(FILTER)
315
316 /**
317 * @brief The different location or places types.
318 */
335 Q_ENUM(PATHTYPE)
336
337 /**
338 * @brief The possible view types for listing the entries in the FileBrowser visual control.
339 */
340 enum VIEW_TYPE : uint_fast8_t {
341 /**
342 * Display the file system entries in a grid view.
343 */
345
346 /**
347 * Display the file system entries in a list, with more information details visible.
348 */
349 LIST_VIEW
350 };
351 Q_ENUM(VIEW_TYPE)
352
353 /**
354 * @brief FMList
355 * @param parent
356 */
357 FMList(QObject *parent = nullptr);
358
359 /**
360 * @brief items
361 * @return
362 */
363 const FMH::MODEL_LIST &items() const final override;
364
365 FMList::SORTBY getSortBy() const;
366 void setSortBy(const FMList::SORTBY &key);
367
368 /**
369 * @private
370 */
371 void componentComplete() override final;
372
373 bool getAutoLoad() const;
374 void setAutoLoad(bool value);
375
376 QString getPath() const;
377 void setPath(const QString &path);
378
379 QString getPathName() const;
380
381 FMList::PATHTYPE getPathType() const;
382
383 QStringList getFilters() const;
384 void setFilters(const QStringList &filters);
385 void resetFilters();
386
387 FMList::FILTER getFilterType() const;
388 void setFilterType(const FMList::FILTER &type);
389 void resetFilterType();
390
391 bool getHidden() const;
392 void setHidden(const bool &state);
393
394 bool getOnlyDirs() const;
395 void setOnlyDirs(const bool &state);
396
397 const QUrl getParentPath();
398
399 bool getFoldersFirst() const;
400 void setFoldersFirst(const bool &value);
401
402 int getCloudDepth() const;
403 void setCloudDepth(const int &value);
404
405 PathStatus getStatus() const;
406
407 void setReadOnly(bool value);
408 bool readOnly() const;
409
410private:
411 FM *fm;
412 void clear();
413 void reset();
414 void setList();
415 void assignList(const FMH::MODEL_LIST &list);
416 void appendToList(const FMH::MODEL_LIST &list);
417 void sortList();
418 void filterContent(const QString &query, const QUrl &path);
419 void setStatus(const PathStatus &status);
420
421 bool saveImageFile(const QImage &image);
422 bool saveTextFile(const QString &data, const QString &format);
423 /**
424 * @brief Gets a model of the files associated with a tag
425 * @param tag The lookup tag
426 * @param filters Filters as regular expression
427 * @return Model of files associated
428 */
429 FMH::MODEL_LIST getTagContent(const QString &tag, const QStringList &filters = {});
430
431 FMH::MODEL_LIST list = {{}};
432
433 bool m_autoLoad = true;
434 QUrl path;
435 QString pathName = QString();
436 QStringList filters = {};
437
438 bool onlyDirs = false;
439 bool hidden = false;
440
441 bool foldersFirst = false;
442 int cloudDepth = 1;
443
444 PathStatus m_status;
445
448 FMList::PATHTYPE pathType = FMList::PATHTYPE::PLACES_PATH;
449
450 NavHistory m_navHistory;
451
452 bool m_readOnly = false;
453
454public Q_SLOTS:
455
456 /**
457 * @brief Refresh the model for new changes. h content listing ill be regenerated.
458 */
459 void refresh();
460
461 /**
462 * @brief Create a new directory within the current directory.
463 * @param name the name of the directory
464 */
465 void createDir(const QString &name);
466
467 /**
468 * @brief Create a new file.
469 * @note To create a custom file, please supply with the correct suffix.
470 * @param name the name of the new file, for example `new.txt`
471 */
472 void createFile(const QString &name);
473
474 /**
475 * @brief Rename a file with from a given URL to the a new name provided
476 * @param url the file URL to be renamed
477 * @param newName the new name for the file
478 */
479 void renameFile(const QString &url, const QString &newName);
480
481 /**
482 * @brief Create a symbolic link to the given URL in the current location.
483 * @param url the file URL to create the link from
484 */
485 void createSymlink(const QString &url);
486
487 /**
488 * @brief Completely remove the set of file URLs provided. This action can not be undone
489 * @param urls the list of file URLS to be removed
490 */
491 void removeFiles(const QStringList &urls);
492
493 /**
494 * @brief Remove and move to the trash the provided set of file URLs
495 * @param urls the list of file URLS to be removed
496 */
497 void moveToTrash(const QStringList &urls);
498
499 /**
500 * @brief Whether the clipboard has a supported type of content.
501 * @return whether the clipboard content is a supported file URL or a text or image raw data.
502 */
503 bool clipboardHasContent() const;
504
505 /**
506 * @brief Copy a list of file URLs into the current directory
507 * @param urls list of files
508 */
509 void copyInto(const QStringList &urls);
510
511 /**
512 * @brief Cut/move a list of file URLs to the current directory
513 * @param urls list of files
514 */
515 void cutInto(const QStringList &urls);
516
517 /**
518 * @brief Handle the paste action.
519 * 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.
520 */
521 void paste();
522
523 /**
524 * @brief Changes the icon of a directory by making use of the directory config file
525 * @param index the index position of the directory in the model
526 * @param iconName then name of the new icon
527 */
528 void setDirIcon(const int &index, const QString &iconName);
529
530 /**
531 * @brief Remove an item from the model, this does not remove the file from the file system
532 * @param index the index position of the file entry
533 */
534 void remove(const int &index);
535
536 /**
537 * @brief Start a search - starting from the current location - for a given name query.
538 * @param query the search query name
539 * @param recursive whether the search should be recursive and look into the subsequent sub-directories structure. By default this is set to `false`.
540 */
541 void search(const QString &query, bool recursive = true);
542
543 /**
544 * @brief The immediate previous path location that was navigated
545 * @return the path URL location
546 */
547 const QUrl previousPath();
548
549 /**
550 * @brief The immediate posterior path location that was navigated
551 * @return the path URL location
552 */
553 const QUrl posteriorPath();
554
555 /**
556 * @brief Given a file name query find if it exists in the current location
557 * @param index the index of the found file entry otherwise `-1`
558 */
559 int indexOfName(const QString &query);
560 int indexOfFile(const QString &url);
561
562Q_SIGNALS:
563 void pathChanged();
564 void pathNameChanged();
565 void pathTypeChanged();
566 void filtersChanged();
567 void filterTypeChanged();
568 void hiddenChanged();
569 void onlyDirsChanged();
570 void sortByChanged();
571 void foldersFirstChanged();
572 void statusChanged();
573 void cloudDepthChanged();
574 void autoLoadChanged();
575 void readOnlyChanged();
576
577 /**
578 * @brief Emitted when the listing process has any error message that needs to be notified.
579 * @param message the warning message text
580 */
581 void warning(QString message);
582
583 /**
584 * @brief Emitted while the file listing is still in progress.
585 * @param percent the loading progress - it goes from 0 to 100.
586 */
587 void progress(int percent);
588};
589
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:319
SORTBY
The possible values to sort the location contents.
Definition fmlist.h:237
@ MODIFIED
The last modified date of the entry file.
Definition fmlist.h:246
FILTER
The possible values to filter the a location content by a mime-type.
Definition fmlist.h:273
@ NONE
Any file type.
Definition fmlist.h:312
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:340
@ ICON_VIEW
Display the file system entries in a grid view.
Definition fmlist.h:344
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()
KIOWIDGETS_EXPORT PasteJob * paste(const QMimeData *mimeData, const QUrl &destDir, JobFlags flags=DefaultFlags)
QString path(const QString &relativePath)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QAction * renameFile(const QObject *recvr, const char *slot, QObject *parent)
QAction * moveToTrash(const QObject *recvr, const char *slot, QObject *parent)
KGuiItem remove()
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 Mon Nov 4 2024 16:32:33 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.