File indexing completed on 2024-04-14 03:49:47

0001 /*
0002     SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "xattrindexer.h"
0008 #include "basicindexingjob.h"
0009 #include "fileindexerconfig.h"
0010 
0011 #include "database.h"
0012 #include "transaction.h"
0013 
0014 #include <QMimeDatabase>
0015 
0016 using namespace Baloo;
0017 
0018 XAttrIndexer::XAttrIndexer(Database* db, const FileIndexerConfig* config, const QStringList& files)
0019     : m_db(db)
0020     , m_config(config)
0021     , m_files(files)
0022 {
0023     Q_ASSERT(m_db);
0024     Q_ASSERT(m_config);
0025     Q_ASSERT(!m_files.isEmpty());
0026 }
0027 
0028 void XAttrIndexer::run()
0029 {
0030     QMimeDatabase mimeDb;
0031     BasicIndexingJob::IndexingLevel level = m_config->onlyBasicIndexing() ? BasicIndexingJob::NoLevel
0032         : BasicIndexingJob::MarkForContentIndexing;
0033 
0034     Transaction tr(m_db, Transaction::ReadWrite);
0035 
0036     for (const QString& filePath : std::as_const(m_files)) {
0037         Q_ASSERT(!filePath.endsWith(QLatin1Char('/')));
0038 
0039         QString fileName = filePath.mid(filePath.lastIndexOf(QLatin1Char('/')) + 1);
0040         if (!m_config->shouldFileBeIndexed(fileName)) {
0041             continue;
0042         }
0043 
0044         QString mimetype = mimeDb.mimeTypeForFile(filePath, QMimeDatabase::MatchExtension).name();
0045 
0046         // FIXME: The BasicIndexingJob extracts too much info. We only need the xattr
0047         BasicIndexingJob job(filePath, mimetype, BasicIndexingJob::NoLevel);
0048         if (!job.index()) {
0049             continue;
0050         }
0051 
0052         // FIXME: This slightly defeats the point of having separate indexers
0053         //        But we can get xattr changes of a file, even when it doesn't exist
0054         //        cause we missed its creation somehow
0055         Baloo::Document doc = job.document();
0056         if (!tr.hasDocument(doc.id())) {
0057             doc.setContentIndexing(level == BasicIndexingJob::MarkForContentIndexing);
0058             tr.addDocument(doc);
0059             continue;
0060         }
0061 
0062         tr.replaceDocument(doc, XAttrTerms | DocumentTime | FileNameTerms | DocumentUrl);
0063     }
0064 
0065     tr.commit();
0066     Q_EMIT done();
0067 }
0068 
0069 #include "moc_xattrindexer.cpp"