File indexing completed on 2024-04-14 15:51:36

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