From cba6c954480129a0d3eb93cb7cfa35df637a2f62 Mon Sep 17 00:00:00 2001 From: Yue-Lan Date: Mon, 27 May 2024 15:37:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=BF=9C=E7=A8=8B=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E6=B5=81=E7=A8=8B=EF=BC=8C=E7=94=A8=E4=BA=8E=E6=99=BA?= =?UTF-8?q?=E8=83=BD=E6=95=B0=E6=8D=AE=E7=AE=A1=E7=90=86=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=88=9B=E5=BB=BA=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit indexupdater和pendingupdatequeue增加信号用于线程间通信 新增RemoteFileEventHelper类,用于管理replica实例 --- libsearch/CMakeLists.txt | 6 ++-- libsearch/fileeventhandler.rep | 6 ++++ libsearch/index/index-updater.cpp | 7 ++++ libsearch/index/index-updater.h | 1 + libsearch/index/pending-file-queue.cpp | 12 +++++++ libsearch/index/pending-file-queue.h | 5 +++ libsearch/remote-file-event-helper.cpp | 48 ++++++++++++++++++++++++++ libsearch/remote-file-event-helper.h | 28 +++++++++++++++ 8 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 libsearch/fileeventhandler.rep create mode 100644 libsearch/remote-file-event-helper.cpp create mode 100644 libsearch/remote-file-event-helper.h diff --git a/libsearch/CMakeLists.txt b/libsearch/CMakeLists.txt index e285378..bd51e9d 100644 --- a/libsearch/CMakeLists.txt +++ b/libsearch/CMakeLists.txt @@ -111,7 +111,7 @@ set(LIBUKUI_SEARCH_SRC icon-loader.cpp icon-loader.h data-collecter.cpp data-collecter.h - + remote-file-event-helper.cpp ) qt5_generate_repc(LIBUKUI_SEARCH_SRC index/monitor.rep REPLICA) set(QRC_FILES resource1.qrc) @@ -119,6 +119,8 @@ file(GLOB TS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/../translations/libukui-search/*. set_source_files_properties(${TS_FILES} PROPERTIES OUTPUT_LOCATION ${CMAKE_BINARY_DIR}/libsearch/.qm) qt5_create_translation(QM_FILES ${CMAKE_CURRENT_SOURCE_DIR} ${TS_FILES}) +qt5_generate_repc(LIBUKUI_SEARCH_SRC fileeventhandler.rep REPLICA) + add_library(${PROJECT_NAME} SHARED ${LIBUKUI_SEARCH_SRC} ${QRC_FILES} @@ -231,4 +233,4 @@ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ukui-search-config.cmake DESTINATION ${CMAKE_CONFIG_INSTALL_DIR}) if(BUILD_TEST) add_subdirectory(test) -endif () \ No newline at end of file +endif () diff --git a/libsearch/fileeventhandler.rep b/libsearch/fileeventhandler.rep new file mode 100644 index 0000000..98aad46 --- /dev/null +++ b/libsearch/fileeventhandler.rep @@ -0,0 +1,6 @@ +#include + +class FileEventHandler +{ + SLOT(void handleFileEvent(int eventType, QString arg1, QString arg2)) +} diff --git a/libsearch/index/index-updater.cpp b/libsearch/index/index-updater.cpp index 2aca5f4..6b63d68 100644 --- a/libsearch/index/index-updater.cpp +++ b/libsearch/index/index-updater.cpp @@ -29,6 +29,7 @@ #include "compatible-define.h" #include "index-status-recorder.h" #include "ai-indexer.h" +#include "pending-file-queue.h" using namespace UkuiSearch; IndexUpdater::IndexUpdater(const QVector& files, QAtomicInt& indexstop, @@ -41,6 +42,7 @@ IndexUpdater::IndexUpdater(const QVector& files, m_contentIndexOcrStop(&contentIndexOcrStop), m_aiIndexStop(&aiIndexStop) { + connect(this, &IndexUpdater::indexUpdated, PendingFileQueue::getInstance(), &PendingFileQueue::signalFileEvent); } void IndexUpdater::updateIndex() { @@ -55,6 +57,11 @@ void IndexUpdater::updateIndex() } qDebug() << "===update basic index==="; for(const PendingFile& file : m_cache) { + // notify ukui-idm + if (file.isCreated()) { + Q_EMIT indexUpdated(1, file.path(), nullptr); + } + if(file.shouldRemoveIndex()) { qDebug() << "| remove:" < #include "index-status-recorder.h" +#include "remote-file-event-helper.h" + using namespace UkuiSearch; static PendingFileQueue *global_instance_pending_file_queue = nullptr; PendingFileQueue::PendingFileQueue(QObject *parent) : QThread(parent) @@ -38,6 +40,9 @@ PendingFileQueue::PendingFileQueue(QObject *parent) : QThread(parent) m_cacheTimer->moveToThread(this); m_minProcessTimer->moveToThread(this); + m_fileEventHelper = new RemoteFileEventHelper; + m_fileEventHelper->moveToThread(this); + // connect(this, &PendingFileQueue::cacheTimerStart, m_cacheTimer, f, Qt::DirectConnection); // connect(this, &PendingFileQueue::minProcessTimerStart, m_minProcessTimer, f,Qt::DirectConnection); connect(this, SIGNAL(cacheTimerStart()), m_cacheTimer, SLOT(start())); @@ -47,6 +52,8 @@ PendingFileQueue::PendingFileQueue(QObject *parent) : QThread(parent) connect(m_cacheTimer, &QTimer::timeout, this, &PendingFileQueue::processCache, Qt::DirectConnection); connect(m_minProcessTimer, &QTimer::timeout, this, &PendingFileQueue::processCache, Qt::DirectConnection); + + connect(this, &PendingFileQueue::signalFileEvent, m_fileEventHelper, &RemoteFileEventHelper::handleFileEventRequest); } PendingFileQueue *PendingFileQueue::getInstance(QObject *parent) @@ -67,6 +74,11 @@ PendingFileQueue::~PendingFileQueue() delete m_minProcessTimer; m_minProcessTimer = nullptr; } + if(m_fileEventHelper) { + delete m_fileEventHelper; + m_fileEventHelper = nullptr; + } + global_instance_pending_file_queue = nullptr; } diff --git a/libsearch/index/pending-file-queue.h b/libsearch/index/pending-file-queue.h index ef3bda3..022a9fa 100644 --- a/libsearch/index/pending-file-queue.h +++ b/libsearch/index/pending-file-queue.h @@ -27,6 +27,8 @@ #include #include "pending-file.h" +class RemoteFileEventHelper; + namespace UkuiSearch { class PendingFileQueue : public QThread { @@ -51,6 +53,8 @@ Q_SIGNALS: void timerStop(); void filesUpdate(const QVector&); + + void signalFileEvent(int eventType, const QString &arg1, const QString &arg2); private: void processCache(); explicit PendingFileQueue(QObject *parent = nullptr); @@ -61,6 +65,7 @@ private: QThread *m_timerThread = nullptr; int m_enqueuetimes = 0; + RemoteFileEventHelper *m_fileEventHelper = nullptr; }; } #endif // PENDINGFILEQUEUE_H diff --git a/libsearch/remote-file-event-helper.cpp b/libsearch/remote-file-event-helper.cpp new file mode 100644 index 0000000..4c8f175 --- /dev/null +++ b/libsearch/remote-file-event-helper.cpp @@ -0,0 +1,48 @@ +#include "remote-file-event-helper.h" +#include "rep_fileeventhandler_replica.h" + +#include +#include + +RemoteFileEventHelper::RemoteFileEventHelper(QObject *parent) : QObject(parent) +{ + setupConnections(); +} + +RemoteFileEventHelper::~RemoteFileEventHelper() +{ + delete m_fileEventHandlerIface; + delete m_remoteNode; +} + +void RemoteFileEventHelper::setupConnections() +{ + m_remoteNode = new QRemoteObjectNode; + m_remoteNode->connectToNode(nodeUrl()); + m_fileEventHandlerIface = m_remoteNode->acquire(); + qDebug() << "connect handle" << m_fileEventHandlerIface; + connect(m_fileEventHandlerIface, &QRemoteObjectReplica::initialized, this, [=]{ + qDebug() << "initilized" << m_fileEventHandlerIface; + }); + connect(m_fileEventHandlerIface, &QRemoteObjectReplica::stateChanged, this, [=](QRemoteObjectReplica::State state, QRemoteObjectReplica::State oldState){ + qDebug() << "state changed" << state << oldState << m_fileEventHandlerIface; + if (state == QRemoteObjectReplica::Suspect) { + qDebug() << "try reconnect"; + m_fileEventHandlerIface->deleteLater(); + m_remoteNode->deleteLater(); + setupConnections(); + } + }); + connect(this, &RemoteFileEventHelper::handleFileEventRequest, m_fileEventHandlerIface, [=](int type, const QString &arg1, const QString &arg2){ + qDebug() << type << arg1 << arg2 << m_fileEventHandlerIface; + if (m_fileEventHandlerIface->state() == QRemoteObjectReplica::Valid) + m_fileEventHandlerIface->handleFileEvent(type, arg1, arg2); + }); +} + +QUrl RemoteFileEventHelper::nodeUrl() +{ + QString displayEnv = (qgetenv("XDG_SESSION_TYPE") == "wayland") ? QLatin1String("WAYLAND_DISPLAY") : QLatin1String("DISPLAY"); + QString display(qgetenv(displayEnv.toUtf8().data())); + return QUrl(QStringLiteral("local:ukui-idm-") + QString(qgetenv("USER")) + display); +} diff --git a/libsearch/remote-file-event-helper.h b/libsearch/remote-file-event-helper.h new file mode 100644 index 0000000..e8dae0c --- /dev/null +++ b/libsearch/remote-file-event-helper.h @@ -0,0 +1,28 @@ +#ifndef REMOTEFILEEVENTHELPER_H +#define REMOTEFILEEVENTHELPER_H + +#include +#include + +class QRemoteObjectNode; +class FileEventHandlerReplica; + +class RemoteFileEventHelper : public QObject +{ + Q_OBJECT +public: + explicit RemoteFileEventHelper(QObject *parent = nullptr); + ~RemoteFileEventHelper(); + +Q_SIGNALS: + void handleFileEventRequest(int type, const QString &arg1, const QString &arg2); + +private: + void setupConnections(); + static QUrl nodeUrl(); + + QRemoteObjectNode *m_remoteNode = nullptr; + FileEventHandlerReplica *m_fileEventHandlerIface = nullptr; +}; + +#endif // REMOTEFILEEVENTHELPER_H