Add the dbus interface of dir watcher for file index.

This commit is contained in:
baijunjie 2022-04-12 14:57:22 +08:00 committed by iaom
parent 407251b865
commit a7cec2da4f
7 changed files with 243 additions and 6 deletions

View File

@ -0,0 +1,11 @@
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="org.ukui.search.fileindex.service">
<method name="appendIndexableListItem">
<arg name="path" type="s" direction="in"/>
</method>
<method name="removeIndexableListItem">
<arg name="path" type="s" direction="in"/>
</method>
</interface>
</node>

View File

@ -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 <QtCore/QMetaObject>
#include <QtCore/QByteArray>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QVariant>
/*
* 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));
}

View File

@ -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 <QtCore/QObject>
#include <QtDBus/QtDBus>
QT_BEGIN_NAMESPACE
class QByteArray;
template<class T> class QList;
template<class Key, class Value> 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", ""
" <interface name=\"org.ukui.search.fileindex\">\n"
" <method name=\"appendIndexableListItem\">\n"
" <arg direction=\"in\" type=\"s\" name=\"path\"/>\n"
" </method>\n"
" <method name=\"removeIndexableListItem\">\n"
" <arg direction=\"in\" type=\"s\" name=\"path\"/>\n"
" </method>\n"
" </interface>\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

View File

@ -8,6 +8,8 @@
#include <fstab.h> #include <fstab.h>
#include <QMutexLocker> #include <QMutexLocker>
#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 std::once_flag flag;
static DirWatcher *global_intance = nullptr; static DirWatcher *global_intance = nullptr;
@ -15,8 +17,10 @@ QMutex DirWatcher::s_mutex;
DirWatcher::DirWatcher(QObject *parent) : QObject(parent) DirWatcher::DirWatcher(QObject *parent) : QObject(parent)
{ {
m_qSettings = new QSettings(CURRENT_INDEXABLE_DIR_SETTINGS, QSettings::IniFormat);
initData(); initData();
initDiskWatcher(); initDiskWatcher();
m_adaptor = new DirWatcherAdaptor(this);
} }
DirWatcher::~DirWatcher() DirWatcher::~DirWatcher()
@ -26,6 +30,9 @@ DirWatcher::~DirWatcher()
g_signal_handler_disconnect(m_volumeMonitor, m_mountRemoveHandle); g_signal_handler_disconnect(m_volumeMonitor, m_mountRemoveHandle);
m_volumeMonitor = nullptr; m_volumeMonitor = nullptr;
} }
if(m_qSettings){
delete m_qSettings;
}
} }
DirWatcher *DirWatcher::getDirWatcher() DirWatcher *DirWatcher::getDirWatcher()
@ -39,8 +46,11 @@ DirWatcher *DirWatcher::getDirWatcher()
QStringList DirWatcher::currentindexableDir() QStringList DirWatcher::currentindexableDir()
{ {
QMutexLocker locker(&s_mutex); QMutexLocker locker(&s_mutex);
QStringList indexableDirList = m_indexableDirList; m_qSettings->beginGroup(INDEXABLE_DIR_VALUE);
return indexableDirList; m_indexableDirList = m_qSettings->value(INDEXABLE_DIR_VALUE).toStringList();
m_qSettings->endGroup();
QStringList indexableDirs = m_indexableDirList;
return indexableDirs;
} }
QStringList DirWatcher::currentBlackListOfIndex() QStringList DirWatcher::currentBlackListOfIndex()
@ -50,6 +60,23 @@ QStringList DirWatcher::currentBlackListOfIndex()
return blackListOfIndex; 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 * @brief DirWatcher::blackListOfDir
* @param dirPath * @param dirPath
@ -184,11 +211,75 @@ void DirWatcher::mountRemoveCallback(GVolumeMonitor *monitor, GMount *gmount, Di
g_object_unref(rootFile); 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() void DirWatcher::initData()
{ {
//目前方案只索引数据盘和家目录。 //适配需求,可索引目录为用户指定。
m_indexableDirList << "/data" << QDir::homePath(); // m_indexableDirList << "/data" << QDir::homePath();
m_blackListOfIndex << "/data/home" << "/data/root"; /* boot里面存放Linux核心文件开机选单与开机所需配置文件等
* backup里面是系统备份文件
* bin放置的是在单人维护模式下还能够被操作的指令bin底下的指令可以被root与一般账号所使用
* dev里面存放设备文件
* etc里面存放了几乎所有的系统主要配置文件
* lib放置最基本的共享库和内核模块lib32lib64libx32分别面向32位64x32 ABIusr下的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目录 //目前方案:可搜索目录(服务)默认根目录,可搜索目录(应用)默认家目录和/data目录
m_searchableListForApplication << "/data" << QDir::homePath(); m_searchableListForApplication << "/data" << QDir::homePath();

View File

@ -1,12 +1,16 @@
#ifndef MOUNTDISKLISTENER_H #ifndef MOUNTDISKLISTENER_H
#define MOUNTDISKLISTENER_H #define MOUNTDISKLISTENER_H
#include "dir-watcher-adaptor.h"
#include <QObject> #include <QObject>
#include <QStorageInfo> #include <QStorageInfo>
#include <QHash> #include <QHash>
#include <QDBusMessage> #include <QDBusMessage>
#include <QDBusObjectPath> #include <QDBusObjectPath>
#include <QMutex> #include <QMutex>
#include <QSettings>
#include <QDBusConnection>
#undef slots #undef slots
#undef signals #undef signals
@ -18,10 +22,15 @@
class DirWatcher : public QObject class DirWatcher : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_CLASSINFO("D-Bus Interface","org.ukui.search.fileindex")
public: public:
static DirWatcher *getDirWatcher(); static DirWatcher *getDirWatcher();
QStringList currentindexableDir(); QStringList currentindexableDir();
QStringList currentBlackListOfIndex(); QStringList currentBlackListOfIndex();
void handleIndexItemAppend(const QString &path);
void handleIndexItemRemove(const QString &path);
void appendBlackListItemOfIndex(const QString &path); void appendBlackListItemOfIndex(const QString &path);
void appendBlackListItemOfIndex(const QStringList &pathList); void appendBlackListItemOfIndex(const QStringList &pathList);
void removeBlackListItemOfIndex(const QString &path); void removeBlackListItemOfIndex(const QString &path);
@ -34,6 +43,9 @@ public:
static void mountAddCallback(GVolumeMonitor *monitor, GMount *gmount, DirWatcher *pThis); static void mountAddCallback(GVolumeMonitor *monitor, GMount *gmount, DirWatcher *pThis);
static void mountRemoveCallback(GVolumeMonitor *monitor, GMount *gmount, DirWatcher *pThis); static void mountRemoveCallback(GVolumeMonitor *monitor, GMount *gmount, DirWatcher *pThis);
public Q_SLOTS: public Q_SLOTS:
void appendIndexableListItem(const QString &path);
void removeIndexableListItem(const QString &path);
void handleDisk(); void handleDisk();
void handleAddedUDiskDevice(QDBusMessage msg); void handleAddedUDiskDevice(QDBusMessage msg);
void handleRemovedUDiskDevice(QDBusMessage msg); void handleRemovedUDiskDevice(QDBusMessage msg);
@ -46,11 +58,16 @@ private:
static QMutex s_mutex; static QMutex s_mutex;
GVolumeMonitor *m_volumeMonitor = nullptr; GVolumeMonitor *m_volumeMonitor = nullptr;
DirWatcherAdaptor *m_adaptor = nullptr;
quint64 m_mountAddHandle; quint64 m_mountAddHandle;
quint64 m_mountRemoveHandle; quint64 m_mountRemoveHandle;
QSettings *m_qSettings = nullptr;
QStringList m_blackListOfIndex; QStringList m_blackListOfIndex;
QStringList m_indexableDirList; QStringList m_indexableDirList;
QStringList m_searchableDirList; QStringList m_searchableDirList;
QStringList m_searchableListForApplication; QStringList m_searchableListForApplication;
QStringList m_autoMountList; QStringList m_autoMountList;
@ -63,6 +80,8 @@ private:
QMap<QString, QStringList> m_currentUDiskDeviceInfo; QMap<QString, QStringList> m_currentUDiskDeviceInfo;
Q_SIGNALS: Q_SIGNALS:
void udiskRemoved(); void udiskRemoved();
void appendIndexItem(const QString&, const QStringList&);
void removeIndexItem(const QString&);
}; };
#endif // MOUNTDISKLISTENER_H #endif // MOUNTDISKLISTENER_H

View File

@ -1,7 +1,9 @@
INCLUDEPATH += $$PWD INCLUDEPATH += $$PWD
HEADERS += \ HEADERS += \
$$PWD/dir-watcher-adaptor.h \
$$PWD/dir-watcher.h $$PWD/dir-watcher.h
SOURCES += \ SOURCES += \
$$PWD/dir-watcher-adaptor.cpp \
$$PWD/dir-watcher.cpp $$PWD/dir-watcher.cpp

View File

@ -1,6 +1,10 @@
#include <QDebug> #include <QDebug>
#include "ukui-search-service.h" #include "ukui-search-service.h"
#include "dir-watcher.h" #include "dir-watcher.h"
#include "dir-watcher-adaptor.h"
#include <QDBusConnection>
#define UKUI_SEARCH_SCHEMAS "org.ukui.search.settings" #define UKUI_SEARCH_SCHEMAS "org.ukui.search.settings"
#define SEARCH_METHOD_KEY "file-index-enable" #define SEARCH_METHOD_KEY "file-index-enable"
using namespace UkuiSearch; using namespace UkuiSearch;
@ -15,7 +19,16 @@ UkuiSearchService::UkuiSearchService(int &argc, char *argv[], const QString &app
this->parseCmd(msg, true); 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(); initGsettings();
} }