diff --git a/libsearch/appdata/app-db-manager.cpp b/libsearch/appdata/app-db-manager.cpp index d37a053..cd61e09 100644 --- a/libsearch/appdata/app-db-manager.cpp +++ b/libsearch/appdata/app-db-manager.cpp @@ -25,13 +25,13 @@ AppDBManager::~AppDBManager() } void AppDBManager::initDateBaseConnection() { - if(!m_database->isValid()) { - qWarning() << m_database->lastError(); + if(!m_database.isValid()) { + qWarning() << m_database.lastError(); QApplication::quit(); } - m_database->setDatabaseName(APP_DATABASE_PATH); - if(!m_database->open()) { - qWarning() << m_database->lastError(); + m_database.setDatabaseName(APP_DATABASE_PATH); + if(!m_database.open()) { + qWarning() << m_database.lastError(); QApplication::quit(); } //todo: 建表 diff --git a/libsearch/appdata/app-db-manager.h b/libsearch/appdata/app-db-manager.h index a5076b3..79affad 100644 --- a/libsearch/appdata/app-db-manager.h +++ b/libsearch/appdata/app-db-manager.h @@ -19,7 +19,7 @@ public: private: explicit AppDBManager(QObject *parent = nullptr); ~AppDBManager(); - QSqlDatabase *m_database = nullptr; + QSqlDatabase m_database; }; } diff --git a/libsearch/appdata/appdata.pri b/libsearch/appdata/appdata.pri index c6b5e43..875aabf 100644 --- a/libsearch/appdata/appdata.pri +++ b/libsearch/appdata/appdata.pri @@ -5,7 +5,7 @@ HEADERS += \ $$PWD/app-info-table-private.h \ $$PWD/app-info-table.h -SOURCES += \ \ +SOURCES += \ $$PWD/app-db-manager.cpp \ $$PWD/app-info-table.cpp diff --git a/libsearch/index/first-index.cpp b/libsearch/index/first-index.cpp index a35f9f5..51c0e35 100644 --- a/libsearch/index/first-index.cpp +++ b/libsearch/index/first-index.cpp @@ -20,6 +20,7 @@ */ //#include #include "first-index.h" +#include "dir-watcher.h" #include #define NEW_QUEUE(a) a = new QQueue(); qDebug("---------------------------%s %s %s new at %d..",__FILE__,__FUNCTION__,#a,__LINE__); @@ -158,8 +159,13 @@ void FirstIndex::run() { sem.acquire(4); sem.acquire(1); mutex1.unlock(); - this->setPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)); - this->Traverse(); + + qInfo() << "index dir" << DirWatcher::getDirWatcher()->currentindexableDir(); + qInfo() << "index block dir" << DirWatcher::getDirWatcher()->currentBlackListOfIndex(); + setPath(DirWatcher::getDirWatcher()->currentindexableDir()); + setBlockPath(DirWatcher::getDirWatcher()->currentBlackListOfIndex()); + Traverse(); + FileUtils::_max_index_count = this->q_index->length(); qDebug() << "max_index_count:" << FileUtils::_max_index_count; sem.release(5); diff --git a/libsearch/index/inotify-watch.cpp b/libsearch/index/inotify-watch.cpp index e5a49a2..52b797b 100644 --- a/libsearch/index/inotify-watch.cpp +++ b/libsearch/index/inotify-watch.cpp @@ -1,19 +1,20 @@ #include "inotify-watch.h" +#include "dir-watcher.h" #include #include #include using namespace UkuiSearch; static InotifyWatch* global_instance_InotifyWatch = nullptr; -UkuiSearch::InotifyWatch *UkuiSearch::InotifyWatch::getInstance(const QString &path) +UkuiSearch::InotifyWatch *UkuiSearch::InotifyWatch::getInstance() { if(!global_instance_InotifyWatch) { - global_instance_InotifyWatch = new InotifyWatch(path); + global_instance_InotifyWatch = new InotifyWatch(); } return global_instance_InotifyWatch; } -UkuiSearch::InotifyWatch::InotifyWatch(const QString &path): Traverse_BFS(path) +UkuiSearch::InotifyWatch::InotifyWatch(): Traverse_BFS() { qDebug() << "setInotifyMaxUserWatches start"; UkuiSearchQDBus usQDBus; @@ -98,17 +99,40 @@ void InotifyWatch::DoSomething(const QFileInfo &info) void InotifyWatch::firstTraverse() { QQueue bfs; - bfs.enqueue(this->path); + for(QString blockPath : m_blockList) { + for(QString path : m_pathList) { + if(FileUtils::isOrUnder(path, blockPath)) { + m_pathList.removeOne(path); + } + } + } + for(QString path : m_pathList) { + addWatch(path); + bfs.enqueue(path); + } + QFileInfoList list; QDir dir; + QStringList tmpList = m_blockList; dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); dir.setSorting(QDir::DirsFirst); while(!bfs.empty()) { dir.setPath(bfs.dequeue()); list = dir.entryInfoList(); for(auto i : list) { + bool isBlocked = false; + for(QString path : tmpList) { + if(i.absoluteFilePath() == path) { + isBlocked = true; + tmpList.removeOne(path); + break; + } + } + if(isBlocked) + continue; + if(i.isDir() && (!(i.isSymLink()))) { - this->addWatch(i.absoluteFilePath()); + addWatch(i.absoluteFilePath()); bfs.enqueue(i.absoluteFilePath()); } } @@ -147,9 +171,9 @@ void InotifyWatch::run() } } - this->addWatch(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)); - this->setPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)); - this->firstTraverse(); + setPath(DirWatcher::getDirWatcher()->currentindexableDir()); + setBlockPath(DirWatcher::getDirWatcher()->currentBlackListOfIndex()); + firstTraverse(); int fifo_fd; char buffer[2]; @@ -385,6 +409,13 @@ void InotifyWatch::eventProcess(const char *buffer, ssize_t len) // qDebug("mask:0x%x,",event->mask); if(event->name[0] != '.') { QString path = currentPath[event->wd] + '/' + event->name; + + //过滤黑名单下的信号 + for(QString i : m_blockList) { + if(FileUtils::isOrUnder(path, i)) + goto next; + } + //Create top dir first, traverse it last. if(event->mask & IN_CREATE) { // qDebug() << "IN_CREATE"; @@ -397,7 +428,7 @@ void InotifyWatch::eventProcess(const char *buffer, ssize_t len) if(event->mask & IN_ISDIR) { if(!QFileInfo(path).isSymLink()){ addWatch(path); - setPath(path); + setPath(QStringList() << path); Traverse(); } } @@ -437,7 +468,7 @@ void InotifyWatch::eventProcess(const char *buffer, ssize_t len) if(!QFileInfo(path).isSymLink()){ addWatch(path); - setPath(path); + setPath(QStringList() << path); Traverse(); } } else { diff --git a/libsearch/index/inotify-watch.h b/libsearch/index/inotify-watch.h index b0dd7a4..3091ceb 100644 --- a/libsearch/index/inotify-watch.h +++ b/libsearch/index/inotify-watch.h @@ -20,7 +20,7 @@ class InotifyWatch : public QThread, public Traverse_BFS { Q_OBJECT public: - static InotifyWatch* getInstance(const QString& path); + static InotifyWatch* getInstance(); bool addWatch(const QString &path); bool removeWatch(const QString &path, bool removeFromDatabase = true); @@ -34,7 +34,7 @@ protected: private Q_SLOTS: void slotEvent(char *buf, ssize_t len); private: - explicit InotifyWatch(const QString& path); + explicit InotifyWatch(); ~InotifyWatch(); char * filter(); void eventProcess(int socket); diff --git a/libsearch/index/search-method-manager.cpp b/libsearch/index/search-method-manager.cpp index c35c0c8..4c458b0 100644 --- a/libsearch/index/search-method-manager.cpp +++ b/libsearch/index/search-method-manager.cpp @@ -1,10 +1,12 @@ #include "search-method-manager.h" +#include "dir-watcher.h" using namespace UkuiSearch; static SearchMethodManager* global_instance = nullptr; SearchMethodManager::SearchMethodManager() { - m_iw = InotifyWatch::getInstance(HOME_PATH); + m_iw = InotifyWatch::getInstance(); + } SearchMethodManager *SearchMethodManager::getInstance() diff --git a/libsearch/index/traverse_bfs.cpp b/libsearch/index/traverse_bfs.cpp index e2d35b9..1e434dc 100644 --- a/libsearch/index/traverse_bfs.cpp +++ b/libsearch/index/traverse_bfs.cpp @@ -18,17 +18,29 @@ * */ #include "traverse_bfs.h" +#include "file-utils.h" using namespace UkuiSearch; -Traverse_BFS::Traverse_BFS(const QString& path) { +Traverse_BFS::Traverse_BFS(const QStringList &path) { Q_ASSERT('/' == path.at(0)); - this->path = path; + m_pathList = path; } void Traverse_BFS::Traverse() { QQueue bfs; - bfs.enqueue(this->path); + for(QString blockPath : m_blockList) { + for(QString path : m_pathList) { + if(FileUtils::isOrUnder(path, blockPath)) { + m_pathList.removeOne(path); + } + } + } + for(QString path : m_pathList) { + bfs.enqueue(path); + } + QFileInfoList list; QDir dir; + QStringList tmpList = m_blockList; // QDir::Hidden dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); dir.setSorting(QDir::DirsFirst); @@ -36,6 +48,17 @@ void Traverse_BFS::Traverse() { dir.setPath(bfs.dequeue()); list = dir.entryInfoList(); for(auto i : list) { + bool isBlocked = false; + for(QString path : tmpList) { + if(i.absoluteFilePath() == path) { + isBlocked = true; + tmpList.removeOne(path); + break; + } + } + if(isBlocked) + continue; + if(i.isDir() && (!(i.isSymLink()))) { bfs.enqueue(i.absoluteFilePath()); } @@ -44,6 +67,11 @@ void Traverse_BFS::Traverse() { } } -void Traverse_BFS::setPath(const QString& path) { - this->path = path; +void Traverse_BFS::setPath(const QStringList &pathList) { + m_pathList = pathList; +} + +void Traverse_BFS::setBlockPath(const QStringList &pathList) +{ + m_blockList =pathList; } diff --git a/libsearch/index/traverse_bfs.h b/libsearch/index/traverse_bfs.h index c67d16c..e52471a 100644 --- a/libsearch/index/traverse_bfs.h +++ b/libsearch/index/traverse_bfs.h @@ -31,10 +31,12 @@ public: void Traverse(); virtual ~Traverse_BFS() = default; virtual void DoSomething(const QFileInfo&) = 0; - void setPath(const QString&); + void setPath(const QStringList&); + void setBlockPath(const QStringList &pathList); protected: - Traverse_BFS(const QString&); - QString path = "/home"; + Traverse_BFS(const QStringList&); + QStringList m_pathList; + QStringList m_blockList; private: Traverse_BFS(const Traverse_BFS&) = delete; void operator=(const Traverse_BFS&) = delete; diff --git a/libsearch/libukui-search-headers.pri b/libsearch/libukui-search-headers.pri index 14b293e..00ec7b0 100644 --- a/libsearch/libukui-search-headers.pri +++ b/libsearch/libukui-search-headers.pri @@ -6,4 +6,5 @@ INCLUDEPATH += $$PWD/pluginmanage INCLUDEPATH += $$PWD/settingsearch INCLUDEPATH += $$PWD/appsearch INCLUDEPATH += $$PWD/searchinterface +INCLUDEPATH += $$PWD/dirwatcher diff --git a/ukui-search-service/ukui-search-service.cpp b/ukui-search-service/ukui-search-service.cpp index a59ad50..5944891 100644 --- a/ukui-search-service/ukui-search-service.cpp +++ b/ukui-search-service/ukui-search-service.cpp @@ -1,5 +1,6 @@ #include #include "ukui-search-service.h" +#include "dir-watcher.h" #define UKUI_SEARCH_SCHEMAS "org.ukui.search.settings" #define SEARCH_METHOD_KEY "indexSearch" using namespace UkuiSearch; @@ -14,6 +15,7 @@ UkuiSearchService::UkuiSearchService(int &argc, char *argv[], const QString &app this->parseCmd(msg, true); }); + DirWatcher::getDirWatcher(); FileUtils::loadHanziTable("://index/pinyinWithoutTone.txt"); initGsettings(); } diff --git a/ukui-search-service/ukui-search-service.pro b/ukui-search-service/ukui-search-service.pro index 53a1ebb..b09fa82 100644 --- a/ukui-search-service/ukui-search-service.pro +++ b/ukui-search-service/ukui-search-service.pro @@ -7,7 +7,7 @@ TARGET = ukui-search-service VERSION = 1.0.0 DEFINES += VERSION='\\"$${VERSION}\\"' CONFIG += c++11 link_pkgconfig no_keywords lrelease -PKGCONFIG += gsettings-qt +PKGCONFIG += gsettings-qt gio-unix-2.0 # The following define makes your compiler emit warnings if you use # any Qt feature that has been marked deprecated (the exact warnings