Baloo

indexcleaner.cpp
1 /*
2  SPDX-FileCopyrightText: 2015 Vishesh Handa <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6 
7 #include "indexcleaner.h"
8 #include "fileindexerconfig.h"
9 
10 #include "database.h"
11 #include "transaction.h"
12 #include "idutils.h"
13 
14 #include "baloodebug.h"
15 
16 #include <QFile>
17 #include <QMimeDatabase>
18 
19 using namespace Baloo;
20 
21 IndexCleaner::IndexCleaner(Database* db, FileIndexerConfig* config)
22  : m_db(db)
23  , m_config(config)
24 {
25  Q_ASSERT(db);
26  Q_ASSERT(config);
27 }
28 
29 void IndexCleaner::run()
30 {
31  QMimeDatabase mimeDb;
32 
33  Transaction tr(m_db, Transaction::ReadWrite);
34 
35  auto shouldDelete = [&](quint64 id) {
36  if (!id) {
37  return false;
38  }
39 
40  const QString url = QFile::decodeName(tr.documentUrl(id));
41 
42  if (!QFile::exists(url)) {
43  qCDebug(BALOO) << "not exists: " << url;
44  return true;
45  }
46 
47  if (!m_config->shouldBeIndexed(url)) {
48  qCDebug(BALOO) << "should not be indexed: " << url;
49  return true;
50  }
51 
52  // FIXME: This mimetype is not completely accurate!
54  if (!m_config->shouldMimeTypeBeIndexed(mimetype)) {
55  qCDebug(BALOO) << "mimetype should not be indexed: " << url << mimetype;
56  return true;
57  }
58 
59  return false;
60  };
61 
62  const auto includeFolders = m_config->includeFolders();
63  for (const QString& folder : includeFolders) {
64  quint64 id = filePathToId(QFile::encodeName(folder));
65  if (id > 0) {
66  tr.removeRecursively(id, shouldDelete);
67  }
68  }
69  const auto excludeFolders = m_config->excludeFolders();
70  for (const QString& folder : excludeFolders) {
71  quint64 id = filePathToId(QFile::encodeName(folder));
72  if (id > 0 && tr.hasDocument(id)) {
73  tr.removeRecursively(id, shouldDelete);
74  }
75  }
76  tr.commit();
77 
78  Q_EMIT done();
79 }
80 
81 #include "moc_indexcleaner.cpp"
QByteArray encodeName(const QString &fileName)
bool exists() const const
KIOCORE_EXPORT MimetypeJob * mimetype(const QUrl &url, JobFlags flags=DefaultFlags)
Implements storage for docIds without any associated data Instantiated for:
Definition: coding.cpp:11
Active config class which emits signals if the config was changed, for example if the KCM saved the c...
KSharedConfigPtr config()
QMimeType mimeTypeForFile(const QString &fileName, QMimeDatabase::MatchMode mode) const const
QString decodeName(const QByteArray &localFileName)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Nov 29 2023 03:56:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.