1/*
2 SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "newfileindexer.h"
8#include "basicindexingjob.h"
9#include "fileindexerconfig.h"
10
11#include "database.h"
12#include "transaction.h"
13
14#include <QMimeDatabase>
15#include <QFileInfo>
16
17using namespace Baloo;
18
19NewFileIndexer::NewFileIndexer(Database* db, const FileIndexerConfig* config, const QStringList& newFiles)
20 : m_db(db)
21 , m_config(config)
22 , m_files(newFiles)
23{
24 Q_ASSERT(m_db);
25 Q_ASSERT(m_config);
26 Q_ASSERT(!m_files.isEmpty());
27}
28
29void NewFileIndexer::run()
30{
31 QMimeDatabase mimeDb;
32 BasicIndexingJob::IndexingLevel level = m_config->onlyBasicIndexing() ? BasicIndexingJob::NoLevel
33 : BasicIndexingJob::MarkForContentIndexing;
34
35 Transaction tr(m_db, Transaction::ReadWrite);
36
37 for (const QString &path : m_files) {
38 auto filePath = path;
39 if (filePath.endsWith(c: QLatin1Char('/'))) {
40 filePath.chop(n: 1);
41 }
42
43 QString mimetype;
44 QFileInfo fileInfo(filePath);
45
46 if (fileInfo.isSymLink()) {
47 continue;
48 }
49
50 if (fileInfo.isDir()) {
51 if (!m_config->shouldFolderBeIndexed(path: filePath)) {
52 continue;
53 }
54 mimetype = QStringLiteral("inode/directory");
55
56 } else {
57 QString fileName = filePath.mid(position: filePath.lastIndexOf(c: QLatin1Char('/')) + 1);
58 if (!m_config->shouldFileBeIndexed(fileName)) {
59 continue;
60 }
61 mimetype = mimeDb.mimeTypeForFile(fileName: filePath, mode: QMimeDatabase::MatchExtension).name();
62 }
63
64 BasicIndexingJob job(filePath, mimetype, level);
65 if (!job.index()) {
66 continue;
67 }
68
69 // The same file can be sent twice though it shouldn't be.
70 // Lets just silently ignore it instead of crashing
71 if (tr.hasDocument(id: job.document().id())) {
72 continue;
73 }
74 tr.addDocument(doc: job.document());
75 }
76
77 tr.commit();
78 Q_EMIT done();
79}
80
81#include "moc_newfileindexer.cpp"
82

source code of baloo/src/file/newfileindexer.cpp