| 1 | /* |
| 2 | This file is part of the KDE Baloo Project |
| 3 | SPDX-FileCopyrightText: 2014 Vishesh Handa <me@vhanda.in> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 6 | */ |
| 7 | |
| 8 | #include "baloosearchmodule.h" |
| 9 | |
| 10 | #include <QDBusConnection> |
| 11 | #include <QUrl> |
| 12 | #include <QTimer> |
| 13 | |
| 14 | #include <kpluginfactory.h> |
| 15 | |
| 16 | namespace |
| 17 | { |
| 18 | inline bool isSearchUrl(const QUrl& url) |
| 19 | { |
| 20 | return url.scheme() == QLatin1String("baloosearch" ) || |
| 21 | url.scheme() == QLatin1String("timeline" ) || |
| 22 | url.scheme() == QLatin1String("tags" ); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | using namespace Baloo; |
| 27 | |
| 28 | SearchModule::SearchModule(QObject* parent, const QList<QVariant>&) |
| 29 | : KDEDModule(parent) |
| 30 | , m_dirNotify(nullptr) |
| 31 | { |
| 32 | QTimer::singleShot(msec: 0, receiver: this, SLOT(init())); |
| 33 | } |
| 34 | |
| 35 | void SearchModule::init() |
| 36 | { |
| 37 | m_dirNotify = new org::kde::KDirNotify(QString(), QString(), |
| 38 | QDBusConnection::sessionBus(), this); |
| 39 | connect(sender: m_dirNotify, signal: &OrgKdeKDirNotifyInterface::enteredDirectory, |
| 40 | context: this, slot: &SearchModule::registerSearchUrl); |
| 41 | connect(sender: m_dirNotify, signal: &OrgKdeKDirNotifyInterface::leftDirectory, |
| 42 | context: this, slot: &SearchModule::unregisterSearchUrl); |
| 43 | |
| 44 | // FIXME: Listen to changes from Baloo!! |
| 45 | // Listen to dbChanged |
| 46 | QDBusConnection con = QDBusConnection::sessionBus(); |
| 47 | con.connect(service: QString(), QStringLiteral("/files" ), QStringLiteral("org.kde.baloo" ), |
| 48 | QStringLiteral("updated" ), receiver: this, SLOT(slotBalooFileDbChanged())); |
| 49 | con.connect(service: QString(), QStringLiteral("/files" ), QStringLiteral("org.kde" ), |
| 50 | QStringLiteral("changed" ), receiver: this, SLOT(slotFileMetaDataChanged(QStringList))); |
| 51 | } |
| 52 | |
| 53 | void SearchModule::registerSearchUrl(const QString& urlString) |
| 54 | { |
| 55 | QUrl url(urlString); |
| 56 | if (isSearchUrl(url)) { |
| 57 | m_searchUrls << url; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void SearchModule::unregisterSearchUrl(const QString& urlString) |
| 62 | { |
| 63 | QUrl url(urlString); |
| 64 | m_searchUrls.removeAll(t: url); |
| 65 | } |
| 66 | |
| 67 | void SearchModule::slotBalooFileDbChanged() |
| 68 | { |
| 69 | for (const QUrl& dirUrl : std::as_const(t&: m_searchUrls)) { |
| 70 | org::kde::KDirNotify::emitFilesAdded(directory: dirUrl); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void SearchModule::slotFileMetaDataChanged(const QStringList& list) |
| 75 | { |
| 76 | QList<QUrl> localFileUrls; |
| 77 | localFileUrls.reserve(asize: list.size()); |
| 78 | for (const QString& path : list) { |
| 79 | localFileUrls << QUrl::fromLocalFile(localfile: path); |
| 80 | } |
| 81 | org::kde::KDirNotify::emitFilesChanged(fileList: localFileUrls); |
| 82 | slotBalooFileDbChanged(); |
| 83 | } |
| 84 | |
| 85 | K_PLUGIN_CLASS_WITH_JSON(SearchModule, "baloosearchmodule.json" ) |
| 86 | |
| 87 | #include "baloosearchmodule.moc" |
| 88 | #include "moc_baloosearchmodule.cpp" |
| 89 | |