File indexing completed on 2024-04-28 07:40:11

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() = default;
0031 
0032 void FileMonitor::addFile(const QString& fileUrl)
0033 {
0034     QString f = fileUrl;
0035     if (f.endsWith(QLatin1Char('/'))) {
0036         f = f.mid(0, f.length() - 1);
0037     }
0038 
0039     d->m_files.insert(f);
0040 }
0041 
0042 void FileMonitor::addFile(const QUrl& url)
0043 {
0044     const QString localFile = url.toLocalFile();
0045     if (!localFile.isEmpty()) {
0046         addFile(localFile);
0047     }
0048 }
0049 
0050 void FileMonitor::setFiles(const QStringList& fileList)
0051 {
0052     d->m_files = QSet<QString>(fileList.begin(), fileList.end());
0053 }
0054 
0055 QStringList FileMonitor::files() const
0056 {
0057     return QList<QString>(d->m_files.begin(), d->m_files.end());
0058 }
0059 
0060 void FileMonitor::clear()
0061 {
0062     d->m_files.clear();
0063 }
0064 
0065 void FileMonitor::slotFileMetaDataChanged(const QStringList& fileUrls)
0066 {
0067     for (const QString& url : fileUrls) {
0068         if (d->m_files.contains(url)) {
0069             Q_EMIT fileMetaDataChanged(url);
0070         }
0071     }
0072 }
0073 
0074 #include "moc_filemonitor.cpp"