1/*
2 SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#ifndef BALOO_FILEINDEXSCHEDULER_H
8#define BALOO_FILEINDEXSCHEDULER_H
9
10#include <QObject>
11#include <QStringList>
12#include <QThreadPool>
13#include <QTimer>
14
15#include "filecontentindexerprovider.h"
16#include "powerstatemonitor.h"
17#include "indexerstate.h"
18#include "timeestimator.h"
19
20namespace Baloo {
21
22class Database;
23class FileIndexerConfig;
24class FileContentIndexer;
25
26class FileIndexScheduler : public QObject
27{
28 Q_OBJECT
29 Q_CLASSINFO("D-Bus Interface", "org.kde.baloo.scheduler")
30
31 Q_PROPERTY(int state READ state NOTIFY stateChanged)
32public:
33 FileIndexScheduler(Database* db, FileIndexerConfig* config, bool firstRun, QObject* parent = nullptr);
34 ~FileIndexScheduler() override;
35 int state() const { return m_indexerState; }
36
37Q_SIGNALS:
38 Q_SCRIPTABLE void stateChanged(int state);
39
40public Q_SLOTS:
41 void indexNewFile(const QString& file) {
42 if (!m_newFiles.contains(str: file)) {
43 m_newFiles << file;
44 if (isIndexerIdle()) {
45 QTimer::singleShot(interval: 0, receiver: this, slot: &FileIndexScheduler::scheduleIndexing);
46 }
47 }
48 }
49
50 void indexModifiedFile(const QString& file) {
51 if (!m_modifiedFiles.contains(str: file)) {
52 m_modifiedFiles << file;
53 if (isIndexerIdle()) {
54 QTimer::singleShot(interval: 0, receiver: this, slot: &FileIndexScheduler::scheduleIndexing);
55 }
56 }
57 }
58
59 void indexXAttrFile(const QString& file) {
60 if (!m_xattrFiles.contains(str: file)) {
61 m_xattrFiles << file;
62 if (isIndexerIdle()) {
63 QTimer::singleShot(interval: 0, receiver: this, slot: &FileIndexScheduler::scheduleIndexing);
64 }
65 }
66 }
67
68 void runnerFinished() {
69 m_isGoingIdle = true;
70 QTimer::singleShot(interval: 0, receiver: this, slot: &FileIndexScheduler::scheduleIndexing);
71 }
72
73 void handleFileRemoved(const QString& file);
74
75 void updateConfig();
76 void scheduleIndexing();
77 void scheduleCheckUnindexedFiles();
78 void scheduleCheckStaleIndexEntries();
79 void startupFinished();
80
81 Q_SCRIPTABLE void suspend() { setSuspend(true); }
82 Q_SCRIPTABLE void resume() { setSuspend(false); }
83 Q_SCRIPTABLE uint getRemainingTime();
84 Q_SCRIPTABLE void checkUnindexedFiles();
85 Q_SCRIPTABLE void checkStaleIndexEntries();
86 Q_SCRIPTABLE uint getBatchSize();
87
88private Q_SLOTS:
89 void powerManagementStatusChanged(bool isOnBattery);
90
91private:
92 void setSuspend(bool suspend);
93 bool isIndexerIdle() {
94 return m_isGoingIdle ||
95 (m_indexerState == Suspended) ||
96 (m_indexerState == Startup) ||
97 (m_indexerState == Idle) ||
98 (m_indexerState == LowPowerIdle);
99 }
100
101 Database* m_db;
102 FileIndexerConfig* m_config;
103
104 QStringList m_newFiles;
105 QStringList m_modifiedFiles;
106 QStringList m_xattrFiles;
107
108 QThreadPool m_threadPool;
109
110 FileContentIndexerProvider m_provider;
111 FileContentIndexer* m_contentIndexer;
112
113 PowerStateMonitor m_powerMonitor;
114
115 IndexerState m_indexerState;
116 TimeEstimator m_timeEstimator;
117 uint m_indexPendingFiles = 0;
118 uint m_indexFinishedFiles = 0;
119
120 bool m_checkUnindexedFiles;
121 bool m_checkStaleIndexEntries;
122 bool m_isGoingIdle;
123 bool m_isSuspended;
124 bool m_isFirstRun;
125 bool m_inStartup;
126};
127
128}
129
130#endif // BALOO_FILEINDEXSCHEDULER_H
131

source code of baloo/src/file/fileindexscheduler.h