File indexing completed on 2024-04-28 03:51:52
0001 /* 0002 This file is part of the KDE Baloo Project 0003 SPDX-FileCopyrightText: 2015 Pinak Ahuja <pinak.ahuja@gmail.com> 0004 0005 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0006 */ 0007 0008 #include "monitor.h" 0009 0010 #include "database.h" 0011 #include "transaction.h" 0012 #include "global.h" 0013 #include "config.h" 0014 0015 #include <QDBusConnection> 0016 #include <QDBusConnectionInterface> 0017 #include <QDebug> 0018 #include <QDBusServiceWatcher> 0019 #include <QProcess> 0020 0021 #include <KFormat> 0022 0023 using namespace Baloo; 0024 Monitor::Monitor(QObject *parent) 0025 : QObject(parent) 0026 , m_bus(QDBusConnection::sessionBus()) 0027 , m_filePath(QStringLiteral("Idle")) 0028 , m_scheduler(nullptr) 0029 , m_fileindexer(nullptr) 0030 , m_remainingTime(QStringLiteral("Estimating")) 0031 { 0032 m_scheduler = new org::kde::baloo::scheduler(QStringLiteral("org.kde.baloo"), 0033 QStringLiteral("/scheduler"), 0034 m_bus, this); 0035 0036 m_fileindexer = new org::kde::baloo::fileindexer(QStringLiteral("org.kde.baloo"), 0037 QStringLiteral("/fileindexer"), 0038 m_bus, this); 0039 0040 connect(m_fileindexer, &org::kde::baloo::fileindexer::startedIndexingFile, 0041 this, &Monitor::newFile); 0042 0043 connect(m_scheduler, &org::kde::baloo::scheduler::stateChanged, 0044 this, &Monitor::slotIndexerStateChanged); 0045 0046 QDBusServiceWatcher* balooWatcher = new QDBusServiceWatcher(m_scheduler->service(), 0047 m_bus, 0048 QDBusServiceWatcher::WatchForOwnerChange, 0049 this); 0050 connect(balooWatcher, &QDBusServiceWatcher::serviceRegistered, this, &Monitor::balooStarted); 0051 connect(balooWatcher, &QDBusServiceWatcher::serviceUnregistered, this, [this]() { 0052 m_balooRunning = false; 0053 m_indexerState = Baloo::Unavailable; 0054 Q_EMIT balooStateChanged(); 0055 Q_EMIT indexerStateChanged(); 0056 }); 0057 0058 if (m_scheduler->isValid()) { 0059 // baloo is already running 0060 balooStarted(); 0061 } 0062 } 0063 0064 void Monitor::newFile(const QString& filePath) 0065 { 0066 m_filePath = filePath; 0067 if (m_totalFiles == 0) { 0068 fetchTotalFiles(); 0069 } 0070 ++m_filesIndexed; 0071 Q_EMIT newFileIndexed(); 0072 0073 auto now = QDeadlineTimer::current(); 0074 if (now > m_remainingTimeTimer) { 0075 updateRemainingTime(); 0076 m_remainingTimeTimer = now + 1000; 0077 } 0078 } 0079 0080 QString Monitor::suspendState() const 0081 { 0082 return m_indexerState == Baloo::Suspended ? QStringLiteral("Resume") : QStringLiteral("Suspend"); 0083 } 0084 0085 void Monitor::toggleSuspendState() 0086 { 0087 if (m_indexerState == Baloo::Suspended) { 0088 m_scheduler->resume(); 0089 } else { 0090 m_scheduler->suspend(); 0091 } 0092 } 0093 0094 void Monitor::balooStarted() 0095 { 0096 m_balooRunning = true; 0097 m_fileindexer->registerMonitor(); 0098 0099 slotIndexerStateChanged(m_scheduler->state()); 0100 Q_EMIT balooStateChanged(); 0101 } 0102 0103 void Monitor::fetchTotalFiles() 0104 { 0105 Baloo::Database *db = Baloo::globalDatabaseInstance(); 0106 if (db->open(Baloo::Database::ReadOnlyDatabase)) { 0107 Baloo::Transaction tr(db, Baloo::Transaction::ReadOnly); 0108 m_totalFiles = tr.size(); 0109 m_filesIndexed = tr.size() - tr.phaseOneSize(); 0110 Q_EMIT totalFilesChanged(); 0111 Q_EMIT newFileIndexed(); 0112 } 0113 } 0114 0115 void Monitor::startBaloo() 0116 { 0117 const QString exe = QStringLiteral(KDE_INSTALL_FULL_LIBEXECDIR_KF "/baloo_file"); 0118 QProcess::startDetached(exe, QStringList()); 0119 } 0120 0121 void Monitor::updateRemainingTime() 0122 { 0123 auto remainingTime = m_scheduler->getRemainingTime(); 0124 if ((remainingTime != m_remainingTimeSeconds) && (remainingTime > 0)) { 0125 m_remainingTime = KFormat().formatSpelloutDuration(remainingTime); 0126 m_remainingTimeSeconds = remainingTime; 0127 Q_EMIT remainingTimeChanged(); 0128 } 0129 } 0130 0131 void Monitor::slotIndexerStateChanged(int state) 0132 { 0133 Baloo::IndexerState newState = static_cast<Baloo::IndexerState>(state); 0134 0135 if (m_indexerState != newState) { 0136 m_indexerState = newState; 0137 fetchTotalFiles(); 0138 if (m_indexerState != Baloo::ContentIndexing) { 0139 m_filePath = QString(); 0140 } 0141 Q_EMIT indexerStateChanged(); 0142 } 0143 } 0144 0145 #include "moc_monitor.cpp"