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

0001 /*
0002     SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #ifndef BALOO_FILEINDEXSCHEDULER_H
0008 #define BALOO_FILEINDEXSCHEDULER_H
0009 
0010 #include <QObject>
0011 #include <QStringList>
0012 #include <QThreadPool>
0013 #include <QTimer>
0014 
0015 #include "filecontentindexerprovider.h"
0016 #include "powerstatemonitor.h"
0017 #include "indexerstate.h"
0018 #include "timeestimator.h"
0019 
0020 namespace Baloo {
0021 
0022 class Database;
0023 class FileIndexerConfig;
0024 class FileContentIndexer;
0025 
0026 class FileIndexScheduler : public QObject
0027 {
0028     Q_OBJECT
0029     Q_CLASSINFO("D-Bus Interface", "org.kde.baloo.scheduler")
0030 
0031     Q_PROPERTY(int state READ state NOTIFY stateChanged)
0032 public:
0033     FileIndexScheduler(Database* db, FileIndexerConfig* config, bool firstRun, QObject* parent = nullptr);
0034     ~FileIndexScheduler() override;
0035     int state() const { return m_indexerState; }
0036 
0037 Q_SIGNALS:
0038     Q_SCRIPTABLE void stateChanged(int state);
0039 
0040 public Q_SLOTS:
0041     void indexNewFile(const QString& file) {
0042         if (!m_newFiles.contains(file)) {
0043             m_newFiles << file;
0044             if (isIndexerIdle()) {
0045                 QTimer::singleShot(0, this, &FileIndexScheduler::scheduleIndexing);
0046             }
0047         }
0048     }
0049 
0050     void indexModifiedFile(const QString& file) {
0051         if (!m_modifiedFiles.contains(file)) {
0052             m_modifiedFiles << file;
0053             if (isIndexerIdle()) {
0054                 QTimer::singleShot(0, this, &FileIndexScheduler::scheduleIndexing);
0055             }
0056         }
0057     }
0058 
0059     void indexXAttrFile(const QString& file) {
0060         if (!m_xattrFiles.contains(file)) {
0061             m_xattrFiles << file;
0062             if (isIndexerIdle()) {
0063                 QTimer::singleShot(0, this, &FileIndexScheduler::scheduleIndexing);
0064             }
0065         }
0066     }
0067 
0068     void runnerFinished() {
0069         m_isGoingIdle = true;
0070         QTimer::singleShot(0, this, &FileIndexScheduler::scheduleIndexing);
0071     }
0072 
0073     void handleFileRemoved(const QString& file);
0074 
0075     void updateConfig();
0076     void scheduleIndexing();
0077     void scheduleCheckUnindexedFiles();
0078     void scheduleCheckStaleIndexEntries();
0079     void startupFinished();
0080 
0081     Q_SCRIPTABLE void suspend() { setSuspend(true); }
0082     Q_SCRIPTABLE void resume() { setSuspend(false); }
0083     Q_SCRIPTABLE uint getRemainingTime();
0084     Q_SCRIPTABLE void checkUnindexedFiles();
0085     Q_SCRIPTABLE void checkStaleIndexEntries();
0086     Q_SCRIPTABLE uint getBatchSize();
0087 
0088 private Q_SLOTS:
0089     void powerManagementStatusChanged(bool isOnBattery);
0090 
0091 private:
0092     void setSuspend(bool suspend);
0093     bool isIndexerIdle() {
0094         return m_isGoingIdle ||
0095                (m_indexerState == Suspended) ||
0096                (m_indexerState == Startup) ||
0097                (m_indexerState == Idle) ||
0098                (m_indexerState == LowPowerIdle);
0099     }
0100 
0101     Database* m_db;
0102     FileIndexerConfig* m_config;
0103 
0104     QStringList m_newFiles;
0105     QStringList m_modifiedFiles;
0106     QStringList m_xattrFiles;
0107 
0108     QThreadPool m_threadPool;
0109 
0110     FileContentIndexerProvider m_provider;
0111     FileContentIndexer* m_contentIndexer;
0112 
0113     PowerStateMonitor m_powerMonitor;
0114 
0115     IndexerState m_indexerState;
0116     TimeEstimator m_timeEstimator;
0117     uint m_indexPendingFiles = 0;
0118     uint m_indexFinishedFiles = 0;
0119 
0120     bool m_checkUnindexedFiles;
0121     bool m_checkStaleIndexEntries;
0122     bool m_isGoingIdle;
0123     bool m_isSuspended;
0124     bool m_isFirstRun;
0125     bool m_inStartup;
0126 };
0127 
0128 }
0129 
0130 #endif // BALOO_FILEINDEXSCHEDULER_H