diff --git a/ukui-search-service-dir-manager/dirwatcher/config.cpp b/ukui-search-service-dir-manager/dirwatcher/config.cpp new file mode 100644 index 0000000..61e6a8d --- /dev/null +++ b/ukui-search-service-dir-manager/dirwatcher/config.cpp @@ -0,0 +1,95 @@ +#include "config.h" +#include +#include +#include + +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 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(); +} diff --git a/ukui-search-service-dir-manager/dirwatcher/config.h b/ukui-search-service-dir-manager/dirwatcher/config.h new file mode 100644 index 0000000..97979c8 --- /dev/null +++ b/ukui-search-service-dir-manager/dirwatcher/config.h @@ -0,0 +1,31 @@ +#ifndef CONFIG_H +#define CONFIG_H +#include +#include +#include +#include "search-dir.h" + +class Config +{ +public: + static Config *self(); + void addDir(const SearchDir& dir); + QStringList removeDir(const SearchDir& dir); + /** + * @brief 兼容模式,自动提取老版本数据生成黑名单 + * @return + */ + bool isCompatibilityMode(); + QVector searchDirObjects() const; + QStringList searchDirs() const; + QStringList blackDirs() const; + QStringList globalBlackList() const; + +private: + Config(); + QSettings *m_settings = nullptr; + bool m_compatibilityMode = false; + QVector m_searchDirs; +}; + +#endif // CONFIG_H diff --git a/ukui-search-service-dir-manager/dirwatcher/dir-watcher-dbus.pri b/ukui-search-service-dir-manager/dirwatcher/dir-watcher-dbus.pri index 6d49f2a..55746b2 100644 --- a/ukui-search-service-dir-manager/dirwatcher/dir-watcher-dbus.pri +++ b/ukui-search-service-dir-manager/dirwatcher/dir-watcher-dbus.pri @@ -1,11 +1,15 @@ INCLUDEPATH += $$PWD HEADERS += \ + $$PWD/config.h \ $$PWD/dir-watcher-adaptor.h \ $$PWD/dir-watcher.h \ + $$PWD/search-dir.h \ $$PWD/volume-manager.h SOURCES += \ + $$PWD/config.cpp \ $$PWD/dir-watcher-adaptor.cpp \ $$PWD/dir-watcher.cpp \ + $$PWD/search-dir.cpp \ $$PWD/volume-manager.cpp diff --git a/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp b/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp index 637e0da..9054740 100644 --- a/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp +++ b/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp @@ -7,6 +7,7 @@ #include #include #include +#include "config.h" #define CURRENT_INDEXABLE_DIR_SETTINGS QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search-current-indexable-dir.conf" #define INDEXABLE_DIR_VALUE "IndexableDir" @@ -526,6 +527,30 @@ bool DirWatcher::removeIndexableListItem(const QString &path) return res; } +int DirWatcher::appendSearchDir(const QString &path) +{ + SearchDir dir(path); + Config::self()->addDir(dir); + return 0; +} + +void DirWatcher::removeSearchDir(const QString &path) +{ + SearchDir dir(path, false); + Config::self()->removeDir(dir); + return; +} + +QStringList DirWatcher::currentSearchDirs() +{ + return Config::self()->searchDirs(); +} + +QStringList DirWatcher::currentBlackList() +{ + return Config::self()->blackDirs(); +} + void DirWatcher::initData() { //适配需求,可索引目录为用户指定。 diff --git a/ukui-search-service-dir-manager/dirwatcher/dir-watcher.h b/ukui-search-service-dir-manager/dirwatcher/dir-watcher.h index 2e12f09..992ff59 100644 --- a/ukui-search-service-dir-manager/dirwatcher/dir-watcher.h +++ b/ukui-search-service-dir-manager/dirwatcher/dir-watcher.h @@ -63,6 +63,14 @@ public Q_SLOTS: Q_SCRIPTABLE int appendIndexableListItem(const QString &path); Q_SCRIPTABLE bool removeIndexableListItem(const QString &path); + //新接口 + int appendSearchDir(const QString &path); + void removeSearchDir(const QString &path); + + QStringList currentSearchDirs(); + QStringList currentBlackList(); + + private: DirWatcher(QObject *parent = nullptr); ~DirWatcher(); diff --git a/ukui-search-service-dir-manager/dirwatcher/search-dir.cpp b/ukui-search-service-dir-manager/dirwatcher/search-dir.cpp new file mode 100644 index 0000000..45e97b1 --- /dev/null +++ b/ukui-search-service-dir-manager/dirwatcher/search-dir.cpp @@ -0,0 +1,47 @@ +#include "search-dir.h" + +SearchDir::SearchDir(const QString &path, bool generateBlackList) +{ + m_path = path; + if(generateBlackList) { + //根据VulumeManager和Congig的数据计算黑名单here + } +} + +SearchDir::ErrorInfo SearchDir::error() +{ + return m_error; +} + +QString SearchDir::errorString() +{ + switch (m_error) { + case ErrorInfo::Successful: + return ""; + case ErrorInfo::Duplicated: + return ""; + case ErrorInfo::UnderBlackList: + return ""; + case ErrorInfo::RepeatMount1: + return ""; + case ErrorInfo::RepeatMount2: + return ""; + case ErrorInfo::NotExists: + return ""; + } +} + +QString SearchDir::getPath() const +{ + return m_path; +} + +void SearchDir::setBlackList(const QStringList &blackList) +{ + m_blackList = blackList; +} + +QStringList SearchDir::getBlackList() const +{ + return m_blackList; +} diff --git a/ukui-search-service-dir-manager/dirwatcher/search-dir.h b/ukui-search-service-dir-manager/dirwatcher/search-dir.h new file mode 100644 index 0000000..e6a9ce2 --- /dev/null +++ b/ukui-search-service-dir-manager/dirwatcher/search-dir.h @@ -0,0 +1,27 @@ +#ifndef SEARCHDIR_H +#define SEARCHDIR_H +#include +class SearchDir +{ +public: + enum ErrorInfo{ + Successful = 0, + Duplicated, + UnderBlackList, + RepeatMount1, + RepeatMount2, + NotExists + }; + SearchDir(const QString& path, bool generateBlackList = true); + ErrorInfo error(); + QString errorString(); + QString getPath() const; + void setBlackList(const QStringList& blackList); + QStringList getBlackList() const; +private: + QString m_path; + QStringList m_blackList; + ErrorInfo m_error = ErrorInfo::Successful; +}; + +#endif // SEARCHDIR_H