#include "dir-watcher.h" #include #include #include static std::once_flag flag; static DirWatcher *global_intance = nullptr; DirWatcher::DirWatcher(QObject *parent) : QObject(parent), m_semaphore(QStringLiteral("ukui-search-dir-manager-dbus-register-sem"), 0, QSystemSemaphore::AccessMode::Open) { m_dbusInterface = new QDBusInterface("com.ukui.search.fileindex.service", "/org/ukui/search/privateDirWatcher", "org.ukui.search.fileindex"); m_semaphore.acquire(); m_semaphore.release(); if (!m_dbusInterface->isValid()) { qCritical() << "Create privateDirWatcher Interface Failed Because: " << QDBusConnection::sessionBus().lastError(); return; } else { connect(m_dbusInterface, SIGNAL(appendIndexItem(QString, QStringList)), this, SLOT(sendAppendSignal(QString, QStringList))); connect(m_dbusInterface, SIGNAL(removeIndexItem(QString)), this, SLOT(sendRemoveSignal(QString))); } } DirWatcher::~DirWatcher() { if (m_dbusInterface) delete m_dbusInterface; m_dbusInterface = nullptr; } DirWatcher *DirWatcher::getDirWatcher() { std::call_once(flag, [ & ] { global_intance = new DirWatcher(); }); return global_intance; } void DirWatcher::initDbusService() { QDBusReply reply = m_dbusInterface->call("initDbusService"); if (!reply.isValid()) { qCritical() << "initDbusService call filed!"; } } QStringList DirWatcher::currentIndexableDir() { QDBusReply reply = m_dbusInterface->call("currentIndexableDir"); if (!reply.isValid()) { qCritical() << "currentIndexableDir call filed!"; return QStringList() << QString(QDir::homePath()); } return reply.value(); } QStringList DirWatcher::currentBlackListOfIndex() { QDBusReply reply = m_dbusInterface->call("currentBlackListOfIndex"); if (!reply.isValid()) { qCritical() << "currentBlackListOfIndex call filed!"; return QStringList(); } return reply.value(); } /** * @brief DirWatcher::blackListOfDir 根据传入目录返回当前目录下的所有不可搜索目录,没有则返回空列表 * @param dirPath 要搜索的目录 */ QStringList DirWatcher::blackListOfDir(const QString &dirPath) { QDBusReply reply = m_dbusInterface->call("blackListOfDir", dirPath); if (!reply.isValid()) { qCritical() << "blackListOfDir call filed!"; return QStringList(); } return reply.value(); } QStringList DirWatcher::currentSearchableDir() { QDBusReply reply = m_dbusInterface->call("currentSearchableDir"); if (!reply.isValid()) { qCritical() << "currentSearchableDir call filed!"; return QStringList(); } return reply.value(); } QStringList DirWatcher::searchableDirForSearchApplication() { QDBusReply reply = m_dbusInterface->call("searchableDirForSearchApplication"); if (!reply.isValid()) { qCritical() << "searchableDirForSearchApplication call filed!"; return QStringList(); } return reply.value(); } void DirWatcher::appendIndexableListItem(const QString &path) { QDBusReply reply = m_dbusInterface->call("appendIndexableListItem", path); if (!reply.isValid()) { qCritical() << "appendIndexableListItem call filed!"; } } void DirWatcher::removeIndexableListItem(const QString &path) { QDBusReply reply = m_dbusInterface->call("removeIndexableListItem", path); if (!reply.isValid()) { qCritical() << "removeIndexableListItem call filed!"; } } void DirWatcher::sendAppendSignal(const QString &path, const QStringList &blockList) { Q_EMIT this->appendIndexItem(path, blockList); } void DirWatcher::sendRemoveSignal(const QString &path) { Q_EMIT this->removeIndexItem(path); }