File indexing completed on 2024-04-21 03:51:45

0001 /*
0002     SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "newfileindexer.h"
0008 #include "basicindexingjob.h"
0009 #include "fileindexerconfig.h"
0010 
0011 #include "database.h"
0012 #include "transaction.h"
0013 
0014 #include <QMimeDatabase>
0015 #include <QFileInfo>
0016 
0017 using namespace Baloo;
0018 
0019 NewFileIndexer::NewFileIndexer(Database* db, const FileIndexerConfig* config, const QStringList& newFiles)
0020     : m_db(db)
0021     , m_config(config)
0022     , m_files(newFiles)
0023 {
0024     Q_ASSERT(m_db);
0025     Q_ASSERT(m_config);
0026     Q_ASSERT(!m_files.isEmpty());
0027 }
0028 
0029 void NewFileIndexer::run()
0030 {
0031     QMimeDatabase mimeDb;
0032     BasicIndexingJob::IndexingLevel level = m_config->onlyBasicIndexing() ? BasicIndexingJob::NoLevel
0033         : BasicIndexingJob::MarkForContentIndexing;
0034 
0035     Transaction tr(m_db, Transaction::ReadWrite);
0036 
0037     for (const QString& filePath : std::as_const(m_files)) {
0038         Q_ASSERT(!filePath.endsWith(QLatin1Char('/')));
0039 
0040         QString mimetype;
0041         QFileInfo fileInfo(filePath);
0042 
0043         if (fileInfo.isSymLink()) {
0044             continue;
0045         }
0046 
0047         if (fileInfo.isDir()) {
0048             if (!m_config->shouldFolderBeIndexed(filePath)) {
0049                 continue;
0050             }
0051             mimetype = QStringLiteral("inode/directory");
0052 
0053         } else {
0054             QString fileName = filePath.mid(filePath.lastIndexOf(QLatin1Char('/')) + 1);
0055             if (!m_config->shouldFileBeIndexed(fileName)) {
0056                 continue;
0057             }
0058             mimetype = mimeDb.mimeTypeForFile(filePath, QMimeDatabase::MatchExtension).name();
0059         }
0060 
0061         BasicIndexingJob job(filePath, mimetype, level);
0062         if (!job.index()) {
0063             continue;
0064         }
0065 
0066         // The same file can be sent twice though it shouldn't be.
0067         // Lets just silently ignore it instead of crashing
0068         if (tr.hasDocument(job.document().id())) {
0069             continue;
0070         }
0071         tr.addDocument(job.document());
0072     }
0073 
0074     tr.commit();
0075     Q_EMIT done();
0076 }
0077 
0078 #include "moc_newfileindexer.cpp"