增加文件内容模糊搜索功能,优化全局设置代码

This commit is contained in:
iaom 2022-11-28 18:11:55 +08:00
parent 3d8cebba41
commit 134d2039ec
12 changed files with 233 additions and 480 deletions

View File

@ -10,5 +10,10 @@
<summary>web engine</summary>
<description>Web engine to search keyword online.</description>
</key>
<key name="content-fuzzy-search" type="b">
<default>false</default>
<summary>content fuzzy search</summary>
<description>Enable or disable fuzzy search for file content.</description>
</key>
</schema>
</schemalist>

View File

@ -5,10 +5,10 @@ INCLUDEPATH += $$PWD
HEADERS += \
$$PWD/search-line-edit.h \
$$PWD/settings-widget.h \
# $$PWD/settings-widget.h \
$$PWD/create-index-ask-dialog.h \
SOURCES += \
$$PWD/search-line-edit.cpp \
$$PWD/settings-widget.cpp \
# $$PWD/settings-widget.cpp \
$$PWD/create-index-ask-dialog.cpp \

View File

@ -29,11 +29,8 @@
#include <QScreen>
#include <QStyleOption>
#include <QPixmap>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
#include <KWindowEffects>
#include <KWindowSystem>
#endif
#include <QtX11Extras/QX11Info>
#include "ukuistylehelper/ukuistylehelper.h"
#include "windowmanager/windowmanager.h"
@ -42,9 +39,6 @@
#define MAIN_MARGINS 0, 0, 0, 0
#define TITLE_MARGINS 0,0,0,0
#define UKUI_SEARCH_SCHEMAS "org.ukui.search.settings"
#define SEARCH_METHOD_KEY "fileIndexEnable"
#define WEB_ENGINE_KEY "webEngine"
#define WINDOW_WIDTH 700
#define WINDOW_HEIGHT 610
#define TITLE_HEIGHT 40
@ -54,6 +48,10 @@
#define ASK_INDEX_TIME 5*1000
#define RESEARCH_TIME 10*1000
#define MAIN_SETTINGS QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search.conf"
#define ENABLE_CREATE_INDEX_ASK_DIALOG "enable_create_index_ask_dialog"
using namespace UkuiSearch;
extern void qt_blurImage(QImage &blurImage, qreal radius, bool quality, int transposed);
/**
@ -72,6 +70,7 @@ MainWindow::MainWindow(QWidget *parent) :
this->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
this->setWindowTitle(tr("ukui-search"));
KWindowSystem::setState(this->winId(),NET::SkipTaskbar | NET::SkipPager | NET::SkipSwitcher );
initSettings();
initUi();
initTimer();
@ -81,7 +80,7 @@ MainWindow::MainWindow(QWidget *parent) :
m_sys_tray_icon->show();
installEventFilter(this);
initConnections();
initGsettings();
// connect(KWindowSystem::self(), &KWindowSystem::activeWindowChanged, this,[&](WId activeWindowId){
// qDebug() << "activeWindowChanged!!!" << activeWindowId;
@ -97,19 +96,9 @@ MainWindow::MainWindow(QWidget *parent) :
this->setText(keyword);
});
connect(ActionTransmiter::getInstance(), &ActionTransmiter::hideUIAction, this, &MainWindow::tryHideMainwindow);
//NEW_TODO, register plugins
// SearchPluginManager::getInstance()->registerPlugin(\\);
// m_stackedWidget->setPlugins(SearchPluginManager::getInstance()->getPluginIds());
// m_stackedWidget->setPlugins(SearchPluginManager::getInstance()->getPluginIds());
}
MainWindow::~MainWindow() {
#if (QT_VERSION < QT_VERSION_CHECK(5, 12, 0))
if(m_settingsWidget) {
delete m_settingsWidget;
m_settingsWidget = NULL;
}
#endif
if(m_askDialog) {
delete m_askDialog;
m_askDialog = NULL;
@ -118,9 +107,9 @@ MainWindow::~MainWindow() {
delete m_askTimer;
m_askTimer = NULL;
}
if(m_search_gsettings) {
delete m_search_gsettings;
m_search_gsettings = NULL;
if(m_searchGsettings) {
delete m_searchGsettings;
m_searchGsettings = NULL;
}
}
@ -129,17 +118,7 @@ MainWindow::~MainWindow() {
*/
void MainWindow::initUi() {
this->setFixedSize(WINDOW_WIDTH, 68);
// this->setStyleSheet("QMainWindow{border:2px solid red;}");
// m_widget = new QWidget(this);
// this->setCentralWidget(m_widget);
// m_widget->setFixedSize(this->size());
// QVBoxLayout * mainlayout = new QVBoxLayout(m_frame);
// mainlayout->setContentsMargins(MAIN_MARGINS);
// m_frame->setLayout(mainlayout);
// m_stackedWidget = new StackedWidget(m_frame);//内容栏
m_searchBarWidget = new SeachBarWidget(this);
m_searchBarWidget->move(this->rect().topLeft());
m_searchBarWidget->show();
@ -148,41 +127,20 @@ void MainWindow::initUi() {
m_searchResultPage->move(0, 58);
this->setFocusProxy(m_searchBarWidget);
// m_searchResultPage->show();
// m_searchWidget = new SeachBarWidget(this);
// m_searchLayout = new SearchBarHLayout(this);
// m_searchWidget->setLayout(m_searchLayout);
// m_searchWidget->setFixedHeight(SEARCH_BAR_SIZE);
// mainlayout->addWidget(m_titleFrame);
// mainlayout->addWidget(m_seachBarWidget);
// mainlayout->addSpacing(8);
// mainlayout->addWidget(m_searchResultPage);
//创建索引询问弹窗
m_askDialog = new CreateIndexAskDialog(this);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
// MotifWmHints ask_dialog_hints;
// ask_dialog_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
// ask_dialog_hints.functions = MWM_FUNC_ALL;
// ask_dialog_hints.decorations = MWM_DECOR_BORDER;
// XAtomHelper::getInstance()->setWindowMotifHint(m_askDialog->winId(), ask_dialog_hints);
#endif
}
void MainWindow::initConnections()
{
connect(m_sys_tray_icon, &QSystemTrayIcon::activated, this, &MainWindow::trayIconActivatedSlot);
QObject::connect(this, &MainWindow::searchMethodChanged, this, [ = ](FileUtils::SearchMethod sm) {
FileUtils::searchMethod = sm;
});
connect(QApplication::primaryScreen(), &QScreen::geometryChanged, this, &MainWindow::monitorResolutionChange);
connect(qApp, &QApplication::primaryScreenChanged, this, &MainWindow::primaryScreenChangedSlot);
connect(m_askDialog, &CreateIndexAskDialog::closed, this, [ = ]() {
m_isAskDialogVisible = false;
});
connect(m_askDialog, &CreateIndexAskDialog::btnClicked, this, [ = ](const bool &is_create_index, const bool &is_ask_again) {
setSearchMethodConfig(is_create_index, is_ask_again);
connect(m_askDialog, &CreateIndexAskDialog::btnClicked, this, [ = ](const bool &isCreateIndex, const bool &isAskAgain) {
setSearchMethodConfig(isCreateIndex, isAskAgain);
});
// connect(m_settingsBtn, &QPushButton::clicked, this, &MainWindow::settingsBtnClickedSlot);
//主题改变时,更新自定义标题栏的图标
@ -242,74 +200,28 @@ void MainWindow::trayIconActivatedSlot(QSystemTrayIcon::ActivationReason reason)
* @param create_index
* @param no_longer_ask
*/
void MainWindow::setSearchMethodConfig(const bool &create_index, const bool &no_longer_ask)
void MainWindow::setSearchMethodConfig(const bool& createIndex, const bool& noLongerAsk)
{
if(no_longer_ask) {
GlobalSettings::getInstance()->setValue(ENABLE_CREATE_INDEX_ASK_DIALOG, "false");
if(noLongerAsk) {
m_settings->setValue(ENABLE_CREATE_INDEX_ASK_DIALOG, false);
} else {
GlobalSettings::getInstance()->setValue(ENABLE_CREATE_INDEX_ASK_DIALOG, "true");
m_settings->setValue(ENABLE_CREATE_INDEX_ASK_DIALOG, true);
}
if(create_index) {
if(m_search_gsettings && m_search_gsettings->keys().contains(SEARCH_METHOD_KEY)) {
m_search_gsettings->set(SEARCH_METHOD_KEY, true);
if(createIndex) {
if(m_searchGsettings && m_searchGsettings->keys().contains(FILE_INDEX_ENABLE_KEY)) {
m_searchGsettings->set(FILE_INDEX_ENABLE_KEY, true);
}
Q_EMIT this->searchMethodChanged(FileUtils::SearchMethod::INDEXSEARCH);
//创建索引十秒后重新搜索一次(如果用户十秒内没有退出搜索界面且没有重新搜索)
m_researchTimer->start();
}
}
/**
* @brief MainWindow::settingsBtnClickedSlot
*/
void MainWindow::settingsBtnClickedSlot()
{
#if (QT_VERSION < QT_VERSION_CHECK(5, 12, 0))
if(m_settingsWidget) { //当此窗口已存在时,仅需置顶
if(!m_settingsWidget->isVisible()) {
centerToScreen(m_settingsWidget);
}
m_settingsWidget->showWidget();
return;
}
m_settingsWidget = new SettingsWidget();
connect(this, &MainWindow::webEngineChanged, m_settingsWidget, [ = ]() {
m_settingsWidget->resetWebEngine();
});
connect(m_settingsWidget, &SettingsWidget::webEngineChanged, this, [ = ](const QString & engine) {
if(m_search_gsettings && m_search_gsettings->keys().contains(WEB_ENGINE_KEY)) {
m_search_gsettings->set(WEB_ENGINE_KEY, engine);
} else {
GlobalSettings::getInstance()->setValue(WEB_ENGINE, engine);
}
});
centerToScreen(m_settingsWidget);
m_settingsWidget->show();
connect(m_settingsWidget, &SettingsWidget::settingWidgetClosed, this, [ = ]() {
QTimer::singleShot(100, this, [ = ] {
// clearSearchResult(); //现暂定从设置页返回主页面不清空搜索结果
this->setWindowState(this->windowState() & ~Qt::WindowMinimized);
this->raise();
this->showNormal();
this->activateWindow();
});
});
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
//打开控制面板的设置页
QProcess process;
process.startDetached("ukui-control-center -m search");
#endif
}
/**
* @brief MainWindow::searchKeywordSlot
* @param keyword
*/
void MainWindow::searchKeywordSlot(const QString &keyword)
{
//NEW_TODO
if(keyword == "") {
if(keyword == "" || keyword.isEmpty()) {
// m_stackedWidget->setPage(int(StackedPage::HomePage));
QTimer::singleShot(10, this, [ = ]() {
m_askTimer->stop();
@ -319,11 +231,13 @@ void MainWindow::searchKeywordSlot(const QString &keyword)
});
} else {
// m_stackedWidget->setPage(int(StackedPage::SearchPage));
QTimer::singleShot(10, this, [ = ]() {
//允许弹窗且当前次搜索(为关闭主界面,算一次搜索过程)未询问且当前为暴力搜索
if(GlobalSettings::getInstance()->getValue(ENABLE_CREATE_INDEX_ASK_DIALOG).toString() != "false" && !m_currentSearchAsked && FileUtils::searchMethod == FileUtils::SearchMethod::DIRECTSEARCH)
if(m_settings->value(ENABLE_CREATE_INDEX_ASK_DIALOG).toBool()
&& !m_currentSearchAsked
&& GlobalSettings::getInstance()->getValue(FILE_INDEX_ENABLE_KEY).toBool() == false) {
m_askTimer->start();
}
Q_EMIT m_searchResultPage->startSearch(keyword);
this->resizeHeight(WINDOW_HEIGHT);
@ -428,16 +342,6 @@ void MainWindow::centerToScreen(QWidget* widget) {
int desk_y = desk_rect.height();
int x = widget->width();
int y = widget->height();
// QDBusInterface primaryScreenInterface("org.ukui.SettingsDaemon",
// "/org/ukui/SettingsDaemon/wayland",
// "org.ukui.SettingsDaemon.wayland",
// QDBusConnection::sessionBus());
// if(QDBusReply<int>(primaryScreenInterface.call("x")).isValid()) {
// QDBusReply<int> width = primaryScreenInterface.call("width");
// QDBusReply<int> height = primaryScreenInterface.call("height");
// desk_x = width;
// desk_y = height;
// }
widget->show();
kdk::WindowManager::setGeometry(this->windowHandle(),QRect(desk_x / 2 - x / 2 + desk_rect.left(),
desk_y / 3 + desk_rect.top(),
@ -450,29 +354,20 @@ void MainWindow::centerToScreen(QWidget* widget) {
// widget->move(desk_x / 2 - x / 2 + desk_rect.left(), desk_y / 3 + desk_rect.top());
}
void MainWindow::initGsettings() {
void MainWindow::initSettings() {
const QByteArray id(UKUI_SEARCH_SCHEMAS);
if(QGSettings::isSchemaInstalled(id)) {
m_search_gsettings = new QGSettings(id);
connect(m_search_gsettings, &QGSettings::changed, this, [ = ](const QString & key) {
if(key == SEARCH_METHOD_KEY) {
bool is_index_search = m_search_gsettings->get(SEARCH_METHOD_KEY).toBool();
this->setSearchMethod(is_index_search);
} else if(key == WEB_ENGINE_KEY) {
QString web_engine = m_search_gsettings->get(WEB_ENGINE_KEY).toString();
GlobalSettings::getInstance()->setValue(WEB_ENGINE, web_engine);
Q_EMIT this->webEngineChanged();
m_searchGsettings = new QGSettings(id);
connect(m_searchGsettings, &QGSettings::changed, this, [ = ](const QString & key) {
if(key == FILE_INDEX_ENABLE_KEY) {
bool isIndexSearch = m_searchGsettings->get(FILE_INDEX_ENABLE_KEY).toBool();
if(m_researchTimer->isActive() && !isIndexSearch) {
m_researchTimer->stop();
}
}
});
if(m_search_gsettings->keys().contains(SEARCH_METHOD_KEY)) {
bool is_index_search = m_search_gsettings->get(SEARCH_METHOD_KEY).toBool();
this->setSearchMethod(is_index_search);
}
if(m_search_gsettings->keys().contains(WEB_ENGINE_KEY)) {
QString web_engine = m_search_gsettings->get(WEB_ENGINE_KEY).toString();
GlobalSettings::getInstance()->setValue(WEB_ENGINE, web_engine);
}
}
m_settings = new QSettings(MAIN_SETTINGS, QSettings::IniFormat, this);
}
//使用GSetting获取当前窗口应该使用的透明度
@ -509,7 +404,7 @@ void MainWindow::initTimer() {
m_askTimer->stop();
} else {
//允许弹窗且当前次搜索(为关闭主界面,算一次搜索过程)未询问且当前为暴力搜索
if(GlobalSettings::getInstance()->getValue(ENABLE_CREATE_INDEX_ASK_DIALOG).toString() != "false" && !m_currentSearchAsked && FileUtils::searchMethod == FileUtils::SearchMethod::DIRECTSEARCH)
if(m_settings->value(ENABLE_CREATE_INDEX_ASK_DIALOG).toBool() && !m_currentSearchAsked && GlobalSettings::getInstance()->getValue(FILE_INDEX_ENABLE_KEY).toBool() == false)
m_askTimer->start();
}
});
@ -539,11 +434,8 @@ bool MainWindow::tryHideMainwindow()
* @brief MainWindow::setSearchMethod
* @param is_index_search true为索引搜索false为暴力搜索
*/
void MainWindow::setSearchMethod(const bool &is_index_search) {
if(is_index_search) {
Q_EMIT this->searchMethodChanged(FileUtils::SearchMethod::INDEXSEARCH);
} else {
Q_EMIT this->searchMethodChanged(FileUtils::SearchMethod::DIRECTSEARCH);
void MainWindow::setSearchMethod(const bool isIndexSearch) {
if(!isIndexSearch) {
m_researchTimer->stop();
}
}

View File

@ -69,14 +69,13 @@ public:
~MainWindow();
/**
* @brief Load the main window
* The position which mainwindow shows follow the ukui-panel.
*/
// The position which mainwindow shows follow the ukui-panel.
void moveToPanel();
// The position which mainwindow shows in the center of screen where the cursor in.
void centerToScreen(QWidget* widget);
void initGsettings();
void initSettings();
protected:
void paintEvent(QPaintEvent *);
@ -87,11 +86,6 @@ protected:
public:
bool eventFilter(QObject *watched, QEvent *event) override;
Q_SIGNALS:
void searchMethodChanged(FileUtils::SearchMethod);
void webEngineChanged();
void setText(QString keyword);
public Q_SLOTS:
/**
* @brief Monitor screen resolution
@ -103,53 +97,37 @@ public Q_SLOTS:
* @param screen: Primary screen
*/
void primaryScreenChangedSlot(QScreen *screen);
void bootOptionsFilter(QString opt); // 过滤终端命令
void clearSearchResult(); //清空搜索结果
void trayIconActivatedSlot(QSystemTrayIcon::ActivationReason reason);
void settingsBtnClickedSlot();
void searchKeywordSlot(const QString&);
void resizeHeight(int height);
void tryHide();
Q_SIGNALS:
void setText(QString keyword);
private:
QWidget *m_widget = nullptr; // central widget
QFrame * m_titleFrame = nullptr; // Title bar frame
QHBoxLayout * m_titleLyt = nullptr; // Title layout
QLabel * m_iconLabel = nullptr; // Icon lable
QLabel * m_titleLabel = nullptr; // Title lable
QPushButton * m_settingsBtn = nullptr; // Menu button
// StackedWidget * m_stackedWidget = nullptr; // Stacked widget
// SearchBarHLayout * m_searchLayout = nullptr; // Search bar layout
// SeachBarWidget * m_searchWidget = nullptr; // Search bar
SeachBarWidget * m_searchBarWidget;
SearchResultPage * m_searchResultPage;
#if (QT_VERSION < QT_VERSION_CHECK(5, 12, 0))
SettingsWidget * m_settingsWidget = nullptr; // Settings Widget
#endif
QStringList m_dirList;
QQueue<QString> * m_search_result_file = nullptr;
QQueue<QString> * m_search_result_dir = nullptr;
QQueue<QPair<QString, QStringList>> * m_search_result_content = nullptr;
QSystemTrayIcon * m_sys_tray_icon = nullptr;
CreateIndexAskDialog * m_askDialog = nullptr;
bool m_isAskDialogVisible = false;
QTimer * m_askTimer = nullptr; //询问是否创建索引弹窗弹出的计时器
QTimer * m_researchTimer = nullptr; //创建索引后重新执行一次搜索的计时器
bool m_currentSearchAsked = false; //本次搜索是否已经询问过是否创建索引了
QGSettings * m_search_gsettings = nullptr;
AppWidgetPlugin *m_appWidgetPlugin = nullptr;
void setSearchMethod(const bool&);
void setSearchMethod(const bool isIndexSearch);
double getTransparentData();
void initTimer();
bool tryHideMainwindow();
void setSearchMethodConfig(const bool&, const bool&);
void setSearchMethodConfig(const bool& createIndex, const bool& noLongerAsk);
SeachBarWidget *m_searchBarWidget;
SearchResultPage *m_searchResultPage;
QSystemTrayIcon *m_sys_tray_icon = nullptr;
CreateIndexAskDialog *m_askDialog = nullptr;
bool m_isAskDialogVisible = false;
QTimer *m_askTimer = nullptr; //询问是否创建索引弹窗弹出的计时器
QTimer *m_researchTimer = nullptr; //创建索引后重新执行一次搜索的计时器
bool m_currentSearchAsked = false; //本次搜索是否已经询问过是否创建索引了
QGSettings *m_searchGsettings = nullptr;
QSettings *m_settings = nullptr;
AppWidgetPlugin *m_appWidgetPlugin = nullptr;
};
}

View File

@ -34,7 +34,6 @@
using namespace UkuiSearch;
size_t FileUtils::maxIndexCount = 0;
unsigned short FileUtils::indexStatus = 0;
FileUtils::SearchMethod FileUtils::searchMethod = FileUtils::SearchMethod::DIRECTSEARCH;
QMap<QString, QStringList> FileUtils::map_chinese2pinyin = QMap<QString, QStringList>();
static QMutex iconMutex;

View File

@ -113,9 +113,6 @@ public:
static size_t maxIndexCount;
static unsigned short indexStatus;
enum class SearchMethod { DIRECTSEARCH = 0, INDEXSEARCH = 1};
static SearchMethod searchMethod;
private:
static void findNodes(const QDomElement &elem, QQueue<QString> &names, QList<QDomElement> &nodes);
static inline void findNodesByAttr(const QDomElement&, QQueue<QString>&, QList<QDomElement>&, const QString &, const QStringList&);

View File

@ -23,32 +23,33 @@
#include "global-settings.h"
using namespace UkuiSearch;
static GlobalSettings *global_instance_of_global_settings = nullptr;
static GlobalSettings *globalInstance = nullptr;
GlobalSettings *GlobalSettings::getInstance() {
if(!global_instance_of_global_settings) {
global_instance_of_global_settings = new GlobalSettings;
if(!globalInstance) {
globalInstance = new GlobalSettings;
}
return global_instance_of_global_settings;
return globalInstance;
}
GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent) {
m_settings = new QSettings(MAIN_SETTINGS, QSettings::IniFormat, this);
// m_settings->setAtomicSyncRequired(false);
m_block_dirs_settings = new QSettings(BLOCK_DIRS, QSettings::IniFormat, this);
m_block_dirs_settings->setIniCodec(QTextCodec::codecForName("UTF-8"));
m_block_dirs_settings->setValue("These_are_block_dirs_conf_for_ukui_search","0");
m_block_dirs_settings->sync();
GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent)
{
//搜索黑名单过滤
m_blockDirsSettings = new QSettings(BLOCK_DIRS, QSettings::IniFormat, this);
m_blockDirsSettings->setIniCodec(QTextCodec::codecForName("UTF-8"));
m_blockDirsSettings->setValue("These_are_block_dirs_conf_for_ukui_search","0");
m_blockDirsSettings->sync();
m_confWatcher = new QFileSystemWatcher(this);
m_confWatcher->addPath(BLOCK_DIRS);
connect(m_confWatcher, &QFileSystemWatcher::fileChanged, this, [ & ]() {
m_block_dirs_settings->sync();
m_blockDirsSettings->sync();
m_confWatcher->addPath(BLOCK_DIRS);
});
m_search_record_settings = new QSettings(SEARCH_HISTORY, QSettings::IniFormat, this);
m_search_record_settings->setIniCodec(QTextCodec::codecForName("UTF-8"));
for(QString i : m_search_record_settings->allKeys()) {
//搜索历史记录
m_searchRecordSettings = new QSettings(SEARCH_HISTORY, QSettings::IniFormat, this);
m_searchRecordSettings->setIniCodec(QTextCodec::codecForName("UTF-8"));
for(QString i : m_searchRecordSettings->allKeys()) {
m_history.append(QUrl::fromPercentEncoding(i.toLocal8Bit()));
}
if(!QDBusConnection::sessionBus().connect("org.kylinssoclient.dbus",
@ -56,86 +57,90 @@ GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent) {
"org.freedesktop.kylinssoclient.interface",
"keyChanged",
this, SLOT(updateSearchHistory(QString))))
qWarning() << "Kylinssoclient Dbus connect fail!";
this->forceSync();
//the default number of transparency in mainwindow is 0.7
//if someone changes the num in mainwindow, here should be modified too
m_cache.insert(TRANSPARENCY_KEY, 0.7);
//全局页面透明度
//the default number of transparency for mainwindow is 0.7
setValue(TRANSPARENCY_KEY, 0.7);
if(QGSettings::isSchemaInstalled(CONTROL_CENTER_PERSONALISE_GSETTINGS_ID)) {
m_trans_gsettings = new QGSettings(CONTROL_CENTER_PERSONALISE_GSETTINGS_ID, QByteArray(), this);
connect(m_trans_gsettings, &QGSettings::changed, this, [ = ](const QString & key) {
m_transGsettings = new QGSettings(CONTROL_CENTER_PERSONALISE_GSETTINGS_ID, QByteArray(), this);
connect(m_transGsettings, &QGSettings::changed, this, [ = ](const QString & key) {
if(key == TRANSPARENCY_KEY) {
m_cache.remove(TRANSPARENCY_KEY);
m_cache.insert(TRANSPARENCY_KEY, m_trans_gsettings->get(TRANSPARENCY_KEY).toDouble());
setValue(TRANSPARENCY_KEY, m_transGsettings->get(TRANSPARENCY_KEY).toDouble());
qApp->paletteChanged(qApp->palette());
Q_EMIT this->transparencyChanged(m_trans_gsettings->get(TRANSPARENCY_KEY).toDouble());
Q_EMIT this->transparencyChanged(m_transGsettings->get(TRANSPARENCY_KEY).toDouble());
}
});
m_cache.remove(TRANSPARENCY_KEY);
m_cache.insert(TRANSPARENCY_KEY, m_trans_gsettings->get(TRANSPARENCY_KEY).toDouble());
if(m_transGsettings->keys().contains(TRANSPARENCY_KEY)) {
setValue(TRANSPARENCY_KEY, m_transGsettings->get(TRANSPARENCY_KEY).toDouble());
}
}
m_cache.insert(STYLE_NAME_KEY, "ukui-light");
m_cache.insert(FONT_SIZE_KEY, 11);
//主题,字体大小
setValue(STYLE_NAME_KEY, "ukui-light");
setValue(FONT_SIZE_KEY, 11);
if(QGSettings::isSchemaInstalled(THEME_GSETTINGS_ID)) {
m_theme_gsettings = new QGSettings(THEME_GSETTINGS_ID, QByteArray(), this);
connect(m_theme_gsettings, &QGSettings::changed, this, [ = ](const QString & key) {
m_themeGsettings = new QGSettings(THEME_GSETTINGS_ID, QByteArray(), this);
connect(m_themeGsettings, &QGSettings::changed, this, [ = ](const QString & key) {
if(key == STYLE_NAME_KEY) {
//当前主题改变时也发出paletteChanged信号通知主界面刷新
m_cache.remove(STYLE_NAME_KEY);
m_cache.insert(STYLE_NAME_KEY, m_theme_gsettings->get(STYLE_NAME_KEY).toString());
setValue(STYLE_NAME_KEY, m_themeGsettings->get(STYLE_NAME_KEY).toString());
qApp->paletteChanged(qApp->palette());
Q_EMIT this->styleChanged(m_theme_gsettings->get(STYLE_NAME_KEY).toString());
Q_EMIT this->styleChanged(m_themeGsettings->get(STYLE_NAME_KEY).toString());
} else if(key == FONT_SIZE_KEY) {
m_cache.remove(FONT_SIZE_KEY);
m_cache.insert(FONT_SIZE_KEY, m_theme_gsettings->get(FONT_SIZE_KEY).toDouble());
setValue(FONT_SIZE_KEY, m_themeGsettings->get(FONT_SIZE_KEY).toDouble());
qApp->paletteChanged(qApp->palette());
} else if (key == ICON_THEME_KEY) {
qApp->paletteChanged(qApp->palette());
}
});
m_cache.remove(STYLE_NAME_KEY);
m_cache.insert(STYLE_NAME_KEY, m_theme_gsettings->get(STYLE_NAME_KEY).toString());
m_cache.remove(FONT_SIZE_KEY);
m_cache.insert(FONT_SIZE_KEY, m_theme_gsettings->get(FONT_SIZE_KEY).toDouble());
if(m_themeGsettings->keys().contains(STYLE_NAME_KEY)) {
setValue(STYLE_NAME_KEY, m_themeGsettings->get(STYLE_NAME_KEY).toString());
}
if(m_themeGsettings->keys().contains(FONT_SIZE_KEY)) {
setValue(FONT_SIZE_KEY, m_themeGsettings->get(FONT_SIZE_KEY).toDouble());
}
}
//文件索引与搜索插件相关设置
setValue(FILE_INDEX_ENABLE_KEY, false);
setValue(WEB_ENGINE_KEY, "baidu");
setValue(CONTENT_FUZZY_SEARCH_KEY, false);
if(QGSettings::isSchemaInstalled(UKUI_SEARCH_SCHEMAS)) {
m_searchGsettings = new QGSettings(UKUI_SEARCH_SCHEMAS, QByteArray(), this);
connect(m_searchGsettings, &QGSettings::changed, this, [ = ](const QString & key) {
if(key == FILE_INDEX_ENABLE_KEY) {
bool fileSearchEnable = m_searchGsettings->get(FILE_INDEX_ENABLE_KEY).toBool();
setValue(FILE_INDEX_ENABLE_KEY, fileSearchEnable);
Q_EMIT fileSearchEnableChanged(fileSearchEnable);
} else if(key == WEB_ENGINE_KEY) {
QString webSearchEngine = m_searchGsettings->get(WEB_ENGINE_KEY).toString();
setValue(WEB_ENGINE_KEY, webSearchEngine);
Q_EMIT webSearchEngineChanged(webSearchEngine);
} else if (key == CONTENT_FUZZY_SEARCH_KEY) {
bool contentFuzzySearch = m_searchGsettings->get(CONTENT_FUZZY_SEARCH_KEY).toBool();
setValue(CONTENT_FUZZY_SEARCH_KEY, contentFuzzySearch);
Q_EMIT contentFuzzySearchEnableChanged(contentFuzzySearch);
}
});
if(m_searchGsettings->keys().contains(FILE_INDEX_ENABLE_KEY)) {
setValue(FILE_INDEX_ENABLE_KEY, m_searchGsettings->get(FILE_INDEX_ENABLE_KEY).toBool());
}
if(m_searchGsettings->keys().contains(WEB_ENGINE_KEY)) {
setValue(WEB_ENGINE_KEY, m_searchGsettings->get(WEB_ENGINE_KEY).toBool());
}
if(m_searchGsettings->keys().contains(CONTENT_FUZZY_SEARCH_KEY)) {
setValue(CONTENT_FUZZY_SEARCH_KEY, m_searchGsettings->get(CONTENT_FUZZY_SEARCH_KEY).toBool());
}
}
}
const QVariant GlobalSettings::getValue(const QString &key) {
return m_cache.value(key);
}
bool GlobalSettings::isExist(const QString &key) {
return !m_cache.value(key).isNull();
}
void GlobalSettings::reset(const QString &key) {
m_cache.remove(key);
QtConcurrent::run([ = ]() {
// if (m_mutex.tryLock(1000)) {
m_settings->remove(key);
m_settings->sync();
// m_mutex.unlock();
// }
});
Q_EMIT this->valueChanged(key);
}
void GlobalSettings::resetAll() {
QStringList tmp = m_cache.keys();
m_cache.clear();
for(auto key : tmp) {
Q_EMIT this->valueChanged(key);
}
QtConcurrent::run([ = ]() {
if(m_mutex.tryLock(1000)) {
m_settings->clear();
m_settings->sync();
m_mutex.unlock();
}
});
m_mutex.lock();
QVariant value = m_cache.value(key);
m_mutex.unlock();
return value;
}
bool GlobalSettings::setBlockDirs(const QString &path, int &returnCode, bool remove) {
@ -145,14 +150,9 @@ bool GlobalSettings::setBlockDirs(const QString &path, int &returnCode, bool rem
return false;
}
m_block_dirs_settings->remove(path);
m_blockDirsSettings->remove(path);
return true;
}
// if(!path.startsWith("/home")) {
// returnCode = QString(tr("I can only search your user directory, it doesn't make any sense if you block an directory which is not in user directory!"));
// returnCode = PATH_NOT_IN_HOME;
// return false;
// }
//why QSetting's key can't start with "/"??
QString pathKey = path.right(path.length() - 1);
@ -161,7 +161,7 @@ bool GlobalSettings::setBlockDirs(const QString &path, int &returnCode, bool rem
pathKey = pathKey.mid(0, pathKey.length() - 1);
}
QStringList blockDirs = m_block_dirs_settings->allKeys();
QStringList blockDirs = m_blockDirsSettings->allKeys();
for(QString i : blockDirs) {
if(FileUtils::isOrUnder(pathKey, i)) {
// returnCode = QString(tr("My parent folder has been blocked!"));
@ -170,43 +170,23 @@ bool GlobalSettings::setBlockDirs(const QString &path, int &returnCode, bool rem
}
if(FileUtils::isOrUnder(i, pathKey))
m_block_dirs_settings->remove(i);
m_blockDirsSettings->remove(i);
}
m_block_dirs_settings->setValue(pathKey, "0");
m_blockDirsSettings->setValue(pathKey, "0");
return true;
}
QStringList GlobalSettings::getBlockDirs() {
return m_block_dirs_settings->allKeys();
return m_blockDirsSettings->allKeys();
}
//void GlobalSettings::appendCloudData(const QString &key, const QString &value)
//{
// QSettings * m_qSettings = new QSettings(CLOUD_FILE, QSettings::IniFormat);
// m_qSettings->beginGroup(key);
// QStringList values = m_qSettings->value(key).toStringList();
// m_qSettings->endGroup();
// if (values.contains(value)) {
// values.removeOne(value);
// }
// values.insert(0,value);
// m_qSettings->beginGroup(key);
// m_qSettings->setValue(key, values);
// m_qSettings->endGroup();
// if (m_qSettings) {
// delete m_qSettings;
// m_qSettings = NULL;
// }
//}
void GlobalSettings::setSearchRecord(const QString &word, const QDateTime &time) {
QStringList keys = m_search_record_settings->allKeys();
QStringList keys = m_searchRecordSettings->allKeys();
if(keys.contains(QString(QUrl::toPercentEncoding(word))))
m_history.removeOne(word);
m_search_record_settings->setValue(QString(QUrl::toPercentEncoding(word)), time.toString("yyyy-MM-dd hh:mm:ss"));
m_searchRecordSettings->setValue(QString(QUrl::toPercentEncoding(word)), time.toString("yyyy-MM-dd hh:mm:ss"));
if(keys.size() >= 20)
m_search_record_settings->remove(QString(QUrl::toPercentEncoding(m_history.takeFirst())));
m_searchRecordSettings->remove(QString(QUrl::toPercentEncoding(m_history.takeFirst())));
m_history.append(word);
}
@ -214,83 +194,18 @@ QStringList GlobalSettings::getSearchRecord() {
return m_history;
}
//bool GlobalSettings::removeOneCloudData(const QString &key, const QString &value)
//{
// if (!QFileInfo(CLOUD_FILE).isFile()) return false;
// QSettings * m_qSettings = new QSettings(CLOUD_FILE, QSettings::IniFormat);
// m_qSettings->beginGroup(key);
// QStringList values = m_qSettings->value(key).toStringList();
// m_qSettings->endGroup();
// if (values.contains(value)) {
// values.removeOne(value);
// } else return false;
// m_qSettings->beginGroup(key);
// m_qSettings->setValue(key, values);
// m_qSettings->endGroup();
// if (m_qSettings) {
// delete m_qSettings;
// m_qSettings = NULL;
// }
// return true;
//}
//bool GlobalSettings::removeAllCloudData(const QString &key)
//{
// if (!QFileInfo(CLOUD_FILE).isFile()) return false;
// QSettings * m_qSettings = new QSettings(CLOUD_FILE, QSettings::IniFormat);
// m_qSettings->beginGroup(key);
// m_qSettings->beginGroup(key);
// m_qSettings->setValue(key, QStringList());
// m_qSettings->endGroup();
// if (m_qSettings) {
// delete m_qSettings;
// m_qSettings = NULL;
// }
// return true;
//}
//QStringList GlobalSettings::getCloudData(const QString &key)
//{
// if (!QFileInfo(CLOUD_FILE).isFile()) return QStringList();
// QSettings * m_qSettings = new QSettings(CLOUD_FILE, QSettings::IniFormat);
// m_qSettings->beginGroup(key);
// QStringList values = m_qSettings->value(key).toStringList();
// m_qSettings->endGroup();
// if(m_qSettings)
// delete m_qSettings;
// return values;
//}
//this method is designed for main process settings only!!
void GlobalSettings::setValue(const QString &key, const QVariant &value) {
m_mutex.lock();
m_cache.insert(key, value);
QtConcurrent::run([ = ]() {
if (m_mutex.tryLock(1000)) {
m_settings->setValue(key, value);
m_settings->sync();
m_mutex.unlock();
}
});
}
void GlobalSettings::forceSync(const QString &key) {
m_settings->sync();
if(key.isNull()) {
m_cache.clear();
for(auto key : m_settings->allKeys()) {
m_cache.insert(key, m_settings->value(key));
}
} else {
m_cache.remove(key);
m_cache.insert(key, m_settings->value(key));
}
m_mutex.unlock();
}
void GlobalSettings::updateSearchHistory(QString key) {
if(key == "search") {
m_search_record_settings->sync();
m_searchRecordSettings->sync();
m_history.clear();
for(QString i : m_search_record_settings->allKeys()) {
for(QString i : m_searchRecordSettings->allKeys()) {
m_history.append(QUrl::fromPercentEncoding(i.toLocal8Bit()));
}
}

View File

@ -45,35 +45,32 @@
#define FONT_SIZE_KEY "systemFontSize"
#define ICON_THEME_KEY "iconThemeName"
#define ENABLE_CREATE_INDEX_ASK_DIALOG "enable_create_index_ask_dialog"
#define UKUI_SEARCH_SCHEMAS "org.ukui.search.settings"
#define FILE_INDEX_ENABLE_KEY "fileIndexEnable"
#define WEB_ENGINE_KEY "webEngine"
#define CONTENT_FUZZY_SEARCH_KEY "contentFuzzySearch"
#define WEB_ENGINE "web_engine"
#define PATH_EMPTY 1;
#define PATH_NOT_IN_HOME 2;
#define PATH_PARENT_BLOCKED 3;
#define MAIN_SETTINGS QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search.conf"
#define BLOCK_DIRS QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search-block-dirs.conf"
#define SEARCH_HISTORY QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search-history.conf"
//#define CLOUD_HISTORY "history"
//#define CLOUD_APPLICATIONS "applications"
namespace UkuiSearch {
/**
* @brief The GlobalSettings class
*
* 使
*/
class LIBSEARCH_EXPORT GlobalSettings : public QObject {
Q_OBJECT
public:
static GlobalSettings *getInstance();
const QVariant getValue(const QString&);
bool isExist(const QString&);
Q_SIGNALS:
void valueChanged(const QString&);
void transparencyChanged(const double&);
void styleChanged(const QString&);
public Q_SLOTS:
void setValue(const QString&, const QVariant&);
void reset(const QString&);
void resetAll();
/**
* @brief setBlockDirs
* set path for blacklist,return true if success,otherwise return false.
@ -84,25 +81,28 @@ public Q_SLOTS:
*/
bool setBlockDirs(const QString& path, int &returnCode, bool remove = false);
QStringList getBlockDirs();
// void appendCloudData(const QString& key, const QString& value);
void setSearchRecord(const QString &word, const QDateTime &time);
QStringList getSearchRecord();
// bool removeOneCloudData(const QString& key, const QString& value);
// bool removeAllCloudData(const QString& key);
// QStringList getCloudData(const QString& key);
void forceSync(const QString& = nullptr);
void updateSearchHistory(QString key);
Q_SIGNALS:
void valueChanged(const QString&);
void transparencyChanged(const double&);
void styleChanged(const QString&);
void fileSearchEnableChanged(bool);
void webSearchEngineChanged(const QString&);
void contentFuzzySearchEnableChanged(bool);
private:
explicit GlobalSettings(QObject *parent = nullptr);
~GlobalSettings() = default;
void setValue(const QString&, const QVariant&);
QSettings* m_settings;
QGSettings* m_trans_gsettings;
QGSettings* m_theme_gsettings;
QSettings *m_block_dirs_settings;
QSettings *m_search_record_settings;
QGSettings *m_transGsettings;
QGSettings *m_themeGsettings;
QGSettings *m_searchGsettings;
QSettings *m_blockDirsSettings;
QSettings *m_searchRecordSettings;
QMap<QString, QVariant> m_cache;
QStringList m_history;
QFileSystemWatcher *m_confWatcher;

View File

@ -18,17 +18,17 @@
*
*/
#include "file-search-plugin.h"
#include "search-manager.h"
#include <QWidget>
#include <QLabel>
#include <QHBoxLayout>
#include <QMessageBox>
#include "search-manager.h"
#define OCR_ICONLABLE_WITH 352
#define OCR_ICONLABLE_HEIGHT 247
using namespace UkuiSearch;
FileSearchPlugin::FileSearchPlugin(QObject *parent) : QObject(parent)
FileSearchPlugin::FileSearchPlugin(QObject *parent) : QObject(parent),m_settings(GlobalSettings::getInstance())
{
SearchPluginIface::Actioninfo open { 0, tr("Open")};
SearchPluginIface::Actioninfo Openpath { 1, tr("Open path")};
@ -60,14 +60,14 @@ void UkuiSearch::FileSearchPlugin::KeywordSearch(QString keyword, DataQueue<Resu
++SearchManager::uniqueSymbolFile;
SearchManager::m_mutexFile.unlock();
if(FileUtils::SearchMethod::DIRECTSEARCH == FileUtils::searchMethod) {
DirectSearch *directSearch;
directSearch = new DirectSearch(keyword, searchResult, FILE_SEARCH_VALUE, SearchManager::uniqueSymbolFile);
m_pool.start(directSearch);
} else if(FileUtils::SearchMethod::INDEXSEARCH == FileUtils::searchMethod) {
if(m_settings->getValue(FILE_INDEX_ENABLE_KEY).toBool()) {
FileSearch *filesearch;
filesearch = new FileSearch(searchResult, SearchManager::uniqueSymbolFile, keyword, FILE_SEARCH_VALUE, 1, 0, 5);
m_pool.start(filesearch);
} else{
DirectSearch *directSearch;
directSearch = new DirectSearch(keyword, searchResult, FILE_SEARCH_VALUE, SearchManager::uniqueSymbolFile);
m_pool.start(directSearch);
}
}
@ -238,7 +238,7 @@ void FileSearchPlugin::initDetailPage()
// return previewPage;
//}
DirSearchPlugin::DirSearchPlugin(QObject *parent) : QObject(parent)
DirSearchPlugin::DirSearchPlugin(QObject *parent) : QObject(parent),m_settings(GlobalSettings::getInstance())
{
SearchPluginIface::Actioninfo open { 0, tr("Open")};
SearchPluginIface::Actioninfo Openpath { 1, tr("Open path")};
@ -270,14 +270,14 @@ void UkuiSearch::DirSearchPlugin::KeywordSearch(QString keyword, DataQueue<Resul
++SearchManager::uniqueSymbolDir;
SearchManager::m_mutexDir.unlock();
if(FileUtils::SearchMethod::DIRECTSEARCH == FileUtils::searchMethod) {
DirectSearch *directSearch;
directSearch = new DirectSearch(keyword, searchResult, DIR_SEARCH_VALUE, SearchManager::uniqueSymbolDir);
m_pool.start(directSearch);
} else if(FileUtils::SearchMethod::INDEXSEARCH == FileUtils::searchMethod) {
if(m_settings->getValue(FILE_INDEX_ENABLE_KEY).toBool()) {
FileSearch *filesearch;
filesearch = new FileSearch(searchResult, SearchManager::uniqueSymbolDir, keyword, DIR_SEARCH_VALUE, 1, 0, 5);
m_pool.start(filesearch);
} else {
DirectSearch *directSearch;
directSearch = new DirectSearch(keyword, searchResult, DIR_SEARCH_VALUE, SearchManager::uniqueSymbolDir);
m_pool.start(directSearch);
}
}
@ -421,7 +421,7 @@ void DirSearchPlugin::initDetailPage()
// return nullptr;
//}
FileContengSearchPlugin::FileContengSearchPlugin(QObject *parent) : QObject(parent)
FileContengSearchPlugin::FileContengSearchPlugin(QObject *parent) : QObject(parent),m_settings(GlobalSettings::getInstance())
{
SearchPluginIface::Actioninfo open { 0, tr("Open")};
SearchPluginIface::Actioninfo Openpath { 1, tr("Open path")};
@ -454,11 +454,9 @@ void UkuiSearch::FileContengSearchPlugin::KeywordSearch(QString keyword, DataQue
SearchManager::m_mutexContent.unlock();
m_keyWord = keyword;
if(FileUtils::SearchMethod::DIRECTSEARCH == FileUtils::searchMethod) {
return;
} else if(FileUtils::SearchMethod::INDEXSEARCH == FileUtils::searchMethod) {
if(m_settings->getValue(FILE_INDEX_ENABLE_KEY).toBool()) {
FileContentSearch *fileContentSearch;
fileContentSearch = new FileContentSearch(searchResult, SearchManager::uniqueSymbolContent, keyword, 0, 5);
fileContentSearch = new FileContentSearch(searchResult, SearchManager::uniqueSymbolContent, keyword, m_settings->getValue(CONTENT_FUZZY_SEARCH_KEY).toBool(), 0, 5);
m_pool.start(fileContentSearch);
}
}

View File

@ -29,11 +29,12 @@
#include <QLabel>
#include <QAction>
#include "libsearch_global.h"
#include "search-plugin-iface.h"
#include "common.h"
#include "action-label.h"
#include "separation-line.h"
#include "global-settings.h"
namespace UkuiSearch {
//internal plugin
class LIBSEARCH_EXPORT FileSearchPlugin : public QObject, public SearchPluginIface
@ -89,6 +90,7 @@ private:
bool m_enable = true;
QList<SearchPluginIface::Actioninfo> m_actionInfo;
QThreadPool m_pool;
GlobalSettings *m_settings = nullptr;
};
class LIBSEARCH_EXPORT DirSearchPlugin : public QObject, public SearchPluginIface
@ -143,6 +145,7 @@ private:
bool m_enable = true;
QList<SearchPluginIface::Actioninfo> m_actionInfo;
QThreadPool m_pool;
GlobalSettings *m_settings = nullptr;
};
class LIBSEARCH_EXPORT FileContengSearchPlugin : public QObject, public SearchPluginIface
@ -200,6 +203,7 @@ private:
bool m_enable = true;
QList<SearchPluginIface::Actioninfo> m_actionInfo;
QThreadPool m_pool;
GlobalSettings *m_settings = nullptr;
};
}

View File

@ -225,13 +225,15 @@ int FileSearch::getResult(Xapian::MSet &result) {
return 0;
}
FileContentSearch::FileContentSearch(DataQueue<SearchPluginIface::ResultInfo> *searchResult, size_t uniqueSymbol, QString keyword, int begin, int num) {
FileContentSearch::FileContentSearch(DataQueue<SearchPluginIface::ResultInfo> *searchResult, size_t uniqueSymbol, QString keyword, bool fuzzy, int begin, int num)
:m_search_result(searchResult),
m_uniqueSymbol(uniqueSymbol),
m_keyword(keyword),
m_fuzzy(fuzzy),
m_begin(begin),
m_num(num)
{
this->setAutoDelete(true);
m_search_result = searchResult;
m_uniqueSymbol = uniqueSymbol;
m_keyword = keyword;
m_begin = begin;
m_num = num;
m_matchDecider = new FileContentMatchDecider();
}
@ -275,18 +277,23 @@ int FileContentSearch::keywordSearchContent() {
std::vector<KeyWord> sKeyWord = ChineseSegmentation::getInstance()->callSegment(m_keyword.toStdString());
//Creat a query
std::string words;
for(int i = 0; i < sKeyWord.size(); i++) {
for(size_t i = 0; i < sKeyWord.size(); i++) {
words.append(sKeyWord.at(i).word).append(" ");
}
// Xapian::Query query = qp.parse_query(words);
std::vector<Xapian::Query> v;
for(int i=0; i<sKeyWord.size(); i++) {
for(size_t i = 0; i < sKeyWord.size(); i++) {
v.push_back(Xapian::Query(sKeyWord.at(i).word));
qDebug() << QString::fromStdString(sKeyWord.at(i).word);
// qDebug() << QString::fromStdString(sKeyWord.at(i).word);
}
Xapian::Query query;
if(m_fuzzy) {
query = Xapian::Query(Xapian::Query::OP_OR, v.begin(), v.end());
} else {
query = Xapian::Query(Xapian::Query::OP_AND, v.begin(), v.end());
}
Xapian::Query query = Xapian::Query(Xapian::Query::OP_AND, v.begin(), v.end());
qDebug() << "keywordSearchContent:" << QString::fromStdString(query.get_description());
@ -316,88 +323,45 @@ int FileContentSearch::getResult(Xapian::MSet &result, std::string &keyWord) {
for(auto it = result.begin(); it != result.end(); ++it) {
Xapian::Document doc = it.get_document();
std::string data = doc.get_data();
double docScoreWeight = it.get_weight();
Xapian::percent docScorePercent = it.get_percent();
// double docScoreWeight = it.get_weight();
// Xapian::percent docScorePercent = it.get_percent();
QString path = QString::fromStdString(doc.get_value(1));
QString suffix = QString::fromStdString(doc.get_value(2));
// QString suffix = QString::fromStdString(doc.get_value(2));
SearchPluginIface::ResultInfo ri;
if(!SearchManager::creatResultInfo(ri, path)) {
continue;
}
// Construct snippets containing keyword.
// QStringList snippets;
// snippets.append(QString::fromStdString( result.snippet(doc.get_data(),400)));
// qWarning()<<QString::fromStdString(s);
auto term = doc.termlist_begin();
std::string wordTobeFound = QString::fromStdString(keyWord).section(" ", 0, 0).toStdString();
// int size = wordTobeFound.length();
term.skip_to(wordTobeFound);
// int count = 0;
// for(auto pos = term.positionlist_begin(); pos != term.positionlist_end() && count < 6; ++pos) {
// std::string s = data.substr((*pos < 60) ? 0 : (*pos - 60), size + 120);
// QString snippet = QString::fromStdString(s);
// if(snippet.size() > 6 + QString::fromStdString(keyWord).size()) {
// snippet.replace(0, 3, "...").replace(snippet.size() - 3, 3, "...");
// } else {
// snippet.append("...").prepend("...");
// }
// ri.description.prepend(SearchPluginIface::DescriptionInfo{"",snippet});
//// snippets.append(snippet);
// QString().swap(snippet);
// std::string().swap(s);
// ++count;
// }
//fix me: make a snippet without cut cjk char.
auto pos = term.positionlist_begin();
QString snippet = FileUtils::chineseSubString(data,*pos,120);
auto termIterator = doc.termlist_begin();
QStringList words = QString::fromStdString(keyWord).split(" ", QString::SkipEmptyParts);
// if(data.length() - *pos < 120) {
// std::string s = data.substr((data.length() < 120) ? 0 : (data.length() - 120), 120);
// snippet = QString::fromStdString(s);
// } else {
// std::string s = data.substr(*pos, 120);
// snippet = QString::fromStdString(s);
// }
for(const QString& wordTobeFound : words) {
std::string term = wordTobeFound.toStdString();
termIterator.skip_to(term);
if(term == *termIterator) {
break;
} else {
termIterator = doc.termlist_begin();
}
}
auto pos = termIterator.positionlist_begin();
QString snippet = FileUtils::chineseSubString(data,*pos,120);
ri.description.prepend(SearchPluginIface::DescriptionInfo{"",snippet});
QString().swap(snippet);
// std::string().swap(s);
std::string().swap(data);
// for(QString i : QString::fromStdString(keyWord).split(" ",QString::SkipEmptyParts))
// {
// std::string word = i.toStdString();
// term.skip_to(word);
// int size = word.size();
// auto pos = term.positionlist_begin();
// std::string s = data.substr((*pos < 60)? 0: (*pos - 60) , size + 120);
// QString snippet = QString::fromStdString(s);
// snippet.replace(0,3,"...").replace(snippet.size()-3,3,"...");
// snippets.append(snippet);
// std::string().swap(word);
// std::string().swap(s);
// QString().swap(snippet);
// }
SearchManager::m_mutexContent.lock();
if(m_uniqueSymbol == SearchManager::uniqueSymbolContent) {
m_search_result->enqueue(ri);
SearchManager::m_mutexContent.unlock();
// snippets.clear();
// QStringList().swap(snippets);
} else {
SearchManager::m_mutexContent.unlock();
return -1;
}
// searchResult.insert(path,snippets);
//qDebug() << "path=" << path << ",weight=" << docScoreWeight << ",percent=" << docScorePercent;
}
// // if(!pathTobeDelete->isEmpty())
// // deleteAllIndex(pathTobeDelete)
return 0;
}

View File

@ -120,7 +120,7 @@ private:
class FileContentSearch : public QRunnable {
public:
explicit FileContentSearch(DataQueue<SearchPluginIface::ResultInfo> *searchResult, size_t uniqueSymbol, QString keyword, int begin = 0, int num = 20);
explicit FileContentSearch(DataQueue<SearchPluginIface::ResultInfo> *searchResult, size_t uniqueSymbol, QString keyword, bool fuzzy,int begin = 0, int num = 20);
~FileContentSearch();
protected:
void run();
@ -132,6 +132,7 @@ private:
FileContentMatchDecider *m_matchDecider;
size_t m_uniqueSymbol;
QString m_keyword;
bool m_fuzzy;
int m_begin = 0;
int m_num = 20;
};