#include "dir-watcher.h" #include #include #include #include #include #include #include #include "config.h" #define CURRENT_INDEXABLE_DIR_SETTINGS QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search-current-indexable-dir.conf" #define INDEXABLE_DIR_VALUE "IndexableDir" #define DEFAULT_INDEXABLE_DIR QDir::homePath() static std::once_flag flag; static DirWatcher *global_intance = nullptr; DirWatcher::DirWatcher(QObject *parent) : QObject(parent) { //兼容旧版配置 Config::self()->processCompatibleCache(); m_adaptor = new DirWatcherAdaptor(this); } DirWatcher::~DirWatcher() { } DirWatcher *DirWatcher::getDirWatcher() { std::call_once(flag, [ & ] { global_intance = new DirWatcher(); }); return global_intance; } QStringList DirWatcher::currentIndexableDir() { return currentSearchDirs(); } QStringList DirWatcher::currentBlackListOfIndex() { return currentBlackList(); } /** * @brief DirWatcher::blackListOfDir 根据传入目录返回当前目录下的所有不可搜索目录,没有则返回空列表 * @param dirPath 要搜索的目录 */ QStringList DirWatcher::blackListOfDir(const QString &dirPath) { return SearchDir::blackListOfDir(dirPath); } void DirWatcher::initDbusService() { QDBusConnection sessionBus = QDBusConnection::sessionBus(); if (!sessionBus.registerObject("/org/ukui/search/fileindex", this, QDBusConnection::ExportAdaptors)){ qWarning() << "ukui-search-fileindex dbus register object failed reason:" << sessionBus.lastError(); } } QStringList DirWatcher::currentSearchableDir() { return currentSearchDirs(); } QStringList DirWatcher::searchableDirForSearchApplication() { return currentSearchDirs(); } int DirWatcher::appendIndexableListItem(const QString &path) { return appendSearchDir(path); /* code: * 0: successful * 1: path or its parent dir has been added * 2: path is or under blacklist * 3: path is in repeat mounted devices and another path which is in the same device has been indexed * 4: another path which is in the same device has been indexed * 5: path is not exists */ } bool DirWatcher::removeIndexableListItem(const QString &path) { removeSearchDir(path); return true; } int DirWatcher::appendSearchDir(const QString &path) { SearchDir dir(path); if (dir.error() == SearchDir::Successful) { Q_EMIT this->appendIndexItem(path, dir.getBlackList()); qDebug() << "Add search dir:" << path << "blacklist:" << dir.getBlackList(); //要添加已索引目录的父目录,先添加索引,再同步配置文件,从而使得下次读取配置文件时不会将子目录排除掉 for (const QString &searchDir : Config::self()->searchDirs()) { if (searchDir.startsWith(path + "/") || path == "/") { SearchDir subDir(searchDir); Config::self()->removeDir(subDir); } } dir.generateBlackList(); Config::self()->addDir(dir); } qWarning() << dir.getPath() << dir.errorString(); return dir.error(); } void DirWatcher::removeSearchDir(const QString &path) { SearchDir dir(path, false); Config::self()->removeDir(dir); Q_EMIT this->removeIndexItem(path); return; } QStringList DirWatcher::currentSearchDirs() { return Config::self()->searchDirs(); } QStringList DirWatcher::currentBlackList() { return Config::self()->blackDirs(); }