ukui-search/ukui-search-service-dir-man.../dirwatcher/dir-watcher.cpp

171 lines
4.7 KiB
C++
Raw Permalink Normal View History

2023-04-11 10:19:35 +08:00
/*
*
* Copyright (C) 2023, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Authors: iaom <zhangpengfei@kylinos.cn>
*/
#include "dir-watcher.h"
#include <QDebug>
#include <QDir>
#include <QDBusConnection>
#include <QDBusArgument>
#include <QThread>
#include <fstab.h>
#include <QMutexLocker>
#include "config.h"
2023-08-15 17:34:41 +08:00
#include "fileindexserviceadaptor.h"
#include "volume-manager.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)
{
//兼容旧版配置
2023-04-08 16:54:01 +08:00
Config::self()->processCompatibleCache();
connect(VolumeManager::self(), &VolumeManager::MountAdded, this, &DirWatcher::mountAdded);
2023-08-15 17:34:41 +08:00
new FileindexAdaptor(this);
}
DirWatcher::~DirWatcher()
{
2023-04-08 16:54:01 +08:00
}
DirWatcher *DirWatcher::getDirWatcher()
{
std::call_once(flag, [ & ] {
global_intance = new DirWatcher();
});
return global_intance;
}
QStringList DirWatcher::currentIndexableDir()
{
2023-04-08 16:54:01 +08:00
return currentSearchDirs();
}
QStringList DirWatcher::currentBlackListOfIndex()
{
2023-04-08 16:54:01 +08:00
return currentBlackList();
}
/**
* @brief DirWatcher::blackListOfDir
* @param dirPath
*/
QStringList DirWatcher::blackListOfDir(const QString &dirPath)
{
2023-04-08 16:54:01 +08:00
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()
{
2023-04-08 16:54:01 +08:00
return currentSearchDirs();
}
QStringList DirWatcher::searchableDirForSearchApplication()
{
2023-04-08 16:54:01 +08:00
return currentSearchDirs();
}
int DirWatcher::appendIndexableListItem(const QString &path)
{
2023-04-08 16:54:01 +08:00
return appendSearchDir(path);
/* code:
2023-04-08 16:54:01 +08:00
* 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)
{
2023-04-08 16:54:01 +08:00
removeSearchDir(path);
return true;
}
int DirWatcher::appendSearchDir(const QString &path)
{
SearchDir dir(path);
2023-04-08 16:54:01 +08:00
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);
2023-04-08 16:54:01 +08:00
Q_EMIT this->removeIndexItem(path);
return;
}
QStringList DirWatcher::currentSearchDirs()
{
return Config::self()->searchDirs();
}
QStringList DirWatcher::currentBlackList()
{
return Config::self()->blackDirs();
}
QStringList DirWatcher::blockDirsForUser()
{
return Config::self()->blockDirsForUser();
}
int DirWatcher::addBlockDirOfUser(const QString &dir)
{
return Config::self()->addBlockDirOfUser(dir);
}
void DirWatcher::removeBlockDirOfUser(const QString &dir)
{
Config::self()->removeBlockDirOfUser(dir);
}