1/*
2 SPDX-FileCopyrightText: 2008-2009 Sebastian Trueg <trueg@kde.org>
3 SPDX-FileCopyrightText: 2012-2014 Vishesh Handa <me@vhanda.in>
4 SPDX-FileCopyrightText: 2020 Benjamin Port <benjamin.port@enioka.com>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#ifndef BALOO_FILEINDEXER_CONFIG_H_
10#define BALOO_FILEINDEXER_CONFIG_H_
11
12#include <QObject>
13#include <QList>
14#include <QSet>
15#include <QDebug>
16
17#include "regexpcache.h"
18
19class BalooSettings;
20
21namespace Baloo
22{
23
24class StorageDevices;
25
26/**
27 * Active config class which emits signals if the config
28 * was changed, for example if the KCM saved the config file.
29 */
30class FileIndexerConfig : public QObject
31{
32 Q_OBJECT
33
34public:
35
36 explicit FileIndexerConfig(QObject* parent = nullptr);
37 ~FileIndexerConfig() override;
38
39 /**
40 * Folders to search for files to index and analyze.
41 * \return list of paths.
42 */
43 QStringList includeFolders() const;
44
45 /**
46 * Folders that are excluded from indexing.
47 * (Descendant folders of an excluded folder can be added
48 * and they will be indexed.)
49 * \return list of paths.
50 */
51 QStringList excludeFolders() const;
52
53 QStringList excludeFilters() const;
54
55 QStringList excludeMimetypes() const;
56
57 bool indexHiddenFilesAndFolders() const;
58
59 bool onlyBasicIndexing() const;
60
61 /**
62 * Check if \p folder can be searched.
63 * \p folder can be searched if itself or one of its descendants is indexed.
64 *
65 * Example:
66 * if ~/foo is not indexed and ~/foo/bar is indexed
67 * then ~/foo can be searched.
68 *
69 * \return \c true if the \p folder can be searched.
70 */
71 bool canBeSearched(const QString& folder) const;
72
73 /**
74 * Check if file or folder \p path should be indexed taking into account
75 * the includeFolders(), the excludeFolders(), and the excludeFilters().
76 * Inclusion takes precedence.
77 *
78 * Be aware that this method does not check if parent dirs
79 * match any of the exclude filters. Only the path of the
80 * dir itself it checked.
81 *
82 * \return \c true if the file or folder at \p path should
83 * be indexed according to the configuration.
84 */
85 bool shouldBeIndexed(const QString& path) const;
86
87 /**
88 * Check if the folder at \p path should be indexed.
89 *
90 * Be aware that this method does not check if parent dirs
91 * match any of the exclude filters. Only the name of the
92 * dir itself it checked.
93 *
94 * \return \c true if the folder at \p path should
95 * be indexed according to the configuration.
96 */
97 bool shouldFolderBeIndexed(const QString& path) const;
98
99 /**
100 * Check \p fileName for all exclude filters. This does
101 * not take file paths into account.
102 *
103 * \return \c true if a file with name \p filename should
104 * be indexed according to the configuration.
105 */
106 bool shouldFileBeIndexed(const QString& fileName) const;
107
108 /**
109 * Checks if \p mimeType should be indexed
110 *
111 * \return \c true if the mimetype should be indexed according
112 * to the configuration
113 */
114 bool shouldMimeTypeBeIndexed(const QString& mimeType) const;
115
116 /**
117 * Check if \p path is in the list of folders to be indexed taking
118 * include and exclude folders into account.
119 * \p folder is set to the folder which was the reason for the decision.
120 */
121 bool folderInFolderList(const QString& path, QString& folder) const;
122
123 /**
124 * Returns the internal version number of the Baloo database
125 */
126 int databaseVersion() const;
127 void setDatabaseVersion(int version);
128
129 bool indexingEnabled() const;
130
131 /**
132 * Returns batch size
133 */
134 uint maxUncomittedFiles() const;
135
136public Q_SLOTS:
137 /**
138 * Reread the config from disk and update the configuration cache.
139 * This is only required for testing as normally the config updates
140 * itself whenever the config file on disk changes.
141 *
142 * \return \c true if the config has actually changed
143 */
144 void forceConfigUpdate();
145
146private:
147
148 void buildFolderCache();
149 void buildExcludeFilterRegExpCache();
150 void buildMimeTypeCache();
151
152 BalooSettings *m_settings;
153
154 struct FolderConfig
155 {
156 QString path;
157 bool isIncluded;
158
159 /// Sort by path length, and on ties lexicographically.
160 /// Longest path first
161 bool operator<(const FolderConfig& other) const;
162 };
163
164 class FolderCache : public std::vector<FolderConfig>
165 {
166 public:
167 void cleanup();
168
169 bool addFolderConfig(const FolderConfig&);
170 };
171 friend QDebug operator<<(QDebug dbg, const FolderConfig& config);
172
173 /// Caching cleaned up list (no duplicates, no non-default entries, etc.)
174 FolderCache m_folderCache;
175 /// Whether the folder cache needs to be rebuilt the next time it is used
176 bool m_folderCacheDirty;
177
178 /// cache of regexp objects for all exclude filters
179 /// to prevent regexp parsing over and over
180 RegExpCache m_excludeFilterRegExpCache;
181
182 /// A set of mimetypes which should never be indexed
183 QSet<QString> m_excludeMimetypes;
184
185 bool m_indexHidden;
186 bool m_onlyBasicIndexing;
187
188 StorageDevices* m_devices;
189
190 const uint m_maxUncomittedFiles;
191};
192
193QDebug operator<<(QDebug dbg, const FileIndexerConfig::FolderCache::value_type&);
194
195}
196
197#endif
198

source code of baloo/src/file/fileindexerconfig.h