From 020f869a29f21dc3f2bb5868e248bb4b735d1468 Mon Sep 17 00:00:00 2001 From: baijunjie Date: Tue, 29 Nov 2022 20:07:53 +0800 Subject: [PATCH] Modify settings search, use dbus interface of ukcc instead of xml file to get data. --- .../settingsearch/settings-search-plugin.cpp | 108 +++++++++++++++++- .../settingsearch/settings-search-plugin.h | 27 ++++- 2 files changed, 131 insertions(+), 4 deletions(-) diff --git a/libsearch/settingsearch/settings-search-plugin.cpp b/libsearch/settingsearch/settings-search-plugin.cpp index 9afb472..bd2ac6a 100644 --- a/libsearch/settingsearch/settings-search-plugin.cpp +++ b/libsearch/settingsearch/settings-search-plugin.cpp @@ -2,8 +2,8 @@ #include #include #include -#include #include +#include #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 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 it : replyMap.toStdMap()) { + const QDBusArgument &dbusArgs = it.second.value(); + dbusArgs.beginArray(); + while (!dbusArgs.atEnd()) { + QVariant tmp; + dbusArgs >> tmp; + const QDBusArgument &args = tmp.value(); + 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 *searchResult) { SearchPluginIface::ResultInfo resultInfo; diff --git a/libsearch/settingsearch/settings-search-plugin.h b/libsearch/settingsearch/settings-search-plugin.h index 3bec896..d0f9405 100644 --- a/libsearch/settingsearch/settings-search-plugin.h +++ b/libsearch/settingsearch/settings-search-plugin.h @@ -10,6 +10,8 @@ #include #include #include +#include + #include "action-label.h" #include "separation-line.h" #include "search-plugin-iface.h" @@ -74,22 +76,43 @@ private: DataQueue *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 *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 *searchResult); void createResultInfo(SearchPluginIface::ResultInfo &resultInfo, const QStringList &itemInfo, const QString &path); QMap> m_searchMap; QMap m_dataMap; + QDBusInterface *m_interface = nullptr; }; } #endif // SETTINGSSEARCHPLUGIN_H