Baloo

xattrindexer.cpp
1/*
2 SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
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
16using namespace Baloo;
17
18XAttrIndexer::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
28void XAttrIndexer::run()
29{
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 &path : m_files) {
37 auto filePath = path;
38 if (filePath.endsWith(QLatin1Char('/'))) {
39 filePath.chop(1);
40 }
41
42 QString fileName = filePath.mid(filePath.lastIndexOf(QLatin1Char('/')) + 1);
43 if (!m_config->shouldFileBeIndexed(fileName)) {
44 continue;
45 }
46
47 QString mimetype = mimeDb.mimeTypeForFile(filePath, QMimeDatabase::MatchExtension).name();
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"
A document represents an indexed file to be stored in the Baloo engine.
Definition document.h:31
void setContentIndexing(bool val)
This flag is used to signify if the file needs its contents to be indexed.
Definition document.cpp:92
Active config class which emits signals if the config was changed, for example if the KCM saved the c...
Implements storage for docIds without any associated data Instantiated for:
Definition coding.cpp:11
KIOCORE_EXPORT MimetypeJob * mimetype(const QUrl &url, JobFlags flags=DefaultFlags)
QString path(const QString &relativePath)
QStringView level(QStringView ifopt)
Q_EMITQ_EMIT
QString tr(const char *sourceText, const char *disambiguation, int n)
void chop(qsizetype n)
QString mid(qsizetype position, qsizetype n) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 14 2024 11:53:02 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.