diff --git a/libsearch/filesystemwatcher/file-system-watcher.cpp b/libsearch/filesystemwatcher/file-system-watcher.cpp index a72a40d..99b12e9 100644 --- a/libsearch/filesystemwatcher/file-system-watcher.cpp +++ b/libsearch/filesystemwatcher/file-system-watcher.cpp @@ -205,6 +205,7 @@ void FileSystemWatcher::eventProcess(int socket) const struct inotify_event* event = (struct inotify_event*)&buf[i]; if(event->name[0] == '.') { + i += sizeof(struct inotify_event) + event->len; continue; } if (event->wd < 0 && (event->mask & EventQueueOverflow)) { diff --git a/ukui-search-app-data-service/app-db-manager.cpp b/ukui-search-app-data-service/app-db-manager.cpp index 58d473b..e61e155 100644 --- a/ukui-search-app-data-service/app-db-manager.cpp +++ b/ukui-search-app-data-service/app-db-manager.cpp @@ -7,9 +7,9 @@ #include #include -#define GENERAL_APP_DESKTOP_PATH "/usr/share/applications/" -#define ANDROID_APP_DESKTOP_PATH QDir::homePath() + "/.local/share/applications/" -#define SNAPD_APP_DESKTOP_PATH "/var/lib/snapd/desktop/applications/" +#define GENERAL_APP_DESKTOP_PATH "/usr/share/applications" +#define ANDROID_APP_DESKTOP_PATH QDir::homePath() + "/.local/share/applications" +#define SNAPD_APP_DESKTOP_PATH "/var/lib/snapd/desktop/applications" #define LAST_LOCALE_NAME QDir::homePath() + "/.config/org.ukui/ukui-search/appdata/last-locale-name.conf" #define LOCALE_NAME_VALUE "CurrentLocaleName" @@ -49,6 +49,100 @@ AppDBManager::AppDBManager(QObject *parent) : QThread(parent), m_database(QSqlDa refreshAllData2DB(); // refreshDataBase(); + //初始化FileSystemWatcher + m_watcher = new FileSystemWatcher; + m_watcher->addWatch(GENERAL_APP_DESKTOP_PATH); + QDir androidDir(ANDROID_APP_DESKTOP_PATH); + if(!androidDir.exists()) { + androidDir.mkpath(ANDROID_APP_DESKTOP_PATH); + } + m_watcher->addWatch(ANDROID_APP_DESKTOP_PATH); + + m_snapdDir = new QDir(SNAPD_APP_DESKTOP_PATH); + if(!m_snapdDir->exists()) { + m_snapdWatcher = new FileSystemWatcher(false); + QDir dir("/var/lib/snapd"); + if (!dir.exists()) { + dir.setPath("/var/lib"); + } + m_snapdPath = dir.absolutePath(); + m_snapdWatcher->addWatch(m_snapdPath); + } else { + m_watcher->addWatch(SNAPD_APP_DESKTOP_PATH); + } + + connect(m_snapdWatcher, &FileSystemWatcher::created, this, [ = ] (const QString &path, bool isDir) { + if (isDir) { + //监测新增目录为/var/lib/snapd时,将其替换为snapdWatcher的watchpath + if (path == "/var/lib/snapd") { + m_snapdWatcher->removeWatch(m_snapdPath); + m_snapdWatcher->addWatch(path); + qDebug() << "~~~~~~~add watch" << path << "~~~~~remove watch" << m_snapdPath; + m_snapdPath = path; + //snapd下的desktop目录可能在还没替换监听目录为/var/lib/snapd时就已经被创建,因此需要特别判断 + QDir dir("/var/lib/snapd/desktop"); + if (dir.exists()) { + if (m_snapdDir->exists()) { + m_watcher->addWatch(SNAPD_APP_DESKTOP_PATH); + m_snapdWatcher->removeWatch(m_snapdPath); + qDebug() << "======add watch" << SNAPD_APP_DESKTOP_PATH << "======remove watch" << m_snapdPath; + } + } + } + //检测到/var/lib/snapd/desktop被创建,则将监听目录替换为/var/lib/snapd/desktop/applications + if (path == "/var/lib/snapd/desktop" and m_snapdDir->exists()) { + m_watcher->addWatch(SNAPD_APP_DESKTOP_PATH); + m_snapdWatcher->removeWatch(m_snapdPath); + qDebug() << "======add watch" << SNAPD_APP_DESKTOP_PATH << "======remove watch" << m_snapdPath; + } + + } + }); + + connect(m_watcher, &FileSystemWatcher::created, this, [ = ] (const QString &desktopfp, bool isDir) { + //event is IN_CREATE or IN_MOVED_TO + if (!isDir and desktopfp.endsWith(".desktop")) { + this->insertDBItem(desktopfp); + } + }); + + connect(m_watcher, &FileSystemWatcher::modified, this, [ = ] (const QString &desktopfp) { + //event is IN_MODIFY + if (desktopfp.endsWith(".desktop")) { + this->updateDBItem(desktopfp); + } + }); + + connect(m_watcher, &FileSystemWatcher::moved, this, [ = ] (const QString &desktopfp, bool isDir) { + //event is IN_MOVED_FROM + if (!isDir) { + if (desktopfp.endsWith(".desktop")) { + this->deleteDBItem(desktopfp); + } + } else { + //event is IN_MOVE_SELF + qWarning() << "Dir:" << desktopfp << "has been moved to other place! Stop the watching of the desktop files in it!"; + } + }); + + connect(m_watcher, &FileSystemWatcher::deleted, this, [ = ] (const QString &desktopfp, bool isDir) { + //event is IN_DELETE + if (!isDir) { + if (desktopfp.endsWith(".desktop")) { + this->deleteDBItem(desktopfp); + } + } else { + //event is IN_DELETE_SELF + qWarning() << "Dir:" << desktopfp << "has been deleted! Stop the watching of the desktop files in it!"; + } + }); + + connect(m_watcher, &FileSystemWatcher::unmounted, this, [ = ] (const QString &desktopfp) { + //event is IN_UNMOUNT + qWarning() << "Dir:" << desktopfp << "has been unmounted! Stop the watching of the desktop files in it!"; + }); + + /* //初始化FileSystemWatcher m_watchAppDir = new QFileSystemWatcher(this); m_watchAppDir->addPath(GENERAL_APP_DESKTOP_PATH); @@ -103,6 +197,7 @@ AppDBManager::AppDBManager(QObject *parent) : QThread(parent), m_database(QSqlDa Q_EMIT this->stopTimer(); this->refreshAllData2DB(); }, Qt::DirectConnection); + */ //监控应用进程开启 connect(KWindowSystem::self(), &KWindowSystem::windowAdded, [ = ](WId id) { @@ -118,10 +213,20 @@ AppDBManager::AppDBManager(QObject *parent) : QThread(parent), m_database(QSqlDa AppDBManager::~AppDBManager() { - if(m_watchAppDir) { - delete m_watchAppDir; + if (m_watcher) { + delete m_watcher; } - m_watchAppDir = NULL; + m_watcher = NULL; + + if (m_snapdWatcher) { + delete m_snapdWatcher; + } + m_snapdWatcher = NULL; + +// if(m_watchAppDir) { +// delete m_watchAppDir; +// } +// m_watchAppDir = NULL; closeDataBase(); } diff --git a/ukui-search-app-data-service/app-db-manager.h b/ukui-search-app-data-service/app-db-manager.h index bb444b0..9699e33 100644 --- a/ukui-search-app-data-service/app-db-manager.h +++ b/ukui-search-app-data-service/app-db-manager.h @@ -3,6 +3,7 @@ #include "app-db-common.h" #include "pending-app-info-queue.h" +#include "file-system-watcher.h" #include #include @@ -12,11 +13,11 @@ #include #include #include -#include #include #include -#include #include +//#include +//#include #define CONNECTION_NAME QLatin1String("ukss-appdb-connection") @@ -132,11 +133,16 @@ private: QSettings *m_qSettings = nullptr; - QTimer *m_timer = nullptr; - QTimer *m_maxProcessTimer = nullptr; +// QTimer *m_timer = nullptr; +// QTimer *m_maxProcessTimer = nullptr; +// QFileSystemWatcher *m_watchAppDir = nullptr; QSqlDatabase m_database; - QFileSystemWatcher *m_watchAppDir = nullptr; + FileSystemWatcher *m_watcher = nullptr; + + QDir *m_snapdDir = nullptr; + QString m_snapdPath; + FileSystemWatcher *m_snapdWatcher = nullptr; //应用黑名单 QStringList m_excludedDesktopfiles = { diff --git a/ukui-search-app-data-service/ukui-search-app-data-service.pro b/ukui-search-app-data-service/ukui-search-app-data-service.pro index b3e7261..03ffc09 100644 --- a/ukui-search-app-data-service/ukui-search-app-data-service.pro +++ b/ukui-search-app-data-service/ukui-search-app-data-service.pro @@ -58,5 +58,6 @@ INCLUDEPATH += $$PWD/../libchinese-segmentation DEPENDPATH += $$PWD/../libchinese-segmentation LIBS += -L$$OUT_PWD/../libsearch/ -lukui-search -INCLUDEPATH += $$PWD/../libsearch +INCLUDEPATH += $$PWD/../libsearch \ + $$PWD/../libsearch/filesystemwatcher DEPENDPATH += $$PWD/../libsearch