| 1 | /* |
| 2 | This file is part of the KDE Baloo Project |
| 3 | SPDX-FileCopyrightText: 2015 Pinak Ahuja <pinak.ahuja@gmail.com> |
| 4 | SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org> |
| 5 | |
| 6 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 7 | */ |
| 8 | |
| 9 | #include "monitorcommand.h" |
| 10 | #include "indexerstate.h" |
| 11 | |
| 12 | #include <QDBusConnection> |
| 13 | #include <QDBusServiceWatcher> |
| 14 | |
| 15 | using namespace Baloo; |
| 16 | |
| 17 | MonitorCommand::MonitorCommand(QObject *parent) |
| 18 | : QObject(parent) |
| 19 | , m_out(stdout) |
| 20 | , m_err(stderr) |
| 21 | , m_indexerDBusInterface(nullptr) |
| 22 | , m_schedulerDBusInterface(nullptr) |
| 23 | , m_dbusServiceWatcher(nullptr) |
| 24 | |
| 25 | { |
| 26 | m_dbusServiceWatcher = new QDBusServiceWatcher( |
| 27 | QStringLiteral("org.kde.baloo" ), QDBusConnection::sessionBus(), |
| 28 | QDBusServiceWatcher::WatchForOwnerChange, this |
| 29 | ); |
| 30 | connect(sender: m_dbusServiceWatcher, signal: &QDBusServiceWatcher::serviceRegistered, |
| 31 | context: this, slot: &MonitorCommand::balooIsAvailable); |
| 32 | connect(sender: m_dbusServiceWatcher, signal: &QDBusServiceWatcher::serviceUnregistered, |
| 33 | context: this, slot: &MonitorCommand::balooIsNotAvailable); |
| 34 | |
| 35 | m_indexerDBusInterface = new org::kde::baloo::fileindexer(QStringLiteral("org.kde.baloo" ), |
| 36 | QStringLiteral("/fileindexer" ), |
| 37 | QDBusConnection::sessionBus(), |
| 38 | this |
| 39 | ); |
| 40 | connect(sender: m_indexerDBusInterface, signal: &org::kde::baloo::fileindexer::startedIndexingFile, |
| 41 | context: this, slot: &MonitorCommand::startedIndexingFile); |
| 42 | connect(sender: m_indexerDBusInterface, signal: &org::kde::baloo::fileindexer::finishedIndexingFile, |
| 43 | context: this, slot: &MonitorCommand::finishedIndexingFile); |
| 44 | |
| 45 | m_schedulerDBusInterface = new org::kde::baloo::scheduler(QStringLiteral("org.kde.baloo" ), |
| 46 | QStringLiteral("/scheduler" ), |
| 47 | QDBusConnection::sessionBus(), |
| 48 | this |
| 49 | ); |
| 50 | connect(sender: m_schedulerDBusInterface, signal: &org::kde::baloo::scheduler::stateChanged, |
| 51 | context: this, slot: &MonitorCommand::stateChanged); |
| 52 | |
| 53 | if (m_indexerDBusInterface->isValid() && m_schedulerDBusInterface->isValid()) { |
| 54 | m_err << i18n("Press ctrl+c to stop monitoring\n" ); |
| 55 | m_err.flush(); |
| 56 | balooIsAvailable(); |
| 57 | stateChanged(state: m_schedulerDBusInterface->state()); |
| 58 | const QString currentFile = m_indexerDBusInterface->currentFile(); |
| 59 | if (!currentFile.isEmpty()) { |
| 60 | startedIndexingFile(filePath: currentFile); |
| 61 | } |
| 62 | } else { |
| 63 | balooIsNotAvailable(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void MonitorCommand::balooIsNotAvailable() |
| 68 | { |
| 69 | m_indexerDBusInterface->unregisterMonitor(); |
| 70 | m_err << i18n("Waiting for file indexer to start\n" ); |
| 71 | m_err << i18n("Press Ctrl+C to stop monitoring\n" ); |
| 72 | m_err.flush(); |
| 73 | } |
| 74 | |
| 75 | void MonitorCommand::balooIsAvailable() |
| 76 | { |
| 77 | m_indexerDBusInterface->registerMonitor(); |
| 78 | m_err << i18n("File indexer is running\n" ); |
| 79 | m_err.flush(); |
| 80 | } |
| 81 | |
| 82 | int MonitorCommand::exec(const QCommandLineParser& parser) |
| 83 | { |
| 84 | Q_UNUSED(parser); |
| 85 | return QCoreApplication::instance()->exec(); |
| 86 | } |
| 87 | |
| 88 | void MonitorCommand::startedIndexingFile(const QString& filePath) |
| 89 | { |
| 90 | if (!m_currentFile.isEmpty()) { |
| 91 | m_out << '\n'; |
| 92 | } |
| 93 | m_currentFile = filePath; |
| 94 | m_out << i18nc("currently indexed file" , "Indexing: %1" , filePath); |
| 95 | m_out.flush(); |
| 96 | } |
| 97 | |
| 98 | void MonitorCommand::finishedIndexingFile(const QString& filePath) |
| 99 | { |
| 100 | Q_UNUSED(filePath); |
| 101 | |
| 102 | m_currentFile.clear(); |
| 103 | m_out << i18n(": Ok\n" ); |
| 104 | m_out.flush(); |
| 105 | } |
| 106 | |
| 107 | void MonitorCommand::stateChanged(int state) |
| 108 | { |
| 109 | m_out << stateString(state) << '\n'; |
| 110 | m_out.flush(); |
| 111 | } |
| 112 | |
| 113 | #include "moc_monitorcommand.cpp" |
| 114 | |