文件索引增加配置索引目录和黑名单功能;顺便解决了一点编译问题。

This commit is contained in:
iaom 2022-03-04 15:15:07 +08:00
parent 5abd667bb8
commit 24a94db0c6
12 changed files with 103 additions and 31 deletions

View File

@ -25,13 +25,13 @@ AppDBManager::~AppDBManager()
} }
void AppDBManager::initDateBaseConnection() void AppDBManager::initDateBaseConnection()
{ {
if(!m_database->isValid()) { if(!m_database.isValid()) {
qWarning() << m_database->lastError(); qWarning() << m_database.lastError();
QApplication::quit(); QApplication::quit();
} }
m_database->setDatabaseName(APP_DATABASE_PATH); m_database.setDatabaseName(APP_DATABASE_PATH);
if(!m_database->open()) { if(!m_database.open()) {
qWarning() << m_database->lastError(); qWarning() << m_database.lastError();
QApplication::quit(); QApplication::quit();
} }
//todo: 建表 //todo: 建表

View File

@ -19,7 +19,7 @@ public:
private: private:
explicit AppDBManager(QObject *parent = nullptr); explicit AppDBManager(QObject *parent = nullptr);
~AppDBManager(); ~AppDBManager();
QSqlDatabase *m_database = nullptr; QSqlDatabase m_database;
}; };
} }

View File

@ -5,7 +5,7 @@ HEADERS += \
$$PWD/app-info-table-private.h \ $$PWD/app-info-table-private.h \
$$PWD/app-info-table.h $$PWD/app-info-table.h
SOURCES += \ \ SOURCES += \
$$PWD/app-db-manager.cpp \ $$PWD/app-db-manager.cpp \
$$PWD/app-info-table.cpp $$PWD/app-info-table.cpp

View File

