Baloo

baloosearchmodule.cpp
1 /*
2  This file is part of the KDE Baloo Project
3  SPDX-FileCopyrightText: 2014 Vishesh Handa <[email protected]>
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(0, this, SLOT(init()));
33 }
34 
35 void SearchModule::init()
36 {
37  m_dirNotify = new org::kde::KDirNotify(QString(), QString(),
39  connect(m_dirNotify, &OrgKdeKDirNotifyInterface::enteredDirectory,
40  this, &SearchModule::registerSearchUrl);
41  connect(m_dirNotify, &OrgKdeKDirNotifyInterface::leftDirectory,
42  this, &SearchModule::unregisterSearchUrl);
43 
44  // FIXME: Listen to changes from Baloo!!
45  // Listen to dbChanged
47  con.connect(QString(), QStringLiteral("/files"), QStringLiteral("org.kde.baloo"),
48  QStringLiteral("updated"), this, SLOT(slotBalooFileDbChanged()));
49  con.connect(QString(), QStringLiteral("/files"), QStringLiteral("org.kde"),
50  QStringLiteral("changed"), 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(url);
65 }
66 
67 void SearchModule::slotBalooFileDbChanged()
68 {
69  for (const QUrl& dirUrl : std::as_const(m_searchUrls)) {
70  org::kde::KDirNotify::emitFilesAdded(dirUrl);
71  }
72 }
73 
74 void SearchModule::slotFileMetaDataChanged(const QStringList& list)
75 {
76  QList<QUrl> localFileUrls;
77  localFileUrls.reserve(list.size());
78  for (const QString& path : list) {
79  localFileUrls << QUrl::fromLocalFile(path);
80  }
81  org::kde::KDirNotify::emitFilesChanged(localFileUrls);
82  slotBalooFileDbChanged();
83 }
84 
85 K_PLUGIN_CLASS_WITH_JSON(SearchModule, "baloosearchmodule.json")
86 
87 #include "baloosearchmodule.moc"
88 #include "moc_baloosearchmodule.cpp"
bool connect(const QString &service, const QString &path, const QString &interface, const QString &name, QObject *receiver, const char *slot)
QString scheme() const const
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
void reserve(int alloc)
int size() const const
QDBusConnection sessionBus()
Implements storage for docIds without any associated data Instantiated for:
Definition: coding.cpp:11
QUrl fromLocalFile(const QString &localFile)
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Dec 11 2023 03:53:56 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.