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 | |
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 &path : m_files) { |
37 | auto filePath = path; |
38 | if (filePath.endsWith(c: QLatin1Char('/'))) { |
39 | filePath.chop(n: 1); |
40 | } |
41 | |
42 | QString fileName = filePath.mid(position: filePath.lastIndexOf(c: QLatin1Char('/')) + 1); |
43 | if (!m_config->shouldFileBeIndexed(fileName)) { |
44 | continue; |
45 | } |
46 | |
47 | QString mimetype = mimeDb.mimeTypeForFile(fileName: filePath, mode: 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(id: doc.id())) { |
60 | doc.setContentIndexing(level == BasicIndexingJob::MarkForContentIndexing); |
61 | tr.addDocument(doc); |
62 | continue; |
63 | } |
64 | |
65 | tr.replaceDocument(doc, operations: XAttrTerms | DocumentTime | FileNameTerms | DocumentUrl); |
66 | } |
67 | |
68 | tr.commit(); |
69 | Q_EMIT done(); |
70 | } |
71 | |
72 | #include "moc_xattrindexer.cpp" |
73 | |