diff --git a/libsearch/dirwatcher/com.ukui.search.fileindex.server.xml b/libsearch/dirwatcher/com.ukui.search.fileindex.server.xml new file mode 100644 index 0000000..b7f460d --- /dev/null +++ b/libsearch/dirwatcher/com.ukui.search.fileindex.server.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/libsearch/dirwatcher/dir-watcher-adaptor.cpp b/libsearch/dirwatcher/dir-watcher-adaptor.cpp new file mode 100644 index 0000000..4bde5e4 --- /dev/null +++ b/libsearch/dirwatcher/dir-watcher-adaptor.cpp @@ -0,0 +1,47 @@ +/* + * This file was generated by qdbusxml2cpp version 0.8 + * Command line was: qdbusxml2cpp com.ukui.search.fileindex.server.xml -i dir-watcher.h -a valueAdaptor + * + * qdbusxml2cpp is Copyright (C) 2020 The Qt Company Ltd. + * + * This is an auto-generated file. + * Do not edit! All changes made to it will be lost. + */ + +#include "dir-watcher-adaptor.h" +#include +#include +#include +#include +#include +#include +#include + +/* + * Implementation of adaptor class DirWatcherAdaptor + */ + +DirWatcherAdaptor::DirWatcherAdaptor(QObject *parent) + : QDBusAbstractAdaptor(parent) +{ + // constructor + setAutoRelaySignals(true); +} + +DirWatcherAdaptor::~DirWatcherAdaptor() +{ + // destructor +} + +void DirWatcherAdaptor::appendIndexableListItem(const QString &path) +{ + // handle method call org.ukui.search.fileindex.service.appendIndexableListItem + QMetaObject::invokeMethod(parent(), "appendIndexableListItem", Q_ARG(QString, path)); +} + +void DirWatcherAdaptor::removeIndexableListItem(const QString &path) +{ + // handle method call org.ukui.search.fileindex.service.removeIndexableListItem + QMetaObject::invokeMethod(parent(), "removeIndexableListItem", Q_ARG(QString, path)); +} + diff --git a/libsearch/dirwatcher/dir-watcher-adaptor.h b/libsearch/dirwatcher/dir-watcher-adaptor.h new file mode 100644 index 0000000..24c9411 --- /dev/null +++ b/libsearch/dirwatcher/dir-watcher-adaptor.h @@ -0,0 +1,54 @@ +/* + * This file was generated by qdbusxml2cpp version 0.8 + * Command line was: qdbusxml2cpp com.ukui.search.fileindex.server.xml -i dir-watcher.h -a valueAdaptor + * + * qdbusxml2cpp is Copyright (C) 2020 The Qt Company Ltd. + * + * This is an auto-generated file. + * This file may have been hand-edited. Look for HAND-EDIT comments + * before re-generating it. + */ + +#ifndef VALUEADAPTOR_H +#define VALUEADAPTOR_H + +#include +#include +QT_BEGIN_NAMESPACE +class QByteArray; +template class QList; +template class QMap; +class QString; +class QStringList; +class QVariant; +QT_END_NAMESPACE + +/* + * Adaptor class for interface org.ukui.search.fileindex + */ +class DirWatcherAdaptor: public QDBusAbstractAdaptor +{ + Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "org.ukui.search.fileindex.service") + Q_CLASSINFO("D-Bus Introspection", "" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" + "") +public: + DirWatcherAdaptor(QObject *parent); + virtual ~DirWatcherAdaptor(); + +public: // PROPERTIES +public Q_SLOTS: // METHODS + void appendIndexableListItem(const QString &path); + void removeIndexableListItem(const QString &path); +Q_SIGNALS: // SIGNALS +}; + +#endif diff --git a/libsearch/dirwatcher/dir-watcher.cpp b/libsearch/dirwatcher/dir-watcher.cpp index 33a1358..2fce31a 100644 --- a/libsearch/dirwatcher/dir-watcher.cpp +++ b/libsearch/dirwatcher/dir-watcher.cpp @@ -8,6 +8,8 @@ #include #include +#define CURRENT_INDEXABLE_DIR_SETTINGS QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search-current-indexable-dir.conf" +#define INDEXABLE_DIR_VALUE "IndexableDir" static std::once_flag flag; static DirWatcher *global_intance = nullptr; @@ -15,8 +17,10 @@ QMutex DirWatcher::s_mutex; DirWatcher::DirWatcher(QObject *parent) : QObject(parent) { + m_qSettings = new QSettings(CURRENT_INDEXABLE_DIR_SETTINGS, QSettings::IniFormat); initData(); initDiskWatcher(); + m_adaptor = new DirWatcherAdaptor(this); } DirWatcher::~DirWatcher() @@ -26,6 +30,9 @@ DirWatcher::~DirWatcher() g_signal_handler_disconnect(m_volumeMonitor, m_mountRemoveHandle); m_volumeMonitor = nullptr; } + if(m_qSettings){ + delete m_qSettings; + } } DirWatcher *DirWatcher::getDirWatcher() @@ -39,8 +46,11 @@ DirWatcher *DirWatcher::getDirWatcher() QStringList DirWatcher::currentindexableDir() { QMutexLocker locker(&s_mutex); - QStringList indexableDirList = m_indexableDirList; - return indexableDirList; + m_qSettings->beginGroup(INDEXABLE_DIR_VALUE); + m_indexableDirList = m_qSettings->value(INDEXABLE_DIR_VALUE).toStringList(); + m_qSettings->endGroup(); + QStringList indexableDirs = m_indexableDirList; + return indexableDirs; } QStringList DirWatcher::currentBlackListOfIndex() @@ -50,6 +60,23 @@ QStringList DirWatcher::currentBlackListOfIndex() return blackListOfIndex; } +void DirWatcher::handleIndexItemAppend(const QString &path) +{ + m_indexableDirList << path; + m_indexableDirList.removeDuplicates(); + m_qSettings->beginGroup(INDEXABLE_DIR_VALUE); + m_qSettings->setValue(INDEXABLE_DIR_VALUE, m_indexableDirList); + m_qSettings->endGroup(); +} + +void DirWatcher::handleIndexItemRemove(const QString &path) +{ + m_indexableDirList.removeAll(path); + m_qSettings->beginGroup(INDEXABLE_DIR_VALUE); + m_qSettings->setValue(INDEXABLE_DIR_VALUE, m_indexableDirList); + m_qSettings->endGroup(); +} + /** * @brief DirWatcher::blackListOfDir 根据传入目录返回当前目录下的所有不可搜索目录,没有则返回空列表 * @param dirPath 要搜索的目录 @@ -184,11 +211,75 @@ void DirWatcher::mountRemoveCallback(GVolumeMonitor *monitor, GMount *gmount, Di g_object_unref(rootFile); } +void DirWatcher::appendIndexableListItem(const QString &path) +{ + if (path == "/") { + this->currentindexableDir(); + this->handleIndexItemAppend(path); + Q_EMIT this->appendIndexItem(path, m_blackListOfIndex); + qDebug() << "index path:" << path << "blacklist:" << m_blackListOfIndex; + return; + } + + QStringList blackList = this->blackListOfDir(path); + //处理要添加索引的路径与索引黑名单中路径为父子关系的情况 + for (const QString& blackListPath : m_blackListOfIndex) { + if (path.startsWith(blackListPath + "/")) { + return; + } + if (blackListPath.startsWith(path + "/")) { + blackList.append(blackListPath); + } + } + + this->currentindexableDir(); + qDebug() << "m_indexableDirList:" << m_indexableDirList; + //处理要添加索引的路径与已索引路径为父子关系的情况 + for (int i = 0; i < m_indexableDirList.length(); i++) { + const QString indexablePath = m_indexableDirList.at(i); + if (path.startsWith(indexablePath + "/")) { + qDebug() << "return in:" << __FILE__ << ":" << __LINE__; + return; + } + if (blackList.contains(indexablePath)) { + qDebug() << "return in:" << __FILE__ << ":" << __LINE__; + return; + } + if (indexablePath.startsWith(path + "/")) { + m_indexableDirList.removeAll(indexablePath); + blackList.append(indexablePath); + } + } + this->handleIndexItemAppend(path); + Q_EMIT this->appendIndexItem(path, blackList); + qDebug() << "index path:" << path << "blacklist:" << blackList; +} + +void DirWatcher::removeIndexableListItem(const QString &path) +{ + this->currentindexableDir(); + this->handleIndexItemRemove(path); + Q_EMIT this->removeIndexItem(path); +} + void DirWatcher::initData() { - //目前方案只索引数据盘和家目录。 - m_indexableDirList << "/data" << QDir::homePath(); - m_blackListOfIndex << "/data/home" << "/data/root"; + //适配需求,可索引目录为用户指定。 +// m_indexableDirList << "/data" << QDir::homePath(); + /* boot里面存放Linux核心文件,开机选单与开机所需配置文件等 + * backup里面是系统备份文件 + * bin放置的是在单人维护模式下还能够被操作的指令,在bin底下的指令可以被root与一般账号所使用。 + * dev里面存放设备文件 + * etc里面存放了几乎所有的系统主要配置文件,包括人员的账号密码文件,各种服务的起始档等 + * lib放置最基本的共享库和内核模块,lib32,lib64,libx32分别面向32位,64位以及x32 ABI。他们都分别连接到usr下的lib*中 + * media一般放置可移除的装置,包括软盘,光盘和一些移动存储设备都自动挂载到这里 + * mnt原本和media用途相同,现用来放置暂时挂载一些额外装置 + * usr是Unix操作系统软件资源所放置的目录,所有系统默认的软件(distribution发布者提供的软件)都会放置到usr底下 + * var目录主要针对常态性变动的文件,包括缓存(cache)、登录档(log file)以及某些软件运作所产生的文件,包括程序文件(lock file, run file),或者如MySQL数据库的文件等 + */ + m_blackListOfIndex << "/boot" << "backup" << "bin" << "/dev" << "/etc" << "/usr" << "/var" + << "/lib" << "lib32" << "lib64" << "libx32" << "/media" << "/mnt" << "cdrom" + << "/sys" << "/proc" << "tmp" << "/srv" << "/sbin" << "/run" << "/opt"; //目前方案:可搜索目录(服务)默认根目录,可搜索目录(应用)默认家目录和/data目录 m_searchableListForApplication << "/data" << QDir::homePath(); diff --git a/libsearch/dirwatcher/dir-watcher.h b/libsearch/dirwatcher/dir-watcher.h index 410844a..bdf79fc 100644 --- a/libsearch/dirwatcher/dir-watcher.h +++ b/libsearch/dirwatcher/dir-watcher.h @@ -1,12 +1,16 @@ #ifndef MOUNTDISKLISTENER_H #define MOUNTDISKLISTENER_H +#include "dir-watcher-adaptor.h" + #include #include #include #include #include #include +#include +#include #undef slots #undef signals @@ -18,10 +22,15 @@ class DirWatcher : public QObject { Q_OBJECT + + Q_CLASSINFO("D-Bus Interface","org.ukui.search.fileindex") + public: static DirWatcher *getDirWatcher(); QStringList currentindexableDir(); QStringList currentBlackListOfIndex(); + void handleIndexItemAppend(const QString &path); + void handleIndexItemRemove(const QString &path); void appendBlackListItemOfIndex(const QString &path); void appendBlackListItemOfIndex(const QStringList &pathList); void removeBlackListItemOfIndex(const QString &path); @@ -34,6 +43,9 @@ public: static void mountAddCallback(GVolumeMonitor *monitor, GMount *gmount, DirWatcher *pThis); static void mountRemoveCallback(GVolumeMonitor *monitor, GMount *gmount, DirWatcher *pThis); public Q_SLOTS: + void appendIndexableListItem(const QString &path); + void removeIndexableListItem(const QString &path); + void handleDisk(); void handleAddedUDiskDevice(QDBusMessage msg); void handleRemovedUDiskDevice(QDBusMessage msg); @@ -46,11 +58,16 @@ private: static QMutex s_mutex; GVolumeMonitor *m_volumeMonitor = nullptr; + + DirWatcherAdaptor *m_adaptor = nullptr; + quint64 m_mountAddHandle; quint64 m_mountRemoveHandle; + QSettings *m_qSettings = nullptr; QStringList m_blackListOfIndex; QStringList m_indexableDirList; + QStringList m_searchableDirList; QStringList m_searchableListForApplication; QStringList m_autoMountList; @@ -63,6 +80,8 @@ private: QMap m_currentUDiskDeviceInfo; Q_SIGNALS: void udiskRemoved(); + void appendIndexItem(const QString&, const QStringList&); + void removeIndexItem(const QString&); }; #endif // MOUNTDISKLISTENER_H diff --git a/libsearch/dirwatcher/dirwatcher.pri b/libsearch/dirwatcher/dirwatcher.pri index 4558940..9661b77 100644 --- a/libsearch/dirwatcher/dirwatcher.pri +++ b/libsearch/dirwatcher/dirwatcher.pri @@ -1,7 +1,9 @@ INCLUDEPATH += $$PWD HEADERS += \ + $$PWD/dir-watcher-adaptor.h \ $$PWD/dir-watcher.h SOURCES += \ + $$PWD/dir-watcher-adaptor.cpp \ $$PWD/dir-watcher.cpp diff --git a/ukui-search-service/ukui-search-service.cpp b/ukui-search-service/ukui-search-service.cpp index aa5d91f..b05452a 100644 --- a/ukui-search-service/ukui-search-service.cpp +++ b/ukui-search-service/ukui-search-service.cpp @@ -1,6 +1,10 @@ #include #include "ukui-search-service.h" #include "dir-watcher.h" +#include "dir-watcher-adaptor.h" + +#include + #define UKUI_SEARCH_SCHEMAS "org.ukui.search.settings" #define SEARCH_METHOD_KEY "file-index-enable" using namespace UkuiSearch; @@ -15,7 +19,16 @@ UkuiSearchService::UkuiSearchService(int &argc, char *argv[], const QString &app this->parseCmd(msg, true); }); - DirWatcher::getDirWatcher(); + //注册服务 + QDBusConnection sessionBus = QDBusConnection::sessionBus(); + if (!sessionBus.registerService("com.ukui.search.fileindex.service")) { + qWarning() << "ukui-search-fileindex dbus register service failed reason:" << sessionBus.lastError(); + } + + if(!sessionBus.registerObject("/org/ukui/search/fileindex", DirWatcher::getDirWatcher(), QDBusConnection::ExportAdaptors)){ + qWarning() << "ukui-search-fileindex dbus register object failed reason:" << sessionBus.lastError(); + } + initGsettings(); }