From 3b8c35c2c2329e3fe9eddf84d8828a8b7a379cb9 Mon Sep 17 00:00:00 2001 From: JunjieBai Date: Wed, 31 May 2023 17:59:20 +0800 Subject: [PATCH] optimize global-settings; Fix the problem that the search window cannot show because it was hidden by something while create-index-dialog is showing. --- frontend/control/search-line-edit.cpp | 3 +- .../control/stack-pages/search-page-section.h | 1 - .../stack-pages/search-result-page.cpp | 3 +- frontend/mainwindow.cpp | 20 +- frontend/mainwindow.h | 2 +- frontend/view/result-view-delegate.cpp | 1 + frontend/view/result-view-delegate.h | 1 - frontend/view/view.pri | 6 +- frontend/view/web-search-view.cpp | 196 ------------------ frontend/view/web-search-view.h | 91 -------- libsearch/global-settings-private.h | 32 +++ libsearch/global-settings.cpp | 177 +++------------- libsearch/global-settings.h | 86 ++------ libsearch/index/file-indexer-config.cpp | 16 ++ libsearch/index/file-indexer-config.h | 5 + libsearch/index/file-search-plugin.cpp | 15 +- libsearch/index/file-search-plugin.h | 3 - libsearch/libsearch.pro | 1 + libsearch/plugininterface/separation-line.cpp | 3 +- libsearch/search-app-widget-plugin/search.cpp | 22 +- libsearch/websearch/web-search-plugin.cpp | 39 ++-- libsearch/websearch/web-search-plugin.h | 4 + 22 files changed, 164 insertions(+), 563 deletions(-) delete mode 100644 frontend/view/web-search-view.cpp delete mode 100644 frontend/view/web-search-view.h create mode 100644 libsearch/global-settings-private.h diff --git a/frontend/control/search-line-edit.cpp b/frontend/control/search-line-edit.cpp index 6eb828a..4d0f666 100644 --- a/frontend/control/search-line-edit.cpp +++ b/frontend/control/search-line-edit.cpp @@ -20,6 +20,7 @@ */ #include "search-line-edit.h" #include +#include #include QT_BEGIN_NAMESPACE @@ -91,7 +92,7 @@ void SearchLineEdit::paintEvent(QPaintEvent *e) QPainter p(this); p.setRenderHint(QPainter::Antialiasing); // 反锯齿; p.setBrush(palette().base()); - p.setOpacity(GlobalSettings::getInstance()->getValue(TRANSPARENCY_KEY).toDouble()); + p.setOpacity(GlobalSettings::getInstance().getValue(TRANSPARENCY_KEY).toDouble()); p.setPen(Qt::NoPen); p.drawRoundedRect(this->rect(), 12, 12); return QLineEdit::paintEvent(e); diff --git a/frontend/control/stack-pages/search-page-section.h b/frontend/control/stack-pages/search-page-section.h index 807f689..7773040 100644 --- a/frontend/control/stack-pages/search-page-section.h +++ b/frontend/control/stack-pages/search-page-section.h @@ -28,7 +28,6 @@ #include "result-view.h" #include "search-plugin-iface.h" #include "best-list-view.h" -#include "web-search-view.h" namespace UkuiSearch { class ResultScrollBar : public QScrollBar diff --git a/frontend/control/stack-pages/search-result-page.cpp b/frontend/control/stack-pages/search-result-page.cpp index b2819da..9f03df2 100644 --- a/frontend/control/stack-pages/search-result-page.cpp +++ b/frontend/control/stack-pages/search-result-page.cpp @@ -19,6 +19,7 @@ * */ #include "search-result-page.h" +#include "global-settings.h" #include QT_BEGIN_NAMESPACE extern void qt_blurImage(QImage &blurImage, qreal radius, bool quality, int transposed); @@ -128,7 +129,7 @@ void SearchResultPage::paintEvent(QPaintEvent *event) QPainter p(this); p.setRenderHint(QPainter::Antialiasing); p.setBrush(palette().base()); - p.setOpacity(GlobalSettings::getInstance()->getValue(TRANSPARENCY_KEY).toDouble()); + p.setOpacity(GlobalSettings::getInstance().getValue(TRANSPARENCY_KEY).toDouble()); p.setPen(Qt::NoPen); p.drawRoundedRect(this->rect().adjusted(10,10,-10,-10), 12, 12); diff --git a/frontend/mainwindow.cpp b/frontend/mainwindow.cpp index cbea3d2..b749b2e 100644 --- a/frontend/mainwindow.cpp +++ b/frontend/mainwindow.cpp @@ -50,7 +50,7 @@ #define MAIN_SETTINGS QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search.conf" #define ENABLE_CREATE_INDEX_ASK_DIALOG "enable_create_index_ask_dialog" - +const static QString FILE_INDEX_ENABLE_KEY = "fileIndexEnable"; using namespace UkuiSearch; extern void qt_blurImage(QImage &blurImage, qreal radius, bool quality, int transposed); @@ -164,8 +164,8 @@ void MainWindow::bootOptionsFilter(QString opt) { clearSearchResult(); centerToScreen(this); this->m_searchBarWidget->setFocus(); - this->activateWindow(); } + this->activateWindow(); } } @@ -236,7 +236,7 @@ void MainWindow::searchKeywordSlot(const QString &keyword) //允许弹窗且当前次搜索(为关闭主界面,算一次搜索过程)未询问且当前为暴力搜索 if(m_settings->value(ENABLE_CREATE_INDEX_ASK_DIALOG).toBool() && !m_currentSearchAsked - && GlobalSettings::getInstance()->getValue(FILE_INDEX_ENABLE_KEY).toBool() == false) { + && !m_isIndexSearch) { m_askTimer->start(); } Q_EMIT m_searchResultPage->startSearch(keyword); @@ -294,10 +294,13 @@ void MainWindow::initSettings() { const QByteArray id(UKUI_SEARCH_SCHEMAS); if(QGSettings::isSchemaInstalled(id)) { m_searchGsettings = new QGSettings(id); + if (m_searchGsettings->keys().contains(FILE_INDEX_ENABLE_KEY)) { + m_isIndexSearch = m_searchGsettings->get(FILE_INDEX_ENABLE_KEY).toBool(); + } 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_isIndexSearch = m_searchGsettings->get(FILE_INDEX_ENABLE_KEY).toBool(); + if(m_researchTimer->isActive() && !m_isIndexSearch) { m_researchTimer->stop(); } } @@ -308,7 +311,7 @@ void MainWindow::initSettings() { //使用GSetting获取当前窗口应该使用的透明度 double MainWindow::getTransparentData() { - return GlobalSettings::getInstance()->getValue(TRANSPARENCY_KEY).toDouble(); + return GlobalSettings::getInstance().getValue(TRANSPARENCY_KEY).toDouble(); } void MainWindow::initTimer() { @@ -325,7 +328,7 @@ void MainWindow::initTimer() { }, Qt::UniqueConnection); return; } - if(this->isVisible()) { + if(this->isVisible() && !m_isIndexSearch) { m_isAskDialogVisible = true; kdk::UkuiStyleHelper::self()->removeHeader(m_askDialog); m_askDialog->show(); @@ -350,8 +353,9 @@ void MainWindow::initTimer() { m_askTimer->stop(); } else { //允许弹窗且当前次搜索(为关闭主界面,算一次搜索过程)未询问且当前为暴力搜索 - if(m_settings->value(ENABLE_CREATE_INDEX_ASK_DIALOG, true).toBool() && !m_currentSearchAsked && GlobalSettings::getInstance()->getValue(FILE_INDEX_ENABLE_KEY).toBool() == false) + if(m_settings->value(ENABLE_CREATE_INDEX_ASK_DIALOG, true).toBool() && !m_currentSearchAsked && !m_isIndexSearch) { m_askTimer->start(); + } } }); } diff --git a/frontend/mainwindow.h b/frontend/mainwindow.h index 378c1d2..01189e6 100644 --- a/frontend/mainwindow.h +++ b/frontend/mainwindow.h @@ -114,7 +114,7 @@ private: QGSettings *m_searchGsettings = nullptr; QSettings *m_settings = nullptr; AppWidgetPlugin *m_appWidgetPlugin = nullptr; - + bool m_isIndexSearch = false; }; } diff --git a/frontend/view/result-view-delegate.cpp b/frontend/view/result-view-delegate.cpp index 4a9f2cb..25db800 100644 --- a/frontend/view/result-view-delegate.cpp +++ b/frontend/view/result-view-delegate.cpp @@ -19,6 +19,7 @@ */ #include "result-view-delegate.h" #include +#include using namespace UkuiSearch; static ResultItemStyle *global_instance_of_item_style = nullptr; diff --git a/frontend/view/result-view-delegate.h b/frontend/view/result-view-delegate.h index fb9eea4..ed915a8 100644 --- a/frontend/view/result-view-delegate.h +++ b/frontend/view/result-view-delegate.h @@ -30,7 +30,6 @@ #include #include #include -#include "global-settings.h" namespace UkuiSearch { class HightLightEffectHelper : public QSyntaxHighlighter diff --git a/frontend/view/view.pri b/frontend/view/view.pri index 04b29eb..78a89f7 100644 --- a/frontend/view/view.pri +++ b/frontend/view/view.pri @@ -3,11 +3,9 @@ INCLUDEPATH += $$PWD HEADERS += \ $$PWD/best-list-view.h \ $$PWD/result-view-delegate.h \ - $$PWD/result-view.h \ - $$PWD/web-search-view.h + $$PWD/result-view.h SOURCES += \ $$PWD/best-list-view.cpp \ $$PWD/result-view-delegate.cpp \ - $$PWD/result-view.cpp \ - $$PWD/web-search-view.cpp + $$PWD/result-view.cpp diff --git a/frontend/view/web-search-view.cpp b/frontend/view/web-search-view.cpp deleted file mode 100644 index fda384a..0000000 --- a/frontend/view/web-search-view.cpp +++ /dev/null @@ -1,196 +0,0 @@ -/* - * - * Copyright (C) 2021, KylinSoft Co., Ltd. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * Authors: jixiaoxu - * - */ -#include -#include -#include "web-search-view.h" -#define MAIN_MARGINS 0,0,0,0 -#define MAIN_SPACING 0 -#define TITLE_HEIGHT 30 -#define VIEW_ICON_SIZE 24 - -using namespace UkuiSearch; -WebSearchView::WebSearchView(QWidget *parent) : QTreeView(parent) -{ - setStyle(ResultItemStyle::getStyle()); - this->setFrameShape(QFrame::NoFrame); - this->viewport()->setAutoFillBackground(false); - this->setRootIsDecorated(false); - this->setIconSize(QSize(VIEW_ICON_SIZE, VIEW_ICON_SIZE)); - this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - this->setSelectionBehavior(QAbstractItemView::SelectRows); - this->setSelectionMode(QAbstractItemView::SingleSelection); - this->setHeaderHidden(true); - m_model = new WebSearchModel(this); - this->setModel(m_model); - m_styleDelegate = new ResultViewDelegate(this); - this->setItemDelegate(m_styleDelegate); -} - -bool WebSearchView::isSelected() -{ - return m_is_selected; -} - -int WebSearchView::showHeight() -{ - return this->rowHeight(this->model()->index(0, 0, QModelIndex())); -} - -QModelIndex WebSearchView::getModlIndex(int row, int column) -{ - return this->m_model->index(row, column); -} - -void WebSearchView::clearSelectedRow() -{ - if (!m_is_selected) { - this->blockSignals(true); - //this->clearSelection(); - this->setCurrentIndex(QModelIndex()); - this->blockSignals(false); - } -} - -void WebSearchView::startSearch(const QString & keyword) -{ - this->m_styleDelegate->setSearchKeyword(keyword); - this->m_model->startSearch(keyword); - m_keyWord = keyword; -} - -void WebSearchView::mouseReleaseEvent(QMouseEvent *event) -{ - QModelIndex index = indexAt(event->pos()); - if (!index.isValid()) { - this->clearSelection(); - } - return QTreeView::mouseReleaseEvent(event); -} - -void WebSearchView::LaunchBrowser() -{ - QString address; - QString engine = GlobalSettings::getInstance()->getValue("web_engine").toString(); - if(!engine.isEmpty()) { - if(engine == "360") { - address = "https://so.com/s?q=" + m_keyWord; //360 - } else if(engine == "sougou") { - address = "https://www.sogou.com/web?query=" + m_keyWord; //搜狗 - } else { - address = "http://baidu.com/s?word=" + m_keyWord; //百度 - } - } else { //默认值 - address = "http://baidu.com/s?word=" + m_keyWord ; //百度 - } - bool res(false); - QDBusInterface * appLaunchInterface = new QDBusInterface("com.kylin.AppManager", - "/com/kylin/AppManager", - "com.kylin.AppManager", - QDBusConnection::sessionBus()); - if(!appLaunchInterface->isValid()) { - qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message()); - res = false; - } else { - appLaunchInterface->setTimeout(10000); - QDBusReply reply = appLaunchInterface->call("LaunchDefaultAppWithUrl", address); - if(reply.isValid()) { - res = reply; - } else { - qWarning() << "SoftWareCenter dbus called failed!"; - res = false; - } - } - if(appLaunchInterface) { - delete appLaunchInterface; - } - appLaunchInterface = NULL; - if (res) - return; - QDesktopServices::openUrl(address); -} - -void WebSearchView::initConnections() -{ - -} - -WebSearchWidget::WebSearchWidget(QWidget *parent) : QWidget(parent) -{ - this->initUi(); - initConnections(); -} - -QString WebSearchWidget::getWidgetName() -{ - return m_titleLabel->text(); -} - -void WebSearchWidget::setEnabled(const bool &enabled) -{ - m_enabled = enabled; -} - -void WebSearchWidget::clearResultSelection() -{ - this->m_webSearchView->setCurrentIndex(QModelIndex()); -} - -QModelIndex WebSearchWidget::getModlIndex(int row, int column) -{ - return this->m_webSearchView->getModlIndex(row, column); -} - -void WebSearchWidget::setResultSelection(const QModelIndex &index) -{ - this->m_webSearchView->setCurrentIndex(index); -} - -void WebSearchWidget::LaunchBrowser() -{ - this->m_webSearchView->LaunchBrowser(); -} - -void WebSearchWidget::initUi() -{ - m_mainLyt = new QVBoxLayout(this); - this->setLayout(m_mainLyt); - m_mainLyt->setContentsMargins(MAIN_MARGINS); - m_mainLyt->setSpacing(MAIN_SPACING); - - m_titleLabel = new TitleLabel(this); - m_titleLabel->setText(tr("Web Page")); - m_titleLabel->setFixedHeight(TITLE_HEIGHT); - - m_webSearchView = new WebSearchView(this); - - m_mainLyt->addWidget(m_titleLabel); - m_mainLyt->addWidget(m_webSearchView); - this->setFixedHeight(m_webSearchView->height() + TITLE_HEIGHT); - this->setFixedWidth(656); -} - -void WebSearchWidget::initConnections() -{ - connect(this, &WebSearchWidget::startSearch, m_webSearchView, &WebSearchView::startSearch); - connect(m_webSearchView, &WebSearchView::clicked, this, [=] () { - this->LaunchBrowser(); - }); -} diff --git a/frontend/view/web-search-view.h b/frontend/view/web-search-view.h deleted file mode 100644 index 22aaa21..0000000 --- a/frontend/view/web-search-view.h +++ /dev/null @@ -1,91 +0,0 @@ -/* - * - * Copyright (C) 2023, KylinSoft Co., Ltd. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * - */ -#ifndef WEBSEARCHVIEW_H -#define WEBSEARCHVIEW_H -#include -#include -#include -#include "web-search-model.h" -#include "result-view-delegate.h" -#include "title-label.h" - -namespace UkuiSearch { -class WebSearchView : public QTreeView -{ - Q_OBJECT -public: - WebSearchView(QWidget *parent = nullptr); - ~WebSearchView() = default; - - bool isSelected(); - int showHeight(); - QModelIndex getModlIndex(int row, int column); - void LaunchBrowser(); - -public Q_SLOTS: - void clearSelectedRow(); - void startSearch(const QString &); - -protected: - void mouseReleaseEvent(QMouseEvent *event); - -private: - void initConnections(); - - WebSearchModel * m_model = nullptr; - bool m_is_selected = false; - ResultViewDelegate * m_styleDelegate = nullptr; - QString m_keyWord; -}; - -class WebSearchWidget : public QWidget -{ - Q_OBJECT -public: - WebSearchWidget(QWidget *parent = nullptr); - ~WebSearchWidget() = default; - - QString getWidgetName(); - void setEnabled(const bool&); - void clearResultSelection(); - QModelIndex getModlIndex(int row, int column); - void setResultSelection(const QModelIndex &index); - void LaunchBrowser(); - -private: - void initUi(); - void initConnections(); - - bool m_enabled = true; - QVBoxLayout * m_mainLyt = nullptr; - QHBoxLayout * m_resultLyt = nullptr; - TitleLabel * m_titleLabel = nullptr; - WebSearchView * m_webSearchView = nullptr; - QLabel * m_queryIcon = nullptr; - -Q_SIGNALS: - void startSearch(const QString &); - void clearSelectedRow(); - void rowClicked(); - -}; - -} -#endif // WEBSEARCHVIEW_H diff --git a/libsearch/global-settings-private.h b/libsearch/global-settings-private.h new file mode 100644 index 0000000..8050e12 --- /dev/null +++ b/libsearch/global-settings-private.h @@ -0,0 +1,32 @@ +#ifndef GLOBALSETTINGSPRIVATE_H +#define GLOBALSETTINGSPRIVATE_H + +#include "global-settings.h" +#include +#include +#include + +namespace UkuiSearch { +class GlobalSettingsPrivate : public QObject +{ + Q_OBJECT +public: + explicit GlobalSettingsPrivate(QObject *parent = nullptr); + const QVariant getValue(const QString&); + +Q_SIGNALS: + void valueChanged(const QString& key, QVariant value); + +private: + void setValue(const QString& key, const QVariant& value); + + QMutex m_mutex; + + QGSettings *m_transGsettings = nullptr; + QGSettings *m_themeGsettings = nullptr; + QMap m_cache; +}; +} + + +#endif // GLOBALSETTINGSPRIVATE_H diff --git a/libsearch/global-settings.cpp b/libsearch/global-settings.cpp index 85ef84a..c60710f 100644 --- a/libsearch/global-settings.cpp +++ b/libsearch/global-settings.cpp @@ -18,52 +18,21 @@ * Modified by: zhangpengfei * */ -#include -#include + #include "global-settings.h" -#define UKUI_SEARCH_SCHEMAS "org.ukui.search.settings" +#include "global-settings-private.h" +#include +#include +#include +#include + +#define CONTROL_CENTER_PERSONALISE_GSETTINGS_ID "org.ukui.control-center.personalise" +#define THEME_GSETTINGS_ID "org.ukui.style" using namespace UkuiSearch; -static GlobalSettings *globalInstance = nullptr; -GlobalSettings *GlobalSettings::getInstance() { - if(!globalInstance) { - globalInstance = new GlobalSettings; - } - return globalInstance; -} - -GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent) +GlobalSettingsPrivate::GlobalSettingsPrivate(QObject *parent) : QObject(parent) { - //搜索黑名单过滤 - m_blockDirsSettings = new QSettings(BLOCK_DIRS, QSettings::IniFormat, this); - m_blockDirsSettings->setIniCodec(QTextCodec::codecForName("UTF-8")); - if (!QFile::exists(BLOCK_DIRS)) { - QFile file(BLOCK_DIRS); - file.open(QIODevice::ReadOnly); - file.close(); - } - m_blockDirsSettings->sync(); - - m_confWatcher = new QFileSystemWatcher(this); - m_confWatcher->addPath(BLOCK_DIRS); - connect(m_confWatcher, &QFileSystemWatcher::fileChanged, this, [ & ]() { - m_blockDirsSettings->sync(); - m_confWatcher->addPath(BLOCK_DIRS); - }); - //搜索历史记录 - 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", - "/org/kylinssoclient/path", - "org.freedesktop.kylinssoclient.interface", - "keyChanged", - this, SLOT(updateSearchHistory(QString)))) - qWarning() << "Kylinssoclient Dbus connect fail!"; - //全局页面透明度 //the default number of transparency for mainwindow is 0.7 setValue(TRANSPARENCY_KEY, 0.7); @@ -71,9 +40,10 @@ GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent) m_transGsettings = new QGSettings(CONTROL_CENTER_PERSONALISE_GSETTINGS_ID, QByteArray(), this); connect(m_transGsettings, &QGSettings::changed, this, [ = ](const QString & key) { if(key == TRANSPARENCY_KEY) { - setValue(TRANSPARENCY_KEY, m_transGsettings->get(TRANSPARENCY_KEY).toDouble()); + double transparency = m_transGsettings->get(TRANSPARENCY_KEY).toDouble(); + setValue(TRANSPARENCY_KEY, transparency); qApp->paletteChanged(qApp->palette()); - Q_EMIT this->transparencyChanged(m_transGsettings->get(TRANSPARENCY_KEY).toDouble()); + Q_EMIT this->valueChanged(TRANSPARENCY_KEY, transparency); } }); if(m_transGsettings->keys().contains(TRANSPARENCY_KEY)) { @@ -91,7 +61,7 @@ GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent) //当前主题改变时也发出paletteChanged信号,通知主界面刷新 setValue(STYLE_NAME_KEY, m_themeGsettings->get(STYLE_NAME_KEY).toString()); qApp->paletteChanged(qApp->palette()); - Q_EMIT this->styleChanged(m_themeGsettings->get(STYLE_NAME_KEY).toString()); + Q_EMIT this->valueChanged(STYLE_NAME_KEY, m_themeGsettings->get(STYLE_NAME_KEY).toString()); } else if(key == FONT_SIZE_KEY) { setValue(FONT_SIZE_KEY, m_themeGsettings->get(FONT_SIZE_KEY).toDouble()); qApp->paletteChanged(qApp->palette()); @@ -106,41 +76,9 @@ GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent) 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).toString()); - } - 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) { +const QVariant GlobalSettingsPrivate::getValue(const QString &key) { m_mutex.lock(); QVariant value = m_cache.value(key); @@ -148,81 +86,24 @@ const QVariant GlobalSettings::getValue(const QString &key) { return value; } -bool GlobalSettings::setBlockDirs(const QString &path, int &returnCode, bool remove) { - if(remove) { - if(path.isEmpty()) { - returnCode = PATH_EMPTY; - return false; - } - - m_blockDirsSettings->remove(path); - return true; - } - - //why QSetting's key can't start with "/"?? - QString pathKey = path.right(path.length() - 1); - - if (pathKey.endsWith(QLatin1Char('/'))) { - pathKey = pathKey.mid(0, pathKey.length() - 1); - } - - QStringList blockDirs = m_blockDirsSettings->allKeys(); - for(QString i : blockDirs) { - if(FileUtils::isOrUnder(pathKey, i)) { -// returnCode = QString(tr("My parent folder has been blocked!")); - returnCode = PATH_PARENT_BLOCKED; - return false; - } - - if(FileUtils::isOrUnder(i, pathKey)) - m_blockDirsSettings->remove(i); - } - m_blockDirsSettings->setValue(pathKey, "0"); - return true; -} - -QStringList GlobalSettings::getBlockDirs() { - QStringList blockList; - QStringList tmp = m_blockDirsSettings->allKeys(); - for (const QString& blockDir : tmp) { - QString wholePath = "/" + blockDir; - if (QFile::exists(wholePath)) { - blockList.append(wholePath); - } else { - m_blockDirsSettings->remove(blockDir); - m_blockDirsSettings->sync(); - } - } - return blockList; -} - -void GlobalSettings::setSearchRecord(const QString &word, const QDateTime &time) { - QStringList keys = m_searchRecordSettings->allKeys(); - if(keys.contains(QString(QUrl::toPercentEncoding(word)))) - m_history.removeOne(word); - m_searchRecordSettings->setValue(QString(QUrl::toPercentEncoding(word)), time.toString("yyyy-MM-dd hh:mm:ss")); - if(keys.size() >= 20) - m_searchRecordSettings->remove(QString(QUrl::toPercentEncoding(m_history.takeFirst()))); - m_history.append(word); -} - -QStringList GlobalSettings::getSearchRecord() { - return m_history; -} - //this method is designed for main process settings only!! -void GlobalSettings::setValue(const QString &key, const QVariant &value) { +void GlobalSettingsPrivate::setValue(const QString &key, const QVariant &value) { m_mutex.lock(); m_cache.insert(key, value); m_mutex.unlock(); } -void GlobalSettings::updateSearchHistory(QString key) { - if(key == "search") { - m_searchRecordSettings->sync(); - m_history.clear(); - for(QString i : m_searchRecordSettings->allKeys()) { - m_history.append(QUrl::fromPercentEncoding(i.toLocal8Bit())); - } - } +GlobalSettings &GlobalSettings::getInstance() { + static GlobalSettings instance; + return instance; +} + +const QVariant GlobalSettings::getValue(const QString& key) +{ + return d->getValue(key); +} + +GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent), d(new GlobalSettingsPrivate(this)) +{ + connect(d, &GlobalSettingsPrivate::valueChanged, this, &GlobalSettings::valueChanged); } diff --git a/libsearch/global-settings.h b/libsearch/global-settings.h index c7830de..17bea52 100644 --- a/libsearch/global-settings.h +++ b/libsearch/global-settings.h @@ -22,44 +22,18 @@ #define GLOBALSETTINGS_H #include -#include -#include -#include -#include -#include -//#include -//If use pkg_config, it wont build succes,why????????? -//My demo can build access yet. -//MouseZhangZh -#include -#include -#include -#include +#include +#include #include "libsearch_global.h" -#include "file-utils.h" - -#define CONTROL_CENTER_PERSONALISE_GSETTINGS_ID "org.ukui.control-center.personalise" -#define TRANSPARENCY_KEY "transparency" -#define THEME_GSETTINGS_ID "org.ukui.style" -#define STYLE_NAME_KEY "styleName" -#define FONT_SIZE_KEY "systemFontSize" -#define ICON_THEME_KEY "iconThemeName" - -#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 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 { + +const static QString TRANSPARENCY_KEY = "transparency"; +const static QString STYLE_NAME_KEY = "styleName"; +const static QString FONT_SIZE_KEY = "systemFontSize"; +const static QString ICON_THEME_KEY = "iconThemeName"; + +class GlobalSettingsPrivate; /** * @brief The GlobalSettings class * 用于同步搜索应用的全局设置 @@ -68,48 +42,18 @@ namespace UkuiSearch { class LIBSEARCH_EXPORT GlobalSettings : public QObject { Q_OBJECT public: - static GlobalSettings *getInstance(); - const QVariant getValue(const QString&); - /** - * @brief setBlockDirs - * set path for blacklist,return true if success,otherwise return false. - * @param path path to be blocked - * @param returnMessage this message will be set when return false. - * @param true to remove blocking,false to set blocking,default set false. - * @return - */ - bool setBlockDirs(const QString& path, int &returnCode, bool remove = false); - QStringList getBlockDirs(); - void setSearchRecord(const QString &word, const QDateTime &time); - QStringList getSearchRecord(); - void updateSearchHistory(QString key); + static GlobalSettings& getInstance(); + const QVariant getValue(const 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); + void valueChanged(const QString& key, QVariant value); private: explicit GlobalSettings(QObject *parent = nullptr); - ~GlobalSettings() = default; - void setValue(const QString&, const QVariant&); - - QGSettings *m_transGsettings; - QGSettings *m_themeGsettings; - QGSettings *m_searchGsettings; - QSettings *m_blockDirsSettings; - QSettings *m_searchRecordSettings; - QMap m_cache; - QStringList m_history; - QFileSystemWatcher *m_confWatcher; - - QMutex m_mutex; -// size_t test = 0; - + GlobalSettings(const GlobalSettings&) = delete; + GlobalSettings& operator =(const GlobalSettings&) = delete; + GlobalSettingsPrivate *d; }; } diff --git a/libsearch/index/file-indexer-config.cpp b/libsearch/index/file-indexer-config.cpp index 847857e..1d39e97 100644 --- a/libsearch/index/file-indexer-config.cpp +++ b/libsearch/index/file-indexer-config.cpp @@ -27,6 +27,7 @@ static const QByteArray UKUI_SEARCH_SCHEMAS = QByteArrayLiteral("org.ukui.search.settings"); static const QString FILE_INDEX_ENABLE_KEY = QStringLiteral("fileIndexEnable"); static const QString CONTENT_INDEX_ENABLE_KEY = QStringLiteral("contentIndexEnable"); +static const QString CONTENT_FUZZY_SEARCH_KEY = QStringLiteral("contentFuzzySearch"); static const QString OCR_ENABLE_KEY = QStringLiteral("ocrEnable"); static const QString META_DATA_INDEX_ENABLE_KEY = QStringLiteral("metaDataIndexEnable"); @@ -98,6 +99,21 @@ bool FileIndexerConfig::isContentIndexEnable() return m_settings->value(CONTENT_INDEX_ENABLE_KEY, true).toBool(); } +bool FileIndexerConfig::isFuzzySearchEnable() +{ + if(m_gsettings) { + if(m_gsettings->keys().contains(CONTENT_FUZZY_SEARCH_KEY)) { + return m_gsettings->get(CONTENT_FUZZY_SEARCH_KEY).toBool(); + } else { + qWarning() << "FileIndexerConfig: Can not find key:" << CONTENT_FUZZY_SEARCH_KEY << "in" << UKUI_SEARCH_SCHEMAS; + return false; + } + } else { + qWarning() << "FileIndexerConfig:" << UKUI_SEARCH_SCHEMAS << " is not found!"; + return false; + } +} + bool FileIndexerConfig::isOCREnable() { return m_settings->value(OCR_ENABLE_KEY, true).toBool(); diff --git a/libsearch/index/file-indexer-config.h b/libsearch/index/file-indexer-config.h index 3ba595c..ec16424 100644 --- a/libsearch/index/file-indexer-config.h +++ b/libsearch/index/file-indexer-config.h @@ -51,6 +51,11 @@ public: * @return 是否启动文本内容索引 */ bool isContentIndexEnable(); + /** + * @brief isFuzzySearchEnable + * @return 是否启动模糊搜索 + */ + bool isFuzzySearchEnable(); /** * @brief isOCREnable * @return 是否激活OCR功能(文件内容索引) diff --git a/libsearch/index/file-search-plugin.cpp b/libsearch/index/file-search-plugin.cpp index 11b2e3d..1a6ed9f 100644 --- a/libsearch/index/file-search-plugin.cpp +++ b/libsearch/index/file-search-plugin.cpp @@ -23,12 +23,13 @@ #include #include #include "search-manager.h" +#include "file-indexer-config.h" #define OCR_ICONLABLE_WITH 352 #define OCR_ICONLABLE_HEIGHT 247 using namespace UkuiSearch; -FileSearchPlugin::FileSearchPlugin(QObject *parent) : QObject(parent),m_settings(GlobalSettings::getInstance()) +FileSearchPlugin::FileSearchPlugin(QObject *parent) : QObject(parent) { SearchPluginIface::Actioninfo open { 0, tr("Open")}; SearchPluginIface::Actioninfo Openpath { 1, tr("Open path")}; @@ -60,7 +61,7 @@ void UkuiSearch::FileSearchPlugin::KeywordSearch(QString keyword, DataQueuegetValue(FILE_INDEX_ENABLE_KEY).toBool()) { + if(FileIndexerConfig::getInstance()->isFileIndexEnable()) { FileSearch *filesearch; filesearch = new FileSearch(searchResult, SearchManager::uniqueSymbolFile, keyword, FILE_SEARCH_VALUE, 1, 0, 5); m_pool.start(filesearch); @@ -237,7 +238,7 @@ void FileSearchPlugin::initDetailPage() // return previewPage; //} -DirSearchPlugin::DirSearchPlugin(QObject *parent) : QObject(parent),m_settings(GlobalSettings::getInstance()) +DirSearchPlugin::DirSearchPlugin(QObject *parent) : QObject(parent) { SearchPluginIface::Actioninfo open { 0, tr("Open")}; SearchPluginIface::Actioninfo Openpath { 1, tr("Open path")}; @@ -269,7 +270,7 @@ void UkuiSearch::DirSearchPlugin::KeywordSearch(QString keyword, DataQueuegetValue(FILE_INDEX_ENABLE_KEY).toBool()) { + if(FileIndexerConfig::getInstance()->isFileIndexEnable()) { FileSearch *filesearch; filesearch = new FileSearch(searchResult, SearchManager::uniqueSymbolDir, keyword, DIR_SEARCH_VALUE, 1, 0, 5); m_pool.start(filesearch); @@ -423,7 +424,7 @@ void DirSearchPlugin::initDetailPage() // return nullptr; //} -FileContengSearchPlugin::FileContengSearchPlugin(QObject *parent) : QObject(parent),m_settings(GlobalSettings::getInstance()) +FileContengSearchPlugin::FileContengSearchPlugin(QObject *parent) : QObject(parent) { SearchPluginIface::Actioninfo open { 0, tr("Open")}; SearchPluginIface::Actioninfo Openpath { 1, tr("Open path")}; @@ -456,9 +457,9 @@ void UkuiSearch::FileContengSearchPlugin::KeywordSearch(QString keyword, DataQue SearchManager::m_mutexContent.unlock(); m_keyWord = keyword; - if(m_settings->getValue(FILE_INDEX_ENABLE_KEY).toBool()) { + if(FileIndexerConfig::getInstance()->isFileIndexEnable()) { FileContentSearch *fileContentSearch; - fileContentSearch = new FileContentSearch(searchResult, SearchManager::uniqueSymbolContent, keyword, m_settings->getValue(CONTENT_FUZZY_SEARCH_KEY).toBool(), 0, 5); + fileContentSearch = new FileContentSearch(searchResult, SearchManager::uniqueSymbolContent, keyword, FileIndexerConfig::getInstance()->isFuzzySearchEnable(), 0, 5); m_pool.start(fileContentSearch); } } diff --git a/libsearch/index/file-search-plugin.h b/libsearch/index/file-search-plugin.h index b289cee..12c8387 100644 --- a/libsearch/index/file-search-plugin.h +++ b/libsearch/index/file-search-plugin.h @@ -90,7 +90,6 @@ private: bool m_enable = true; QList m_actionInfo; QThreadPool m_pool; - GlobalSettings *m_settings = nullptr; }; class LIBSEARCH_EXPORT DirSearchPlugin : public QObject, public SearchPluginIface @@ -145,7 +144,6 @@ private: bool m_enable = true; QList m_actionInfo; QThreadPool m_pool; - GlobalSettings *m_settings = nullptr; }; class LIBSEARCH_EXPORT FileContengSearchPlugin : public QObject, public SearchPluginIface @@ -203,7 +201,6 @@ private: bool m_enable = true; QList m_actionInfo; QThreadPool m_pool; - GlobalSettings *m_settings = nullptr; }; } diff --git a/libsearch/libsearch.pro b/libsearch/libsearch.pro index 5acd334..50827ad 100644 --- a/libsearch/libsearch.pro +++ b/libsearch/libsearch.pro @@ -56,6 +56,7 @@ SOURCES += \ HEADERS += \ common.h \ file-utils.h \ + global-settings-private.h \ global-settings.h \ gobject-template.h \ libsearch_global.h \ diff --git a/libsearch/plugininterface/separation-line.cpp b/libsearch/plugininterface/separation-line.cpp index d6e5a06..846eb99 100644 --- a/libsearch/plugininterface/separation-line.cpp +++ b/libsearch/plugininterface/separation-line.cpp @@ -19,6 +19,7 @@ */ #include "separation-line.h" #include "global-settings.h" +#include #define NOMORL_LINE_STYLE "QFrame{background: rgba(0,0,0,0.1);}" #define DARK_LINE_STYLE "QFrame{background: rgba(255, 255, 255, 0.16);}" @@ -34,7 +35,7 @@ SeparationLine::SeparationLine(QWidget *parent) : QFrame(parent) void SeparationLine::setLineStyle() { - QString type = GlobalSettings::getInstance()->getValue(STYLE_NAME_KEY).toString(); + QString type = GlobalSettings::getInstance().getValue(STYLE_NAME_KEY).toString(); if (type == "ukui-dark") { this->setStyleSheet(DARK_LINE_STYLE); } else { diff --git a/libsearch/search-app-widget-plugin/search.cpp b/libsearch/search-app-widget-plugin/search.cpp index 013963a..b1ca19f 100644 --- a/libsearch/search-app-widget-plugin/search.cpp +++ b/libsearch/search-app-widget-plugin/search.cpp @@ -28,11 +28,6 @@ #include #include -#define CONTROL_CENTER_PERSONALISE_GSETTINGS_ID "org.ukui.control-center.personalise" -#define TRANSPARENCY_KEY "transparency" -#define THEME_GSETTINGS_ID "org.ukui.style" -#define STYLE_NAME_KEY "styleName" - using namespace UkuiSearch; AppWidgetPlugin::AppWidgetPlugin(QString providername, QObject *parent) : KAppWidgetProvider(providername, parent) @@ -49,14 +44,11 @@ AppWidgetPlugin::AppWidgetPlugin(QString providername, QObject *parent) : KAppWi m_manager = new AppWidget::KAppWidgetManager(this); - connect(GlobalSettings::getInstance(), &GlobalSettings::transparencyChanged, [ & ] { - qDebug() << "transparency changed"; - this->appWidgetUpdate(); - }); - - connect(GlobalSettings::getInstance(), &GlobalSettings::styleChanged, [ & ] { - qDebug() << "style changed"; - this->appWidgetUpdate(); + connect(&GlobalSettings::getInstance(), &GlobalSettings::valueChanged, [ & ] (const QString& key, QVariant value) { + if (key == TRANSPARENCY_KEY || key == STYLE_NAME_KEY) { + this->appWidgetUpdate(); + qDebug() << key << " has changed to" << value; + } }); } @@ -76,7 +68,7 @@ void AppWidgetPlugin::appWidgetRecevie(const QString &eventname, const QString & void AppWidgetPlugin::appWidgetUpdate() { - QString theme = GlobalSettings::getInstance()->getValue(STYLE_NAME_KEY).toString(); + QString theme = GlobalSettings::getInstance().getValue(STYLE_NAME_KEY).toString(); QJsonObject obj; if (theme == "ukui-dark") { obj.insert("red", QJsonValue(0)); @@ -90,7 +82,7 @@ void AppWidgetPlugin::appWidgetUpdate() obj.insert("placeHolderTextColor", QJsonValue("#72000000")); } - obj.insert("alpha", QJsonValue(GlobalSettings::getInstance()->getValue(TRANSPARENCY_KEY).toDouble())); + obj.insert("alpha", QJsonValue(GlobalSettings::getInstance().getValue(TRANSPARENCY_KEY).toDouble())); QString jsonData = QString(QJsonDocument(obj).toJson()); QVariantMap dataMap; diff --git a/libsearch/websearch/web-search-plugin.cpp b/libsearch/websearch/web-search-plugin.cpp index 3b6e52f..67bc92a 100644 --- a/libsearch/websearch/web-search-plugin.cpp +++ b/libsearch/websearch/web-search-plugin.cpp @@ -1,15 +1,30 @@ -#include -#include -#include #include "web-search-plugin.h" #include "global-settings.h" -#define WEB_ENGINE_KEY "webEngine" +#include +#include +#include +#include +#include + +#define UKUI_SEARCH_SCHEMAS "org.ukui.search.settings" #define BROWSERTYPE "x-scheme-handler/http" #define DESKTOPPATH "/usr/share/applications/" +#define WEB_ENGINE_KEY "webEngine" using namespace UkuiSearch; WebSearchPlugin::WebSearchPlugin(QObject *parent) : QObject(parent) { + if (QGSettings::isSchemaInstalled(UKUI_SEARCH_SCHEMAS)) { + m_settings = new QGSettings(UKUI_SEARCH_SCHEMAS, QByteArray(), this); + if (m_settings->keys().contains(WEB_ENGINE_KEY)) { + m_webEngine = m_settings->get(WEB_ENGINE_KEY).toString(); + } + connect(m_settings, &QGSettings::changed, this, [ & ] (const QString& key) { + if (key == WEB_ENGINE_KEY) { + m_webEngine = m_settings->get(WEB_ENGINE_KEY).toString(); + } + }); + } SearchPluginIface::Actioninfo open { 0, tr("Start browser search")}; m_actionInfo << open; initDetailPage(); @@ -37,10 +52,7 @@ void UkuiSearch::WebSearchPlugin::KeywordSearch(QString keyword, DataQueuegetValue("webEngine").toString(); - if(!engine.isEmpty()) { - if(engine == "360") { + if(!m_webEngine.isEmpty()) { + if(m_webEngine == "360") { address = "https://so.com/s?q=" + m_keyWord; //360 - } else if(engine == "sougou") { + } else if(m_webEngine == "sougou") { address = "https://www.sogou.com/web?query=" + m_keyWord; //搜狗 } else { address = "http://baidu.com/s?word=" + m_keyWord; //百度 @@ -125,14 +136,14 @@ void UkuiSearch::WebSearchPlugin::initDetailPage() m_detailLyt->setContentsMargins(8, 0, 16, 0); m_iconLabel = new QLabel(m_detailPage); m_iconLabel->setAlignment(Qt::AlignCenter); - QString type = GlobalSettings::getInstance()->getValue(STYLE_NAME_KEY).toString(); + QString type = GlobalSettings::getInstance().getValue(STYLE_NAME_KEY).toString(); if (type == "ukui-dark") { m_iconLabel->setPixmap(QIcon(":/res/icons/search-web-dark.svg").pixmap(128, 128)); } else { m_iconLabel->setPixmap(QIcon(":/res/icons/search-web-default.svg").pixmap(128, 128)); } connect(qApp, &QApplication::paletteChanged, this, [=] () { - QString type = GlobalSettings::getInstance()->getValue(STYLE_NAME_KEY).toString(); + QString type = GlobalSettings::getInstance().getValue(STYLE_NAME_KEY).toString(); if (type == "ukui-dark") { m_iconLabel->setPixmap(QIcon(":/res/icons/search-web-dark.svg").pixmap(128, 128)); } else { diff --git a/libsearch/websearch/web-search-plugin.h b/libsearch/websearch/web-search-plugin.h index 87d2414..70e998f 100644 --- a/libsearch/websearch/web-search-plugin.h +++ b/libsearch/websearch/web-search-plugin.h @@ -13,6 +13,7 @@ #include "action-label.h" #include "search-plugin-iface.h" #include +#include namespace UkuiSearch { @@ -48,6 +49,9 @@ private: QVBoxLayout * m_actionLyt = nullptr; + QGSettings *m_settings = nullptr; + QString m_webEngine; + bool m_enable = true; QList m_actionInfo;