ukui-search/libsearch/notesearch/note-search-plugin.cpp

257 lines
8.8 KiB
C++
Raw Normal View History

2023-04-11 10:19:35 +08:00
/*
*
* 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/>.
*
*
*/
2021-08-26 08:20:38 +08:00
#include "note-search-plugin.h"
#include <QWidget>
#include <QLabel>
2021-09-08 15:31:47 +08:00
#include <qt5xdg/xdgicon.h>
2023-04-19 15:14:36 +08:00
#include <QDebug>
2021-08-26 08:20:38 +08:00
#include "file-utils.h"
#include "chinese-segmentation.h"
2021-12-14 14:43:35 +08:00
using namespace UkuiSearch;
2021-08-26 08:20:38 +08:00
NoteSearchPlugin::NoteSearchPlugin(QObject *parent)
{
Q_UNUSED(parent)
2021-08-26 08:20:38 +08:00
g_uniqueSymbol = 0;
SearchPluginIface::Actioninfo open { 0, tr("Open")};
m_actionInfo << open;
m_pool.setMaxThreadCount(1);
m_pool.setExpiryTimeout(1000);
initDetailPage();
}
const QString NoteSearchPlugin::name()
{
return "Note Search";
2021-08-26 08:20:38 +08:00
}
const QString NoteSearchPlugin::description()
{
return tr("Note Search.");
}
QString NoteSearchPlugin::getPluginName()
{
return tr("Note Search");
}
void NoteSearchPlugin::KeywordSearch(QString keyword, DataQueue<SearchPluginIface::ResultInfo> *searchResult)
{
g_mutex.lock();
++g_uniqueSymbol;
g_mutex.unlock();
m_keyword = keyword;
2021-08-26 08:20:38 +08:00
NoteSearch *ns = new NoteSearch(searchResult, keyword, g_uniqueSymbol);
m_pool.start(ns);
}
void NoteSearchPlugin::stopSearch()
{
g_mutex.lock();
++g_uniqueSymbol;
g_mutex.unlock();
}
2021-08-26 08:20:38 +08:00
QList<SearchPluginIface::Actioninfo> NoteSearchPlugin::getActioninfo(int type)
{
Q_UNUSED(type)
2021-08-26 08:20:38 +08:00
return m_actionInfo;
}
void NoteSearchPlugin::openAction(int actionkey, QString key, int type)
{
Q_UNUSED(type)
2021-08-26 08:20:38 +08:00
switch (actionkey) {
case 0:
2022-05-10 13:49:54 +08:00
{
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<bool> reply = appLaunchInterface->call("LaunchAppWithArguments", "ukui-notebook.desktop", QStringList() << "--show" << key);
if(reply.isValid()) {
res = reply;
} else {
qWarning() << "SoftWareCenter dbus called failed!";
res = false;
}
}
if(appLaunchInterface) {
delete appLaunchInterface;
}
appLaunchInterface = NULL;
if (res)
break;
QProcess process;
process.startDetached("ukui-notebook", {"--show", key});
2021-08-26 08:20:38 +08:00
break;
2022-05-10 13:49:54 +08:00
}
2021-08-26 08:20:38 +08:00
default:
break;
}
}
QWidget *NoteSearchPlugin::detailPage(const SearchPluginIface::ResultInfo &ri)
{
m_currentActionKey = ri.actionKey;
m_iconLabel->setPixmap(ri.icon.pixmap(120, 120));
QFontMetrics fontMetrics = m_nameLabel->fontMetrics();
QString showname = fontMetrics.elidedText(ri.name, Qt::ElideRight, 215); //当字体长度超过215时显示为省略号
m_nameLabel->setText(FileUtils::setAllTextBold(showname));
2021-08-26 08:20:38 +08:00
if(QString::compare(showname, ri.name)) {
m_nameLabel->setToolTip(ri.name);
} else {
m_nameLabel->setToolTip("");
2021-08-26 08:20:38 +08:00
}
m_pluginLabel->setText(tr("Application"));
QString showDesc = fontMetrics.elidedText(/*ri.description.at(0).key + " " + */ri.description.at(0).value, Qt::ElideRight, m_descLabel->width() * 2); //当字体长度超过215时显示为省略号
m_descLabel->setText(FileUtils::getHtmlText(FileUtils::wrapData(m_descLabel, showDesc), m_keyword));
2021-08-26 08:20:38 +08:00
m_descFrame->show();
m_line_2->show();
return m_detailPage;
}
void NoteSearchPlugin::initDetailPage()
{
m_detailPage = new QWidget();
m_detailPage->setFixedWidth(360);
m_detailPage->setAttribute(Qt::WA_TranslucentBackground);
m_detailLyt = new QVBoxLayout(m_detailPage);
m_detailLyt->setContentsMargins(8, 0, 16, 0);
m_iconLabel = new QLabel(m_detailPage);
m_iconLabel->setAlignment(Qt::AlignCenter);
m_iconLabel->setFixedHeight(128);
m_nameFrame = new QFrame(m_detailPage);
m_nameFrameLyt = new QHBoxLayout(m_nameFrame);
m_nameFrame->setLayout(m_nameFrameLyt);
m_nameFrameLyt->setContentsMargins(8, 0, 0, 0);
m_nameLabel = new QLabel(m_nameFrame);
m_nameLabel->setMaximumWidth(280);
m_pluginLabel = new QLabel(m_nameFrame);
m_pluginLabel->setText(tr("Note Search"));
m_pluginLabel->setEnabled(false);
m_nameFrameLyt->addWidget(m_nameLabel);
m_nameFrameLyt->addStretch();
m_nameFrameLyt->addWidget(m_pluginLabel);
m_line_1 = new SeparationLine(m_detailPage);
2021-08-26 08:20:38 +08:00
m_descFrame = new QFrame(m_detailPage);
m_descFrameLyt = new QVBoxLayout(m_descFrame);
m_descLabel = new QLabel(m_descFrame);
m_descLabel->setTextFormat(Qt::AutoText);
2021-08-26 08:20:38 +08:00
m_descLabel->setWordWrap(true);
m_descFrameLyt->addWidget(m_descLabel);
m_descFrame->setLayout(m_descFrameLyt);
m_descFrameLyt->setContentsMargins(8, 0, 0, 0);
m_line_2 = new SeparationLine(m_detailPage);
2021-08-26 08:20:38 +08:00
m_actionFrame = new QFrame(m_detailPage);
m_actionFrameLyt = new QVBoxLayout(m_actionFrame);
m_actionFrameLyt->setContentsMargins(8, 0, 0, 0);
m_actionLabel1 = new ActionLabel(tr("Open"), m_currentActionKey, m_actionFrame);
m_actionFrameLyt->addWidget(m_actionLabel1);;
m_actionFrame->setLayout(m_actionFrameLyt);
m_detailLyt->addSpacing(50);
m_detailLyt->addWidget(m_iconLabel);
m_detailLyt->addWidget(m_nameFrame);
m_detailLyt->addWidget(m_line_1);
m_detailLyt->addWidget(m_descFrame);
m_detailLyt->addWidget(m_line_2);
m_detailLyt->addWidget(m_actionFrame);
m_detailPage->setLayout(m_detailLyt);
m_detailLyt->addStretch();
connect(m_actionLabel1, &ActionLabel::actionTriggered, [ & ](){
openAction(0, m_currentActionKey, 0);
});
}
NoteSearch::NoteSearch(DataQueue<SearchPluginIface::ResultInfo> *searchResult, const QString &keyword, size_t uniqueSymbol) {
this->setAutoDelete(true);
this->m_searchResult = searchResult;
this->m_keyword = keyword;
this->m_uniqueSymbol = uniqueSymbol;
}
void NoteSearch::run() {
2023-03-20 15:21:58 +08:00
std::vector<KeyWord> sKeyWordVec = ChineseSegmentation::getInstance()->callSegment(m_keyword);
2021-08-26 08:20:38 +08:00
QStringList keywordList;
for (KeyWord sKeyWord : sKeyWordVec) {
2021-08-26 08:20:38 +08:00
keywordList.append(QString::fromStdString(sKeyWord.word));
}
QDBusInterface qi("org.ukui.note", "/org/ukui/note", "org.ukui.note.interface", QDBusConnection::sessionBus());
QDBusReply<QVariantMap> reply = qi.call("keywordMatch", keywordList);
if(reply.isValid()) {
if (m_uniqueSymbol ^ g_uniqueSymbol) {
qDebug() << m_uniqueSymbol << g_uniqueSymbol;
return;
} else {
qDebug() << m_uniqueSymbol << g_uniqueSymbol;
for (std::pair<QString, QVariant> it : reply.value().toStdMap()) {
qDebug() << it.first;
qDebug() << it.second;
qDebug() << it.second.value<QDBusArgument>().currentType();
QDBusArgument dbusArgs = it.second.value<QDBusArgument>();
QStringList str;
dbusArgs.beginArray();
while (!dbusArgs.atEnd()) {
QVariant tmp;
dbusArgs >> tmp;
str.append(tmp.toString());
}
dbusArgs.endArray();
qDebug() << str;
SearchPluginIface::ResultInfo ri(
XdgIcon::fromTheme("kylin-notebook", QIcon(":/res/icons/desktop.png")),
str.at(1),
QVector<SearchPluginIface::DescriptionInfo>() << SearchPluginIface::DescriptionInfo {
2021-08-26 08:20:38 +08:00
key : QString(tr("Note Description:")),
value : str.at(0)
},
it.first
);
if (m_uniqueSymbol ^ g_uniqueSymbol) {
qDebug() << m_uniqueSymbol << g_uniqueSymbol;
return;
} else {
m_searchResult->enqueue(ri);
}
2021-08-26 08:20:38 +08:00
}
}
} else {
qWarning() << "Note dbus called failed!" << qi.lastError();
}
}