1/*
2 SPDX-FileCopyrightText: 2013 Vishesh Handa <me@vhanda.in>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "filemonitor.h"
8
9#include <QSet>
10#include <QString>
11#include <QStringList>
12#include <QDBusConnection>
13
14using namespace Baloo;
15
16class BALOO_CORE_NO_EXPORT FileMonitor::Private {
17public:
18 QSet<QString> m_files;
19};
20
21FileMonitor::FileMonitor(QObject* parent)
22 : QObject(parent)
23 , d(new Private)
24{
25 QDBusConnection con = QDBusConnection::sessionBus();
26 con.connect(service: QString(), QStringLiteral("/files"), QStringLiteral("org.kde"),
27 QStringLiteral("changed"), receiver: this, SLOT(slotFileMetaDataChanged(QStringList)));
28}
29
30FileMonitor::~FileMonitor() = default;
31
32void FileMonitor::addFile(const QString& fileUrl)
33{
34 QString f = fileUrl;
35 if (f.endsWith(c: QLatin1Char('/'))) {
36 f = f.mid(position: 0, n: f.length() - 1);
37 }
38
39 d->m_files.insert(value: f);
40}
41
42void FileMonitor::addFile(const QUrl& url)
43{
44 const QString localFile = url.toLocalFile();
45 if (!localFile.isEmpty()) {
46 addFile(fileUrl: localFile);
47 }
48}
49
50void FileMonitor::setFiles(const QStringList& fileList)
51{
52 d->m_files = QSet<QString>(fileList.begin(), fileList.end());
53}
54
55QStringList FileMonitor::files() const
56{
57 return QList<QString>(d->m_files.begin(), d->m_files.end());
58}
59
60void FileMonitor::clear()
61{
62 d->m_files.clear();
63}
64
65void FileMonitor::slotFileMetaDataChanged(const QStringList& fileUrls)
66{
67 for (const QString& url : fileUrls) {
68 if (d->m_files.contains(value: url)) {
69 Q_EMIT fileMetaDataChanged(fileUrl: url);
70 }
71 }
72}
73
74#include "moc_filemonitor.cpp"
75

source code of baloo/src/lib/filemonitor.cpp