File indexing completed on 2024-04-21 11:25:13

0001 /*
0002     SPDX-FileCopyrightText: 2013 Vishesh Handa <me@vhanda.in>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "filemonitor.h"
0008 
0009 #include <QSet>
0010 #include <QString>
0011 #include <QStringList>
0012 #include <QDBusConnection>
0013 
0014 using namespace Baloo;
0015 
0016 class BALOO_CORE_NO_EXPORT FileMonitor::Private {
0017 public:
0018     QSet<QString> m_files;
0019 };
0020 
0021 FileMonitor::FileMonitor(QObject* parent)
0022     : QObject(parent)
0023     , d(new Private)
0024 {
0025     QDBusConnection con = QDBusConnection::sessionBus();
0026     con.connect(QString(), QStringLiteral("/files"), QStringLiteral("org.kde"),
0027                 QStringLiteral("changed"), this, SLOT(slotFileMetaDataChanged(QStringList)));
0028 }
0029 
0030 FileMonitor::~FileMonitor()
0031 {
0032     delete d;
0033 }
0034 
0035 void FileMonitor::addFile(const QString& fileUrl)
0036 {
0037     QString f = fileUrl;
0038     if (f.endsWith(QLatin1Char('/'))) {
0039         f = f.mid(0, f.length() - 1);
0040     }
0041 
0042     d->m_files.insert(f);
0043 }
0044 
0045 void FileMonitor::addFile(const QUrl& url)
0046 {
0047     const QString localFile = url.toLocalFile();
0048     if (!localFile.isEmpty()) {
0049         addFile(localFile);
0050     }
0051 }
0052 
0053 void FileMonitor::setFiles(const QStringList& fileList)
0054 {
0055     d->m_files = QSet<QString>(fileList.begin(), fileList.end());
0056 }
0057 
0058 QStringList FileMonitor::files() const
0059 {
0060     return QList<QString>(d->m_files.begin(), d->m_files.end());
0061 }
0062 
0063 void FileMonitor::clear()
0064 {
0065     d->m_files.clear();
0066 }
0067 
0068 void FileMonitor::slotFileMetaDataChanged(const QStringList& fileUrls)
0069 {
0070     for (const QString& url : fileUrls) {
0071         if (d->m_files.contains(url)) {
0072             Q_EMIT fileMetaDataChanged(url);
0073         }
0074     }
0075 }
0076 
0077 #include "moc_filemonitor.cpp"