Baloo

filtereddiriterator.cpp
1/*
2 SPDX-FileCopyrightText: 2014 Vishesh Handa <me@vhanda.in>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "filtereddiriterator.h"
8#include "fileindexerconfig.h"
9
10using namespace Baloo;
11
12FilteredDirIterator::FilteredDirIterator(const FileIndexerConfig* config, const QString& folder, Filter filter)
13 : m_config(config)
14 , m_currentIter(nullptr)
15 , m_filters(QDir::NoDotAndDotDot | QDir::Readable | QDir::NoSymLinks | QDir::Hidden)
16 , m_firstItem(false)
17{
18 if (filter == DirsOnly) {
19 m_filters |= QDir::Dirs;
20 } else if (filter == FilesAndDirs) {
21 m_filters |= (QDir::Files | QDir::Dirs);
22 }
23
24 if (!m_config || m_config->shouldFolderBeIndexed(folder)) {
25 m_currentIter = new QDirIterator(folder, m_filters);
26 m_firstItem = true;
27 }
28}
29
30FilteredDirIterator::~FilteredDirIterator()
31{
32 delete m_currentIter;
33}
34
35QString FilteredDirIterator::next()
36{
37 if (m_firstItem) {
38 m_firstItem = false;
39 m_filePath = m_currentIter->path();
40 m_fileInfo = QFileInfo(m_filePath);
41 return m_filePath;
42 }
43
44 m_filePath.clear();
45 if (!m_currentIter) {
46 m_fileInfo = QFileInfo();
47 return QString();
48 }
49
50 bool shouldIndexHidden = false;
51 if (m_config) {
52 shouldIndexHidden = m_config->indexHiddenFilesAndFolders();
53 }
54
55 while (true) {
56 // Last entry in the current directory found, try the next
57 // directory from the stack
58 // Loop until the directory is non-empty, or the stack is empty
59 while (!m_currentIter->hasNext()) {
60 delete m_currentIter;
61 m_currentIter = nullptr;
62
63 if (m_paths.isEmpty()) {
64 m_fileInfo = QFileInfo();
65 return QString();
66 }
67
68 const QString path = m_paths.pop();
69 m_currentIter = new QDirIterator(path, m_filters);
70 }
71
72 m_filePath = m_currentIter->next();
73 m_fileInfo = m_currentIter->fileInfo();
74
75 auto shouldIndexFolder = [&] () -> bool {
76 if (!m_config) {
77 return !m_fileInfo.isHidden();
78 }
79 QString folder;
80 if (!m_config->folderInFolderList(m_filePath, folder)) {
81 return false;
82 }
83 if ((folder == m_filePath) || (folder == QString(m_filePath).append(QLatin1Char('/')))) {
84 return true;
85 }
86 if (!shouldIndexHidden && m_fileInfo.isHidden()) {
87 return false;
88 }
89 return m_config->shouldFileBeIndexed(m_fileInfo.fileName());
90 };
91
92 if (m_fileInfo.isDir()) {
93 if (shouldIndexFolder()) {
94 // Push the current item to the directory stack
95 m_paths.push(m_filePath);
96 return m_filePath;
97 }
98 }
99 else if (m_fileInfo.isFile()) {
100 if ((!shouldIndexHidden) && m_fileInfo.isHidden()) {
101 continue;
102 }
103 if ((!m_config) || m_config->shouldFileBeIndexed(m_fileInfo.fileName())) {
104 return m_filePath;
105 }
106 }
107 }
108}
109
110QString FilteredDirIterator::filePath() const
111{
112 return m_filePath;
113}
114
115QFileInfo FilteredDirIterator::fileInfo() const
116{
117 return m_fileInfo;
118}
Active config class which emits signals if the config was changed, for example if the KCM saved the c...
bool folderInFolderList(const QString &path, QString &folder) const
Check if path is in the list of folders to be indexed taking include and exclude folders into account...
bool shouldFileBeIndexed(const QString &fileName) const
Check fileName for all exclude filters.
Implements storage for docIds without any associated data Instantiated for:
Definition coding.cpp:11
QString path(const QString &relativePath)
QFileInfo fileInfo() const const
bool hasNext() const const
QString next()
QString path() const const
QString fileName() const const
bool isDir() const const
bool isFile() const const
bool isHidden() const const
void push(const T &t)
void clear()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.