| 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 | |
| 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(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 | |
| 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 | // the generated code doesn't let you fetch properties asynchronously |
| 100 | auto methodCall = |
| 101 | QDBusMessage::createMethodCall(destination: m_scheduler->service(), path: m_scheduler->path(), QStringLiteral("org.freedesktop.DBus.Properties" ), QStringLiteral("Get" )); |
| 102 | methodCall << m_scheduler->interface() << QStringLiteral("state" ); |
| 103 | auto pendingCall = m_scheduler->connection().asyncCall(message: methodCall, timeout: m_scheduler->timeout()); |
| 104 | auto *watcher = new QDBusPendingCallWatcher(pendingCall, this); |
| 105 | connect(sender: watcher, signal: &QDBusPendingCallWatcher::finished, context: this, slot: [this](QDBusPendingCallWatcher *call) { |
| 106 | QDBusPendingReply<QDBusVariant> state = *call; |
| 107 | |
| 108 | if (state.isError()) { |
| 109 | qWarning() << "Error fetching Baloo indexer state:" << state.error().message(); |
| 110 | } else { |
| 111 | slotIndexerStateChanged(state: state.value().variant().toInt()); |
| 112 | Q_EMIT balooStateChanged(); |
| 113 | } |
| 114 | |
| 115 | call->deleteLater(); |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | void Monitor::fetchTotalFiles() |
| 120 | { |
| 121 | Baloo::Database *db = Baloo::globalDatabaseInstance(); |
| 122 | if (db->open(mode: Baloo::Database::ReadOnlyDatabase) == Baloo::Database::OpenResult::Success) { |
| 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 | |
| 131 | void Monitor::startBaloo() |
| 132 | { |
| 133 | const QString exe = QStringLiteral(KDE_INSTALL_FULL_LIBEXECDIR_KF "/baloo_file" ); |
| 134 | QProcess::startDetached(program: exe, arguments: QStringList()); |
| 135 | } |
| 136 | |
| 137 | void Monitor::updateRemainingTime() |
| 138 | { |
| 139 | auto remainingTime = m_scheduler->getRemainingTime(); |
| 140 | auto *watcher = new QDBusPendingCallWatcher(remainingTime, this); |
| 141 | connect(sender: watcher, signal: &QDBusPendingCallWatcher::finished, context: this, slot: [this](QDBusPendingCallWatcher *call) { |
| 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(msecs: remainingTime); |
| 149 | m_remainingTimeSeconds = remainingTime; |
| 150 | Q_EMIT remainingTimeChanged(); |
| 151 | } |
| 152 | |
| 153 | call->deleteLater(); |
| 154 | }); |
| 155 | } |
| 156 | |
| 157 | void 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" |
| 172 | |