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);
51 connect(balooWatcher, &QDBusServiceWatcher::serviceUnregistered, this, [this]() {
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
73 auto now = QDeadlineTimer::current();
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 // the generated code doesn't let you fetch properties asynchronously
100 auto methodCall =
101 QDBusMessage::createMethodCall(m_scheduler->service(), m_scheduler->path(), QStringLiteral("org.freedesktop.DBus.Properties"), QStringLiteral("Get"));
102 methodCall << m_scheduler->interface() << QStringLiteral("state");
103 auto pendingCall = m_scheduler->connection().asyncCall(methodCall, m_scheduler->timeout());
104 auto *watcher = new QDBusPendingCallWatcher(pendingCall, this);
107
108 if (state.isError()) {
109 qWarning() << "Error fetching Baloo indexer state:" << state.error().message();
110 } else {
111 slotIndexerStateChanged(state.value().variant().toInt());
112 Q_EMIT balooStateChanged();
113 }
114
115 call->deleteLater();
116 });
117}
118
119void Monitor::fetchTotalFiles()
120{
121 Baloo::Database *db = Baloo::globalDatabaseInstance();
122 if (db->open(Baloo::Database::ReadOnlyDatabase)) {
123 Baloo::Transaction tr(db, Baloo::Transaction::ReadOnly);
124 m_totalFiles = tr.size();
125 m_filesIndexed = tr.size() - tr.phaseOneSize();
126 Q_EMIT totalFilesChanged();
127 Q_EMIT newFileIndexed();
128 }
129}
130
131void Monitor::startBaloo()
132{
133 const QString exe = QStringLiteral(KDE_INSTALL_FULL_LIBEXECDIR_KF "/baloo_file");
135}
136
137void Monitor::updateRemainingTime()
138{
139 auto remainingTime = m_scheduler->getRemainingTime();
140 auto *watcher = new QDBusPendingCallWatcher(remainingTime, this);
142 QDBusPendingReply<uint> remainingTime = *call;
143
144 if (remainingTime.isError()) {
145 m_remainingTime = remainingTime.error().message();
146 Q_EMIT remainingTimeChanged();
147 } else if ((remainingTime != m_remainingTimeSeconds) && (remainingTime > 0)) {
148 m_remainingTime = KFormat().formatSpelloutDuration(remainingTime);
149 m_remainingTimeSeconds = remainingTime;
150 Q_EMIT remainingTimeChanged();
151 }
152
153 call->deleteLater();
154 });
155}
156
157void Monitor::slotIndexerStateChanged(int state)
158{
159 Baloo::IndexerState newState = static_cast<Baloo::IndexerState>(state);
160
161 if (m_indexerState != newState) {
162 m_indexerState = newState;
163 fetchTotalFiles();
164 if (m_indexerState != Baloo::ContentIndexing) {
165 m_filePath = QString();
166 }
167 Q_EMIT indexerStateChanged();
168 }
169}
170
171#include "moc_monitor.cpp"
QString formatSpelloutDuration(quint64 msecs) const
Implements storage for docIds without any associated data Instantiated for:
Definition coding.cpp:11
QString message() const const
QDBusMessage createMethodCall(const QString &service, const QString &path, const QString &interface, const QString &method)
void finished(QDBusPendingCallWatcher *self)
QDBusError error() const const
bool isError() const const
typename Select< 0 >::Type value() const const
void serviceRegistered(const QString &serviceName)
void serviceUnregistered(const QString &serviceName)
QDeadlineTimer current(Qt::TimerType timerType)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
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 Sat Dec 21 2024 16:56:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.