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(sender: m_fileindexer, signal: &org::kde::baloo::fileindexer::startedIndexingFile,
41 context: this, slot: &Monitor::newFile);
42
43 connect(sender: m_scheduler, signal: &org::kde::baloo::scheduler::stateChanged,
44 context: this, slot: &Monitor::slotIndexerStateChanged);
45
46 QDBusServiceWatcher* balooWatcher = new QDBusServiceWatcher(m_scheduler->service(),
47 m_bus,
48 QDBusServiceWatcher::WatchForOwnerChange,
49 this);
50 connect(sender: balooWatcher, signal: &QDBusServiceWatcher::serviceRegistered, context: this, slot: &Monitor::balooStarted);
51 connect(sender: balooWatcher, signal: &QDBusServiceWatcher::serviceUnregistered, context: this, slot: [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 slotIndexerStateChanged(state: m_scheduler->state());
100 Q_EMIT balooStateChanged();
101}
102
103void Monitor::fetchTotalFiles()
104{
105 Baloo::Database *db = Baloo::globalDatabaseInstance();
106 if (db->open(mode: 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");
118 QProcess::startDetached(program: exe, arguments: QStringList());
119}
120
121void Monitor::updateRemainingTime()
122{
123 auto remainingTime = m_scheduler->getRemainingTime();
124 if ((remainingTime != m_remainingTimeSeconds) && (remainingTime > 0)) {
125 m_remainingTime = KFormat().formatSpelloutDuration(msecs: 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"
146

source code of baloo/src/qml/experimental/monitor.cpp