增加重构的dirWatcher部分接口定义

This commit is contained in:
iaom 2023-04-06 10:39:10 +08:00
parent af3d9da3fa
commit 7624347cb8
7 changed files with 237 additions and 0 deletions

View File

@ -0,0 +1,95 @@
#include "config.h"
#include <QDir>
#include <QUrl>
#include <mutex>
static const QString HOME_PATH = QDir::homePath();
static const QString CURRENT_INDEXABLE_DIR_SETTINGS = HOME_PATH + "/.config/org.ukui/ukui-search/ukui-search-current-indexable-dir.conf";
static const QString INDEXABLE_DIR_KEY = "IndexableDir"; //兼容历史版本
static const QString CONFIG_VERSION_KEY = "ConfigVersion";
static const QString CONFIG_VERSION = "1.0";
static const QString GLOBAL_BLACK_LIST_KEY = "GlobalBlackList";
static const QStringList GLOBAL_BLACK_LIST{"/proc", "/sys", "/dev", "/tmp", "/run"};
static std::once_flag flag;
static Config *global_intance = nullptr;
Config *Config::self()
{
std::call_once(flag, [ & ] {
global_intance = new Config();
});
return global_intance;
}
void Config::addDir(const SearchDir &dir)
{
m_settings->setValue(dir.getPath(), dir.getBlackList());
m_settings->sync();
}
QStringList Config::removeDir(const SearchDir &dir)
{
QString url = QUrl::fromLocalFile(dir.getPath()).toString();
QStringList blackDirs = m_settings->value(url).toStringList();
m_settings->remove(url);
m_settings->sync();
return blackDirs;
}
bool Config::isCompatibilityMode()
{
return m_compatibilityMode;
}
QStringList Config::searchDirs() const
{
QStringList tmp;
for(const SearchDir& dir : m_searchDirs) {
tmp.append(dir.getPath());
}
return tmp;
}
QStringList Config::blackDirs() const
{
QStringList tmp;
for(const SearchDir& dir : m_searchDirs) {
tmp.append(dir.getBlackList());
}
return tmp;
}
QStringList Config::globalBlackList() const
{
return m_settings->value(GLOBAL_BLACK_LIST_KEY).toStringList();
}
QVector<SearchDir> Config::searchDirObjects() const
{
return m_searchDirs;
}
Config::Config()
{
m_settings = new QSettings(CURRENT_INDEXABLE_DIR_SETTINGS, QSettings::IniFormat);
if(!(m_settings->value(CONFIG_VERSION_KEY).toString() == CONFIG_VERSION)) {
m_compatibilityMode = true;
for(const QString& path : m_settings->value(INDEXABLE_DIR_KEY).toStringList()) {
SearchDir dir(QUrl::fromLocalFile(path).toString());
m_searchDirs.append(dir);
m_settings->setValue(QUrl::fromLocalFile(path).toString(), dir.getBlackList());
}
m_settings->remove(INDEXABLE_DIR_KEY);
m_settings->setValue(CONFIG_VERSION_KEY, CONFIG_VERSION);
} else {
for(const QString& urlStr : m_settings->allKeys()) {
SearchDir dir(QUrl(urlStr).toString(), false);
dir.setBlackList(m_settings->value(urlStr).toStringList());
m_searchDirs.append(dir);
}
}
if(m_settings->value(GLOBAL_BLACK_LIST_KEY).isNull()) {
m_settings->setValue(GLOBAL_BLACK_LIST_KEY, GLOBAL_BLACK_LIST);
}
m_settings->sync();
}

View File

@ -0,0 +1,31 @@
#ifndef CONFIG_H
#define CONFIG_H
#include <QString>
#include <QSettings>
#include <QVector>
#include "search-dir.h"
class Config
{
public:
static Config *self();
void addDir(const SearchDir& dir);
QStringList removeDir(const SearchDir& dir);
/**
* @brief
* @return
*/
bool isCompatibilityMode();
QVector<SearchDir> searchDirObjects() const;
QStringList searchDirs() const;
QStringList blackDirs() const;
QStringList globalBlackList() const;
private:
Config();
QSettings *m_settings = nullptr;
bool m_compatibilityMode = false;
QVector<SearchDir> m_searchDirs;
};
#endif // CONFIG_H

View File

@ -1,11 +1,15 @@
INCLUDEPATH += $$PWD
HEADERS += \
$$PWD/config.h \
$$PWD/dir-watcher-adaptor.h \
$$PWD/dir-watcher.h \
$$PWD/search-dir.h \
$$PWD/volume-manager.h
SOURCES += \
$$PWD/config.cpp \
$$PWD/dir-watcher-adaptor.cpp \
$$PWD/dir-watcher.cpp \
$$PWD/search-dir.cpp \
$$PWD/volume-manager.cpp

View File

@ -7,6 +7,7 @@
#include <QThread>
#include <fstab.h>
#include <QMutexLocker>
#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"
@ -526,6 +527,30 @@ bool DirWatcher::removeIndexableListItem(const QString &path)
return res;
}
int DirWatcher::appendSearchDir(const QString &path)
{
SearchDir dir(path);
Config::self()->addDir(dir);
return 0;
}
void DirWatcher::removeSearchDir(const QString &path)
{
SearchDir dir(path, false);
Config::self()->removeDir(dir);
return;
}
QStringList DirWatcher::currentSearchDirs()
{
return Config::self()->searchDirs();
}
QStringList DirWatcher::currentBlackList()
{
return Config::self()->blackDirs();
}
void DirWatcher::initData()
{
//适配需求,可索引目录为用户指定。

View File

@ -63,6 +63,14 @@ public Q_SLOTS:
Q_SCRIPTABLE int appendIndexableListItem(const QString &path);
Q_SCRIPTABLE bool removeIndexableListItem(const QString &path);
//新接口
int appendSearchDir(const QString &path);
void removeSearchDir(const QString &path);
QStringList currentSearchDirs();
QStringList currentBlackList();
private:
DirWatcher(QObject *parent = nullptr);
~DirWatcher();

View File

@ -0,0 +1,47 @@
#include "search-dir.h"
SearchDir::SearchDir(const QString &path, bool generateBlackList)
{
m_path = path;
if(generateBlackList) {
//根据VulumeManager和Congig的数据计算黑名单here
}
}
SearchDir::ErrorInfo SearchDir::error()
{
return m_error;
}
QString SearchDir::errorString()
{
switch (m_error) {
case ErrorInfo::Successful:
return "";
case ErrorInfo::Duplicated:
return "";
case ErrorInfo::UnderBlackList:
return "";
case ErrorInfo::RepeatMount1:
return "";
case ErrorInfo::RepeatMount2:
return "";
case ErrorInfo::NotExists:
return "";
}
}
QString SearchDir::getPath() const
{
return m_path;
}
void SearchDir::setBlackList(const QStringList &blackList)
{
m_blackList = blackList;
}
QStringList SearchDir::getBlackList() const
{
return m_blackList;
}

View File

@ -0,0 +1,27 @@
#ifndef SEARCHDIR_H
#define SEARCHDIR_H
#include <QStringList>
class SearchDir
{
public:
enum ErrorInfo{
Successful = 0,
Duplicated,
UnderBlackList,
RepeatMount1,
RepeatMount2,
NotExists
};
SearchDir(const QString& path, bool generateBlackList = true);
ErrorInfo error();
QString errorString();
QString getPath() const;
void setBlackList(const QStringList& blackList);
QStringList getBlackList() const;
private:
QString m_path;
QStringList m_blackList;
ErrorInfo m_error = ErrorInfo::Successful;
};
#endif // SEARCHDIR_H