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

KDE's Doxygen guidelines are available online.