File indexing completed on 2024-05-12 15:32:23

0001 /*
0002     This file is part of the KDE Baloo Project
0003     SPDX-FileCopyrightText: 2014 Vishesh Handa <me@vhanda.in>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "baloosearchmodule.h"
0009 
0010 #include <QDBusConnection>
0011 #include <QUrl>
0012 #include <QTimer>
0013 
0014 #include <kpluginfactory.h>
0015 
0016 namespace
0017 {
0018     inline bool isSearchUrl(const QUrl& url)
0019     {
0020         return url.scheme() == QLatin1String("baloosearch") ||
0021                url.scheme() == QLatin1String("timeline") ||
0022                url.scheme() == QLatin1String("tags");
0023     }
0024 }
0025 
0026 using namespace Baloo;
0027 
0028 SearchModule::SearchModule(QObject* parent, const QList<QVariant>&)
0029     : KDEDModule(parent)
0030     , m_dirNotify(nullptr)
0031 {
0032     QTimer::singleShot(0, this, SLOT(init()));
0033 }
0034 
0035 void SearchModule::init()
0036 {
0037     m_dirNotify = new org::kde::KDirNotify(QString(), QString(),
0038                                            QDBusConnection::sessionBus(), this);
0039     connect(m_dirNotify, &OrgKdeKDirNotifyInterface::enteredDirectory,
0040             this, &SearchModule::registerSearchUrl);
0041     connect(m_dirNotify, &OrgKdeKDirNotifyInterface::leftDirectory,
0042             this, &SearchModule::unregisterSearchUrl);
0043 
0044     // FIXME: Listen to changes from Baloo!!
0045     // Listen to dbChanged
0046     QDBusConnection con = QDBusConnection::sessionBus();
0047     con.connect(QString(), QStringLiteral("/files"), QStringLiteral("org.kde.baloo"),
0048                 QStringLiteral("updated"), this, SLOT(slotBalooFileDbChanged()));
0049     con.connect(QString(), QStringLiteral("/files"), QStringLiteral("org.kde"),
0050                 QStringLiteral("changed"), this, SLOT(slotFileMetaDataChanged(QStringList)));
0051 }
0052 
0053 void SearchModule::registerSearchUrl(const QString& urlString)
0054 {
0055     QUrl url(urlString);
0056     if (isSearchUrl(url)) {
0057         m_searchUrls << url;
0058     }
0059 }
0060 
0061 void SearchModule::unregisterSearchUrl(const QString& urlString)
0062 {
0063     QUrl url(urlString);
0064     m_searchUrls.removeAll(url);
0065 }
0066 
0067 void SearchModule::slotBalooFileDbChanged()
0068 {
0069     for (const QUrl& dirUrl : std::as_const(m_searchUrls)) {
0070         org::kde::KDirNotify::emitFilesAdded(dirUrl);
0071     }
0072 }
0073 
0074 void SearchModule::slotFileMetaDataChanged(const QStringList& list)
0075 {
0076     QList<QUrl> localFileUrls;
0077     localFileUrls.reserve(list.size());
0078     for (const QString& path : list) {
0079         localFileUrls << QUrl::fromLocalFile(path);
0080     }
0081     org::kde::KDirNotify::emitFilesChanged(localFileUrls);
0082     slotBalooFileDbChanged();
0083 }
0084 
0085 K_PLUGIN_CLASS_WITH_JSON(SearchModule, "baloosearchmodule.json")
0086 
0087 #include "baloosearchmodule.moc"
0088 #include "moc_baloosearchmodule.cpp"