Modify settings search, use dbus interface of ukcc instead of xml file to get data.

This commit is contained in:
baijunjie 2022-11-29 20:07:53 +08:00 committed by iaom
parent d684692f51
commit c3f18f80d4
2 changed files with 131 additions and 4 deletions

View File

@ -2,8 +2,8 @@
#include <QDomDocument>
#include <QProcessEnvironment>
#include <QWidget>
#include <QDBusInterface>
#include <QDBusReply>
#include <QDBusArgument>
#include "settings-search-plugin.h"
#include "file-utils.h"
@ -193,7 +193,101 @@ SettingsMatch *SettingsMatch::getInstance()
void SettingsMatch::run()
{
this->initDataOfXml();
// this->initDataOfXml();
this->initData();
}
SettingsMatch::SettingsMatch()
{
m_interface = new QDBusInterface("org.ukui.ukcc.session",
"/",
"org.ukui.ukcc.session.interface", QDBusConnection::sessionBus(), this);
if (m_interface->isValid()) {
connect(m_interface, SIGNAL(searchItemsAdd(QVariantMap)), this, SLOT(addItem(QVariantMap)));
connect(m_interface, SIGNAL(searchItemsDelete(QVariantMap)), this, SLOT(removeItem(QVariantMap)));
} else {
qCritical() << "Ukcc interface error:" << m_interface->lastError();
}
}
/**
* @brief SettingsMatch::initData
* dbus接口的设置项信息读到内存
*/
void SettingsMatch::initData()
{
QDBusReply<QVariantMap> reply = m_interface->call("getSearchItems");
if (reply.isValid()) {
this->parsingArgsOfDbus(reply.value(), HandleType::Add);
} else {
qWarning() << "Fail to call ukcc's getSearchItems.";
}
}
void SettingsMatch::parsingArgsOfDbus(QVariantMap replyMap, HandleTypes type)
{
for (std::pair<QString, QVariant> it : replyMap.toStdMap()) {
const QDBusArgument &dbusArgs = it.second.value<QDBusArgument>();
dbusArgs.beginArray();
while (!dbusArgs.atEnd()) {
QVariant tmp;
dbusArgs >> tmp;
const QDBusArgument &args = tmp.value<QDBusArgument>();
QVariantMap itemInfo;
args >> itemInfo;
this->handleData(itemInfo, type);
}
dbusArgs.endArray();
}
}
void SettingsMatch::handleData(const QVariantMap &itemInfo, HandleTypes type)
{
QString enNameOfModule = itemInfo.value("superordinate").toString();
QString enNameOfPlugin = itemInfo.value("plugin").toString();
QString enNameOfFunc = itemInfo.value("subEnglish").toString();
QString dataKey= enNameOfModule + "/" + enNameOfPlugin;
QString localName = itemInfo.value("plugin" + QLocale::system().name()).toString();
QStringList englishSearchResult = m_searchMap[enNameOfModule].value(enNameOfPlugin);
if (type == HandleType::Add) {
englishSearchResult.append(enNameOfFunc);
//二级目录
this->add2DataMap(dataKey, enNameOfPlugin, localName);
//三级目录
dataKey = dataKey + "/" + enNameOfFunc;
localName = itemInfo.value("sub" + QLocale::system().name()).toString();
this->add2DataMap(dataKey, enNameOfFunc, localName);
}
if (type == HandleType::Delete) {
englishSearchResult.removeOne(enNameOfFunc);
m_dataMap.remove(dataKey + "/" + enNameOfFunc);
if (englishSearchResult.isEmpty()) {
m_dataMap.remove(dataKey);
m_searchMap[enNameOfModule].remove(enNameOfPlugin);
return;
}
}
//update searchMap
m_searchMap[enNameOfModule].insert(enNameOfPlugin, englishSearchResult);
}
void SettingsMatch::add2DataMap(const QString &key, const QString &enName, const QString &localName)
{
if (m_dataMap.contains(key)) {
return;
}
QStringList aItemInfo{enName, localName};
if (QLocale::system().language() == QLocale::Chinese) {
aItemInfo << FileUtils::findMultiToneWords(localName);
}
m_dataMap.insert(key, aItemInfo);
}
/**
@ -302,6 +396,16 @@ void SettingsMatch::startSearch(QString &keyword, size_t uniqueSymbol, DataQueue
}
}
void SettingsMatch::addItem(QVariantMap replyMap)
{
this->parsingArgsOfDbus(replyMap, HandleType::Add);
}
void SettingsMatch::removeItem(QVariantMap replyMap)
{
this->parsingArgsOfDbus(replyMap, HandleType::Delete);
}
void SettingsMatch::matchDataMap(QString &key, QString &keyword, size_t uniqueSymbol, DataQueue<SearchPluginIface::ResultInfo> *searchResult)
{
SearchPluginIface::ResultInfo resultInfo;

View File

@ -10,6 +10,8 @@
#include <QLabel>
#include <QAction>
#include <QDomDocument>
#include <QDBusInterface>
#include "action-label.h"
#include "separation-line.h"
#include "search-plugin-iface.h"
@ -74,22 +76,43 @@ private:
DataQueue<SearchPluginIface::ResultInfo> *m_searchResult = nullptr;
};
class SettingsMatch :public QObject , public QThread {
class SettingsMatch :public QThread {
Q_OBJECT
public:
enum HandleType {
Add,
Delete
};
Q_DECLARE_FLAGS(HandleTypes, HandleType)
static SettingsMatch *getInstance();
void startSearch(QString &keyword, size_t uniqueSymbol, DataQueue<SearchPluginIface::ResultInfo> *searchResult);
public Q_SLOTS:
void addItem(QVariantMap replyMap);
void removeItem(QVariantMap replyMap);
protected:
void run() override;
private:
explicit SettingsMatch() = default;
explicit SettingsMatch();
~SettingsMatch() = default;
//parsing dbus of ukcc
void initData();
void parsingArgsOfDbus(QVariantMap replyMap, HandleTypes type);
void handleData(const QVariantMap &itemInfo, HandleTypes type);
void add2DataMap(const QString &key, const QString &enName, const QString &localName);
//Parsing xml
void initDataOfXml();
bool matchCommonEnvironment(QDomNode childNode);
void matchNodes(QDomNode node);
//search
void matchDataMap(QString &key, QString &keyword, size_t uniqueSymbol, DataQueue<SearchPluginIface::ResultInfo> *searchResult);
void createResultInfo(SearchPluginIface::ResultInfo &resultInfo, const QStringList &itemInfo, const QString &path);
QMap<QString, QMap<QString, QStringList>> m_searchMap;
QMap<QString, QStringList> m_dataMap;
QDBusInterface *m_interface = nullptr;
};
}
#endif // SETTINGSSEARCHPLUGIN_H