9 #include "pendingfilequeue.h"
15 using namespace Baloo;
17 PendingFileQueue::PendingFileQueue(
QObject* parent)
20 m_cacheTimer.setInterval(10);
21 m_cacheTimer.setSingleShot(
true);
26 m_trackingTime = 120 * 1000;
28 m_clearRecentlyEmittedTimer.setInterval(m_trackingTime);
29 m_clearRecentlyEmittedTimer.setSingleShot(
true);
34 m_minTimeout = 5 * 1000;
35 m_maxTimeout = 60 * 1000;
36 m_pendingFilesTimer.setInterval(m_minTimeout);
37 m_pendingFilesTimer.setSingleShot(
true);
43 PendingFileQueue::~PendingFileQueue()
47 void PendingFileQueue::enqueue(
const PendingFile& file)
52 const auto keepFile = [&file](
const PendingFile& pending) {
53 return !pending.path().startsWith(file.path());
55 const auto end = m_cache.end();
57 const auto droppedFilesBegin = std::partition(m_cache.begin(), end, keepFile);
58 for (
auto it = droppedFilesBegin; it !=
end; it++) {
59 m_pendingFiles.remove(it->path());
60 m_recentlyEmitted.remove(it->path());
62 m_cache.erase(droppedFilesBegin, end);
65 if (file.shouldRemoveIndex()) {
66 m_cache.removeOne(file);
67 m_pendingFiles.remove(file.path());
68 Q_EMIT removeFileIndex(file.path());
72 int i = m_cache.indexOf(file);
76 m_cache[i].
merge(file);
82 void PendingFileQueue::processCache(
const QTime& currentTime)
84 for (
const PendingFile& file : std::as_const(m_cache)) {
85 if (file.shouldIndexXAttrOnly()) {
86 Q_EMIT indexXAttr(file.path());
88 else if (file.shouldIndexContents()) {
89 if (m_pendingFiles.contains(file.path())) {
90 QTime time = m_pendingFiles[file.path()];
92 int msecondsLeft = currentTime.
msecsTo(time);
93 msecondsLeft = qBound(m_minTimeout, msecondsLeft * 2, m_maxTimeout);
95 time = currentTime.
addMSecs(msecondsLeft);
96 m_pendingFiles[file.path()] = time;
98 else if (m_recentlyEmitted.contains(file.path())) {
100 m_pendingFiles[file.path()] = time;
103 if (file.isNewFile()) {
104 Q_EMIT indexNewFile(file.path());
106 Q_EMIT indexModifiedFile(file.path());
108 m_recentlyEmitted.insert(file.path(), currentTime);
111 Q_ASSERT_X(
false,
"FileWatch",
"The PendingFile should always have some flags set");
117 if (!m_pendingFiles.isEmpty() && !m_pendingFilesTimer.isActive()) {
118 m_pendingFilesTimer.setInterval(m_minTimeout);
119 m_pendingFilesTimer.start();
122 if (!m_recentlyEmitted.isEmpty() && !m_clearRecentlyEmittedTimer.isActive()) {
123 m_clearRecentlyEmittedTimer.setInterval(m_trackingTime);
124 m_clearRecentlyEmittedTimer.start();
128 void PendingFileQueue::clearRecentlyEmitted(
const QTime& time)
130 int nextUpdate = m_trackingTime;
133 while (it.hasNext()) {
136 int msecondsSinceEmitted = it.value().msecsTo(time);
137 if (msecondsSinceEmitted >= m_trackingTime) {
140 int timeLeft = m_trackingTime - msecondsSinceEmitted;
141 nextUpdate = qMin(nextUpdate, timeLeft);
145 if (!m_recentlyEmitted.isEmpty()) {
146 m_clearRecentlyEmittedTimer.setInterval(nextUpdate);
147 m_clearRecentlyEmittedTimer.start();
151 void PendingFileQueue::processPendingFiles(
const QTime& currentTime)
153 int nextUpdate = m_maxTimeout;
156 while (it.hasNext()) {
159 int mSecondsLeft = currentTime.
msecsTo(it.value());
160 if (mSecondsLeft <= 0) {
161 Q_EMIT indexModifiedFile(it.key());
162 m_recentlyEmitted.insert(it.key(), currentTime);
167 nextUpdate = qMin(mSecondsLeft, nextUpdate);
171 if (!m_pendingFiles.isEmpty()) {
172 m_pendingFilesTimer.setInterval(nextUpdate);
173 m_pendingFilesTimer.start();
176 if (!m_recentlyEmitted.isEmpty() && !m_clearRecentlyEmittedTimer.isActive()) {
177 m_clearRecentlyEmittedTimer.setInterval(m_trackingTime);
178 m_clearRecentlyEmittedTimer.start();
182 void PendingFileQueue::setTrackingTime(
int seconds)
184 m_trackingTime = seconds * 1000;
187 void PendingFileQueue::setMinimumTimeout(
int seconds)
189 m_minTimeout = seconds * 1000;
192 void PendingFileQueue::setMaximumTimeout(
int seconds)
194 m_maxTimeout = seconds * 1000;
197 #include "moc_pendingfilequeue.cpp"