搜索目录配置文件改为使用json存储.
This commit is contained in:
parent
360f991846
commit
6ec699e231
|
@ -19,12 +19,18 @@
|
|||
*/
|
||||
#include "config.h"
|
||||
#include <QDir>
|
||||
#include <QUrl>
|
||||
#include <mutex>
|
||||
#include <QFile>
|
||||
#include <QDebug>
|
||||
#include <QMap>
|
||||
#include <QVariant>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonArray>
|
||||
|
||||
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 CONFIG_VERSION_KEY = "ConfigVersion";
|
||||
static const QString CONFIG_VERSION = "1.0";
|
||||
|
@ -45,19 +51,22 @@ Config *Config::self()
|
|||
|
||||
void Config::addDir(const SearchDir &dir)
|
||||
{
|
||||
m_searchDirs.append(dir);
|
||||
m_settings->beginGroup(SEARCH_DIRS_GROUP);
|
||||
m_settings->setValue(QUrl::fromLocalFile(dir.getPath()).toString(), dir.getBlackList());
|
||||
m_settings->endGroup();
|
||||
m_settings->sync();
|
||||
QJsonObject searchDirData = m_settingsData.value(SEARCH_DIRS_GROUP).toObject();
|
||||
QJsonArray blackList;
|
||||
for(const QString& path : dir.getBlackList()) {
|
||||
blackList.append(path);
|
||||
}
|
||||
searchDirData.insert(dir.getPath(), blackList);
|
||||
m_settingsData.insert(SEARCH_DIRS_GROUP, searchDirData);
|
||||
save(m_settingsData);
|
||||
}
|
||||
|
||||
QStringList Config::removeDir(const SearchDir &dir)
|
||||
{
|
||||
QString url = QUrl::fromLocalFile(dir.getPath()).toString();
|
||||
QStringList blackDirs = m_settings->value(SEARCH_DIRS_GROUP + "/" + url).toStringList();
|
||||
removeDir(url);
|
||||
m_searchDirs.removeOne(dir);
|
||||
QJsonObject searchDirData = m_settingsData.value(SEARCH_DIRS_GROUP).toObject();
|
||||
QStringList blackDirs = searchDirData.take(dir.getPath()).toVariant().toStringList();
|
||||
m_settingsData.insert(SEARCH_DIRS_GROUP, searchDirData);
|
||||
save(m_settingsData);
|
||||
return blackDirs;
|
||||
}
|
||||
|
||||
|
@ -66,119 +75,178 @@ bool Config::isCompatibilityMode()
|
|||
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 tmp;
|
||||
for(const SearchDir& dir : m_searchDirs) {
|
||||
if(QFile::exists(dir.getPath())) {
|
||||
tmp.append(dir.getPath());
|
||||
bool needSave = false;
|
||||
QJsonObject searchDirData = m_settingsData.value(SEARCH_DIRS_GROUP).toObject();
|
||||
for(const QString& path : searchDirData.keys()) {
|
||||
if(QFile::exists(path)) {
|
||||
tmp.append(path);
|
||||
} else {
|
||||
m_searchDirs.removeOne(dir);
|
||||
removeDir(QUrl::fromLocalFile(dir.getPath()).toString());
|
||||
searchDirData.remove(path);
|
||||
needSave = true;
|
||||
}
|
||||
}
|
||||
if(needSave) {
|
||||
m_settingsData.insert(SEARCH_DIRS_GROUP, searchDirData);
|
||||
save(m_settingsData);
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
QStringList Config::blackDirs()
|
||||
{
|
||||
QStringList tmp;
|
||||
for(const SearchDir& dir : m_searchDirs) {
|
||||
if(QFile::exists(dir.getPath())) {
|
||||
tmp.append(dir.getBlackList());
|
||||
bool needSave = false;
|
||||
QJsonObject searchDir = m_settingsData.value(SEARCH_DIRS_GROUP).toObject();
|
||||
for(const QString& path : searchDir.keys()) {
|
||||
if(QFile::exists(path)) {
|
||||
tmp.append(searchDir.value(path).toVariant().toStringList());
|
||||
} else {
|
||||
m_searchDirs.removeOne(dir);
|
||||
removeDir(QUrl::fromLocalFile(dir.getPath()).toString());
|
||||
searchDir.remove(path);
|
||||
needSave = true;
|
||||
}
|
||||
}
|
||||
if(needSave) {
|
||||
m_settingsData.insert(SEARCH_DIRS_GROUP, searchDir);
|
||||
save(m_settingsData);
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
QStringList Config::globalBlackList() const
|
||||
{
|
||||
return m_settings->value(GLOBAL_SETTINGS_GROUP + "/" + GLOBAL_BLACK_LIST_KEY).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;
|
||||
return m_settingsData.value(GLOBAL_SETTINGS_GROUP).toObject().value(GLOBAL_BLACK_LIST_KEY).toVariant().toStringList();
|
||||
}
|
||||
|
||||
Config::Config()
|
||||
{
|
||||
m_settings = new QSettings(CURRENT_INDEXABLE_DIR_SETTINGS, QSettings::IniFormat);
|
||||
if(!(m_settings->value(GLOBAL_SETTINGS_GROUP + "/" + CONFIG_VERSION_KEY).toString() == CONFIG_VERSION)) {
|
||||
m_oldSettings = new QSettings(OLD_INDEXABLE_DIR_SETTINGS, QSettings::IniFormat);
|
||||
QFile settingsFile(SEARCH_DIRS_SETTINGS);
|
||||
|
||||
if(!settingsFile.exists()) {
|
||||
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)) {
|
||||
SearchDir dir(path, false);
|
||||
qDebug() << "Found old config path" << path;
|
||||
m_compatibleCache.append(dir);
|
||||
}
|
||||
}
|
||||
m_settings->remove(INDEXABLE_DIR_KEY);
|
||||
m_settings->beginGroup(GLOBAL_SETTINGS_GROUP);
|
||||
m_settings->setValue(CONFIG_VERSION_KEY, CONFIG_VERSION);
|
||||
m_settings->endGroup();
|
||||
} else {
|
||||
m_settings->beginGroup(SEARCH_DIRS_GROUP);
|
||||
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);
|
||||
}
|
||||
|
||||
//老版本配置会在更新后被清空,如果allKeys为空说明已经运行过新版本,此时如果新版配置不存在则添加家目录作为默认搜索目录
|
||||
if(m_compatibleCache.isEmpty() && m_oldSettings->allKeys().isEmpty()) {
|
||||
SearchDir dir(HOME_PATH, false);
|
||||
m_compatibleCache.append(dir);
|
||||
}
|
||||
m_settings->endGroup();
|
||||
m_oldSettings->clear();
|
||||
m_oldSettings->sync();
|
||||
initSettingsData(m_settingsData);
|
||||
save(m_settingsData);
|
||||
return;
|
||||
}
|
||||
|
||||
m_settings->beginGroup(GLOBAL_SETTINGS_GROUP);
|
||||
if(m_settings->value(GLOBAL_BLACK_LIST_KEY).isNull()) {
|
||||
m_settings->setValue(GLOBAL_BLACK_LIST_KEY, GLOBAL_BLACK_LIST);
|
||||
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 {
|
||||
m_settingsData.swap(settingsData);
|
||||
}
|
||||
m_settings->endGroup();
|
||||
|
||||
m_settings->sync();
|
||||
QStringList pathToRemove;
|
||||
for(const QString& path : m_settingsData.value(SEARCH_DIRS_GROUP).toObject().keys()) {
|
||||
if(!QFile::exists(path)) {
|
||||
pathToRemove.append(path);
|
||||
}
|
||||
}
|
||||
if(!pathToRemove.isEmpty()) {
|
||||
removeDir(pathToRemove);
|
||||
}
|
||||
}
|
||||
|
||||
void Config::removeDir(const QString &url)
|
||||
void Config::removeDir(const QStringList &paths)
|
||||
{
|
||||
m_settings->beginGroup(SEARCH_DIRS_GROUP);
|
||||
m_settings->remove(url);
|
||||
m_settings->endGroup();
|
||||
m_settings->sync();
|
||||
QJsonObject searchDirData = m_settingsData.value(SEARCH_DIRS_GROUP).toObject();
|
||||
for(const QString& path : paths) {
|
||||
searchDirData.remove(path);
|
||||
}
|
||||
m_settingsData.insert(SEARCH_DIRS_GROUP, searchDirData);
|
||||
save(m_settingsData);
|
||||
}
|
||||
|
||||
void Config::processCompatibleCache()
|
||||
{
|
||||
if(this->isCompatibilityMode()) {
|
||||
m_settings->beginGroup(SEARCH_DIRS_GROUP);
|
||||
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) {
|
||||
dir.generateBlackList();
|
||||
if (dir.error() == SearchDir::ErrorInfo::Successful) {
|
||||
m_searchDirs.append(dir);
|
||||
m_settings->setValue(QUrl::fromLocalFile(dir.getPath()).toString(), dir.getBlackList());
|
||||
if(this->isCompatibilityMode() && !m_compatibleCache.isEmpty()) {
|
||||
QJsonObject searchDirsdata;
|
||||
for (SearchDir dir : m_compatibleCache) {
|
||||
dir.generateBlackList();
|
||||
if (dir.error() == SearchDir::ErrorInfo::Successful) {
|
||||
QJsonArray blackList;
|
||||
for(const QString& path : dir.getBlackList()) {
|
||||
blackList.append(path);
|
||||
}
|
||||
searchDirsdata.insert(dir.getPath(), blackList);
|
||||
m_settingsData.insert(SEARCH_DIRS_GROUP, searchDirsdata);
|
||||
}
|
||||
}
|
||||
m_settings->endGroup();
|
||||
|
||||
m_settings->sync();
|
||||
save(m_settingsData);
|
||||
m_compatibleCache.clear();
|
||||
m_compatibilityMode = false;
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include <QString>
|
||||
#include <QSettings>
|
||||
#include <QVector>
|
||||
#include <QJsonObject>
|
||||
#include "search-dir.h"
|
||||
|
||||
class Config
|
||||
|
@ -35,19 +36,20 @@ public:
|
|||
* @brief 处理老版本数据,需要调用
|
||||
*/
|
||||
void processCompatibleCache();
|
||||
QVector<SearchDir> searchDirObjects();
|
||||
QStringList searchDirs();
|
||||
QStringList blackDirs();
|
||||
QStringList globalBlackList() const;
|
||||
|
||||
private:
|
||||
Config();
|
||||
void removeDir(const QString& url);
|
||||
void removeDir(const QStringList& paths);
|
||||
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;
|
||||
QVector<SearchDir> m_searchDirs;
|
||||
QVector<SearchDir> m_compatibleCache;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue