Baloo

monitor.cpp
1 /*
2  This file is part of the KDE Baloo Project
3  SPDX-FileCopyrightText: 2015 Pinak Ahuja <[email protected]>
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 
23 using namespace Baloo;
24 Monitor::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 
64 void 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 
80 QString Monitor::suspendState() const
81 {
82  return m_indexerState == Baloo::Suspended ? QStringLiteral("Resume") : QStringLiteral("Suspend");
83 }
84 
85 void Monitor::toggleSuspendState()
86 {
87  if (m_indexerState == Baloo::Suspended) {
88  m_scheduler->resume();
89  } else {
90  m_scheduler->suspend();
91  }
92 }
93 
94 void Monitor::balooStarted()
95 {
96  m_balooRunning = true;
97  m_fileindexer->registerMonitor();
98 
99  slotIndexerStateChanged(m_scheduler->state());
100  Q_EMIT balooStateChanged();
101 }
102 
103 void 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 
115 void Monitor::startBaloo()
116 {
117  const QString exe = QStringLiteral(KDE_INSTALL_FULL_LIBEXECDIR "/baloo_file");
119 }
120 
121 void 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 
131 void 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"
Q_EMITQ_EMIT
void serviceUnregistered(const QString &serviceName)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Implements storage for docIds without any associated data Instantiated for:
Definition: coding.cpp:11
void serviceRegistered(const QString &serviceName)
QDeadlineTimer current(Qt::TimerType timerType)
bool startDetached(qint64 *pid)
QString formatSpelloutDuration(quint64 msecs) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Nov 29 2023 03:56:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.