MauiKit File Browsing

fileloader.cpp
1#include "fileloader.h"
2#include "fmstatic.h"
3#include "tagging.h"
4
5#include <QDebug>
6#include <QDirIterator>
7
8using namespace FMH;
9
10std::function<FMH::MODEL(const QUrl &url)> FileLoader::informer = &FMStatic::getFileInfoModel;
11
12FileLoader::FileLoader(QObject *parent) : QObject(parent)
13 , m_thread(new QThread)
14{
15 qRegisterMetaType<QDir::Filters>("QDir::Filters");
16 qRegisterMetaType<FMH::MODEL>("FMH::MODEL");
17 qRegisterMetaType<FMH::MODEL_LIST>("FMH::MODEL_LIST");
18 this->moveToThread(m_thread);
19 connect(m_thread, &QThread::finished, m_thread, &QObject::deleteLater);
20 connect(this, &FileLoader::start, this, &FileLoader::getFiles);
21 m_thread->start();
22}
23
24FileLoader::~FileLoader()
25{
26 m_thread->quit();
27 m_thread->wait();
28}
29
30void FileLoader::setBatchCount(const uint &count)
31{
32 m_batchCount = count;
33}
34
36{
37 return m_batchCount;
38}
39
40void FileLoader::requestPath(const QList<QUrl> &urls, const bool &recursive, const QStringList &nameFilters, const QDir::Filters &filters, const uint &limit)
41{
42 qDebug() << "FROM file loader" << urls;
43 Q_EMIT this->start(urls, recursive, nameFilters, filters, limit);
44}
45
46void FileLoader::getFiles(QList<QUrl> paths, bool recursive, const QStringList &nameFilters, const QDir::Filters &filters, uint limit)
47{
48 qDebug() << "GETTING FILES";
49 uint count = 0; // total count
50 uint i = 0; // count per batch
51 uint batch = 0; // batches count
52 MODEL_LIST res;
53 MODEL_LIST res_batch;
54
56 {
57 qWarning() << "FileLoader Informaer can not be nullptr";
58 return;
59 }
60
61 for (const auto &path : paths)
62 {
64 {
65 for(const auto &url : Tagging::getInstance()->getTagUrls(path.toString().replace(QStringLiteral("tags:///"), QStringLiteral("")),
66 nameFilters,
67 true,
68 limit))
69 {
70 MODEL map = FileLoader::informer(url);
71
72 if (map.isEmpty())
73 continue;
74
75 Q_EMIT itemReady(map, paths);
76 res << map;
77 res_batch << map;
78 i++;
79 count++;
80
81 if (i == m_batchCount) // send a batch
82 {
83 Q_EMIT itemsReady(res_batch, paths);
84 res_batch.clear();
85 batch++;
86 i = 0;
87 }
88
89 if (count == limit)
90 break;
91 }
92
93 continue;
94 }
95
96 if (QFileInfo(path.toLocalFile()).isDir() && path.isLocalFile() && fileExists(path))
97 {
98 QDir dir(path.toLocalFile());
99 dir.setNameFilters(nameFilters);
100 dir.setFilter(filters);
101 dir.setSorting(QDir::SortFlag::DirsFirst | QDir::SortFlag::Time);
102
104
105 while (it.hasNext())
106 {
107 const auto url = QUrl::fromLocalFile(it.next());
109
110 if (map.isEmpty())
111 continue;
112
113 Q_EMIT itemReady(map, paths);
114 res << map;
115 res_batch << map;
116 i++;
117 count++;
118
119 if (i == m_batchCount) // send a batch
120 {
121 Q_EMIT itemsReady(res_batch, paths);
122 res_batch.clear();
123 batch++;
124 i = 0;
125 }
126
127 if (count == limit)
128 break;
129 }
130 }
131
132 if (count == limit)
133 break;
134 }
135 Q_EMIT itemsReady(res_batch, paths);
136 Q_EMIT finished(res, paths);
137}
static std::function< FMH::MODEL(const QUrl &url)> informer
A callback function which structures the retrieved file URLs, with the required information.
Definition fileloader.h:116
void setBatchCount(const uint &count)
Set the amount of items to be dispatched via the itemReady signal.
void finished(FMH::MODEL_LIST items, QList< QUrl > urls)
Emitted once the operation has completely finished retrieving all the existing files or reached the l...
void requestPath(const QList< QUrl > &urls, const bool &recursive, const QStringList &nameFilters={}, const QDir::Filters &filters=QDir::Files, const uint &limit=99999)
Sends the request to start iterating throughout all the given location URLs, and with the given param...
void itemReady(FMH::MODEL item, QList< QUrl > urls)
Emitted for every single item that becomes available.
uint batchCount() const
The amount of items which will be dispatched while iterating throughout all the given directories.
void itemsReady(FMH::MODEL_LIST items, QList< QUrl > urls)
Emitted when the batch of file items is ready.
static FMStatic::PATHTYPE_KEY getPathType(const QUrl &url)
Given a file URL with a well defined scheme, get the PATHTYPE_KEY.
Definition fmstatic.cpp:646
static const FMH::MODEL getFileInfoModel(const QUrl &path)
getFileInfoModel
Definition fmstatic.cpp:560
@ TAGS_PATH
A tag location.
Definition fmstatic.h:276
static Tagging * getInstance()
Returns an instance to the tagging object.
Definition tagging.cpp:70
Q_SCRIPTABLE Q_NOREPLY void start()
bool fileExists(const QUrl &path)
QHash< MODEL_KEY, QString > MODEL
QString path(const QString &relativePath)
KIOCORE_EXPORT QString dir(const QString &fileClass)
bool isDir() const const
void clear()
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
void moveToThread(QThread *targetThread)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
void finished()
void quit()
void start(Priority priority)
bool wait(QDeadlineTimer deadline)
QUrl fromLocalFile(const QString &localFile)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:10:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.