From 6b06460e79cd0a26cfc9d436e2a5fca58c1733da Mon Sep 17 00:00:00 2001 From: JunjieBai Date: Wed, 7 Dec 2022 14:04:21 +0800 Subject: [PATCH] Add a switch for fuzzy search. Add the logic to custom the search scope. Modify the ui of search plugin of ukcc. --- libsearch/index/search-manager.cpp | 2 +- search-ukcc-plugin/search-ukcc-plugin.pro | 2 +- search-ukcc-plugin/search.cpp | 469 +++++++++++++++--- search-ukcc-plugin/search.h | 57 ++- search-ukcc-plugin/translations/bo_CN.ts | 116 ++++- search-ukcc-plugin/translations/zh_CN.ts | 116 ++++- .../com.ukui.search.fileindex.server.xml | 2 + .../dirwatcher/dir-watcher-adaptor.cpp | 12 +- .../dirwatcher/dir-watcher-adaptor.h | 6 +- .../dirwatcher/dir-watcher.cpp | 95 +++- .../dirwatcher/dir-watcher.h | 21 +- .../ukui-search-dir-manager-dbus.cpp | 2 + 12 files changed, 742 insertions(+), 158 deletions(-) diff --git a/libsearch/index/search-manager.cpp b/libsearch/index/search-manager.cpp index 1e4130d..dd3aff9 100644 --- a/libsearch/index/search-manager.cpp +++ b/libsearch/index/search-manager.cpp @@ -494,7 +494,7 @@ DirectSearch::DirectSearch(QString keyword, DataQueuegetBlockDirs(); - QStringList searchPath = DirWatcher::getDirWatcher()->searchableDirForSearchApplication(); + QStringList searchPath = DirWatcher::getDirWatcher()->currentIndexableDir(); QQueue bfs; for (const QString &path : searchPath) { if (blockList.contains(path)) { diff --git a/search-ukcc-plugin/search-ukcc-plugin.pro b/search-ukcc-plugin/search-ukcc-plugin.pro index 930ca06..0b806a2 100644 --- a/search-ukcc-plugin/search-ukcc-plugin.pro +++ b/search-ukcc-plugin/search-ukcc-plugin.pro @@ -1,4 +1,4 @@ -QT += widgets +QT += widgets dbus TEMPLATE = lib TARGET = $$qtLibraryTarget(search-ukcc-plugin) diff --git a/search-ukcc-plugin/search.cpp b/search-ukcc-plugin/search.cpp index ab33986..25f8076 100644 --- a/search-ukcc-plugin/search.cpp +++ b/search-ukcc-plugin/search.cpp @@ -13,6 +13,18 @@ Search::Search() m_plugin_name = tr("Search"); m_plugin_type = SEARCH_F; + m_interface = new QDBusInterface("com.ukui.search.fileindex.service", + "/org/ukui/search/privateDirWatcher", + "org.ukui.search.fileindex", + QDBusConnection::sessionBus(), + this); + + m_setSearchDirInterface = new QDBusInterface("com.ukui.search.fileindex.service", + "/org/ukui/search/fileindex", + "org.ukui.search.fileindex", + QDBusConnection::sessionBus(), + this); + m_dirSettings = new QSettings(QDir::homePath() + CONFIG_FILE, QSettings::NativeFormat, this); m_dirSettings->setIniCodec(QTextCodec::codecForName("UTF-8")); @@ -37,8 +49,11 @@ int Search::pluginTypes() QWidget *Search::pluginUi() { initUi(); - connect(m_addBlockDirWidget, &QPushButton::clicked, this, &Search::onBtnAddFolderClicked); + initFileDialog(); + initSearchDirs(); initBlockDirsList(); + connect(m_addSearchDirBtn, &QPushButton::clicked, this, &Search::onAddSearchDirBtnClicked); + connect(m_addBlockDirWidget, &QPushButton::clicked, this, &Search::onBtnAddBlockFolderClicked); if (m_gsettings) { //按钮状态初始化 @@ -46,9 +61,13 @@ QWidget *Search::pluginUi() //当前是否使用索引搜索/暴力搜索 bool is_index_search_on = m_gsettings->get(SEARCH_METHOD_KEY).toBool(); m_searchMethodBtn->setChecked(is_index_search_on); + if (is_index_search_on) { + m_indexSetFrame->show(); + } } else { m_searchMethodBtn->setEnabled(false); } + if (m_gsettings->keys().contains(WEB_ENGINE_KEY)) { //当前网页搜索的搜索引擎 QString engine = m_gsettings->get(WEB_ENGINE_KEY).toString(); @@ -56,6 +75,19 @@ QWidget *Search::pluginUi() } else { m_webEngineFrame->mCombox->setEnabled(false); } + + if (m_gsettings->keys().contains(CONTENT_SEARCH_KEY)) { + //是否为模糊搜索 + bool isFuzzy = m_gsettings->get(CONTENT_SEARCH_KEY).toBool(); + if (isFuzzy) { + m_fuzzyBtn->setChecked(true); + } else { + m_preciseBtn->setChecked(true); + } + } else { + m_fuzzyBtn->setEnabled(false); + m_preciseBtn->setEnabled(false); + } //监听gsettings值改变,更新控制面板UI connect(m_gsettings, &QGSettings::changed, this, [ = ](const QString &key) { if (key == SEARCH_METHOD_KEY) { @@ -68,12 +100,31 @@ QWidget *Search::pluginUi() m_webEngineFrame->mCombox->blockSignals(true); m_webEngineFrame->mCombox->setCurrentIndex(m_webEngineFrame->mCombox->findData(engine)); m_webEngineFrame->mCombox->blockSignals(false); + } else if (key == CONTENT_SEARCH_KEY) { + bool isFuzzy = m_gsettings->get(CONTENT_SEARCH_KEY).toBool(); + if (isFuzzy) { + m_fuzzyBtn->setChecked(true); + } else { + m_preciseBtn->setChecked(true); + } } }); connect(m_searchMethodBtn, &kdk::KSwitchButton::stateChanged, this, [ = ](bool checked) { if (m_gsettings && m_gsettings->keys().contains(SEARCH_METHOD_KEY)) { m_gsettings->set(SEARCH_METHOD_KEY, checked); } + if (checked) { + m_indexSetFrame->show(); + } else { + m_indexSetFrame->hide(); + } + }); + connect(m_indexMethodBtnGroup, QOverload::of(&QButtonGroup::buttonToggled),[ = ](int id, bool checked) { + if (id == -3) {//fuzzyBtn's id + if (m_gsettings && m_gsettings->keys().contains(CONTENT_SEARCH_KEY)) { + m_gsettings->set(CONTENT_SEARCH_KEY, checked); + } + } }); connect(m_webEngineFrame->mCombox, QOverload::of(&QComboBox::currentIndexChanged), this, [=](int index) { if (m_gsettings && m_gsettings->keys().contains(WEB_ENGINE_KEY)) { @@ -124,17 +175,30 @@ void Search::initUi() m_mainLyt = new QVBoxLayout(m_pluginWidget); m_pluginWidget->setLayout(m_mainLyt); - m_titleLabel = new TitleLabel(m_setFrame); + m_titleLabel = new TitleLabel(m_pluginWidget); m_titleLabel->setText(tr("Search")); m_mainLyt->addWidget(m_titleLabel); - //设置搜索模式部分的ui + //设置网页搜索引擎部分的ui + //~ contents_path /Search/Default web searching engine + m_webEngineFrame = new ComboxFrame(tr("Default web searching engine"), m_pluginWidget); + m_webEngineFrame->setContentsMargins(8, 0, 8, 0);// ComboxFrame右侧自带8的边距,左右边距各是16所以分别设为8 + m_webEngineFrame->setFixedHeight(56); + m_webEngineFrame->setMinimumWidth(550); + m_webEngineFrame->mCombox->insertItem(0, QIcon("/usr/share/ukui-search/search-ukcc-plugin/image/baidu.svg"), tr("baidu"), "baidu"); + m_webEngineFrame->mCombox->insertItem(1, QIcon("/usr/share/ukui-search/search-ukcc-plugin/image/sougou.svg"), tr("sougou"), "sougou"); + m_webEngineFrame->mCombox->insertItem(2, QIcon("/usr/share/ukui-search/search-ukcc-plugin/image/360.svg"), tr("360"), "360"); + m_mainLyt->addWidget(m_webEngineFrame); + + //设置索引部分的ui m_setFrame = new QFrame(m_pluginWidget); m_setFrame->setFrameShape(QFrame::Shape::Box); m_setFrameLyt = new QVBoxLayout(m_setFrame); m_setFrameLyt->setContentsMargins(0, 0, 0, 0); m_setFrameLyt->setSpacing(0); + //~ contents_path /Search/Create index + //索引开关UI m_searchMethodFrame = new QFrame(m_setFrame); m_searchMethodFrame->setMinimumWidth(550); m_searchMethodLyt = new QHBoxLayout(m_searchMethodFrame); @@ -148,7 +212,6 @@ void Search::initUi() m_descLabel1 = new QLabel(m_descFrame); m_descLabel2 = new QLabel(m_descFrame); - //~ contents_path /Search/Create index m_descLabel1->setText(tr("Create index")); m_descLabel2->setText(tr("Creating index can help you getting results quickly.")); m_descLabel2->setEnabled(false); @@ -158,27 +221,100 @@ void Search::initUi() m_searchMethodLyt->addWidget(m_descFrame); m_searchMethodLyt->addStretch(); m_searchMethodLyt->addWidget(m_searchMethodBtn); - m_setFrameLyt->addWidget(m_searchMethodFrame); - QFrame *line = new QFrame(m_setFrame); + m_indexSetFrame = new QFrame(m_setFrame); + m_indexSetLyt = new QVBoxLayout(m_indexSetFrame); + m_indexSetLyt->setContentsMargins(0, 0, 0, 0); + m_indexSetLyt->setSpacing(0); + + //分隔线 + QFrame *line = new QFrame(m_indexSetFrame); line->setFixedHeight(1); line->setLineWidth(0); line->setFrameShape(QFrame::HLine); line->setFrameShadow(QFrame::Sunken); - m_setFrameLyt->addWidget(line); - //设置网页搜索引擎部分的ui - //~ contents_path /Search/Default web searching engine - m_webEngineFrame = new ComboxFrame(tr("Default web searching engine"), m_searchMethodFrame); - m_webEngineFrame->setContentsMargins(8, 0, 8, 0);// ComboxFrame右侧自带8的边距,左右边距各是16所以分别设为8 - m_webEngineFrame->setFixedHeight(56); - m_webEngineFrame->setMinimumWidth(550); - m_webEngineFrame->mCombox->insertItem(0, QIcon("/usr/share/ukui-search/search-ukcc-plugin/image/baidu.svg"), tr("baidu"), "baidu"); - m_webEngineFrame->mCombox->insertItem(1, QIcon("/usr/share/ukui-search/search-ukcc-plugin/image/sougou.svg"), tr("sougou"), "sougou"); - m_webEngineFrame->mCombox->insertItem(2, QIcon("/usr/share/ukui-search/search-ukcc-plugin/image/360.svg"), tr("360"), "360"); - m_setFrameLyt->addWidget(m_webEngineFrame); + //设置索引模式的ui + m_indexMethodFrame = new QFrame(m_indexSetFrame); + m_indexMethodLyt = new QVBoxLayout(m_indexMethodFrame); + m_indexMethodLyt->setContentsMargins(8, 16, 0, 0);//radiobutton本身左边有8间距 + m_indexMethodFrame->setLayout(m_indexMethodLyt); + + m_indexMethodDescLabel = new QLabel(m_indexMethodFrame); + m_indexMethodDescLabel->setContentsMargins(8, 0, 0, 0); + m_indexMethodDescLabel->setText(tr("File Content Search")); + + m_preciseBtnFrame = new QFrame(m_indexMethodFrame); + m_preciseBtnLyt = new QHBoxLayout(m_preciseBtnFrame); + m_preciseBtnFrame->setLayout(m_preciseBtnLyt); + m_preciseBtn = new QRadioButton(tr("precise Search"), m_indexMethodFrame); + m_preciseDescLabel = new QLabel(m_indexMethodFrame); + m_preciseDescLabel->setText(tr("show the results that exactly match the keyword")); + m_preciseDescLabel->setEnabled(false); + m_preciseBtnLyt->addWidget(m_preciseBtn); + m_preciseBtnLyt->addWidget(m_preciseDescLabel); + m_preciseBtnLyt->addStretch(); + + m_fuzzyBtnFrame = new QFrame(m_indexMethodFrame); + m_fuzzyBtnLyt = new QHBoxLayout(m_fuzzyBtnFrame); + m_fuzzyBtnFrame->setLayout(m_fuzzyBtnLyt); + m_fuzzyBtn = new QRadioButton(tr("Fuzzy Search"), m_indexMethodFrame); + m_fuzzyDescLabel = new QLabel(m_indexMethodFrame); + m_fuzzyDescLabel->setText(tr("show more results that match the keyword")); + m_fuzzyDescLabel->setEnabled(false); + m_fuzzyBtnLyt->addWidget(m_fuzzyBtn); + m_fuzzyBtnLyt->addWidget(m_fuzzyDescLabel); + m_fuzzyBtnLyt->addStretch(); + + m_indexMethodBtnGroup = new QButtonGroup(m_indexSetFrame); + m_indexMethodBtnGroup->addButton(m_preciseBtn); + m_indexMethodBtnGroup->addButton(m_fuzzyBtn); + m_indexMethodBtnGroup->setExclusive(true); + + m_indexMethodLyt->addWidget(m_indexMethodDescLabel); + m_indexMethodLyt->addWidget(m_preciseBtnFrame); + m_indexMethodLyt->addWidget(m_fuzzyBtnFrame); + + m_indexSetLyt->addWidget(line); + m_indexSetLyt->addWidget(m_indexMethodFrame); + + m_setFrameLyt->addWidget(m_searchMethodFrame); + m_setFrameLyt->addWidget(m_indexSetFrame); + m_indexSetFrame->hide();//默认隐藏,根据是否开索引来初始化 + m_mainLyt->addWidget(m_setFrame); + //添加搜索目录部分ui + m_searchDirTitleLabel = new TitleLabel(m_pluginWidget); + m_searchDirTitleLabel->setText(tr("Search Folders")); + + m_searchDirDescLabel = new QLabel(m_pluginWidget); + m_searchDirDescLabel->setContentsMargins(16, 0, 0, 0); //TitleLabel自带16边距,QLabel需要自己设 + m_searchDirDescLabel->setEnabled(false); + m_searchDirDescLabel->setWordWrap(true); + m_searchDirDescLabel->setText(tr("Following folders will be searched. You can set it by adding and removing folders.")); + + + m_searchDirsFrame = new QFrame(m_pluginWidget); + m_searchDirsFrame->setFrameShape(QFrame::Shape::Box); + m_searchDirLyt = new QVBoxLayout(m_searchDirsFrame); + m_searchDirsFrame->setLayout(m_searchDirLyt); + m_searchDirLyt->setContentsMargins(0, 0, 0, 0); + m_searchDirLyt->setSpacing(2); + m_searchDirLyt->setDirection(QBoxLayout::BottomToTop); + + m_addSearchDirFrame = new QFrame(m_searchDirsFrame); + m_addSearchDirFrame->setFrameShape(QFrame::Shape::NoFrame); + m_addSearchDirFrame->setFixedHeight(60); + + m_addSearchDirBtn = new AddBtn(m_addSearchDirFrame); + m_searchDirLyt->addWidget(m_addSearchDirBtn); + + m_mainLyt->addSpacing(32); + m_mainLyt->addWidget(m_searchDirTitleLabel); + m_mainLyt->addWidget(m_searchDirDescLabel); + m_mainLyt->addWidget(m_searchDirsFrame); + //设置黑名单文件夹部分的ui m_blockDirTitleLabel = new TitleLabel(m_pluginWidget); @@ -250,6 +386,97 @@ void Search::initUi() m_mainLyt->setContentsMargins(0, 0, 0, 0); } +void Search::initFileDialog() +{ + //添加黑名单对话框 + m_blockDirDialog = new QFileDialog(m_pluginWidget); +// fileDialog->setFileMode(QFileDialog::Directory); //允许查看文件和文件夹,但只允许选择文件夹 + m_blockDirDialog->setFileMode(QFileDialog::DirectoryOnly); //只允许查看文件夹 +// fileDialog->setViewMode(QFileDialog::Detail); + m_blockDirDialog->setDirectory(QDir::homePath()); + m_blockDirDialog->setNameFilter(tr("Directories")); + m_blockDirDialog->setWindowTitle(tr("select blocked folder")); + m_blockDirDialog->setLabelText(QFileDialog::Accept, tr("Select")); + m_blockDirDialog->setLabelText(QFileDialog::LookIn, tr("Position: ")); + m_blockDirDialog->setLabelText(QFileDialog::FileName, tr("FileName: ")); + m_blockDirDialog->setLabelText(QFileDialog::FileType, tr("FileType: ")); + m_blockDirDialog->setLabelText(QFileDialog::Reject, tr("Cancel")); + connect(m_blockDirDialog, &QFileDialog::finished, [ = ] (int result) { + qWarning() << "=======" << result; + if (result == QDialog::Accepted) { + QString selectedDir = m_blockDirDialog->selectedFiles().first(); + qDebug() << "Selected a folder in onBtnAddClicked(): " << selectedDir; + int returnCode = setBlockDir(selectedDir, true); + switch (returnCode) { + case ReturnCode::Succeed : + qDebug() << "Add blocked folder succeed! path = " << selectedDir; + getBlockDirs(); + break; + case ReturnCode::PathEmpty : + qWarning() << "Add blocked folder failed, choosen path is empty! path = " << selectedDir; + QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add blocked folder failed, choosen path is empty!")); + break; +// case ReturnCode::NotInHomeDir : +// qWarning() << "Add blocked folder failed, it is not in home path! path = " << selectedDir; +// QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add blocked folder failed, it is not in home path!")); +// break; + case ReturnCode::ParentExist : + qWarning() << "Add blocked folder failed, its parent dir is exist! path = " << selectedDir; + QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add blocked folder failed, its parent dir is exist!")); + break; + case ReturnCode::HasBeenBlocked : + qWarning() << "Add blocked folder failed, it has been already blocked! path = " << selectedDir; + QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add blocked folder failed, it has been already blocked!")); + break; + default: + break; + } + } + }); + + //添加搜索目录对话框 + m_searchDirDialog = new QFileDialog(m_pluginWidget); + m_searchDirDialog->setFileMode(QFileDialog::DirectoryOnly); //只允许查看文件夹 + m_searchDirDialog->setDirectory(QDir::homePath()); + m_searchDirDialog->setNameFilter(tr("Directories")); + m_searchDirDialog->setWindowTitle(tr("select search folder")); + m_searchDirDialog->setLabelText(QFileDialog::Accept, tr("Select")); + m_searchDirDialog->setLabelText(QFileDialog::LookIn, tr("Position: ")); + m_searchDirDialog->setLabelText(QFileDialog::FileName, tr("FileName: ")); + m_searchDirDialog->setLabelText(QFileDialog::FileType, tr("FileType: ")); + m_searchDirDialog->setLabelText(QFileDialog::Reject, tr("Cancel")); + connect(m_searchDirDialog, &QFileDialog::finished, this, [ = ] (int result) { + if (result == QDialog::Accepted) { + QString selectedDir = m_searchDirDialog->selectedFiles().first(); + qDebug() << "Selected a folder in onAddSearchDirBtnClicked(): " << selectedDir; + int returnCode = setSearchDir(selectedDir, true); + switch (returnCode) { + case 1: + qDebug() << "Add search folder succeed! path = " << selectedDir; + break; + case -1: + QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add search folder failed, choosen path or its parent dir has been added!")); + break; + case -2: + QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add search folder failed, choosen path is not supported currently!")); + break; + case -3: + QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add search folder failed, choosen path is in repeat mounted devices and another path which is in the same device has been added!")); + break; + case -4: + QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add search folder failed, another path which is in the same device has been added!")); + break; + case -5: + QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add search folder failed, choosen path is not exists!")); + break; + default: + break; + } + } + }); + +} + /** * @brief Search::getBlockDirs 从配置文件获取黑名单并将黑名单列表传入 */ @@ -277,9 +504,9 @@ int Search::setBlockDir(const QString &dirPath, const bool &is_add) removeBlockDirFromList(dirPath); return ReturnCode::Succeed; } - if (!dirPath.startsWith(QDir::homePath())) { - return ReturnCode::NotInHomeDir; - } +// if (!dirPath.startsWith(QDir::homePath())) { +// return ReturnCode::NotInHomeDir; +// } QString pathKey = dirPath.right(dirPath.length() - 1); @@ -311,18 +538,148 @@ void Search::initBlockDirsList() getBlockDirs(); for (QString path: m_blockDirs) { QString wholePath = QString("/%1").arg(path); - if (QFileInfo(wholePath).isDir() && path.startsWith("home")) { + if (QFileInfo(wholePath).isDir() /*&& path.startsWith("home")*/) { appendBlockDirToList(wholePath); } } } +void Search::initSearchDirs() +{ + if (m_interface->isValid()) { + QDBusReply reply = m_interface->call("currentIndexableDir"); + if (reply.isValid()) { + for (const QString &path : reply.value()) { + appendSearchDirToList(path); + } + } else { + qCritical() << "Fail to call currentIndexableDir."; + } + } else { + qCritical() << "fileindex dbus error:" << m_interface->lastError(); + } +} + +int Search::setSearchDir(const QString &dirPath, const bool isAdd) +{ + if (!m_setSearchDirInterface->isValid()) { + return 0; + } + + if (isAdd) { + QDBusReply indexDirsRpl = m_interface->call("currentIndexableDir"); + QStringList indexDirs; + if (indexDirsRpl.isValid()) { + indexDirs = indexDirsRpl.value(); + } + + QDBusReply appendIndexRpl = m_setSearchDirInterface->call("appendIndexableListItem", dirPath); + if (appendIndexRpl.isValid()) { + if (appendIndexRpl.value() == 1) { + this->appendSearchDirToList(dirPath); + if (!indexDirs.isEmpty()) { + indexDirsRpl = m_interface->call("currentIndexableDir"); + if (indexDirsRpl.isValid() && (indexDirsRpl.value().size() < indexDirs.size() + 1)) { + QStringList dirsAfterAppend = indexDirsRpl.value(); + for (const QString& dir : indexDirs) { + if (!dirsAfterAppend.contains(dir)) { + this->removeSearchDirFromList(dir); + } + } + } + } + } + return appendIndexRpl.value(); + } + } else { + QDBusReply reply = m_setSearchDirInterface->call("removeIndexableListItem", dirPath); + if (reply.isValid()) { + if (reply.value()) { + this->removeSearchDirFromList(dirPath); + } + return reply.value(); + } + } + + return 0; +} + +void Search::appendSearchDirToList(const QString &path) +{ + HoverWidget * dirWidget = new HoverWidget(path, m_searchDirsFrame); + dirWidget->setObjectName(path); + dirWidget->setMinimumWidth(550); + QHBoxLayout * dirWidgetLyt = new QHBoxLayout(dirWidget); + dirWidgetLyt->setSpacing(8); + dirWidgetLyt->setContentsMargins(0, 0, 0, 0); + dirWidget->setLayout(dirWidgetLyt); + QFrame * dirFrame = new QFrame(dirWidget); + dirFrame->setFrameShape(QFrame::Shape::Box); + dirFrame->setFixedHeight(50); + QHBoxLayout * dirFrameLayout = new QHBoxLayout(dirFrame); + dirFrameLayout->setSpacing(16); + dirFrameLayout->setContentsMargins(16, 0, 16, 0); + QLabel * iconLabel = new QLabel(dirFrame); + QLabel * pathLabel = new QLabel(dirFrame); + + dirFrameLayout->addWidget(iconLabel); + iconLabel->setPixmap(QIcon::fromTheme("inode-directory").pixmap(QSize(24, 24))); + pathLabel->setText(path); + dirFrameLayout->addWidget(pathLabel); + dirFrameLayout->addStretch(); + QPushButton * delBtn = new QPushButton(dirFrame); + delBtn->setIcon(QIcon::fromTheme("edit-delete-symbolic")); + delBtn->setProperty("useButtonPalette", true); + delBtn->setFixedSize(30, 30); + delBtn->setToolTip(tr("delete")); + delBtn->setFlat(true); + + + delBtn->hide(); + dirFrameLayout->addWidget(delBtn); + dirWidgetLyt->addWidget(dirFrame); + + QFrame *line = new QFrame(dirWidget); + line->setObjectName(path); + line->setFixedHeight(1); + line->setLineWidth(0); + line->setFrameShape(QFrame::HLine); + line->setFrameShadow(QFrame::Sunken); + + m_searchDirLyt->addWidget(line); + m_searchDirLyt->addWidget(dirWidget); + connect(delBtn, &QPushButton::clicked, this, [ = ]() { + setSearchDir(path, false); + }); + connect(dirWidget, &HoverWidget::enterWidget, this, [ = ]() { + delBtn->show(); + }); + connect(dirWidget, &HoverWidget::leaveWidget, this, [ = ]() { + delBtn->hide(); + }); +} + +void Search::removeSearchDirFromList(const QString &path) +{ + HoverWidget *delDirWidget = m_searchDirsFrame->findChild(path); + if (delDirWidget) { + m_searchDirLyt->removeWidget(delDirWidget); + delDirWidget->deleteLater(); + qDebug() << "Delete folder of search succeed! path = " << path; + } + QFrame *line = m_searchDirsFrame->findChild(path); + if (line) { + m_searchDirLyt->removeWidget(line); + line->deleteLater(); + qDebug()<< "Delete line of search folder:" << path; + } +} + void Search::appendBlockDirToList(const QString &path) { HoverWidget * dirWidget = new HoverWidget(path, m_blockDirsFrame); dirWidget->setObjectName(path); dirWidget->setMinimumWidth(550); - dirWidget->setAttribute(Qt::WA_DeleteOnClose); QHBoxLayout * dirWidgetLyt = new QHBoxLayout(dirWidget); dirWidgetLyt->setSpacing(8); dirWidgetLyt->setContentsMargins(0, 0, 0, 0); @@ -355,6 +712,7 @@ void Search::appendBlockDirToList(const QString &path) // dirWidgetLyt->addWidget(delBtn); QFrame *line = new QFrame(m_blockDirsFrame); + line->setObjectName(path); line->setFixedHeight(1); line->setLineWidth(0); line->setFrameShape(QFrame::HLine); @@ -366,8 +724,6 @@ void Search::appendBlockDirToList(const QString &path) connect(delBtn, &QPushButton::clicked, this, [ = ]() { setBlockDir(path, false); getBlockDirs(); - m_blockDirsLyt->removeWidget(line); - line->deleteLater(); }); connect(dirWidget, &HoverWidget::enterWidget, this, [ = ]() { delBtn->show(); @@ -381,60 +737,29 @@ void Search::removeBlockDirFromList(const QString &path) { HoverWidget * delDirWidget = m_blockDirsFrame->findChild(path); if (delDirWidget) { - qDebug() << "Delete folder succeed! path = " << path; - delDirWidget->close(); + qDebug() << "Delete blocked folder succeed! path = " << path; + m_blockDirsLyt->removeWidget(delDirWidget); + delDirWidget->deleteLater(); + } + QFrame *line = m_blockDirsFrame->findChild(path); + if (line) { + m_blockDirsLyt->removeWidget(line); + line->deleteLater(); + qDebug() << "Delete line of blocked folder:" << path; } } void Search::setupConnection() { - connect(m_addBlockDirWidget, &QPushButton::clicked, this, &Search::onBtnAddFolderClicked); + connect(m_addBlockDirWidget, &QPushButton::clicked, this, &Search::onBtnAddBlockFolderClicked); } -void Search::onBtnAddFolderClicked() +void Search::onBtnAddBlockFolderClicked() { - QFileDialog * fileDialog = new QFileDialog(m_pluginWidget); -// fileDialog->setFileMode(QFileDialog::Directory); //允许查看文件和文件夹,但只允许选择文件夹 - fileDialog->setFileMode(QFileDialog::DirectoryOnly); //只允许查看文件夹 -// fileDialog->setViewMode(QFileDialog::Detail); - fileDialog->setDirectory(QDir::homePath()); - fileDialog->setNameFilter(tr("Directories")); - fileDialog->setWindowTitle(tr("select blocked folder")); - fileDialog->setLabelText(QFileDialog::Accept, tr("Select")); - fileDialog->setLabelText(QFileDialog::LookIn, tr("Position: ")); - fileDialog->setLabelText(QFileDialog::FileName, tr("FileName: ")); - fileDialog->setLabelText(QFileDialog::FileType, tr("FileType: ")); - fileDialog->setLabelText(QFileDialog::Reject, tr("Cancel")); - if(fileDialog->exec() != QDialog::Accepted) { - fileDialog->deleteLater(); - return; - } - QString selectedDir = 0; - selectedDir = fileDialog->selectedFiles().first(); - qDebug() << "Selected a folder in onBtnAddClicked(): " << selectedDir; - int returnCode = setBlockDir(selectedDir, true); - switch (returnCode) { - case ReturnCode::Succeed : - qDebug() << "Add blocked folder succeed! path = " << selectedDir; - getBlockDirs(); - break; - case ReturnCode::PathEmpty : - qWarning() << "Add blocked folder failed, choosen path is empty! path = " << selectedDir; - QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add blocked folder failed, choosen path is empty!")); - break; - case ReturnCode::NotInHomeDir : - qWarning() << "Add blocked folder failed, it is not in home path! path = " << selectedDir; - QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add blocked folder failed, it is not in home path!")); - break; - case ReturnCode::ParentExist : - qWarning() << "Add blocked folder failed, its parent dir is exist! path = " << selectedDir; - QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add blocked folder failed, its parent dir is exist!")); - break; - case ReturnCode::HasBeenBlocked : - qWarning() << "Add blocked folder failed, it has been already blocked! path = " << selectedDir; - QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add blocked folder failed, it has been already blocked!")); - break; - default: - break; - } + m_blockDirDialog->open(); +} + +void Search::onAddSearchDirBtnClicked() +{ + m_searchDirDialog->open(); } diff --git a/search-ukcc-plugin/search.h b/search-ukcc-plugin/search.h index 6c61ae2..5f362f4 100644 --- a/search-ukcc-plugin/search.h +++ b/search-ukcc-plugin/search.h @@ -3,6 +3,8 @@ #include #include +#include +#include #include #include #include @@ -11,6 +13,8 @@ #include #include #include +#include +#include #include #include @@ -27,6 +31,7 @@ #define UKUI_SEARCH_SCHEMAS "org.ukui.search.settings" #define SEARCH_METHOD_KEY "fileIndexEnable" #define WEB_ENGINE_KEY "webEngine" +#define CONTENT_SEARCH_KEY "contentFuzzySearch" //TODO #define CONFIG_FILE "/.config/org.ukui/ukui-search/ukui-search-block-dirs.conf" @@ -68,9 +73,15 @@ private: QVBoxLayout * m_mainLyt = nullptr; TitleLabel * m_titleLabel = nullptr; + //设置搜索引擎 +// TitleLabel * m_webEngineLabel = nullptr; + ComboxFrame * m_webEngineFrame = nullptr; + QVBoxLayout * m_webEngineLyt = nullptr; + + //索引详细设置 QFrame *m_setFrame = nullptr; QVBoxLayout *m_setFrameLyt = nullptr; - //设置搜索模式 + //索引开关 QFrame *m_descFrame = nullptr; QVBoxLayout *m_descFrameLyt = nullptr; QLabel *m_descLabel1 = nullptr; @@ -79,10 +90,33 @@ private: QHBoxLayout *m_searchMethodLyt = nullptr; // QLabel *m_searchMethodLabel = nullptr; kdk::KSwitchButton *m_searchMethodBtn = nullptr; - //设置搜索引擎 - TitleLabel * m_webEngineLabel = nullptr; - ComboxFrame * m_webEngineFrame = nullptr; - QVBoxLayout * m_webEngineLyt = nullptr; + //设置索引搜索模式 + QFrame *m_indexSetFrame = nullptr; + QVBoxLayout *m_indexSetLyt = nullptr; + //模糊搜索开关 + QFrame *m_indexMethodFrame = nullptr; + QVBoxLayout *m_indexMethodLyt = nullptr; + QLabel *m_indexMethodDescLabel = nullptr; + //模糊搜索按钮 + QButtonGroup *m_indexMethodBtnGroup = nullptr; + QFrame *m_fuzzyBtnFrame = nullptr; + QHBoxLayout *m_fuzzyBtnLyt = nullptr; + QRadioButton *m_fuzzyBtn = nullptr; + QLabel *m_fuzzyDescLabel = nullptr; + //精确搜索按钮 + QFrame *m_preciseBtnFrame = nullptr; + QHBoxLayout *m_preciseBtnLyt = nullptr; + QRadioButton *m_preciseBtn = nullptr; + QLabel *m_preciseDescLabel = nullptr; + + //设置当前搜索目录 + TitleLabel *m_searchDirTitleLabel = nullptr; + QLabel *m_searchDirDescLabel = nullptr; + QFrame *m_searchDirsFrame = nullptr; + QVBoxLayout *m_searchDirLyt = nullptr; + QLabel *m_searchDirLabel = nullptr; + AddBtn *m_addSearchDirBtn = nullptr; + QFrame *m_addSearchDirFrame = nullptr; //设置黑名单 TitleLabel * m_blockDirTitleLabel = nullptr; @@ -95,6 +129,10 @@ private: QLabel * m_addBlockDirLabel = nullptr; QHBoxLayout * m_addBlockDirLyt = nullptr; + void initFileDialog(); + QFileDialog *m_blockDirDialog = nullptr; + QFileDialog *m_searchDirDialog = nullptr; + QStringList m_blockDirs; QSettings * m_dirSettings = nullptr; void getBlockDirs(); @@ -104,11 +142,18 @@ private: void initBlockDirsList(); // void refreshBlockDirsList(); + QDBusInterface *m_interface = nullptr; + QDBusInterface *m_setSearchDirInterface = nullptr; + void initSearchDirs(); + int setSearchDir(const QString &dirPath, const bool isAdd); + void appendSearchDirToList(const QString &path); + void removeSearchDirFromList(const QString &path); void setupConnection(); private slots: - void onBtnAddFolderClicked(); + void onBtnAddBlockFolderClicked(); + void onAddSearchDirBtnClicked(); }; #endif // SEARCH_H diff --git a/search-ukcc-plugin/translations/bo_CN.ts b/search-ukcc-plugin/translations/bo_CN.ts index 426237f..3dac6d7 100644 --- a/search-ukcc-plugin/translations/bo_CN.ts +++ b/search-ukcc-plugin/translations/bo_CN.ts @@ -5,50 +5,49 @@ Search - + Search འཚོལ་ཞིབ། - + Create index གསར་འཛུགས་འཚོལ་ཞིབ་བྱེད་པར་ཁྲིད་སྟོན། - /Search/Create index - + Creating index can help you getting results quickly. སྟོན་གྲངས་གསར་སྐྲུན་བྱས་ན་ཁྱོད་ལ་མགྱོགས་མྱུར་ངང་གྲུབ་འབྲས་ཐོབ་པར་རོགས་རམ་བྱེད་ཐུབ། - + Default web searching engine ཁོག་གི་དྲ་ཤོག་གཏོད་པ་བཤེར་འཚོལ་རིགས་དབྱིབས། /Search/Default web searching engine - + baidu པའེ་ཏུའུ། - + sougou སོའོ་གོའུ། - + 360 360 - + Block Folders ལྐོག་བཀོད་མིང་ཐོ། /Search/Block Folders - + Following folders will not be searched. You can set it by adding and removing folders. གཤམ་གྱི་ཡིག་སྣོད་འཚོལ་བཤེར་མི་བྱེད། ཡིག་སྣོད་གསར་སྣོན་དང་གསུབ་འཕྲི་བྱས་ཚེ་ཡིག་ཆའི་དཀར་ཆག་སྒྲིག་འགོད་བྱ་ཐུབ། @@ -57,50 +56,96 @@ བསལ་འདེམས་ཀྱི་དཀར་ཆག། - + + delete བསུབ་པ། - + + Directories དཀར་ཆག - + + File Content Search + + + + + precise Search + + + + + show more results that match the keyword + + + + + Fuzzy Search + + + + + show the results that exactly match the keyword + + + + + Search Folders + + + + + Following folders will be searched. You can set it by adding and removing folders. + + + + select blocked folder བཀག་སྡོམ་བྱས་པའི་ཡིག་སྣོད་གདམ་གསེས - + + Select བདམས་ཐོན་བྱུང་བ། - + + Position: གོ་གནས་ནི། - + + FileName: ཡིག་ཆའི་མིང་ནི། - + + FileType: ཡིག་ཆའི་རིགས་དབྱིབས་ནི། - + + Cancel ཕྱིར་འཐེན། - + + + + + Warning ཐ་ཚིག་སྒྲོག་པ། @@ -110,9 +155,8 @@ སྦྱོར་རྟ་ལྐོག་བཀོད་མིང་ཐོ་ཕམ་ཁ་བསལ་འདེམས་ཀྱི་ཐབས་ལམ་སྟོང་བ་རེད། - Add blocked folder failed, it is not in home path! - སྦྱོར་རྟ་ལྐོག་བཀོད་མིང་ཐོ་ཕམ་ཁ་བསལ་འདེམས་ཀྱི་དཀར་ཆག་མི་ཁྱིམ་དཀར་ཆག་འོག། + སྦྱོར་རྟ་ལྐོག་བཀོད་མིང་ཐོ་ཕམ་ཁ་བསལ་འདེམས་ཀྱི་དཀར་ཆག་མི་ཁྱིམ་དཀར་ཆག་འོག། @@ -124,5 +168,35 @@ Add blocked folder failed, it has been already blocked! སྦྱོར་རྟ་ལྐོག་བཀོད་མིང་ཐོ་ཕམ་ཁ་བསལ་འདེམས་ཀྱི་དཀར་ཆག་ནི་ལྐོག་བཀོད་མིང་ཐོ་འི་ཁྲོད་ཀྱི་དཀར་ཆག་འོག + + + select search folder + + + + + Add search folder failed, choosen path is not supported currently! + + + + + Add search folder failed, another path which is in the same device has been added! + + + + + Add search folder failed, choosen path or its parent dir has been added! + + + + + Add search folder failed, choosen path is in repeat mounted devices and another path which is in the same device has been added! + + + + + Add search folder failed, choosen path is not exists! + + diff --git a/search-ukcc-plugin/translations/zh_CN.ts b/search-ukcc-plugin/translations/zh_CN.ts index 6f66061..68fefbd 100644 --- a/search-ukcc-plugin/translations/zh_CN.ts +++ b/search-ukcc-plugin/translations/zh_CN.ts @@ -5,50 +5,49 @@ Search - + Search 全局搜索 - + Create index 创建索引 - /Search/Create index - + Creating index can help you getting results quickly. 开启之后可以快速获取搜索结果 - + Default web searching engine 默认互联网搜索引擎 /Search/Default web searching engine - + baidu 百度 - + sougou 搜狗 - + 360 360 - + Block Folders 排除的文件夹 /Search/Block Folders - + Following folders will not be searched. You can set it by adding and removing folders. 搜索将不查看以下文件夹,通过添加和删除可以设置排除的文件夹位置 @@ -57,50 +56,96 @@ 添加 - + + delete 删除 - + + Directories 文件夹 - + + File Content Search + 搜索文本内容 + + + + precise Search + 精确搜索 + + + + show more results that match the keyword + 显示更多与输入内容匹配的搜索结果 + + + + Fuzzy Search + 模糊搜索 + + + + show the results that exactly match the keyword + 仅显示与输入内容完全一致的搜索结果 + + + + Search Folders + 搜索范围 + + + + Following folders will be searched. You can set it by adding and removing folders. + 以下文件的内容将出现在全局搜索的结果中 + + + select blocked folder 选择排除的文件夹 - + + Select 选择 - + + Position: 位置 - + + FileName: 文件名 - + + FileType: 类型 - + + Cancel 取消 - + + + + + Warning 警告 @@ -110,9 +155,8 @@ 添加失败,选择的路径为空! - Add blocked folder failed, it is not in home path! - 添加失败,添加的路径不在家目录下! + 添加失败,添加的路径不在家目录下! @@ -124,5 +168,35 @@ Add blocked folder failed, it has been already blocked! 添加失败,这个文件夹已经被添加过了! + + + select search folder + 选择要搜索的文件夹 + + + + Add search folder failed, choosen path is not supported currently! + 添加失败!暂不支持该目录! + + + + Add search folder failed, another path which is in the same device has been added! + 添加失败!文件夹位于重复挂载设备下,相同内容的文件夹已被添加! + + + + Add search folder failed, choosen path or its parent dir has been added! + 添加失败!该目录或其父目录已被添加! + + + + Add search folder failed, choosen path is in repeat mounted devices and another path which is in the same device has been added! + 添加失败!文件夹位于重复挂载设备下,且该设备另一个挂载点的文件夹已被添加! + + + + Add search folder failed, choosen path is not exists! + 添加失败!要添加的路径不存在! + diff --git a/ukui-search-service-dir-manager/dirwatcher/com.ukui.search.fileindex.server.xml b/ukui-search-service-dir-manager/dirwatcher/com.ukui.search.fileindex.server.xml index 6ee254f..014cdfc 100644 --- a/ukui-search-service-dir-manager/dirwatcher/com.ukui.search.fileindex.server.xml +++ b/ukui-search-service-dir-manager/dirwatcher/com.ukui.search.fileindex.server.xml @@ -2,9 +2,11 @@ + + diff --git a/ukui-search-service-dir-manager/dirwatcher/dir-watcher-adaptor.cpp b/ukui-search-service-dir-manager/dirwatcher/dir-watcher-adaptor.cpp index 953db27..ae5b25e 100644 --- a/ukui-search-service-dir-manager/dirwatcher/dir-watcher-adaptor.cpp +++ b/ukui-search-service-dir-manager/dirwatcher/dir-watcher-adaptor.cpp @@ -33,15 +33,19 @@ DirWatcherAdaptor::~DirWatcherAdaptor() // destructor } -void DirWatcherAdaptor::appendIndexableListItem(const QString &path) +int DirWatcherAdaptor::appendIndexableListItem(const QString &path) { // handle method call org.ukui.search.fileindex.appendIndexableListItem - QMetaObject::invokeMethod(parent(), "appendIndexableListItem", Q_ARG(QString, path)); + int out0; + QMetaObject::invokeMethod(parent(), "appendIndexableListItem", Q_RETURN_ARG(int, out0), Q_ARG(QString, path)); + return out0; } -void DirWatcherAdaptor::removeIndexableListItem(const QString &path) +bool DirWatcherAdaptor::removeIndexableListItem(const QString &path) { // handle method call org.ukui.search.fileindex.removeIndexableListItem - QMetaObject::invokeMethod(parent(), "removeIndexableListItem", Q_ARG(QString, path)); + bool out0; + QMetaObject::invokeMethod(parent(), "removeIndexableListItem", Q_RETURN_ARG(bool, out0), Q_ARG(QString, path)); + return out0; } diff --git a/ukui-search-service-dir-manager/dirwatcher/dir-watcher-adaptor.h b/ukui-search-service-dir-manager/dirwatcher/dir-watcher-adaptor.h index 062a383..be4c3d8 100644 --- a/ukui-search-service-dir-manager/dirwatcher/dir-watcher-adaptor.h +++ b/ukui-search-service-dir-manager/dirwatcher/dir-watcher-adaptor.h @@ -33,9 +33,11 @@ class DirWatcherAdaptor: public QDBusAbstractAdaptor Q_CLASSINFO("D-Bus Introspection", "" " \n" " \n" +" \n" " \n" " \n" " \n" +" \n" " \n" " \n" " \n" @@ -46,8 +48,8 @@ public: public: // PROPERTIES public Q_SLOTS: // METHODS - void appendIndexableListItem(const QString &path); - void removeIndexableListItem(const QString &path); + int appendIndexableListItem(const QString &path); + bool removeIndexableListItem(const QString &path); Q_SIGNALS: // SIGNALS }; diff --git a/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp b/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp index 1a8747f..6c3e461 100644 --- a/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp +++ b/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp @@ -56,11 +56,25 @@ DirWatcher *DirWatcher::getDirWatcher() QStringList DirWatcher::currentIndexableDir() { QMutexLocker locker(&s_mutex); + this->updateIndexableDirs(); + return m_indexableDirList; +} + +void DirWatcher::updateIndexableDirs() +{ m_qSettings->beginGroup(INDEXABLE_DIR_VALUE); m_indexableDirList = m_qSettings->value(INDEXABLE_DIR_VALUE).toStringList(); m_qSettings->endGroup(); QStringList indexableDirs = m_indexableDirList; - return indexableDirs; + for (const QString& dir : m_indexableDirList) { + if (!QFileInfo(dir).isDir()) { + indexableDirs.removeAll(dir); + } + } + m_qSettings->beginGroup(INDEXABLE_DIR_VALUE); + m_qSettings->setValue(INDEXABLE_DIR_VALUE, indexableDirs); + m_qSettings->endGroup(); + m_indexableDirList = indexableDirs; } QStringList DirWatcher::currentBlackListOfIndex() @@ -70,28 +84,30 @@ QStringList DirWatcher::currentBlackListOfIndex() return blackListOfIndex; } -void DirWatcher::handleIndexItemAppend(const QString &path, QStringList &blackList) +bool DirWatcher::handleIndexItemAppend(const QString &path, QStringList &blackList) { //排除要添加的路径已被索引的情况 if (m_indexableDirList.contains(path)) { qDebug() << QString("index path %1 is already added.").arg(path); - return; + return false; } //处理添加路径非根目录时,要添加索引的路径与已索引路径为父子关系的情况 if (path != "/") { QString indexablePath; + QStringList tmp = m_indexableDirList; for (int i = 0; i < m_indexableDirList.length(); i++) { indexablePath = m_indexableDirList.at(i); if (path.startsWith(indexablePath + "/")) { qCritical() << QString("The parent of the path:%1 has been added.").arg(path); - return; + return false; } if (indexablePath.startsWith(path + "/")) { - m_indexableDirList.removeAll(indexablePath); + tmp.removeAll(indexablePath); blackList.append(indexablePath); } } + m_indexableDirList = tmp; } m_indexableDirList << path; @@ -101,20 +117,22 @@ void DirWatcher::handleIndexItemAppend(const QString &path, QStringList &blackLi blackList.removeDuplicates(); Q_EMIT this->appendIndexItem(path, blackList); qDebug() << "index path:" << path << "blacklist:" << blackList; + return true; } -void DirWatcher::handleIndexItemRemove(const QString &path) +bool DirWatcher::handleIndexItemRemove(const QString &path) { this->currentIndexableDir(); QMutexLocker locker(&s_mutex); if (!m_indexableDirList.contains(path)) { qWarning() << QString("The path: %1 is not indexed").arg(path); - return; + return false; } m_indexableDirList.removeAll(path); m_qSettings->beginGroup(INDEXABLE_DIR_VALUE); m_qSettings->setValue(INDEXABLE_DIR_VALUE, m_indexableDirList); m_qSettings->endGroup(); + return true; } /** @@ -278,13 +296,24 @@ void DirWatcher::mountRemoveCallback(GVolumeMonitor *monitor, GMount *gmount, Di g_object_unref(rootFile); } -void DirWatcher::appendIndexableListItem(const QString &path) +int DirWatcher::appendIndexableListItem(const QString &path) { + int resultCode = 1; + /* code: + * 1: successful + * -1: path or its parent dir has been added + * -2: path is or under blacklist + * -3: path is in repeat mounted devices and another path which is in the same device has been indexed + * -4: another path which is in the same device has been indexed + * -5: path is not exists + */ + //排除path不存在的情况 QFile file(path); if (!file.exists()) { qWarning() << QString("target path:%1 is not exists!").arg(path); - return; + resultCode = -5; + return resultCode; } //同步配置文件中的已索引目录 @@ -296,15 +325,18 @@ void DirWatcher::appendIndexableListItem(const QString &path) //根目录特殊处理 if (path == "/") { - this->handleIndexItemAppend(path, m_blackListOfIndex); - return; + if (!this->handleIndexItemAppend(path, m_blackListOfIndex)) { + resultCode = -1; + } + return resultCode; } //处理要添加索引的路径与索引黑名单中路径为父子关系的情况 for (const QString& blackListPath : m_blackListOfIndex) { if (path.startsWith(blackListPath + "/") or path == blackListPath) { qCritical() << QString("path:%1 is or under the blacklistpath:%2.").arg(path, blackListPath); - return; + resultCode = -2; + return resultCode; } if (blackListPath.startsWith(path + "/")) { @@ -348,7 +380,8 @@ void DirWatcher::appendIndexableListItem(const QString &path) //(1)(4)直接返回 if (pathToBeAddedIsRepeatedDevice and addedPathHasRepeatedDevice) { qCritical() << "current path is in repeat mounted devices and another path which is in the same device has been indexed!"; - return; + resultCode = -3; + return resultCode; } //(2)(4)将要添加索引目录相应的重复挂载路径添加到黑名单 if (pathToBeAddedHasRepeatedDevice and addedPathHasRepeatedDevice) { @@ -360,7 +393,8 @@ void DirWatcher::appendIndexableListItem(const QString &path) QString pathAfterReplace = repeatedDir + addedRelativeDir; if (path.startsWith(pathAfterReplace) or path == pathAfterReplace) { qCritical() << QString("another path:%1 which is in the same device has been indexed").arg(pathAfterReplace); - return; + resultCode = -4; + return resultCode; } else { blackList.append(pathAfterReplace); break; @@ -405,19 +439,26 @@ void DirWatcher::appendIndexableListItem(const QString &path) if (path.startsWith(spec + "/") or path == spec) { tmp.replace(0, spec.length(), mountPoint); - this->handleIndexItemAppend(tmp, blackList); - qDebug() << QString("The path:%1 has been replaced into %2").arg(path, tmp); - return; + if (this->handleIndexItemAppend(tmp, blackList)) { + qDebug() << QString("The path:%1 has been replaced into %2").arg(path, tmp); + } else { + resultCode = -1; + } + return resultCode; } } - this->handleIndexItemAppend(path, blackList); + if (!this->handleIndexItemAppend(path, blackList)) { + resultCode = -1; + } + return resultCode; } -void DirWatcher::removeIndexableListItem(const QString &path) +bool DirWatcher::removeIndexableListItem(const QString &path) { - this->handleIndexItemRemove(path); + bool res = this->handleIndexItemRemove(path); Q_EMIT this->removeIndexItem(path); + return res; } void DirWatcher::initData() @@ -437,9 +478,11 @@ void DirWatcher::initData() */ //将磁盘分区后其他分区都会挂载到media下,多块硬盘也会挂到media,因此media放开,mnt同理; //backup是备份文件,tmp是临时文件,也都放开 - m_blackListOfIndex << "/boot" << "/bin" << "/dev" << "/etc" << "/usr" << "/var" - << "/lib" << "/lib32" << "/lib64" << "/libx32" << "/cdrom" - << "/sys" << "/proc" << "/srv" << "/sbin" << "/run" << "/opt"; +// m_blackListOfIndex << "/boot" << "/bin" << "/dev" << "/etc" << "/usr" << "/var" +// << "/lib" << "/lib32" << "/lib64" << "/libx32" << "/cdrom" +// << "/sys" << "/proc" << "/srv" << "/sbin" << "/run" << "/opt"; + //专用机需求:只屏蔽/proc, /sys, /dev, /tmp, /run + m_blackListOfIndex << "/proc" << "/sys" << "/dev" << "/tmp" << "/run"; //目前方案:可搜索目录(服务)默认根目录,可搜索目录(应用)默认家目录和/data目录 QDir dir("/data"); @@ -553,13 +596,13 @@ void DirWatcher::handleDisk() } } - //将u盘设备在/data和/home下的所有挂载点添加到索引黑名单, 目前由于未知原因收不到udisk挂载信号因此暂时不生效 + //将u盘设备添加到索引黑名单 if (!m_currentUDiskDeviceInfo.isEmpty()) { for (auto t = m_currentUDiskDeviceInfo.constBegin(); t != m_currentUDiskDeviceInfo.constEnd(); t++) { for (QString udiskDevice: t.value()) { - if (udiskDevice.startsWith("/data") || udiskDevice.startsWith("/home")) { +// if (udiskDevice.startsWith("/data") || udiskDevice.startsWith("/home")) { m_blackListOfIndex.append(udiskDevice); - } +// } } } } diff --git a/ukui-search-service-dir-manager/dirwatcher/dir-watcher.h b/ukui-search-service-dir-manager/dirwatcher/dir-watcher.h index ffe6016..d702c23 100644 --- a/ukui-search-service-dir-manager/dirwatcher/dir-watcher.h +++ b/ukui-search-service-dir-manager/dirwatcher/dir-watcher.h @@ -48,19 +48,32 @@ public Q_SLOTS: QStringList searchableDirForSearchApplication(); QStringList blackListOfDir(const QString &dirPath); - Q_SCRIPTABLE void appendIndexableListItem(const QString &path); - Q_SCRIPTABLE void removeIndexableListItem(const QString &path); + /** + * @brief DirWatcher::appendIndexableListItem + * add a item to indexable dirs + * @param path: the path to be added to the index dirs list + * @return int: the result code + * 1: successful + * -1: path or its parent dir has been added + * -2: path is or under blacklist + * -3: path is in repeat mounted devices and another path which is in the same device has been indexed + * -4: another path which is in the same device has been indexed + * -5: path is not exists + */ + Q_SCRIPTABLE int appendIndexableListItem(const QString &path); + Q_SCRIPTABLE bool removeIndexableListItem(const QString &path); private: DirWatcher(QObject *parent = nullptr); ~DirWatcher(); void initData(); void initDiskWatcher(); + void updateIndexableDirs(); void handleDisk(); - void handleIndexItemAppend(const QString &path, QStringList &blackList); - void handleIndexItemRemove(const QString &path); + bool handleIndexItemAppend(const QString &path, QStringList &blackList); + bool handleIndexItemRemove(const QString &path); static QMutex s_mutex; diff --git a/ukui-search-service-dir-manager/ukui-search-dir-manager-dbus.cpp b/ukui-search-service-dir-manager/ukui-search-dir-manager-dbus.cpp index 868e146..344f4c0 100644 --- a/ukui-search-service-dir-manager/ukui-search-dir-manager-dbus.cpp +++ b/ukui-search-service-dir-manager/ukui-search-dir-manager-dbus.cpp @@ -46,6 +46,8 @@ void UkuiSearchDirManagerDbus::parseCmd(QString msg, bool isPrimary) parser.addOption(quitOption); if (isPrimary) { + const QStringList args = QString(msg).split(' '); + parser.process(args); if (parser.isSet(quitOption)) { qApp->quit(); return;