Baloo

xattrindexer.cpp
1 /*
2  SPDX-FileCopyrightText: 2015 Vishesh Handa <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6 
7 #include "xattrindexer.h"
8 #include "basicindexingjob.h"
9 #include "fileindexerconfig.h"
10 
11 #include "database.h"
12 #include "transaction.h"
13 
14 #include <QMimeDatabase>
15 
16 using namespace Baloo;
17 
18 XAttrIndexer::XAttrIndexer(Database* db, const FileIndexerConfig* config, const QStringList& files)
19  : m_db(db)
20  , m_config(config)
21  , m_files(files)
22 {
23  Q_ASSERT(m_db);
24  Q_ASSERT(m_config);
25  Q_ASSERT(!m_files.isEmpty());
26 }
27 
28 void XAttrIndexer::run()
29 {
30  QMimeDatabase mimeDb;
31  BasicIndexingJob::IndexingLevel level = m_config->onlyBasicIndexing() ? BasicIndexingJob::NoLevel
32  : BasicIndexingJob::MarkForContentIndexing;
33 
34  Transaction tr(m_db, Transaction::ReadWrite);
35 
36  for (const QString& filePath : std::as_const(m_files)) {
37  Q_ASSERT(!filePath.endsWith(QLatin1Char('/')));
38 
39  QString fileName = filePath.mid(filePath.lastIndexOf(QLatin1Char('/')) + 1);
40  if (!m_config->shouldFileBeIndexed(fileName)) {
41  continue;
42  }
43 
45  if (!m_config->shouldMimeTypeBeIndexed(mimetype)) {
46  continue;
47  }
48 
49  // FIXME: The BasicIndexingJob extracts too much info. We only need the xattr
50  BasicIndexingJob job(filePath, mimetype, BasicIndexingJob::NoLevel);
51  if (!job.index()) {
52  continue;
53  }
54 
55  // FIXME: This slightly defeats the point of having separate indexers
56  // But we can get xattr changes of a file, even when it doesn't exist
57  // cause we missed its creation somehow
58  Baloo::Document doc = job.document();
59  if (!tr.hasDocument(doc.id())) {
60  doc.setContentIndexing(level == BasicIndexingJob::MarkForContentIndexing);
61  tr.addDocument(doc);
62  continue;
63  }
64 
65  tr.replaceDocument(doc, XAttrTerms | DocumentTime | FileNameTerms | DocumentUrl);
66  }
67 
68  tr.commit();
69  Q_EMIT done();
70 }
71 
72 #include "moc_xattrindexer.cpp"
QStringView level(QStringView ifopt)
void setContentIndexing(bool val)
This flag is used to signify if the file needs its contents to be indexed.
Definition: document.cpp:92
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 mid(int position, int n) const const
A document represents an indexed file to be stored in the Baloo engine.
Definition: document.h:30
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.