File indexing completed on 2024-04-21 05:51:48

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2005 Dominik Seichter <domseichter@web.de>
0003 
0004 #include "threadedlister.h"
0005 
0006 #include "krenamemodel.h"
0007 
0008 #include <kio/job.h>
0009 #include <kio/jobclasses.h>
0010 
0011 #include <QMutex>
0012 #include <QRegExp>
0013 
0014 QMutex ThreadedLister::s_mutex;
0015 
0016 ThreadedLister::ThreadedLister(const QUrl &dirname, QWidget *cache, KRenameModel *model)
0017     : QObject(nullptr), m_dirname(dirname), m_cache(cache), m_model(model)
0018 {
0019     m_listHiddenFiles  = false;
0020     m_listRecursive    = false;
0021     m_listDirnamesOnly = false;
0022     m_listDirnames     = false;
0023     m_eSplitMode       = m_model->splitMode();
0024     m_dot              = m_model->splitDot();
0025 
0026     qRegisterMetaType<KFileItemList>("KFileItemList");
0027 }
0028 
0029 ThreadedLister::~ThreadedLister()
0030 {
0031 }
0032 
0033 void ThreadedLister::run()
0034 {
0035     s_mutex.lock();
0036     if (m_listDirnames || m_listDirnamesOnly) {
0037         QString name = m_dirname.fileName();
0038         if (!m_listHiddenFiles && !name.startsWith(QLatin1Char('.'))) {
0039             KRenameFile::List list;
0040             list.append(KRenameFile(m_dirname, true, m_eSplitMode, m_dot));
0041 
0042             m_model->addFiles(list);
0043         }
0044     }
0045     s_mutex.unlock();
0046 
0047     KIO::ListJob *job   = nullptr; // Will delete itself automatically
0048     KIO::JobFlags flags = KIO::HideProgressInfo;
0049     if (m_listRecursive) {
0050         job = KIO::listRecursive(m_dirname, flags, m_listHiddenFiles);
0051     } else {
0052         job = KIO::listDir(m_dirname, flags, m_listHiddenFiles);
0053     }
0054 
0055     connect(job, &KIO::ListJob::entries, this, &ThreadedLister::foundItem);
0056     connect(job, &KIO::ListJob::result, this, &ThreadedLister::completed);
0057 
0058     job->start();
0059 }
0060 
0061 void ThreadedLister::foundItem(KIO::Job *, const KIO::UDSEntryList &list)
0062 {
0063     QString displayName;
0064     QRegExp filter(m_filter);
0065     filter.setPatternSyntax(QRegExp::Wildcard);
0066 
0067     m_files.reserve(m_files.count() + list.count());
0068 
0069     KIO::UDSEntryList::const_iterator it = list.begin();
0070     while (it != list.end()) {
0071         displayName = (*it).stringValue(KIO::UDSEntry::UDS_NAME);
0072         if (!filter.isEmpty() && !filter.exactMatch(displayName)) {
0073             // does not match filter
0074             // skip it
0075             ++it;
0076         } else {
0077             QUrl url = m_dirname;
0078             url = url.adjusted(QUrl::StripTrailingSlash);
0079             url.setPath(url.path() + '/' + (displayName));
0080 
0081             if ((m_listDirnames || m_listDirnamesOnly) && (*it).isDir()) {
0082                 // Filter out parent and current directory
0083                 if (displayName != "." && displayName != "..") {
0084                     m_files.append(KRenameFile(KFileItem(*it, url), m_eSplitMode, m_dot));
0085                 }
0086             } else if (!m_listDirnamesOnly && !(*it).isDir()) {
0087                 m_files.append(KRenameFile(KFileItem(*it, url), m_eSplitMode, m_dot));
0088             }
0089 
0090             ++it;
0091         }
0092     }
0093 }
0094 
0095 void ThreadedLister::completed()
0096 {
0097     if (m_files.count() > 0) {
0098         // We add the files in the completed slot
0099         // and not directly in the foundItem slot,
0100         // as the latter can produce deadlocks if
0101         // we get a signal while we keep the mutex!
0102         //s_mutex.lock();
0103         m_model->addFiles(m_files);
0104         //s_mutex.unlock();
0105     }
0106 
0107     Q_EMIT listerDone(this);
0108 }
0109 
0110 #include "moc_threadedlister.cpp"