ukui-search/search-ukcc-plugin/search.cpp

837 lines
33 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
*
* 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 <https://www.gnu.org/licenses/>.
*
*
*/
#include "search.h"
#include <QTranslator>
#include <QApplication>
Search::Search()
{
QTranslator* translator = new QTranslator(this);
if(!translator->load("/usr/share/ukui-search/search-ukcc-plugin/translations/" + QLocale::system().name())) {
qWarning() << "/usr/share/ukui-search/search-ukcc-plugin/translations/" + QLocale::system().name() << "load failed";
}
QApplication::installTranslator(translator);
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);
if (m_interface->isValid()) {
connect(m_interface, SIGNAL(indexDirsChanged()), this, SLOT(refreshSearchDirsUi()));
}
m_setSearchDirInterface = new QDBusInterface("com.ukui.search.fileindex.service",
"/org/ukui/search/fileindex",
"org.ukui.search.fileindex",
QDBusConnection::sessionBus(),
this);
const QByteArray id(UKUI_SEARCH_SCHEMAS);
if (QGSettings::isSchemaInstalled(id)) {
m_gsettings = new QGSettings(id, QByteArray(), this);
} else {
qCritical() << UKUI_SEARCH_SCHEMAS << " not installed!\n";
}
}
QString Search::plugini18nName()
{
return m_plugin_name;
}
int Search::pluginTypes()
{
return m_plugin_type;
}
QWidget *Search::pluginUi()
{
initUi();
initFileDialog();
initSearchDirs();
initBlockDirsList();
connect(m_addSearchDirBtn, &QPushButton::clicked, this, &Search::onAddSearchDirBtnClicked);
connect(m_addBlockDirWidget, &QPushButton::clicked, this, &Search::onBtnAddBlockFolderClicked);
if (m_gsettings) {
//按钮状态初始化
if (m_gsettings->keys().contains(SEARCH_METHOD_KEY)) {
//当前是否使用索引搜索/暴力搜索
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();
m_webEngineFrame->mCombox->setCurrentIndex(m_webEngineFrame->mCombox->findData(engine));
} 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) {
bool isIndexSearchOn = m_gsettings->get(SEARCH_METHOD_KEY).toBool();
m_searchMethodBtn->blockSignals(true);
m_searchMethodBtn->setChecked(isIndexSearchOn);
m_searchMethodBtn->blockSignals(false);
if (isIndexSearchOn) {
m_indexSetFrame->show();
} else {
m_indexSetFrame->hide();
}
} else if (key == WEB_ENGINE_KEY) {
QString engine = m_gsettings->get(WEB_ENGINE_KEY).toString();
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);
}
});
connect(m_indexMethodBtnGroup, QOverload<int, bool>::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<int>::of(&QComboBox::currentIndexChanged), this, [=] {
if (m_gsettings && m_gsettings->keys().contains(WEB_ENGINE_KEY)) {
m_gsettings->set(WEB_ENGINE_KEY, m_webEngineFrame->mCombox->currentData().toString());
}
});
} else {
qCritical() << "Gsettings of the search plugin for ukcc is not initialized!";
m_searchMethodBtn->setEnabled(false);
m_webEngineFrame->mCombox->setEnabled(false);
}
return m_pluginWidget;
}
const QString Search::name() const
{
return QStringLiteral("Search");
}
QString Search::translationPath() const
{
return QStringLiteral("/usr/share/ukui-search/search-ukcc-plugin/translations/%1.ts");
}
bool Search::isShowOnHomePage() const
{
return true;
}
QIcon Search::icon() const
{
return QIcon::fromTheme("search-symbolic");
}
bool Search::isEnable() const
{
return true;
}
/**
* @brief Search::initUi 初始化此插件UI
*/
void Search::initUi()
{
m_pluginWidget = new QWidget;
m_pluginWidget->setAttribute(Qt::WA_DeleteOnClose);
m_mainLyt = new QVBoxLayout(m_pluginWidget);
m_pluginWidget->setLayout(m_mainLyt);
m_titleLabel = new TitleLabel(m_pluginWidget);
//~ contents_path /Search/Search
m_titleLabel->setText(tr("Search"));
m_mainLyt->addWidget(m_titleLabel);
//设置网页搜索引擎部分的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);
//索引开关UI
m_searchMethodFrame = new QFrame(m_setFrame);
m_searchMethodFrame->setMinimumWidth(550);
m_searchMethodLyt = new QHBoxLayout(m_searchMethodFrame);
m_searchMethodLyt->setContentsMargins(16, 18, 16, 21);
m_searchMethodFrame->setLayout(m_searchMethodLyt);
m_descFrame = new QFrame(m_searchMethodFrame);
m_descFrameLyt = new QVBoxLayout(m_descFrame);
m_descFrameLyt->setContentsMargins(0, 0, 0, 0);
m_descFrame->setLayout(m_descFrameLyt);
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);
m_descFrameLyt->addWidget(m_descLabel1);
m_descFrameLyt->addWidget(m_descLabel2);
m_searchMethodBtn = new kdk::KSwitchButton(m_searchMethodFrame);
m_searchMethodLyt->addWidget(m_descFrame);
m_searchMethodLyt->addStretch();
m_searchMethodLyt->addWidget(m_searchMethodBtn);
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);
//设置索引模式的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);
//~ contents_path /Search/File Content Search
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);
//~ contents_path /Search/Search Folders
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);
//~ contents_path /Search/Block Folders
m_blockDirTitleLabel->setText(tr("Block Folders"));
m_blockDirDescLabel = new QLabel(m_pluginWidget);
m_blockDirDescLabel->setContentsMargins(16, 0, 0, 0); //TitleLabel自带16边距,QLabel需要自己设
m_blockDirDescLabel->setEnabled(false);
m_blockDirDescLabel->setWordWrap(true);
m_blockDirDescLabel->setText(tr("Following folders will not be searched. You can set it by adding and removing folders."));
m_blockDirsFrame = new QFrame(m_pluginWidget);
m_blockDirsFrame->setFrameShape(QFrame::Shape::Box);
m_blockDirsLyt = new QVBoxLayout(m_blockDirsFrame);
m_blockDirsLyt->setDirection(QBoxLayout::BottomToTop);
m_blockDirsFrame->setLayout(m_blockDirsLyt);
m_blockDirsLyt->setContentsMargins(0, 0, 0, 0);
m_blockDirsLyt->setSpacing(2);
QFrame * m_addBlockDirFrame = new QFrame(m_blockDirsFrame);
m_addBlockDirFrame->setFrameShape(QFrame::Shape::NoFrame);
m_addBlockDirFrame->setFixedHeight(60);
// m_addBlockDirWidget = new QPushButton(m_addBlockDirFrame);
m_addBlockDirWidget = new AddBtn(m_addBlockDirFrame);
// m_addBlockDirWidget->setFixedHeight(60);
// m_addBlockDirWidget->setObjectName("addBlockDirWidget");
// QPalette pal;
// QBrush brush = pal.highlight(); //获取window的色值
// QColor highLightColor = brush.color();
// QString stringColor = QString("rgba(%1,%2,%3)") //叠加20%白色
// .arg(highLightColor.red()*0.8 + 255*0.2)
// .arg(highLightColor.green()*0.8 + 255*0.2)
// .arg(highLightColor.blue()*0.8 + 255*0.2);
// m_addBlockDirWidget->setStyleSheet(QString("HoverWidget#addBlockDirWidget{background: palette(button);\
// border-radius: 4px;}\
// HoverWidget:hover:!pressed#addBlockDirWidget{background: %1; \
// border-radius: 4px;}").arg(stringColor));
//之前自己写的添加按钮目前方案用控制面板提供的AddBtn后面有变动再放出来(属性全都注掉了有问题找ukcc寻求帮助
// m_addBlockDirWidget->setProperty("useButtonPalette", true);
//// m_addBlockDirWidget->setStyleSheet("QPushButton:!checked{background: palette(base);}");
// m_addBlockDirWidget->setFlat(true);
// m_addBlockDirIcon = new QLabel(m_addBlockDirWidget);
// m_addBlockDirIcon->setPixmap(QIcon("/usr/share/ukui-search/search-ukcc-plugin/image/add.svg").pixmap(12, 12));
// m_addBlockDirIcon->setProperty("useIconHighlightEffect", true);
// m_addBlockDirIcon->setProperty("iconHighlightEffectMode", 1);
// m_addBlockDirLabel = new QLabel(m_addBlockDirWidget);
// m_addBlockDirLabel->setText(tr("Choose folder"));
// m_addBlockDirLyt = new QHBoxLayout(m_addBlockDirWidget);
// m_addBlockDirWidget->setLayout(m_addBlockDirLyt);
m_blockDirsLyt->addWidget(m_addBlockDirWidget);
// m_addBlockDirLyt->addStretch();
// m_addBlockDirLyt->addWidget(m_addBlockDirIcon);
// m_addBlockDirLyt->addWidget(m_addBlockDirLabel);
// m_addBlockDirLyt->addStretch();
m_mainLyt->addSpacing(32);
m_mainLyt->addWidget(m_blockDirTitleLabel);
m_mainLyt->addWidget(m_blockDirDescLabel);
m_mainLyt->addWidget(m_blockDirsFrame);
m_mainLyt->addStretch();
m_mainLyt->setContentsMargins(0, 0, 0, 0);
}
void Search::refreshSearchDirsUi()
{
qWarning() << "==========refreshUi!!!!";
while (m_searchDirLyt->count() - 1) {
QWidget *widget = m_searchDirLyt->itemAt(m_searchDirLyt->count() - 1)->widget();
m_searchDirLyt->removeWidget(widget);
widget->deleteLater();
}
initSearchDirs();
}
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::Successful :
qDebug() << "Add blocked folder succeed! path = " << selectedDir;
getBlockDirs();
break;
case ReturnCode::Duplicated :
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 has been added!"));
break;
case ReturnCode::NotExists :
qWarning() << "Add blocked folder failed, it's not exist! path = " << selectedDir;
QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add blocked folder failed, choosen path is not 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 already been blocked!"));
break;
case ReturnCode::Hidden :
qWarning() << "Add blocked folder failed, it has been hidden! path = " << selectedDir;
QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add search folder failed, hidden path is not supported!"));
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 ReturnCode::Successful:
qDebug() << "Add search folder succeed! path = " << selectedDir;
break;
case ReturnCode::Duplicated:
QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add search folder failed, choosen path or its parent dir has been added!"));
break;
case ReturnCode::UnderBlackList:
QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add search folder failed, choosen path is not supported currently!"));
break;
case ReturnCode::RepeatMount1:
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 ReturnCode::RepeatMount2:
QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add search folder failed, another path which is in the same device has been added!"));
break;
case ReturnCode::NotExists:
QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add search folder failed, choosen path is not exists!"));
break;
case ReturnCode::Hidden :
QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add search folder failed, hidden path is not supported!"));
break;
case ReturnCode::PermissionDenied:
QMessageBox::warning(m_pluginWidget, tr("Warning"), tr("Add search folder failed, permission denied!"));
break;
default:
break;
}
}
});
}
/**
* @brief Search::getBlockDirs 从配置文件获取黑名单并将黑名单列表传入
*/
void Search::getBlockDirs()
{
if (m_interface->isValid()) {
QDBusReply<QStringList> reply = m_interface->call("blockDirsForUser");
if (reply.isValid()) {
m_blockDirs = reply.value();
}
}
}
/**
* @brief Search::setBlockDir 尝试写入新的黑名单文件夹
* @param dirPath 待添加到黑名单的文件夹路径
* @param is_add 是否是在添加黑名单
* @return 0成功 !0添加失败的错误代码
*/
int Search::setBlockDir(const QString &dirPath, const bool &is_add)
{
if (!QFile::exists(dirPath)) {
return ReturnCode::NotExists;
}
QStringList pathSections = dirPath.split("/");
for (const QString &section : pathSections) {
if (section.startsWith(".")) {
return ReturnCode::Hidden;
}
}
if (!m_interface->isValid()) {
return ReturnCode::DirWatcherError;
}
if (!is_add) {
//删除黑名单目录
m_interface->call("removeBlockDirOfUser", dirPath);
removeBlockDirFromList(dirPath);
return ReturnCode::Successful;
}
QStringList oldBlockList = m_blockDirs;
getBlockDirs();
for (const QString& oldBlockDir : oldBlockList) {
if (!m_blockDirs.contains(oldBlockDir)) {
removeBlockDirFromList(oldBlockDir);
}
}
for (QString dir : m_blockDirs) {
if (dirPath == dir) {
return ReturnCode::HasBeenBlocked;
}
if (dirPath.startsWith(dir + "/") || dir == "/") {
return ReturnCode::Duplicated;
}
//有它的子文件夹已被添加,删除这些子文件夹
if (dir.startsWith(dirPath + "/") || dirPath == "/") {
removeBlockDirFromList(dir);
}
}
m_interface->call("addBlockDirOfUser", dirPath);
appendBlockDirToList(dirPath);
return ReturnCode::Successful;
}
/**
* @brief Search::initBlockDirsList 初始化黑名单列表
*/
void Search::initBlockDirsList()
{
getBlockDirs();
for (QString path: m_blockDirs) {
if (QFileInfo(path).isDir() /*&& path.startsWith("home")*/) {
appendBlockDirToList(path);
}
}
}
void Search::initSearchDirs()
{
if (m_interface->isValid()) {
QDBusReply<QStringList> 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)
{
QFileInfo info(dirPath);
if (!(info.isExecutable() && info.isReadable())) {
return ReturnCode::PermissionDenied;
}
if (!m_setSearchDirInterface->isValid()) {
return ReturnCode::DirWatcherError;
}
QStringList pathSections = dirPath.split("/");
for (const QString &section : pathSections) {
if (section.startsWith(".")) {
return ReturnCode::Hidden;
}
}
if (isAdd) {
QDBusReply<QStringList> indexDirsRpl = m_interface->call("currentIndexableDir");
QStringList indexDirs;
if (indexDirsRpl.isValid()) {
indexDirs = indexDirsRpl.value();
}
QDBusReply<int> appendIndexRpl = m_setSearchDirInterface->call("appendIndexableListItem", dirPath);
if (appendIndexRpl.isValid()) {
if (appendIndexRpl.value() == 0) {
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<bool> reply = m_setSearchDirInterface->call("removeIndexableListItem", dirPath);
if (reply.isValid()) {
if (reply.value()) {
this->removeSearchDirFromList(dirPath);
}
return reply.value();
}
}
return ReturnCode::Successful;
}
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<HoverWidget *>(path);
if (delDirWidget) {
m_searchDirLyt->removeWidget(delDirWidget);
delDirWidget->deleteLater();
qDebug() << "Delete folder of search succeed! path = " << path;
}
QFrame *line = m_searchDirsFrame->findChild<QFrame*>(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);
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);
// dirWidgetLyt->addWidget(delBtn);
QFrame *line = new QFrame(m_blockDirsFrame);
line->setObjectName(path);
line->setFixedHeight(1);
line->setLineWidth(0);
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);
// m_setFrameLyt->addWidget(line);
m_blockDirsLyt->addWidget(line);
m_blockDirsLyt->addWidget(dirWidget);
connect(delBtn, &QPushButton::clicked, this, [ = ]() {
setBlockDir(path, false);
getBlockDirs();
});
connect(dirWidget, &HoverWidget::enterWidget, this, [ = ]() {
delBtn->show();
});
connect(dirWidget, &HoverWidget::leaveWidget, this, [ = ]() {
delBtn->hide();
});
}
void Search::removeBlockDirFromList(const QString &path)
{
HoverWidget * delDirWidget = m_blockDirsFrame->findChild<HoverWidget *>(path);
if (delDirWidget) {
qDebug() << "Delete blocked folder succeed! path = " << path;
m_blockDirsLyt->removeWidget(delDirWidget);
delDirWidget->deleteLater();
}
QFrame *line = m_blockDirsFrame->findChild<QFrame*>(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::onBtnAddBlockFolderClicked);
}
void Search::onBtnAddBlockFolderClicked()
{
// m_blockDirDialog->open();
m_blockDirDialog->exec();
}
void Search::onAddSearchDirBtnClicked()
{
//open()会导致cpu占用拉满
// m_searchDirDialog->open();
m_searchDirDialog->exec();
}