Warning, file /maui/mauikit-filebrowsing/src/code/fileloader.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /***
0002  * Copyright (C) 2018 Camilo Higuita
0003  * This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
0004  * This is free software, and you are welcome to redistribute it
0005  * under certain conditions; type `show c' for details.
0006  * 
0007  * This program is free software: you can redistribute it and/or modify
0008  * it under the terms of the GNU General Public License as published by
0009  * the Free Software Foundation, either version 3 of the License, or
0010  * (at your option) any later version.
0011  * 
0012  * This program is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015  * GNU General Public License for more details.
0016  * 
0017  * You should have received a copy of the GNU General Public License
0018  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0019  ***/
0020 #pragma once
0021 
0022 #include <QDir>
0023 #include <QObject>
0024 #include <QThread>
0025 #include <QUrl>
0026 
0027 
0028 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0029 #include <MauiKit3/Core/fmh.h>
0030 #else
0031 #include <MauiKit4/Core/fmh.h>
0032 #endif
0033 
0034 #include "filebrowsing_export.h"
0035 
0036 namespace FMH
0037 {
0038     /**
0039      * @brief The FileLoader class asynchronously loads batches of files from a given list of local directories or tags, allowing to filter them by name, mime-types, and much more.
0040      * 
0041      * The execution of the file listing will be moved into a different thread, - so to retrieve the information you will depend on the exposed signals and the `informer` callback function to give structure to the data. 
0042      * @see informer
0043      * 
0044      * The following code snippet demonstrates the usage, by listing all the content in the downloads and pictures local directories.
0045      * 
0046      * @code
0047      * #include <QCoreApplication>
0048      * #include <QDebug>
0049      * #include <QUrl>
0050      * #include <QObject>
0051      * #include <iostream>
0052      * 
0053      * #include <MauiKit4/FileBrowsing/fileloader.h>
0054      * #include <MauiKit4/FileBrowsing/fmstatic.h>
0055      * 
0056      * int main(int argc, char *argv[])
0057      * {
0058      *    QCoreApplication a(argc, argv);
0059      * 
0060      *    FMH::FileLoader loader;
0061      *    QStringList urls = {FMStatic::DownloadsPath, FMStatic::PicturesPath};
0062      * 
0063      *    QObject::connect(&loader, &FMH::FileLoader::itemsReady, [=](FMH::MODEL_LIST items, QList<QUrl> urls) {
0064      *                         for(const auto &item : items)
0065      *                             qDebug() << item[FMH::MODEL_KEY::NAME];
0066      * 
0067      *                         qDebug() << "items ready for:" << urls << items.length(); });
0068      * 
0069      *    QObject::connect(&loader, &FMH::FileLoader::finished, [=](FMH::MODEL_LIST items, QList<QUrl> urls) {
0070      *          qDebug() << "Finished process" << urls << items.length(); });
0071      * 
0072      *    loader.setBatchCount(10);
0073      *    loader.requestPath(QUrl::fromStringList(urls), true);
0074      * 
0075      *    return a.exec();
0076      * }
0077      * @endcode
0078      * 
0079      */
0080     class FILEBROWSING_EXPORT FileLoader : public QObject
0081     {
0082         Q_OBJECT
0083         
0084     public:
0085         
0086         /**
0087          * @brief Creates a new instance, the execution will be moved to a different thread.
0088          */
0089         FileLoader(QObject *parent = nullptr);
0090         ~FileLoader();
0091         
0092         /**
0093          * @brief Set the amount of items to be dispatched via the `itemReady` signal.
0094          * This allows to dispatch item files which are ready and don't have to wait for the operation to finished completely, in case there are too many files to wait for.
0095          * @see itemsReady
0096          * @param count the amount of items
0097          */
0098         void setBatchCount(const uint &count);
0099         
0100         /**
0101          * @brief The amount of items which will be dispatched while iterating throughout all the given directories.
0102          */
0103         uint batchCount() const;
0104         
0105         /**
0106          * @brief Sends the request to start iterating throughout all the given location URLs, and with the given parameters. 
0107          * @param urls the list of directories or locations to iterate. This operation only supports local directories and tags.
0108          * @param recursive Whether the iteration should be done recursively and navigate sub-folder structures
0109          * @param nameFilters a list of filtering strings, this can be used with regular expressions, for example `*.jpg` to only list files ending with the given suffix. Dy default this is set to an empty array, so nothing will be filtered.
0110          * @param filters the possible QDir filters. By default this is set to `QDir::Files`.
0111          * @param limit the limit of files to retrieve. By default this is is set to `99999`.
0112          */
0113         void requestPath(const QList<QUrl> &urls, const bool &recursive, const QStringList &nameFilters = {}, const QDir::Filters &filters = QDir::Files, const uint &limit = 99999);
0114         
0115         /**
0116          * @brief A callback function which structures the retrieved file URLs, with the required information. This callback function will receive the file URL, and expects a FMH::MODEL to be formed and returned. By default this informer callback function is set to `FMStatic::getFileInfoModel`, which retrieves basic information about a file.
0117          * @see FMStatic::getFileInfoModel
0118          * @param url the file URL retrieved
0119          * @return the formed data model based on the given file URL
0120          */
0121         static std::function<FMH::MODEL(const QUrl &url)> informer;
0122         
0123     private Q_SLOTS:
0124         /**
0125          * @private
0126          */
0127         void getFiles(QList<QUrl> paths, bool recursive, const QStringList &nameFilters, const QDir::Filters &filters, uint limit);
0128         
0129     Q_SIGNALS:
0130         /**
0131          * @brief Emitted once the operation has completely finished retrieving all the existing files or reached the limit number of requested files.
0132          * @param items all of the retrieved items. The items data model is created using the `informer` callback function. 
0133          * @see informer
0134          * @param urls the list of directories given for which the item were retrieved.
0135          */
0136         void finished(FMH::MODEL_LIST items, QList<QUrl> urls);
0137         
0138         /**
0139          * @private
0140          */
0141         void start(QList<QUrl> urls, bool recursive, QStringList nameFilters, QDir::Filters filters, uint limit);
0142         
0143         /**
0144          * @brief Emitted when the batch of file items is ready.
0145          * @see setBatchCount
0146          * @param items the packaged list of items, formed by the `informer` callback function.
0147          * @param urls the list of directories given for which the item were retrieved.
0148          */
0149         void itemsReady(FMH::MODEL_LIST items, QList<QUrl> urls);
0150         
0151         /**
0152          * @brief Emitted for every single item that becomes available.
0153          * @param item the packaged item, formed by the `informer` callback function.
0154          * @param urls the list of directories given for which the item were retrieved.
0155          */
0156         void itemReady(FMH::MODEL item, QList<QUrl> urls);
0157         
0158     private:
0159         QThread *m_thread;
0160         uint m_batchCount = 1500;
0161     };
0162 }
0163