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>
|
|
|
|
|
*/
|
2022-04-25 10:23:20 +08:00
|
|
|
|
#include "dir-watcher.h"
|
|
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QDBusConnection>
|
|
|
|
|
#include <QDBusArgument>
|
|
|
|
|
#include <QThread>
|
|
|
|
|
#include <fstab.h>
|
|
|
|
|
#include <QMutexLocker>
|
2023-04-06 10:39:10 +08:00
|
|
|
|
#include "config.h"
|
2022-04-25 10:23:20 +08:00
|
|
|
|
|
|
|
|
|
#define CURRENT_INDEXABLE_DIR_SETTINGS QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search-current-indexable-dir.conf"
|
|
|
|
|
#define INDEXABLE_DIR_VALUE "IndexableDir"
|
2022-05-31 10:48:13 +08:00
|
|
|
|
#define DEFAULT_INDEXABLE_DIR QDir::homePath()
|
2022-04-25 10:23:20 +08:00
|
|
|
|
|
|
|
|
|
static std::once_flag flag;
|
|
|
|
|
static DirWatcher *global_intance = nullptr;
|
|
|
|
|
|
|
|
|
|
DirWatcher::DirWatcher(QObject *parent) : QObject(parent)
|
|
|
|
|
{
|
2023-04-08 11:45:59 +08:00
|
|
|
|
//兼容旧版配置
|
2023-04-08 16:54:01 +08:00
|
|
|
|
Config::self()->processCompatibleCache();
|
2023-04-08 11:45:59 +08:00
|
|
|
|
|
2022-04-25 10:23:20 +08:00
|
|
|
|
m_adaptor = new DirWatcherAdaptor(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DirWatcher::~DirWatcher()
|
|
|
|
|
{
|
2023-04-08 16:54:01 +08:00
|
|
|
|
|
2022-04-25 10:23:20 +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();
|
2022-12-07 14:04:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-25 10:23:20 +08:00
|
|
|
|
|
|
|
|
|
QStringList DirWatcher::currentBlackListOfIndex()
|
|
|
|
|
{
|
2023-04-08 16:54:01 +08:00
|
|
|
|
return currentBlackList();
|
2022-04-25 10:23:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief DirWatcher::blackListOfDir 根据传入目录返回当前目录下的所有不可搜索目录,没有则返回空列表
|
|
|
|
|
* @param dirPath 要搜索的目录
|
|
|
|
|
*/
|
|
|
|
|
QStringList DirWatcher::blackListOfDir(const QString &dirPath)
|
|
|
|
|
{
|
2023-04-08 16:54:01 +08:00
|
|
|
|
return SearchDir::blackListOfDir(dirPath);
|
2022-04-25 10:23:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
2022-04-25 10:23:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList DirWatcher::searchableDirForSearchApplication()
|
|
|
|
|
{
|
2023-04-08 16:54:01 +08:00
|
|
|
|
return currentSearchDirs();
|
2022-04-25 10:23:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 14:04:21 +08:00
|
|
|
|
int DirWatcher::appendIndexableListItem(const QString &path)
|
2022-04-25 10:23:20 +08:00
|
|
|
|
{
|
2023-04-08 16:54:01 +08:00
|
|
|
|
return appendSearchDir(path);
|
2022-12-07 14:04:21 +08:00
|
|
|
|
/* 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
|
2022-12-07 14:04:21 +08:00
|
|
|
|
*/
|
2022-04-25 10:23:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 14:04:21 +08:00
|
|
|
|
bool DirWatcher::removeIndexableListItem(const QString &path)
|
2022-04-25 10:23:20 +08:00
|
|
|
|
{
|
2023-04-08 16:54:01 +08:00
|
|
|
|
removeSearchDir(path);
|
|
|
|
|
return true;
|
2022-04-25 10:23:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 10:39:10 +08:00
|
|
|
|
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();
|
2023-04-06 10:39:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2023-04-06 10:39:10 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList DirWatcher::currentSearchDirs()
|
|
|
|
|
{
|
|
|
|
|
return Config::self()->searchDirs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList DirWatcher::currentBlackList()
|
|
|
|
|
{
|
|
|
|
|
return Config::self()->blackDirs();
|
|
|
|
|
}
|
2023-05-06 09:19:50 +08:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|