Baloo

monitor.cpp
1/*
2 This file is part of the KDE Baloo Project
3 SPDX-FileCopyrightText: 2015 Pinak Ahuja <pinak.ahuja@gmail.com>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include "monitor.h"
9
10#include "database.h"
11#include "transaction.h"
12#include "global.h"
13#include "config.h"
14
15#include <QDBusConnection>
16#include <QDBusConnectionInterface>
17#include <QDebug>
18#include <QDBusServiceWatcher>
19#include <QProcess>
20
21#include <KFormat>
22
23using namespace Baloo;
24Monitor::Monitor(QObject *parent)
25 : QObject(parent)
26 , m_bus(QDBusConnection::sessionBus())
27 , m_filePath(QStringLiteral("Idle"))
28 , m_scheduler(nullptr)
29 , m_fileindexer(nullptr)
30 , m_remainingTime(QStringLiteral("Estimating"))
31{
32 m_scheduler = new org::kde::baloo::scheduler(QStringLiteral("org.kde.baloo"),
33 QStringLiteral("/scheduler"),
34 m_bus, this);
35
36 m_fileindexer = new org::kde::baloo::fileindexer(QStringLiteral("org.kde.baloo"),
37 QStringLiteral("/fileindexer"),
38 m_bus, this);
39
40 connect(m_fileindexer, &org::kde::baloo::fileindexer::startedIndexingFile,
41 this, &Monitor::newFile);
42
43 connect(m_scheduler, &org::kde::baloo::scheduler::stateChanged,
44 this, &Monitor::slotIndexerStateChanged);
45
46 QDBusServiceWatcher* balooWatcher = new QDBusServiceWatcher(m_scheduler->service(),
47 m_bus,
49 this);
50 connect(balooWatcher, &QDBusServiceWatcher::serviceRegistered, this, &Monitor::balooStarted);
52 m_balooRunning = false;
53 m_indexerState = Baloo::Unavailable;
54 Q_EMIT balooStateChanged();
55 Q_EMIT indexerStateChanged();
56 });
57
58 if (m_scheduler->isValid()) {
59 // baloo is already running
60 balooStarted();
61 }
62}
63
64void Monitor::newFile(const QString& filePath)
65{
66 m_filePath = filePath;
67 if (m_totalFiles == 0) {
68 fetchTotalFiles();
69 }
70 ++m_filesIndexed;
71 Q_EMIT newFileIndexed();
72
74 if (now > m_remainingTimeTimer) {
75 updateRemainingTime();
76 m_remainingTimeTimer = now + 1000;
77 }
78}
79
80QString Monitor::suspendState() const
81{
82 return m_indexerState == Baloo::Suspended ? QStringLiteral("Resume") : QStringLiteral("Suspend");
83}
84
85void Monitor::toggleSuspendState()
86{
87 if (m_indexerState == Baloo::Suspended) {
88 m_scheduler->resume();
89 } else {
90 m_scheduler->suspend();
91 }
92}
93
94void Monitor::balooStarted()
95{
96 m_balooRunning = true;
97 m_fileindexer->registerMonitor();
98
99 slotIndexerStateChanged(m_scheduler->state());
100 Q_EMIT balooStateChanged();
101}
102
103void Monitor::fetchTotalFiles()
104{
105 Baloo::Database *db = Baloo::globalDatabaseInstance();
106 if (db->open(Baloo::Database::ReadOnlyDatabase)) {
107 Baloo::Transaction tr(db, Baloo::Transaction::ReadOnly);
108 m_totalFiles = tr.size();
109 m_filesIndexed = tr.size() - tr.phaseOneSize();
110 Q_EMIT totalFilesChanged();
111 Q_EMIT newFileIndexed();
112 }
113}
114
115void Monitor::startBaloo()
116{
117 const QString exe = QStringLiteral(KDE_INSTALL_FULL_LIBEXECDIR_KF "/baloo_file");
119}
120
121void Monitor::updateRemainingTime()
122{
123 auto remainingTime = m_scheduler->getRemainingTime();
124 if ((remainingTime != m_remainingTimeSeconds) && (remainingTime > 0)) {
125 m_remainingTime = KFormat().formatSpelloutDuration(remainingTime);
126 m_remainingTimeSeconds = remainingTime;
127 Q_EMIT remainingTimeChanged();
128 }
129}
130
131void Monitor::slotIndexerStateChanged(int state)
132{
133 Baloo::IndexerState newState = static_cast<Baloo::IndexerState>(state);
134
135 if (m_indexerState != newState) {
136 m_indexerState = newState;
137 fetchTotalFiles();
138 if (m_indexerState != Baloo::ContentIndexing) {
139 m_filePath = QString();
140 }
141 Q_EMIT indexerStateChanged();
142 }
143}
144
145#include "moc_monitor.cpp"
QString formatSpelloutDuration(quint64 msecs) const
Implements storage for docIds without any associated data Instantiated for:
Definition coding.cpp:11
void serviceRegistered(const QString &serviceName)
void serviceUnregistered(const QString &serviceName)
QDeadlineTimer current(Qt::TimerType timerType)
Q_EMITQ_EMIT
QString tr(const char *sourceText, const char *disambiguation, int n)
bool startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory, qint64 *pid)
qsizetype size() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:16 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.