Use json to optimize the config file of user's block dir.
This commit is contained in:
parent
02dfef8627
commit
e52bc8ca76
|
@ -99,6 +99,16 @@ QStringList DirWatcher::blackListOfDir(const QString &dirPath)
|
||||||
return reply.value();
|
return reply.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QStringList DirWatcher::getBlockDirsOfUser()
|
||||||
|
{
|
||||||
|
QDBusReply<QStringList> reply = m_dbusInterface->call("blockDirsForUser");
|
||||||
|
if (!reply.isValid()) {
|
||||||
|
qCritical() << "blockDirsForUser call filed!";
|
||||||
|
return QStringList();
|
||||||
|
}
|
||||||
|
return reply.value();
|
||||||
|
}
|
||||||
|
|
||||||
QStringList DirWatcher::currentSearchableDir()
|
QStringList DirWatcher::currentSearchableDir()
|
||||||
{
|
{
|
||||||
QDBusReply<QStringList> reply = m_dbusInterface->call("currentSearchableDir");
|
QDBusReply<QStringList> reply = m_dbusInterface->call("currentSearchableDir");
|
||||||
|
|
|
@ -39,6 +39,8 @@ public Q_SLOTS:
|
||||||
QStringList searchableDirForSearchApplication();
|
QStringList searchableDirForSearchApplication();
|
||||||
QStringList blackListOfDir(const QString &dirPath);
|
QStringList blackListOfDir(const QString &dirPath);
|
||||||
|
|
||||||
|
QStringList getBlockDirsOfUser();
|
||||||
|
|
||||||
void appendSearchDir(const QString &path);
|
void appendSearchDir(const QString &path);
|
||||||
void removeSearchDir(const QString &path);
|
void removeSearchDir(const QString &path);
|
||||||
void appendIndexableListItem(const QString &path);
|
void appendIndexableListItem(const QString &path);
|
||||||
|
|
|
@ -47,7 +47,7 @@ int SearchManager::getCurrentIndexCount() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SearchManager::isBlocked(QString &path) {
|
bool SearchManager::isBlocked(QString &path) {
|
||||||
QStringList blockList = GlobalSettings::getInstance()->getBlockDirs();
|
QStringList blockList = DirWatcher::getDirWatcher()->getBlockDirsOfUser();
|
||||||
for(QString i : blockList) {
|
for(QString i : blockList) {
|
||||||
if(FileUtils::isOrUnder(path, i))
|
if(FileUtils::isOrUnder(path, i))
|
||||||
return true;
|
return true;
|
||||||
|
@ -493,7 +493,7 @@ DirectSearch::DirectSearch(QString keyword, DataQueue<SearchPluginIface::ResultI
|
||||||
}
|
}
|
||||||
|
|
||||||
void DirectSearch::run() {
|
void DirectSearch::run() {
|
||||||
QStringList blockList = GlobalSettings::getInstance()->getBlockDirs();
|
QStringList blockList = DirWatcher::getDirWatcher()->getBlockDirsOfUser();
|
||||||
QStringList searchPath = DirWatcher::getDirWatcher()->currentIndexableDir();
|
QStringList searchPath = DirWatcher::getDirWatcher()->currentIndexableDir();
|
||||||
QQueue<QString> bfs;
|
QQueue<QString> bfs;
|
||||||
for (const QString &path : searchPath) {
|
for (const QString &path : searchPath) {
|
||||||
|
|
|
@ -48,9 +48,6 @@ Search::Search()
|
||||||
QDBusConnection::sessionBus(),
|
QDBusConnection::sessionBus(),
|
||||||
this);
|
this);
|
||||||
|
|
||||||
m_dirSettings = new QSettings(QDir::homePath() + CONFIG_FILE, QSettings::NativeFormat, this);
|
|
||||||
m_dirSettings->setIniCodec(QTextCodec::codecForName("UTF-8"));
|
|
||||||
|
|
||||||
const QByteArray id(UKUI_SEARCH_SCHEMAS);
|
const QByteArray id(UKUI_SEARCH_SCHEMAS);
|
||||||
if (QGSettings::isSchemaInstalled(id)) {
|
if (QGSettings::isSchemaInstalled(id)) {
|
||||||
m_gsettings = new QGSettings(id, QByteArray(), this);
|
m_gsettings = new QGSettings(id, QByteArray(), this);
|
||||||
|
@ -522,17 +519,10 @@ void Search::initFileDialog()
|
||||||
*/
|
*/
|
||||||
void Search::getBlockDirs()
|
void Search::getBlockDirs()
|
||||||
{
|
{
|
||||||
m_blockDirs.clear();
|
if (m_interface->isValid()) {
|
||||||
if (m_dirSettings) {
|
QDBusReply<QStringList> reply = m_interface->call("blockDirsForUser");
|
||||||
QStringList blockList = m_dirSettings->allKeys();
|
if (reply.isValid()) {
|
||||||
for (const QString &blockDir : blockList) {
|
m_blockDirs = reply.value();
|
||||||
QString wholePath = "/" + blockDir;
|
|
||||||
if (QFile::exists(wholePath)) {
|
|
||||||
m_blockDirs << wholePath;
|
|
||||||
} else {
|
|
||||||
m_dirSettings->remove(blockDir);
|
|
||||||
m_dirSettings->sync();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -556,10 +546,13 @@ int Search::setBlockDir(const QString &dirPath, const bool &is_add)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!m_interface->isValid()) {
|
||||||
|
return ReturnCode::DirWatcherError;
|
||||||
|
}
|
||||||
|
|
||||||
if (!is_add) {
|
if (!is_add) {
|
||||||
//删除黑名单目录
|
//删除黑名单目录
|
||||||
m_dirSettings->remove(dirPath);
|
m_interface->call("removeBlockDirOfUser", dirPath);
|
||||||
m_dirSettings->sync();
|
|
||||||
removeBlockDirFromList(dirPath);
|
removeBlockDirFromList(dirPath);
|
||||||
return ReturnCode::Successful;
|
return ReturnCode::Successful;
|
||||||
}
|
}
|
||||||
|
@ -583,12 +576,10 @@ int Search::setBlockDir(const QString &dirPath, const bool &is_add)
|
||||||
|
|
||||||
//有它的子文件夹已被添加,删除这些子文件夹
|
//有它的子文件夹已被添加,删除这些子文件夹
|
||||||
if (dir.startsWith(dirPath + "/") || dirPath == "/") {
|
if (dir.startsWith(dirPath + "/") || dirPath == "/") {
|
||||||
m_dirSettings->remove(dir);
|
|
||||||
removeBlockDirFromList(dir);
|
removeBlockDirFromList(dir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_dirSettings->setValue(dirPath, "0");
|
m_interface->call("addBlockDirOfUser", dirPath);
|
||||||
m_dirSettings->sync();
|
|
||||||
appendBlockDirToList(dirPath);
|
appendBlockDirToList(dirPath);
|
||||||
return ReturnCode::Successful;
|
return ReturnCode::Successful;
|
||||||
}
|
}
|
||||||
|
|
|
@ -160,7 +160,6 @@ private:
|
||||||
QFileDialog *m_searchDirDialog = nullptr;
|
QFileDialog *m_searchDirDialog = nullptr;
|
||||||
|
|
||||||
QStringList m_blockDirs;
|
QStringList m_blockDirs;
|
||||||
QSettings * m_dirSettings = nullptr;
|
|
||||||
void getBlockDirs();
|
void getBlockDirs();
|
||||||
int setBlockDir(const QString &dirPath, const bool &is_add = true);
|
int setBlockDir(const QString &dirPath, const bool &is_add = true);
|
||||||
void appendBlockDirToList(const QString &path);
|
void appendBlockDirToList(const QString &path);
|
||||||
|
|
|
@ -28,12 +28,14 @@
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
|
|
||||||
static const QString HOME_PATH = QDir::homePath();
|
static const QString HOME_PATH = QDir::homePath();
|
||||||
|
static const QString OLD_BLOCK_DIR_SETTINGS = HOME_PATH + "/.config/org.ukui/ukui-search/ukui-search-block-dirs.conf";
|
||||||
static const QString OLD_INDEXABLE_DIR_SETTINGS = HOME_PATH + "/.config/org.ukui/ukui-search/ukui-search-current-indexable-dir.conf";
|
static const QString OLD_INDEXABLE_DIR_SETTINGS = HOME_PATH + "/.config/org.ukui/ukui-search/ukui-search-current-indexable-dir.conf";
|
||||||
static const QString SEARCH_DIRS_SETTINGS = HOME_PATH + "/.config/org.ukui/ukui-search/ukui-search-dirs.json";
|
static const QString SEARCH_DIRS_SETTINGS = HOME_PATH + "/.config/org.ukui/ukui-search/ukui-search-dirs.json";
|
||||||
static const QString SEARCH_DIRS_SETTINGS_DIR = HOME_PATH + "/.config/org.ukui/ukui-search";
|
static const QString SEARCH_DIRS_SETTINGS_DIR = HOME_PATH + "/.config/org.ukui/ukui-search";
|
||||||
static const QString INDEXABLE_DIR_KEY = "IndexableDir"; //兼容历史版本
|
static const QString INDEXABLE_DIR_KEY = "IndexableDir"; //兼容历史版本
|
||||||
static const QString CONFIG_VERSION_KEY = "ConfigVersion";
|
static const QString CONFIG_VERSION_KEY = "ConfigVersion";
|
||||||
static const QString CONFIG_VERSION = "1.0";
|
static const QString CONFIG_VERSION = "1.0";
|
||||||
|
static const QString BLOCK_DIRS_FOR_USER_KEY = "BlockDirsForUser";
|
||||||
static const QString GLOBAL_BLACK_LIST_KEY = "GlobalBlackList";
|
static const QString GLOBAL_BLACK_LIST_KEY = "GlobalBlackList";
|
||||||
static const QStringList GLOBAL_BLACK_LIST{"/proc", "/sys", "/dev", "/tmp", "/run"};
|
static const QStringList GLOBAL_BLACK_LIST{"/proc", "/sys", "/dev", "/tmp", "/run"};
|
||||||
static const QString GLOBAL_SETTINGS_GROUP = "GlobalSettings";
|
static const QString GLOBAL_SETTINGS_GROUP = "GlobalSettings";
|
||||||
|
@ -70,6 +72,56 @@ QStringList Config::removeDir(const SearchDir &dir)
|
||||||
return blackDirs;
|
return blackDirs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Config::addBlockDirOfUser(const QString &dir)
|
||||||
|
{
|
||||||
|
if (!QFile::exists(dir)) {
|
||||||
|
return SearchDir::NotExists;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonObject globalSettings = m_settingsData.value(GLOBAL_SETTINGS_GROUP).toObject();
|
||||||
|
QJsonArray blockDirsOfUser = globalSettings.value(BLOCK_DIRS_FOR_USER_KEY).toArray();
|
||||||
|
|
||||||
|
for (auto it = blockDirsOfUser.begin(); it !=blockDirsOfUser.end();) {
|
||||||
|
QString blockDir = (*it).toString();
|
||||||
|
if (dir == blockDir || dir.startsWith(blockDir + "/") || blockDir == "/") {
|
||||||
|
return SearchDir::Duplicated;
|
||||||
|
}
|
||||||
|
//有子文件夹已被添加,删除这些子文件夹
|
||||||
|
if (blockDir.startsWith(dir + "/") || dir == "/") {
|
||||||
|
it = blockDirsOfUser.erase(it);
|
||||||
|
} else {
|
||||||
|
it++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
blockDirsOfUser.append(dir);
|
||||||
|
globalSettings.insert(BLOCK_DIRS_FOR_USER_KEY, blockDirsOfUser);
|
||||||
|
m_settingsData.insert(GLOBAL_SETTINGS_GROUP, globalSettings);
|
||||||
|
save(m_settingsData);
|
||||||
|
return SearchDir::Successful;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Config::removeBlockDirOfUser(const QString &dir)
|
||||||
|
{
|
||||||
|
QJsonObject globalSettings = m_settingsData.value(GLOBAL_SETTINGS_GROUP).toObject();
|
||||||
|
QJsonArray blockDirs = globalSettings.value(BLOCK_DIRS_FOR_USER_KEY).toArray();
|
||||||
|
bool removeItem(false);
|
||||||
|
|
||||||
|
for (auto it = blockDirs.begin(); it != blockDirs.end();) {
|
||||||
|
if ((*it).toString() == dir) {
|
||||||
|
it = blockDirs.erase(it);
|
||||||
|
removeItem = true;
|
||||||
|
} else {
|
||||||
|
it++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (removeItem) {
|
||||||
|
globalSettings.insert(BLOCK_DIRS_FOR_USER_KEY, blockDirs);
|
||||||
|
m_settingsData.insert(GLOBAL_SETTINGS_GROUP, globalSettings);
|
||||||
|
save(m_settingsData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool Config::isCompatibilityMode()
|
bool Config::isCompatibilityMode()
|
||||||
{
|
{
|
||||||
return m_compatibilityMode;
|
return m_compatibilityMode;
|
||||||
|
@ -158,15 +210,54 @@ QStringList Config::globalBlackList() const
|
||||||
return m_settingsData.value(GLOBAL_SETTINGS_GROUP).toObject().value(GLOBAL_BLACK_LIST_KEY).toVariant().toStringList();
|
return m_settingsData.value(GLOBAL_SETTINGS_GROUP).toObject().value(GLOBAL_BLACK_LIST_KEY).toVariant().toStringList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QStringList Config::blockDirsForUser()
|
||||||
|
{
|
||||||
|
bool needSave;
|
||||||
|
QStringList blockDirs;
|
||||||
|
QJsonObject globalSettings = m_settingsData.value(GLOBAL_SETTINGS_GROUP).toObject();
|
||||||
|
for (const QString& dir : globalSettings.value(BLOCK_DIRS_FOR_USER_KEY).toVariant().toStringList()) {
|
||||||
|
if (QFile::exists(dir)) {
|
||||||
|
blockDirs << dir;
|
||||||
|
} else {
|
||||||
|
needSave = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (needSave) {
|
||||||
|
QJsonArray array;
|
||||||
|
for (const QString& dir : blockDirs) {
|
||||||
|
array << dir;
|
||||||
|
}
|
||||||
|
globalSettings.insert(BLOCK_DIRS_FOR_USER_KEY, array);
|
||||||
|
m_settingsData.insert(GLOBAL_SETTINGS_GROUP, globalSettings);
|
||||||
|
save(m_settingsData);
|
||||||
|
}
|
||||||
|
return blockDirs;
|
||||||
|
}
|
||||||
|
|
||||||
Config::Config()
|
Config::Config()
|
||||||
{
|
{
|
||||||
m_oldSettings = new QSettings(OLD_INDEXABLE_DIR_SETTINGS, QSettings::IniFormat);
|
QJsonArray blockDirsForUser;
|
||||||
|
bool oldBlockSettingsExists(false);
|
||||||
|
if (QFile::exists(OLD_BLOCK_DIR_SETTINGS)) {
|
||||||
|
oldBlockSettingsExists = true;
|
||||||
|
QSettings oldBlockSettings(OLD_BLOCK_DIR_SETTINGS, QSettings::IniFormat);
|
||||||
|
for(const QString& blockDir : oldBlockSettings.allKeys()) {
|
||||||
|
QString wholePath = "/" + blockDir;
|
||||||
|
if(QFile::exists(wholePath)) {
|
||||||
|
blockDirsForUser.append(wholePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
QFile::remove(OLD_BLOCK_DIR_SETTINGS);
|
||||||
|
}
|
||||||
|
|
||||||
|
QSettings oldSettings(OLD_INDEXABLE_DIR_SETTINGS, QSettings::IniFormat);
|
||||||
QFile settingsFile(SEARCH_DIRS_SETTINGS);
|
QFile settingsFile(SEARCH_DIRS_SETTINGS);
|
||||||
|
|
||||||
if(!settingsFile.exists()) {
|
if(!settingsFile.exists()) {
|
||||||
m_compatibilityMode = true;
|
m_compatibilityMode = true;
|
||||||
qDebug() << m_oldSettings->value(INDEXABLE_DIR_KEY + "/" + INDEXABLE_DIR_KEY).toStringList();
|
qDebug() << oldSettings.value(INDEXABLE_DIR_KEY + "/" + INDEXABLE_DIR_KEY).toStringList();
|
||||||
for(const QString& path : m_oldSettings->value(INDEXABLE_DIR_KEY + "/" + INDEXABLE_DIR_KEY).toStringList()) {
|
for(const QString& path : oldSettings.value(INDEXABLE_DIR_KEY + "/" + INDEXABLE_DIR_KEY).toStringList()) {
|
||||||
if(QFile::exists(path)) {
|
if(QFile::exists(path)) {
|
||||||
SearchDir dir(path, false);
|
SearchDir dir(path, false);
|
||||||
qDebug() << "Found old config path" << path;
|
qDebug() << "Found old config path" << path;
|
||||||
|
@ -175,13 +266,19 @@ Config::Config()
|
||||||
}
|
}
|
||||||
|
|
||||||
//老版本配置会在更新后被清空,如果allKeys为空说明已经运行过新版本,此时如果新版配置不存在则添加家目录作为默认搜索目录
|
//老版本配置会在更新后被清空,如果allKeys为空说明已经运行过新版本,此时如果新版配置不存在则添加家目录作为默认搜索目录
|
||||||
if(m_compatibleCache.isEmpty() && m_oldSettings->allKeys().isEmpty()) {
|
if(m_compatibleCache.isEmpty() && oldSettings.allKeys().isEmpty()) {
|
||||||
SearchDir dir(HOME_PATH, false);
|
SearchDir dir(HOME_PATH, false);
|
||||||
m_compatibleCache.append(dir);
|
m_compatibleCache.append(dir);
|
||||||
}
|
}
|
||||||
m_oldSettings->clear();
|
oldSettings.clear();
|
||||||
m_oldSettings->sync();
|
oldSettings.sync();
|
||||||
|
|
||||||
initSettingsData(m_settingsData);
|
initSettingsData(m_settingsData);
|
||||||
|
if (!blockDirsForUser.isEmpty()) {
|
||||||
|
QJsonObject globalSettings = m_settingsData.value(GLOBAL_SETTINGS_GROUP).toObject();
|
||||||
|
globalSettings.insert(BLOCK_DIRS_FOR_USER_KEY, blockDirsForUser);
|
||||||
|
m_settingsData.insert(GLOBAL_SETTINGS_GROUP, globalSettings);
|
||||||
|
}
|
||||||
save(m_settingsData);
|
save(m_settingsData);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -208,6 +305,13 @@ Config::Config()
|
||||||
//TODO:这里做一些兼容处理
|
//TODO:这里做一些兼容处理
|
||||||
} else {
|
} else {
|
||||||
m_settingsData.swap(settingsData);
|
m_settingsData.swap(settingsData);
|
||||||
|
//旧的配置文件存在则需要进行同步旧配置
|
||||||
|
if (oldBlockSettingsExists) {
|
||||||
|
QJsonObject globalSettings = m_settingsData.value(GLOBAL_SETTINGS_GROUP).toObject();
|
||||||
|
globalSettings.insert(BLOCK_DIRS_FOR_USER_KEY, blockDirsForUser);
|
||||||
|
m_settingsData.insert(GLOBAL_SETTINGS_GROUP, globalSettings);
|
||||||
|
save(m_settingsData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList pathToRemove;
|
QStringList pathToRemove;
|
||||||
|
|
|
@ -32,6 +32,9 @@ public:
|
||||||
void addDir(const SearchDir& dir);
|
void addDir(const SearchDir& dir);
|
||||||
QStringList removeDir(const SearchDir& dir);
|
QStringList removeDir(const SearchDir& dir);
|
||||||
|
|
||||||
|
int addBlockDirOfUser(const QString& dir);
|
||||||
|
void removeBlockDirOfUser(const QString& dir);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 处理老版本数据,需要调用
|
* @brief 处理老版本数据,需要调用
|
||||||
*/
|
*/
|
||||||
|
@ -39,6 +42,7 @@ public:
|
||||||
QStringList searchDirs();
|
QStringList searchDirs();
|
||||||
QStringList blackDirs();
|
QStringList blackDirs();
|
||||||
QStringList globalBlackList() const;
|
QStringList globalBlackList() const;
|
||||||
|
QStringList blockDirsForUser();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Config();
|
Config();
|
||||||
|
@ -47,7 +51,6 @@ private:
|
||||||
void save(const QJsonObject &jsonDocData);
|
void save(const QJsonObject &jsonDocData);
|
||||||
void initSettingsData(QJsonObject &data);
|
void initSettingsData(QJsonObject &data);
|
||||||
|
|
||||||
QSettings *m_oldSettings = nullptr;
|
|
||||||
QJsonObject m_settingsData;
|
QJsonObject m_settingsData;
|
||||||
bool m_compatibilityMode = false;
|
bool m_compatibilityMode = false;
|
||||||
QVector<SearchDir> m_compatibleCache;
|
QVector<SearchDir> m_compatibleCache;
|
||||||
|
|
|
@ -150,3 +150,18 @@ QStringList DirWatcher::currentBlackList()
|
||||||
{
|
{
|
||||||
return Config::self()->blackDirs();
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -71,6 +71,9 @@ public Q_SLOTS:
|
||||||
QStringList currentSearchDirs();
|
QStringList currentSearchDirs();
|
||||||
QStringList currentBlackList();
|
QStringList currentBlackList();
|
||||||
|
|
||||||
|
QStringList blockDirsForUser();
|
||||||
|
int addBlockDirOfUser(const QString& dir);
|
||||||
|
void removeBlockDirOfUser(const QString& dir);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
DirWatcher(QObject *parent = nullptr);
|
DirWatcher(QObject *parent = nullptr);
|
||||||
|
|
Loading…
Reference in New Issue