@ -20,6 +20,7 @@
*/ */
//#include <QtConcurrent> //#include <QtConcurrent>
#include "first-index.h" #include "first-index.h"
#include "dir-watcher.h"
#include <QDebug> #include <QDebug>
#define NEW_QUEUE(a) a = new QQueue<QString>(); qDebug("---------------------------%s %s %s new at %d..",__FILE__,__FUNCTION__,#a,__LINE__); #define NEW_QUEUE(a) a = new QQueue<QString>(); qDebug("---------------------------%s %s %s new at %d..",__FILE__,__FUNCTION__,#a,__LINE__);
@ -158,8 +159,13 @@ void FirstIndex::run() {
sem.acquire(4); sem.acquire(4);
sem.acquire(1); sem.acquire(1);
mutex1.unlock(); 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(); FileUtils::_max_index_count = this->q_index->length();
qDebug() << "max_index_count:" << FileUtils::_max_index_count; qDebug() << "max_index_count:" << FileUtils::_max_index_count;
sem.release(5); sem.release(5);

View File

@ -1,19 +1,20 @@
#include "inotify-watch.h" #include "inotify-watch.h"
#include "dir-watcher.h"
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <malloc.h> #include <malloc.h>
#include <errno.h> #include <errno.h>
using namespace UkuiSearch; using namespace UkuiSearch;
static InotifyWatch* global_instance_InotifyWatch = nullptr; static InotifyWatch* global_instance_InotifyWatch = nullptr;
UkuiSearch::InotifyWatch *UkuiSearch::InotifyWatch::getInstance(const QString &path) UkuiSearch::InotifyWatch *UkuiSearch::InotifyWatch::getInstance()
{ {
if(!global_instance_InotifyWatch) { if(!global_instance_InotifyWatch) {
global_instance_InotifyWatch = new InotifyWatch(path); global_instance_InotifyWatch = new InotifyWatch();
} }
return global_instance_InotifyWatch; return global_instance_InotifyWatch;
} }
UkuiSearch::InotifyWatch::InotifyWatch(const QString &path): Traverse_BFS(path) UkuiSearch::InotifyWatch::InotifyWatch(): Traverse_BFS()
{ {
qDebug() << "setInotifyMaxUserWatches start"; qDebug() << "setInotifyMaxUserWatches start";
UkuiSearchQDBus usQDBus; UkuiSearchQDBus usQDBus;
@ -98,17 +99,40 @@ void InotifyWatch::DoSomething(const QFileInfo &info)
void InotifyWatch::firstTraverse() void InotifyWatch::firstTraverse()
{ {
QQueue<QString> bfs; QQueue<QString> 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; QFileInfoList list;
QDir dir; QDir dir;
QStringList tmpList = m_blockList;
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
dir.setSorting(QDir::DirsFirst); dir.setSorting(QDir::DirsFirst);
while(!bfs.empty()) { while(!bfs.empty()) {
dir.setPath(bfs.dequeue()); dir.setPath(bfs.dequeue());
list = dir.entryInfoList(); list = dir.entryInfoList();
for(auto i : list) { 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()))) { if(i.isDir() && (!(i.isSymLink()))) {
this->addWatch(i.absoluteFilePath()); addWatch(i.absoluteFilePath());
bfs.enqueue(i.absoluteFilePath()); bfs.enqueue(i.absoluteFilePath());
} }
} }
@ -147,9 +171,9 @@ void InotifyWatch::run()
} }
} }
this->addWatch(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)); setPath(DirWatcher::getDirWatcher()->currentindexableDir());
this->setPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)); setBlockPath(DirWatcher::getDirWatcher()->currentBlackListOfIndex());
this->firstTraverse(); firstTraverse();
int fifo_fd; int fifo_fd;
char buffer[2]; char buffer[2];
@ -385,6 +409,13 @@ void InotifyWatch::eventProcess(const char *buffer, ssize_t len)
// qDebug("mask:0x%x,",event->mask); // qDebug("mask:0x%x,",event->mask);
if(event->name[0] != '.') { if(event->name[0] != '.') {
QString path = currentPath[event->wd] + '/' + event->name; 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. //Create top dir first, traverse it last.
if(event->mask & IN_CREATE) { if(event->mask & IN_CREATE) {
// qDebug() << "IN_CREATE"; // qDebug() << "IN_CREATE";
@ -397,7 +428,7 @@ void InotifyWatch::eventProcess(const char *buffer, ssize_t len)
if(event->mask & IN_ISDIR) { if(event->mask & IN_ISDIR) {
if(!QFileInfo(path).isSymLink()){ if(!QFileInfo(path).isSymLink()){
addWatch(path); addWatch(path);
setPath(path); setPath(QStringList() << path);
Traverse(); Traverse();
} }
} }
@ -437,7 +468,7 @@ void InotifyWatch::eventProcess(const char *buffer, ssize_t len)
if(!QFileInfo(path).isSymLink()){ if(!QFileInfo(path).isSymLink()){
addWatch(path); addWatch(path);
setPath(path); setPath(QStringList() << path);
Traverse(); Traverse();
} }
} else { } else {

View File

@ -20,7 +20,7 @@ class InotifyWatch : public QThread, public Traverse_BFS
{ {
Q_OBJECT Q_OBJECT
public: public:
static InotifyWatch* getInstance(const QString& path); static InotifyWatch* getInstance();
bool addWatch(const QString &path); bool addWatch(const QString &path);
bool removeWatch(const QString &path, bool removeFromDatabase = true); bool removeWatch(const QString &path, bool removeFromDatabase = true);
@ -34,7 +34,7 @@ protected:
private Q_SLOTS: private Q_SLOTS:
void slotEvent(char *buf, ssize_t len); void slotEvent(char *buf, ssize_t len);
private: private:
explicit InotifyWatch(const QString& path); explicit InotifyWatch();
~InotifyWatch(); ~InotifyWatch();
char * filter(); char * filter();
void eventProcess(int socket); void eventProcess(int socket);

View File

@ -1,10 +1,12 @@
#include "search-method-manager.h" #include "search-method-manager.h"
#include "dir-watcher.h"
using namespace UkuiSearch; using namespace UkuiSearch;
static SearchMethodManager* global_instance = nullptr; static SearchMethodManager* global_instance = nullptr;
SearchMethodManager::SearchMethodManager() SearchMethodManager::SearchMethodManager()
{ {
m_iw = InotifyWatch::getInstance(HOME_PATH); m_iw = InotifyWatch::getInstance();
} }
SearchMethodManager *SearchMethodManager::getInstance() SearchMethodManager *SearchMethodManager::getInstance()

View File

@ -18,17 +18,29 @@
* *
*/ */
#include "traverse_bfs.h" #include "traverse_bfs.h"
#include "file-utils.h"
using namespace UkuiSearch; using namespace UkuiSearch;
Traverse_BFS::Traverse_BFS(const QString& path) { Traverse_BFS::Traverse_BFS(const QStringList &path) {
Q_ASSERT('/' == path.at(0)); Q_ASSERT('/' == path.at(0));
this->path = path; m_pathList = path;
} }
void Traverse_BFS::Traverse() { void Traverse_BFS::Traverse() {
QQueue<QString> bfs; QQueue<QString> 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; QFileInfoList list;
QDir dir; QDir dir;
QStringList tmpList = m_blockList;
// QDir::Hidden // QDir::Hidden
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
dir.setSorting(QDir::DirsFirst); dir.setSorting(QDir::DirsFirst);
@ -36,6 +48,17 @@ void Traverse_BFS::Traverse() {
dir.setPath(bfs.dequeue()); dir.setPath(bfs.dequeue());
list = dir.entryInfoList(); list = dir.entryInfoList();
for(auto i : list) { 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()))) { if(i.isDir() && (!(i.isSymLink()))) {
bfs.enqueue(i.absoluteFilePath()); bfs.enqueue(i.absoluteFilePath());
} }
@ -44,6 +67,11 @@ void Traverse_BFS::Traverse() {
} }
} }
void Traverse_BFS::setPath(const QString& path) { void Traverse_BFS::setPath(const QStringList &pathList) {
this->path = path; m_pathList = pathList;
}
void Traverse_BFS::setBlockPath(const QStringList &pathList)
{
m_blockList =pathList;
} }

View File

@ -31,10 +31,12 @@ public:
void Traverse(); void Traverse();
virtual ~Traverse_BFS() = default; virtual ~Traverse_BFS() = default;
virtual void DoSomething(const QFileInfo&) = 0; virtual void DoSomething(const QFileInfo&) = 0;
void setPath(const QString&); void setPath(const QStringList&);
void setBlockPath(const QStringList &pathList);
protected: protected:
Traverse_BFS(const QString&); Traverse_BFS(const QStringList&);
QString path = "/home"; QStringList m_pathList;
QStringList m_blockList;
private: private:
Traverse_BFS(const Traverse_BFS&) = delete; Traverse_BFS(const Traverse_BFS&) = delete;
void operator=(const Traverse_BFS&) = delete; void operator=(const Traverse_BFS&) = delete;

View File

@ -6,4 +6,5 @@ INCLUDEPATH += $$PWD/pluginmanage
INCLUDEPATH += $$PWD/settingsearch INCLUDEPATH += $$PWD/settingsearch
INCLUDEPATH += $$PWD/appsearch INCLUDEPATH += $$PWD/appsearch
INCLUDEPATH += $$PWD/searchinterface INCLUDEPATH += $$PWD/searchinterface
INCLUDEPATH += $$PWD/dirwatcher

View File

@ -1,5 +1,6 @@
#include <QDebug> #include <QDebug>
#include "ukui-search-service.h" #include "ukui-search-service.h"
#include "dir-watcher.h"
#define UKUI_SEARCH_SCHEMAS "org.ukui.search.settings" #define UKUI_SEARCH_SCHEMAS "org.ukui.search.settings"
#define SEARCH_METHOD_KEY "indexSearch" #define SEARCH_METHOD_KEY "indexSearch"
using namespace UkuiSearch; using namespace UkuiSearch;
@ -14,6 +15,7 @@ UkuiSearchService::UkuiSearchService(int &argc, char *argv[], const QString &app
this->parseCmd(msg, true); this->parseCmd(msg, true);
}); });
DirWatcher::getDirWatcher();
FileUtils::loadHanziTable("://index/pinyinWithoutTone.txt"); FileUtils::loadHanziTable("://index/pinyinWithoutTone.txt");
initGsettings(); initGsettings();
} }

View File

@ -7,7 +7,7 @@ TARGET = ukui-search-service
VERSION = 1.0.0 VERSION = 1.0.0
DEFINES += VERSION='\\"$${VERSION}\\"' DEFINES += VERSION='\\"$${VERSION}\\"'
CONFIG += c++11 link_pkgconfig no_keywords lrelease 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 # The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings # any Qt feature that has been marked deprecated (the exact warnings