File indexing completed on 2024-04-28 03:51:53

0001 /*
0002     This file is part of the KDE Baloo Project
0003     SPDX-FileCopyrightText: 2015 Pinak Ahuja <pinak.ahuja@gmail.com>
0004     SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "monitorcommand.h"
0010 #include "indexerstate.h"
0011 
0012 #include <QDBusConnection>
0013 #include <QDBusServiceWatcher>
0014 
0015 using namespace Baloo;
0016 
0017 MonitorCommand::MonitorCommand(QObject *parent)
0018     : QObject(parent)
0019     , m_out(stdout)
0020     , m_err(stderr)
0021     , m_indexerDBusInterface(nullptr)
0022     , m_schedulerDBusInterface(nullptr)
0023     , m_dbusServiceWatcher(nullptr)
0024 
0025 {
0026     m_dbusServiceWatcher = new QDBusServiceWatcher(
0027         QStringLiteral("org.kde.baloo"), QDBusConnection::sessionBus(),
0028         QDBusServiceWatcher::WatchForOwnerChange, this
0029     );
0030     connect(m_dbusServiceWatcher, &QDBusServiceWatcher::serviceRegistered,
0031             this, &MonitorCommand::balooIsAvailable);
0032     connect(m_dbusServiceWatcher, &QDBusServiceWatcher::serviceUnregistered,
0033             this, &MonitorCommand::balooIsNotAvailable);
0034 
0035     m_indexerDBusInterface = new org::kde::baloo::fileindexer(QStringLiteral("org.kde.baloo"),
0036         QStringLiteral("/fileindexer"),
0037         QDBusConnection::sessionBus(),
0038         this
0039     );
0040     connect(m_indexerDBusInterface, &org::kde::baloo::fileindexer::startedIndexingFile,
0041         this, &MonitorCommand::startedIndexingFile);
0042     connect(m_indexerDBusInterface, &org::kde::baloo::fileindexer::finishedIndexingFile,
0043         this, &MonitorCommand::finishedIndexingFile);
0044 
0045     m_schedulerDBusInterface = new org::kde::baloo::scheduler(QStringLiteral("org.kde.baloo"),
0046         QStringLiteral("/scheduler"),
0047         QDBusConnection::sessionBus(),
0048         this
0049     );
0050     connect(m_schedulerDBusInterface, &org::kde::baloo::scheduler::stateChanged,
0051         this, &MonitorCommand::stateChanged);
0052 
0053     if (m_indexerDBusInterface->isValid() && m_schedulerDBusInterface->isValid()) {
0054         m_err << i18n("Press ctrl+c to stop monitoring\n");
0055         m_err.flush();
0056         balooIsAvailable();
0057         stateChanged(m_schedulerDBusInterface->state());
0058         const QString currentFile = m_indexerDBusInterface->currentFile();
0059         if (!currentFile.isEmpty()) {
0060             startedIndexingFile(currentFile);
0061         }
0062     } else {
0063         balooIsNotAvailable();
0064     }
0065 }
0066 
0067 void MonitorCommand::balooIsNotAvailable()
0068 {
0069     m_indexerDBusInterface->unregisterMonitor();
0070     m_err << i18n("Waiting for file indexer to start\n");
0071     m_err << i18n("Press Ctrl+C to stop monitoring\n");
0072     m_err.flush();
0073 }
0074 
0075 void MonitorCommand::balooIsAvailable()
0076 {
0077     m_indexerDBusInterface->registerMonitor();
0078     m_err << i18n("File indexer is running\n");
0079     m_err.flush();
0080 }
0081 
0082 int MonitorCommand::exec(const QCommandLineParser& parser)
0083 {
0084     Q_UNUSED(parser);
0085     return QCoreApplication::instance()->exec();
0086 }
0087 
0088 void MonitorCommand::startedIndexingFile(const QString& filePath)
0089 {
0090     if (!m_currentFile.isEmpty()) {
0091     m_out << '\n';
0092     }
0093     m_currentFile = filePath;
0094     m_out << i18nc("currently indexed file", "Indexing: %1", filePath);
0095     m_out.flush();
0096 }
0097 
0098 void MonitorCommand::finishedIndexingFile(const QString& filePath)
0099 {
0100     Q_UNUSED(filePath);
0101 
0102     m_currentFile.clear();
0103     m_out << i18n(": Ok\n");
0104     m_out.flush();
0105 }
0106 
0107 void MonitorCommand::stateChanged(int state)
0108 {
0109     m_out << stateString(state) << '\n';
0110     m_out.flush();
0111 }
0112 
0113 #include "moc_monitorcommand.cpp"