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

0001 #include "fileloader.h"
0002 #include "fmstatic.h"
0003 #include "tagging.h"
0004 
0005 #include <QDebug>
0006 #include <QDirIterator>
0007 
0008 using namespace FMH;
0009 
0010 std::function<FMH::MODEL(const QUrl &url)> FileLoader::informer = &FMStatic::getFileInfoModel;
0011 
0012 FileLoader::FileLoader(QObject *parent) : QObject(parent)
0013   , m_thread(new QThread)
0014 {
0015     qRegisterMetaType<QDir::Filters>("QDir::Filters");
0016     qRegisterMetaType<FMH::MODEL>("FMH::MODEL");
0017     qRegisterMetaType<FMH::MODEL_LIST>("FMH::MODEL_LIST");
0018     this->moveToThread(m_thread);
0019     connect(m_thread, &QThread::finished, m_thread, &QObject::deleteLater);
0020     connect(this, &FileLoader::start, this, &FileLoader::getFiles);
0021     m_thread->start();
0022 }
0023 
0024 FileLoader::~FileLoader()
0025 {
0026     m_thread->quit();
0027     m_thread->wait();
0028 }
0029 
0030 void FileLoader::setBatchCount(const uint &count)
0031 {
0032     m_batchCount = count;
0033 }
0034 
0035 uint FileLoader::batchCount() const
0036 {
0037     return m_batchCount;
0038 }
0039 
0040 void FileLoader::requestPath(const QList<QUrl> &urls, const bool &recursive, const QStringList &nameFilters, const QDir::Filters &filters, const uint &limit)
0041 {
0042     qDebug() << "FROM file loader" << urls;
0043     Q_EMIT this->start(urls, recursive, nameFilters, filters, limit);
0044 }
0045 
0046 void FileLoader::getFiles(QList<QUrl> paths, bool recursive, const QStringList &nameFilters, const QDir::Filters &filters, uint limit)
0047 {
0048     qDebug() << "GETTING FILES";
0049     uint count = 0; // total count
0050     uint i = 0; // count per batch
0051     uint batch = 0; // batches count
0052     MODEL_LIST res;
0053     MODEL_LIST res_batch;
0054 
0055     if (!FileLoader::informer)
0056     {
0057         qWarning() << "FileLoader Informaer can not be nullptr";
0058         return;
0059     }
0060 
0061     for (const auto &path : paths)
0062     {
0063         if(FMStatic::getPathType(path) == FMStatic::PATHTYPE_KEY::TAGS_PATH)
0064         {
0065             for(const auto &url : Tagging::getInstance()->getTagUrls(path.toString().replace(QStringLiteral("tags:///"), QStringLiteral("")),
0066                                                                      nameFilters,
0067                                                                      true,
0068                                                                      limit))
0069             {               
0070                 MODEL map = FileLoader::informer(url);
0071                 
0072                 if (map.isEmpty())
0073                     continue;
0074                 
0075                 Q_EMIT itemReady(map, paths);
0076                 res << map;
0077                 res_batch << map;
0078                 i++;
0079                 count++;
0080                 
0081                 if (i == m_batchCount) // send a batch
0082                 {
0083                     Q_EMIT itemsReady(res_batch, paths);
0084                     res_batch.clear();
0085                     batch++;
0086                     i = 0;
0087                 }
0088                 
0089                 if (count == limit)
0090                     break;
0091             }
0092             
0093             continue;
0094         }
0095         
0096         if (QFileInfo(path.toLocalFile()).isDir() && path.isLocalFile() && fileExists(path))
0097         {
0098             QDir dir(path.toLocalFile());
0099             dir.setNameFilters(nameFilters);
0100             dir.setFilter(filters);
0101             dir.setSorting(QDir::SortFlag::DirsFirst | QDir::SortFlag::Time);
0102             
0103             QDirIterator it(dir, recursive ? QDirIterator::Subdirectories : QDirIterator::NoIteratorFlags);
0104 
0105             while (it.hasNext())
0106             {
0107                 const auto url = QUrl::fromLocalFile(it.next());
0108                 MODEL map = FileLoader::informer(url);
0109 
0110                 if (map.isEmpty())
0111                     continue;
0112 
0113                 Q_EMIT itemReady(map, paths);
0114                 res << map;
0115                 res_batch << map;
0116                 i++;
0117                 count++;
0118 
0119                 if (i == m_batchCount) // send a batch
0120                 {
0121                     Q_EMIT itemsReady(res_batch, paths);
0122                     res_batch.clear();
0123                     batch++;
0124                     i = 0;
0125                 }
0126 
0127                 if (count == limit)
0128                     break;
0129             }
0130         }
0131 
0132         if (count == limit)
0133             break;
0134     }
0135     Q_EMIT itemsReady(res_batch, paths);
0136     Q_EMIT finished(res, paths);
0137 }