diff --git a/src/qml/experimental/monitor.cpp b/src/qml/experimental/monitor.cpp index f32e6134..8a438cbf 100644 --- a/src/qml/experimental/monitor.cpp +++ b/src/qml/experimental/monitor.cpp @@ -1,156 +1,159 @@ /* * This file is part of the KDE Baloo Project * Copyright (C) 2015 Pinak Ahuja * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . * */ #include "monitor.h" #include "database.h" #include "transaction.h" #include "global.h" #include #include #include #include #include #include #include using namespace Baloo; Monitor::Monitor(QObject *parent) : QObject(parent) , m_bus(QDBusConnection::sessionBus()) , m_filePath(QStringLiteral("Idle")) , m_scheduler(nullptr) , m_fileindexer(nullptr) , m_remainingTime(QStringLiteral("Estimating")) { m_scheduler = new org::kde::baloo::scheduler(QStringLiteral("org.kde.baloo"), QStringLiteral("/scheduler"), m_bus, this); m_fileindexer = new org::kde::baloo::fileindexer(QStringLiteral("org.kde.baloo"), QStringLiteral("/fileindexer"), m_bus, this); connect(m_fileindexer, &org::kde::baloo::fileindexer::startedIndexingFile, this, &Monitor::newFile); connect(m_scheduler, &org::kde::baloo::scheduler::stateChanged, this, &Monitor::slotIndexerStateChanged); QDBusServiceWatcher* balooWatcher = new QDBusServiceWatcher(m_scheduler->service(), m_bus, QDBusServiceWatcher::WatchForOwnerChange, this); connect(balooWatcher, &QDBusServiceWatcher::serviceRegistered, this, &Monitor::balooStarted); connect(balooWatcher, &QDBusServiceWatcher::serviceUnregistered, this, [this]() { m_balooRunning = false; m_indexerState = Baloo::Unavailable; emit balooStateChanged(); emit indexerStateChanged(); }); if (m_scheduler->isValid()) { // baloo is already running balooStarted(); } } void Monitor::newFile(const QString& filePath) { m_filePath = filePath; if (m_totalFiles == 0) { fetchTotalFiles(); } ++m_filesIndexed; Q_EMIT newFileIndexed(); - if (m_remainingTimeTimer.elapsed() > 1000) { + auto now = QDeadlineTimer::current(); + if (now > m_remainingTimeTimer) { updateRemainingTime(); - m_remainingTimeTimer.restart(); + m_remainingTimeTimer = now + 1000; } } QString Monitor::suspendState() const { return m_indexerState == Baloo::Suspended ? QStringLiteral("Resume") : QStringLiteral("Suspend"); } void Monitor::toggleSuspendState() { if (m_indexerState == Baloo::Suspended) { m_scheduler->resume(); } else { m_scheduler->suspend(); } } void Monitor::balooStarted() { m_balooRunning = true; m_fileindexer->registerMonitor(); slotIndexerStateChanged(m_scheduler->state()); Q_EMIT balooStateChanged(); } void Monitor::fetchTotalFiles() { Baloo::Database *db = Baloo::globalDatabaseInstance(); if (db->open(Baloo::Database::ReadOnlyDatabase)) { Baloo::Transaction tr(db, Baloo::Transaction::ReadOnly); m_totalFiles = tr.size(); m_filesIndexed = tr.size() - tr.phaseOneSize(); Q_EMIT totalFilesChanged(); Q_EMIT newFileIndexed(); } } void Monitor::startBaloo() { const QString exe = QStandardPaths::findExecutable(QStringLiteral("baloo_file")); QProcess::startDetached(exe); } void Monitor::updateRemainingTime() { - m_remainingTime = KFormat().formatSpelloutDuration(m_scheduler->getRemainingTime()); - Q_EMIT remainingTimeChanged(); + auto remainingTime = m_scheduler->getRemainingTime(); + if ((remainingTime != m_remainingTimeSeconds) && (remainingTime > 0)) { + m_remainingTime = KFormat().formatSpelloutDuration(remainingTime); + m_remainingTimeSeconds = remainingTime; + Q_EMIT remainingTimeChanged(); + } } void Monitor::slotIndexerStateChanged(int state) { Baloo::IndexerState newState = static_cast(state); if (m_indexerState != newState) { m_indexerState = newState; fetchTotalFiles(); - if (m_indexerState == Baloo::ContentIndexing) { - m_remainingTimeTimer.start(); - } else { + if (m_indexerState != Baloo::ContentIndexing) { m_filePath = QString(); } Q_EMIT indexerStateChanged(); } } diff --git a/src/qml/experimental/monitor.h b/src/qml/experimental/monitor.h index eac29f00..f19f4931 100644 --- a/src/qml/experimental/monitor.h +++ b/src/qml/experimental/monitor.h @@ -1,91 +1,93 @@ /* * This file is part of the KDE Baloo Project * Copyright (C) 2015 Pinak Ahuja * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . * */ #ifndef BALOOMONITOR_MONITOR_H #define BALOOMONITOR_MONITOR_H +#include #include #include #include "indexerstate.h" #include "schedulerinterface.h" #include "fileindexerinterface.h" namespace Baloo { class Monitor : public QObject { Q_OBJECT Q_PROPERTY(QString filePath READ filePath NOTIFY newFileIndexed) Q_PROPERTY(QString suspendState READ suspendState NOTIFY indexerStateChanged) Q_PROPERTY(bool balooRunning MEMBER m_balooRunning NOTIFY balooStateChanged) Q_PROPERTY(uint totalFiles MEMBER m_totalFiles NOTIFY totalFilesChanged) Q_PROPERTY(uint filesIndexed MEMBER m_filesIndexed NOTIFY newFileIndexed) Q_PROPERTY(QString remainingTime READ remainingTime NOTIFY remainingTimeChanged) Q_PROPERTY(QString stateString READ stateString NOTIFY indexerStateChanged) Q_PROPERTY(int state READ state NOTIFY indexerStateChanged) public: explicit Monitor(QObject* parent = nullptr); // Property readers QString filePath() const { return m_filePath; } QString suspendState() const; QString remainingTime() const { return m_remainingTime; } QString stateString() const { return Baloo::stateString(m_indexerState); } int state() const { return static_cast(m_indexerState); } // Invokable methods Q_INVOKABLE void toggleSuspendState(); Q_INVOKABLE void startBaloo(); Q_SIGNALS: void newFileIndexed(); void balooStateChanged(); void totalFilesChanged(); void remainingTimeChanged(); void indexerStateChanged(); private Q_SLOTS: void newFile(const QString& filePath); void balooStarted(); void slotIndexerStateChanged(int state); private: void fetchTotalFiles(); void updateRemainingTime(); QDBusConnection m_bus; QString m_filePath; bool m_balooRunning = false; Baloo::IndexerState m_indexerState = Baloo::Unavailable; - QElapsedTimer m_remainingTimeTimer; + QDeadlineTimer m_remainingTimeTimer = QDeadlineTimer(0); org::kde::baloo::scheduler* m_scheduler; org::kde::baloo::fileindexer* m_fileindexer; uint m_totalFiles = 0; uint m_filesIndexed = 0; QString m_remainingTime; + uint m_remainingTimeSeconds = 0; }; } #endif //BALOOMONITOR_MONITOR_H