forked from openkylin/ukui-search
Use json to optimize the config file of user's block dir.
This commit is contained in:
parent
f9f6fecd1d
commit
7c0b582f46
|
@ -99,6 +99,16 @@ QStringList DirWatcher::blackListOfDir(const QString &dirPath)
|
|||
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()
|
||||
{
|
||||
QDBusReply<QStringList> reply = m_dbusInterface->call("currentSearchableDir");
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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, DataQueue<SearchPluginIface::ResultI
|
|||
}
|
||||
|
||||
void DirectSearch::run() {
|
||||
QStringList blockList = GlobalSettings::getInstance()->getBlockDirs();
|
||||
QStringList blockList = DirWatcher::getDirWatcher()->getBlockDirsOfUser();
|
||||
QStringList searchPath = DirWatcher::getDirWatcher()->currentIndexableDir();
|
||||
QQueue<QString> bfs;
|
||||
for (const QString &path : searchPath) {
|
||||
|
|
|
@ -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<QStringList> 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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -28,12 +28,14 @@
|
|||
#include <QJsonArray>
|
||||
|
||||
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;
|
||||
|
|
|
@ -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<SearchDir> m_compatibleCache;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue