ukui-search/ukui-search-service-dir-man.../dirwatcher/config.cpp

96 lines
2.8 KiB
C++
Raw Normal View History

#include "config.h"
#include <QDir>
#include <QUrl>
#include <mutex>
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 INDEXABLE_DIR_KEY = "IndexableDir"; //兼容历史版本
static const QString CONFIG_VERSION_KEY = "ConfigVersion";
static const QString CONFIG_VERSION = "1.0";
static const QString GLOBAL_BLACK_LIST_KEY = "GlobalBlackList";
static const QStringList GLOBAL_BLACK_LIST{"/proc", "/sys", "/dev", "/tmp", "/run"};
static std::once_flag flag;
static Config *global_intance = nullptr;
Config *Config::self()
{
std::call_once(flag, [ & ] {
global_intance = new Config();
});
return global_intance;
}
void Config::addDir(const SearchDir &dir)
{
m_settings->setValue(dir.getPath(), dir.getBlackList());
m_settings->sync();
}
QStringList Config::removeDir(const SearchDir &dir)
{
QString url = QUrl::fromLocalFile(dir.getPath()).toString();
QStringList blackDirs = m_settings->value(url).toStringList();
m_settings->remove(url);
m_settings->sync();
return blackDirs;
}
bool Config::isCompatibilityMode()
{
return m_compatibilityMode;
}
QStringList Config::searchDirs() const
{
QStringList tmp;
for(const SearchDir& dir : m_searchDirs) {
tmp.append(dir.getPath());
}
return tmp;
}
QStringList Config::blackDirs() const
{
QStringList tmp;
for(const SearchDir& dir : m_searchDirs) {
tmp.append(dir.getBlackList());
}
return tmp;
}
QStringList Config::globalBlackList() const
{
return m_settings->value(GLOBAL_BLACK_LIST_KEY).toStringList();
}
QVector<SearchDir> Config::searchDirObjects() const
{
return m_searchDirs;
}
Config::Config()
{
m_settings = new QSettings(CURRENT_INDEXABLE_DIR_SETTINGS, QSettings::IniFormat);
if(!(m_settings->value(CONFIG_VERSION_KEY).toString() == CONFIG_VERSION)) {
m_compatibilityMode = true;
for(const QString& path : m_settings->value(INDEXABLE_DIR_KEY).toStringList()) {
SearchDir dir(QUrl::fromLocalFile(path).toString());
m_searchDirs.append(dir);
m_settings->setValue(QUrl::fromLocalFile(path).toString(), dir.getBlackList());
}
m_settings->remove(INDEXABLE_DIR_KEY);
m_settings->setValue(CONFIG_VERSION_KEY, CONFIG_VERSION);
} else {
for(const QString& urlStr : m_settings->allKeys()) {
SearchDir dir(QUrl(urlStr).toString(), false);
dir.setBlackList(m_settings->value(urlStr).toStringList());
m_searchDirs.append(dir);
}
}
if(m_settings->value(GLOBAL_BLACK_LIST_KEY).isNull()) {
m_settings->setValue(GLOBAL_BLACK_LIST_KEY, GLOBAL_BLACK_LIST);
}
m_settings->sync();
}