搜索目录配置文件改为使用json存储.

This commit is contained in:
iaom 2023-04-11 20:53:28 +08:00
parent eb0b77035d
commit 5481dd7ed8
2 changed files with 153 additions and 83 deletions

View File

@ -19,12 +19,18 @@
*/ */
#include "config.h" #include "config.h"
#include <QDir> #include <QDir>
#include <QUrl>
#include <mutex> #include <mutex>
#include <QFile> #include <QFile>
#include <QDebug>
#include <QMap>
#include <QVariant>
#include <QJsonDocument>
#include <QJsonArray>
static const QString HOME_PATH = QDir::homePath(); 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 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 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";
@ -45,19 +51,22 @@ Config *Config::self()
void Config::addDir(const SearchDir &dir) void Config::addDir(const SearchDir &dir)
{ {
m_searchDirs.append(dir); QJsonObject searchDirData = m_settingsData.value(SEARCH_DIRS_GROUP).toObject();
m_settings->beginGroup(SEARCH_DIRS_GROUP); QJsonArray blackList;
m_settings->setValue(QUrl::fromLocalFile(dir.getPath()).toString(), dir.getBlackList()); for(const QString& path : dir.getBlackList()) {
m_settings->endGroup(); blackList.append(path);
m_settings->sync(); }
searchDirData.insert(dir.getPath(), blackList);
m_settingsData.insert(SEARCH_DIRS_GROUP, searchDirData);
save(m_settingsData);
} }
QStringList Config::removeDir(const SearchDir &dir) QStringList Config::removeDir(const SearchDir &dir)
{ {
QString url = QUrl::fromLocalFile(dir.getPath()).toString(); QJsonObject searchDirData = m_settingsData.value(SEARCH_DIRS_GROUP).toObject();
QStringList blackDirs = m_settings->value(SEARCH_DIRS_GROUP + "/" + url).toStringList(); QStringList blackDirs = searchDirData.take(dir.getPath()).toVariant().toStringList();
removeDir(url); m_settingsData.insert(SEARCH_DIRS_GROUP, searchDirData);
m_searchDirs.removeOne(dir); save(m_settingsData);
return blackDirs; return blackDirs;
} }
@ -66,119 +75,178 @@ bool Config::isCompatibilityMode()
return m_compatibilityMode; return m_compatibilityMode;
} }
void Config::save(const QJsonObject &jsonDocData)
{
QDir dir;
QString configFileDir(SEARCH_DIRS_SETTINGS_DIR);
if (!dir.exists(configFileDir)) {
if (!dir.mkdir(configFileDir)) {
qWarning() << "Unable to create settings config file.";
return;
}
}
QFile settingsFile(SEARCH_DIRS_SETTINGS);
if(!settingsFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
qWarning() << "Fail to open file " << SEARCH_DIRS_SETTINGS;
}
if (settingsFile.write(QJsonDocument(jsonDocData).toJson()) == -1) {
qWarning() << "Error saving configuration file.";
}
settingsFile.flush();
settingsFile.close();
}
void Config::initSettingsData(QJsonObject &data)
{
//全局配置
QJsonObject globalSettings;
QJsonArray blackList;
for(const QString& path : GLOBAL_BLACK_LIST) {
blackList.append(path);
}
globalSettings.insert(GLOBAL_BLACK_LIST_KEY, blackList);
data.insert(CONFIG_VERSION_KEY, CONFIG_VERSION);
data.insert(GLOBAL_SETTINGS_GROUP, globalSettings);
}
QStringList Config::searchDirs() QStringList Config::searchDirs()
{ {
QStringList tmp; QStringList tmp;
for(const SearchDir& dir : m_searchDirs) { bool needSave = false;
if(QFile::exists(dir.getPath())) { QJsonObject searchDirData = m_settingsData.value(SEARCH_DIRS_GROUP).toObject();
tmp.append(dir.getPath()); for(const QString& path : searchDirData.keys()) {
if(QFile::exists(path)) {
tmp.append(path);
} else { } else {
m_searchDirs.removeOne(dir); searchDirData.remove(path);
removeDir(QUrl::fromLocalFile(dir.getPath()).toString()); needSave = true;
} }
} }
if(needSave) {
m_settingsData.insert(SEARCH_DIRS_GROUP, searchDirData);
save(m_settingsData);
}
return tmp; return tmp;
} }
QStringList Config::blackDirs() QStringList Config::blackDirs()
{ {
QStringList tmp; QStringList tmp;
for(const SearchDir& dir : m_searchDirs) { bool needSave = false;
if(QFile::exists(dir.getPath())) { QJsonObject searchDir = m_settingsData.value(SEARCH_DIRS_GROUP).toObject();
tmp.append(dir.getBlackList()); for(const QString& path : searchDir.keys()) {
if(QFile::exists(path)) {
tmp.append(searchDir.value(path).toVariant().toStringList());
} else { } else {
m_searchDirs.removeOne(dir); searchDir.remove(path);
removeDir(QUrl::fromLocalFile(dir.getPath()).toString()); needSave = true;
} }
} }
if(needSave) {
m_settingsData.insert(SEARCH_DIRS_GROUP, searchDir);
save(m_settingsData);
}
return tmp; return tmp;
} }
QStringList Config::globalBlackList() const QStringList Config::globalBlackList() const
{ {
return m_settings->value(GLOBAL_SETTINGS_GROUP + "/" + GLOBAL_BLACK_LIST_KEY).toStringList(); return m_settingsData.value(GLOBAL_SETTINGS_GROUP).toObject().value(GLOBAL_BLACK_LIST_KEY).toVariant().toStringList();
}
QVector<SearchDir> Config::searchDirObjects()
{
for(const SearchDir& dir : m_searchDirs) {
if(!QFile::exists(dir.getPath())) {
m_searchDirs.removeOne(dir);
removeDir(QUrl::fromLocalFile(dir.getPath()).toString());
}
}
return m_searchDirs;
} }
Config::Config() Config::Config()
{ {
m_settings = new QSettings(CURRENT_INDEXABLE_DIR_SETTINGS, QSettings::IniFormat); m_oldSettings = new QSettings(OLD_INDEXABLE_DIR_SETTINGS, QSettings::IniFormat);
if(!(m_settings->value(GLOBAL_SETTINGS_GROUP + "/" + CONFIG_VERSION_KEY).toString() == CONFIG_VERSION)) { QFile settingsFile(SEARCH_DIRS_SETTINGS);
if(!settingsFile.exists()) {
m_compatibilityMode = true; m_compatibilityMode = true;
for(const QString& path : m_settings->value(INDEXABLE_DIR_KEY + "/" + INDEXABLE_DIR_KEY).toStringList()) { 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()) {
if(QFile::exists(path)) { if(QFile::exists(path)) {
SearchDir dir(path, false); SearchDir dir(path, false);
qDebug() << "Found old config path" << path;
m_compatibleCache.append(dir); m_compatibleCache.append(dir);
} }
} }
m_settings->remove(INDEXABLE_DIR_KEY);
m_settings->beginGroup(GLOBAL_SETTINGS_GROUP); //老版本配置会在更新后被清空如果allKeys为空说明已经运行过新版本此时如果新版配置不存在则添加家目录作为默认搜索目录
m_settings->setValue(CONFIG_VERSION_KEY, CONFIG_VERSION); if(m_compatibleCache.isEmpty() && m_oldSettings->allKeys().isEmpty()) {
m_settings->endGroup(); SearchDir dir(HOME_PATH, false);
m_compatibleCache.append(dir);
}
m_oldSettings->clear();
m_oldSettings->sync();
initSettingsData(m_settingsData);
save(m_settingsData);
return;
}
if (!settingsFile.open(QFile::ReadOnly)) {
qWarning() << "SettingsManagerPrivate: configuration file " << settingsFile.fileName() << "open failed !";
return;
}
QByteArray byteArray = settingsFile.readAll();
settingsFile.close();
QJsonParseError errRpt;
QJsonDocument jsonDocument(QJsonDocument::fromJson(byteArray, &errRpt));
if (errRpt.error != QJsonParseError::NoError) {
qWarning() << "Incorrect configuration files. JSON parse error";
initSettingsData(m_settingsData);
save(m_settingsData);
return;
}
// 读取配置文件
QJsonObject settingsData = jsonDocument.object();
QString version = settingsData.value(CONFIG_VERSION_KEY).toString();
if (version != QString(CONFIG_VERSION)) {
qWarning() << "Settings version check failed, old: " << version << " new:" << CONFIG_VERSION;
//TODO:这里做一些兼容处理
} else { } else {
m_settings->beginGroup(SEARCH_DIRS_GROUP); m_settingsData.swap(settingsData);
for(const QString& urlStr : m_settings->allKeys()) {
QString path = QUrl(urlStr).toLocalFile();
if(QFile::exists(path)) {
SearchDir dir(path, false);
dir.setBlackList(m_settings->value(urlStr).toStringList());
m_searchDirs.append(dir);
} else {
removeDir(urlStr);
}
}
m_settings->endGroup();
} }
m_settings->beginGroup(GLOBAL_SETTINGS_GROUP); QStringList pathToRemove;
if(m_settings->value(GLOBAL_BLACK_LIST_KEY).isNull()) { for(const QString& path : m_settingsData.value(SEARCH_DIRS_GROUP).toObject().keys()) {
m_settings->setValue(GLOBAL_BLACK_LIST_KEY, GLOBAL_BLACK_LIST); if(!QFile::exists(path)) {
pathToRemove.append(path);
}
}
if(!pathToRemove.isEmpty()) {
removeDir(pathToRemove);
} }
m_settings->endGroup();
m_settings->sync();
} }
void Config::removeDir(const QString &url) void Config::removeDir(const QStringList &paths)
{ {
m_settings->beginGroup(SEARCH_DIRS_GROUP); QJsonObject searchDirData = m_settingsData.value(SEARCH_DIRS_GROUP).toObject();
m_settings->remove(url); for(const QString& path : paths) {
m_settings->endGroup(); searchDirData.remove(path);
m_settings->sync(); }
m_settingsData.insert(SEARCH_DIRS_GROUP, searchDirData);
save(m_settingsData);
} }
void Config::processCompatibleCache() void Config::processCompatibleCache()
{ {
if(this->isCompatibilityMode()) { if(this->isCompatibilityMode() && !m_compatibleCache.isEmpty()) {
m_settings->beginGroup(SEARCH_DIRS_GROUP); QJsonObject searchDirsdata;
if (m_compatibleCache.isEmpty()) {
SearchDir defaultDir(QDir::homePath());
if (defaultDir.error() == SearchDir::ErrorInfo::Successful) {
m_searchDirs.append(defaultDir);
m_settings->setValue(QUrl::fromLocalFile(defaultDir.getPath()).toString(), defaultDir.getBlackList());
}
} else {
for (SearchDir dir : m_compatibleCache) { for (SearchDir dir : m_compatibleCache) {
dir.generateBlackList(); dir.generateBlackList();
if (dir.error() == SearchDir::ErrorInfo::Successful) { if (dir.error() == SearchDir::ErrorInfo::Successful) {
m_searchDirs.append(dir); QJsonArray blackList;
m_settings->setValue(QUrl::fromLocalFile(dir.getPath()).toString(), dir.getBlackList()); for(const QString& path : dir.getBlackList()) {
blackList.append(path);
}
searchDirsdata.insert(dir.getPath(), blackList);
m_settingsData.insert(SEARCH_DIRS_GROUP, searchDirsdata);
} }
} }
} save(m_settingsData);
m_settings->endGroup();
m_settings->sync();
m_compatibleCache.clear(); m_compatibleCache.clear();
m_compatibilityMode = false; m_compatibilityMode = false;
} }

View File

@ -22,6 +22,7 @@
#include <QString> #include <QString>
#include <QSettings> #include <QSettings>
#include <QVector> #include <QVector>
#include <QJsonObject>
#include "search-dir.h" #include "search-dir.h"
class Config class Config
@ -35,19 +36,20 @@ public:
* @brief * @brief
*/ */
void processCompatibleCache(); void processCompatibleCache();
QVector<SearchDir> searchDirObjects();
QStringList searchDirs(); QStringList searchDirs();
QStringList blackDirs(); QStringList blackDirs();
QStringList globalBlackList() const; QStringList globalBlackList() const;
private: private:
Config(); Config();
void removeDir(const QString& url); void removeDir(const QStringList& paths);
bool isCompatibilityMode(); bool isCompatibilityMode();
void save(const QJsonObject &jsonDocData);
void initSettingsData(QJsonObject &data);
QSettings *m_settings = nullptr; QSettings *m_oldSettings = nullptr;
QJsonObject m_settingsData;
bool m_compatibilityMode = false; bool m_compatibilityMode = false;
QVector<SearchDir> m_searchDirs;
QVector<SearchDir> m_compatibleCache; QVector<SearchDir> m_compatibleCache;
}; };