From e52bc8ca769c74ae5f8f2347e92801809d1890c0 Mon Sep 17 00:00:00 2001 From: JunjieBai Date: Sat, 6 May 2023 09:19:50 +0800 Subject: [PATCH] Use json to optimize the config file of user's block dir. --- libsearch/dirwatcher/dir-watcher.cpp | 10 ++ libsearch/dirwatcher/dir-watcher.h | 2 + libsearch/index/search-manager.cpp | 4 +- search-ukcc-plugin/search.cpp | 29 ++--- search-ukcc-plugin/search.h | 1 - .../dirwatcher/config.cpp | 116 +++++++++++++++++- .../dirwatcher/config.h | 5 +- .../dirwatcher/dir-watcher.cpp | 15 +++ .../dirwatcher/dir-watcher.h | 3 + 9 files changed, 156 insertions(+), 29 deletions(-) diff --git a/libsearch/dirwatcher/dir-watcher.cpp b/libsearch/dirwatcher/dir-watcher.cpp index 4c5891a..4f0442b 100644 --- a/libsearch/dirwatcher/dir-watcher.cpp +++ b/libsearch/dirwatcher/dir-watcher.cpp @@ -99,6 +99,16 @@ QStringList DirWatcher::blackListOfDir(const QString &dirPath) return reply.value(); } +QStringList DirWatcher::getBlockDirsOfUser() +{ + QDBusReply reply = m_dbusInterface->call("blockDirsForUser"); + if (!reply.isValid()) { + qCritical() << "blockDirsForUser call filed!"; + return QStringList(); + } + return reply.value(); +} + QStringList DirWatcher::currentSearchableDir() { QDBusReply reply = m_dbusInterface->call("currentSearchableDir"); diff --git a/libsearch/dirwatcher/dir-watcher.h b/libsearch/dirwatcher/dir-watcher.h index 2b5bc57..7a5b3b1 100644 --- a/libsearch/dirwatcher/dir-watcher.h +++ b/libsearch/dirwatcher/dir-watcher.h @@ -39,6 +39,8 @@ public Q_SLOTS: QStringList searchableDirForSearchApplication(); QStringList blackListOfDir(const QString &dirPath); + QStringList getBlockDirsOfUser(); + void appendSearchDir(const QString &path); void removeSearchDir(const QString &path); void appendIndexableListItem(const QString &path); diff --git a/libsearch/index/search-manager.cpp b/libsearch/index/search-manager.cpp index b05068b..4d6df63 100644 --- a/libsearch/index/search-manager.cpp +++ b/libsearch/index/search-manager.cpp @@ -47,7 +47,7 @@ int SearchManager::getCurrentIndexCount() { } bool SearchManager::isBlocked(QString &path) { - QStringList blockList = GlobalSettings::getInstance()->getBlockDirs(); + QStringList blockList = DirWatcher::getDirWatcher()->getBlockDirsOfUser(); for(QString i : blockList) { if(FileUtils::isOrUnder(path, i)) return true; @@ -493,7 +493,7 @@ DirectSearch::DirectSearch(QString keyword, DataQueuegetBlockDirs(); + QStringList blockList = DirWatcher::getDirWatcher()->getBlockDirsOfUser(); QStringList searchPath = DirWatcher::getDirWatcher()->currentIndexableDir(); QQueue bfs; for (const QString &path : searchPath) { diff --git a/search-ukcc-plugin/search.cpp b/search-ukcc-plugin/search.cpp index 09d6019..348d113 100644 --- a/search-ukcc-plugin/search.cpp +++ b/search-ukcc-plugin/search.cpp @@ -48,9 +48,6 @@ Search::Search() QDBusConnection::sessionBus(), 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); if (QGSettings::isSchemaInstalled(id)) { m_gsettings = new QGSettings(id, QByteArray(), this); @@ -522,17 +519,10 @@ void Search::initFileDialog() */ void Search::getBlockDirs() { - m_blockDirs.clear(); - if (m_dirSettings) { - QStringList blockList = m_dirSettings->allKeys(); - for (const QString &blockDir : blockList) { - QString wholePath = "/" + blockDir; - if (QFile::exists(wholePath)) { - m_blockDirs << wholePath; - } else { - m_dirSettings->remove(blockDir); - m_dirSettings->sync(); - } + if (m_interface->isValid()) { + QDBusReply reply = m_interface->call("blockDirsForUser"); + if (reply.isValid()) { + m_blockDirs = reply.value(); } } } @@ -556,10 +546,13 @@ int Search::setBlockDir(const QString &dirPath, const bool &is_add) } } + if (!m_interface->isValid()) { + return ReturnCode::DirWatcherError; + } + if (!is_add) { //删除黑名单目录 - m_dirSettings->remove(dirPath); - m_dirSettings->sync(); + m_interface->call("removeBlockDirOfUser", dirPath); removeBlockDirFromList(dirPath); return ReturnCode::Successful; } @@ -583,12 +576,10 @@ int Search::setBlockDir(const QString &dirPath, const bool &is_add) //有它的子文件夹已被添加,删除这些子文件夹 if (dir.startsWith(dirPath + "/") || dirPath == "/") { - m_dirSettings->remove(dir); removeBlockDirFromList(dir); } } - m_dirSettings->setValue(dirPath, "0"); - m_dirSettings->sync(); + m_interface->call("addBlockDirOfUser", dirPath); appendBlockDirToList(dirPath); return ReturnCode::Successful; } diff --git a/search-ukcc-plugin/search.h b/search-ukcc-plugin/search.h index 68dc0de..aa1c0e9 100644 --- a/search-ukcc-plugin/search.h +++ b/search-ukcc-plugin/search.h @@ -160,7 +160,6 @@ private: QFileDialog *m_searchDirDialog = nullptr; QStringList m_blockDirs; - QSettings * m_dirSettings = nullptr; void getBlockDirs(); int setBlockDir(const QString &dirPath, const bool &is_add = true); void appendBlockDirToList(const QString &path); diff --git a/ukui-search-service-dir-manager/dirwatcher/config.cpp b/ukui-search-service-dir-manager/dirwatcher/config.cpp index b101ff9..1af8b68 100644 --- a/ukui-search-service-dir-manager/dirwatcher/config.cpp +++ b/ukui-search-service-dir-manager/dirwatcher/config.cpp @@ -28,12 +28,14 @@ #include 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 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 INDEXABLE_DIR_KEY = "IndexableDir"; //兼容历史版本 static const QString CONFIG_VERSION_KEY = "ConfigVersion"; 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 QStringList GLOBAL_BLACK_LIST{"/proc", "/sys", "/dev", "/tmp", "/run"}; static const QString GLOBAL_SETTINGS_GROUP = "GlobalSettings"; @@ -70,6 +72,56 @@ QStringList Config::removeDir(const SearchDir &dir) 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() { 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(); } +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() { - 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); if(!settingsFile.exists()) { m_compatibilityMode = true; - qDebug() << m_oldSettings->value(INDEXABLE_DIR_KEY + "/" + INDEXABLE_DIR_KEY).toStringList(); - for(const QString& path : m_oldSettings->value(INDEXABLE_DIR_KEY + "/" + INDEXABLE_DIR_KEY).toStringList()) { + qDebug() << 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)) { SearchDir dir(path, false); qDebug() << "Found old config path" << path; @@ -175,13 +266,19 @@ Config::Config() } //老版本配置会在更新后被清空,如果allKeys为空说明已经运行过新版本,此时如果新版配置不存在则添加家目录作为默认搜索目录 - if(m_compatibleCache.isEmpty() && m_oldSettings->allKeys().isEmpty()) { + if(m_compatibleCache.isEmpty() && oldSettings.allKeys().isEmpty()) { SearchDir dir(HOME_PATH, false); m_compatibleCache.append(dir); } - m_oldSettings->clear(); - m_oldSettings->sync(); + oldSettings.clear(); + oldSettings.sync(); + 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); return; } @@ -208,6 +305,13 @@ Config::Config() //TODO:这里做一些兼容处理 } else { 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; diff --git a/ukui-search-service-dir-manager/dirwatcher/config.h b/ukui-search-service-dir-manager/dirwatcher/config.h index f9f5a99..97b6a29 100644 --- a/ukui-search-service-dir-manager/dirwatcher/config.h +++ b/ukui-search-service-dir-manager/dirwatcher/config.h @@ -32,6 +32,9 @@ public: void addDir(const SearchDir& dir); QStringList removeDir(const SearchDir& dir); + int addBlockDirOfUser(const QString& dir); + void removeBlockDirOfUser(const QString& dir); + /** * @brief 处理老版本数据,需要调用 */ @@ -39,6 +42,7 @@ public: QStringList searchDirs(); QStringList blackDirs(); QStringList globalBlackList() const; + QStringList blockDirsForUser(); private: Config(); @@ -47,7 +51,6 @@ private: void save(const QJsonObject &jsonDocData); void initSettingsData(QJsonObject &data); - QSettings *m_oldSettings = nullptr; QJsonObject m_settingsData; bool m_compatibilityMode = false; QVector m_compatibleCache; diff --git a/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp b/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp index 59116bd..5c3e73a 100644 --- a/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp +++ b/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp @@ -150,3 +150,18 @@ 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); +} diff --git a/ukui-search-service-dir-manager/dirwatcher/dir-watcher.h b/ukui-search-service-dir-manager/dirwatcher/dir-watcher.h index 3f6adc1..39feb99 100644 --- a/ukui-search-service-dir-manager/dirwatcher/dir-watcher.h +++ b/ukui-search-service-dir-manager/dirwatcher/dir-watcher.h @@ -71,6 +71,9 @@ public Q_SLOTS: QStringList currentSearchDirs(); QStringList currentBlackList(); + QStringList blockDirsForUser(); + int addBlockDirOfUser(const QString& dir); + void removeBlockDirOfUser(const QString& dir); private: DirWatcher(QObject *parent = nullptr);