Add app search plugin.

This commit is contained in:
iaom 2021-06-17 14:08:55 +08:00
parent 77a3164ee5
commit a88bdf25d7
9 changed files with 316 additions and 8 deletions

View File

@ -283,7 +283,7 @@ int main(int argc, char *argv[]) {
QObject::connect(&app, &QtSingleApplication::messageReceived, w, &MainWindow::bootOptionsFilter);
// Start app search thread
AppMatch::getAppMatch()->start();
// AppMatch::getAppMatch()->start();
// NEW_TODO
// Set threads which in global thread pool expiry time in 5ms, some prolems here

View File

@ -0,0 +1,207 @@
#include "app-search-plugin.h"
#include <gio/gdesktopappinfo.h>
using namespace Zeeker;
size_t AppSearchPlugin::uniqueSymbol = 0;
QMutex AppSearchPlugin::m_mutex;
AppSearchPlugin::AppSearchPlugin(QObject *parent) : QObject(parent)
{
SearchPluginIface::Actioninfo open { 0, tr("Open")};
SearchPluginIface::Actioninfo addtoDesktop { 1, tr("Add Shortcut to Desktop")};
SearchPluginIface::Actioninfo addtoPanel { 2, tr("Add Shortcut to Panel")};
SearchPluginIface::Actioninfo install { 0, tr("Install")};
m_actionInfo_installed << open << addtoDesktop << addtoPanel;
m_actionInfo_not_installed << install;
AppMatch::getAppMatch()->start();
m_pool.setMaxThreadCount(2);
m_pool.setExpiryTimeout(1000);
}
const QString AppSearchPlugin::name()
{
return tr("Applications Search");
}
const QString AppSearchPlugin::description()
{
return tr("Applications Search");
}
QString AppSearchPlugin::getPluginName()
{
return tr("Applications Search");
}
void AppSearchPlugin::KeywordSearch(QString keyword, DataQueue<SearchPluginIface::ResultInfo> *searchResult)
{
m_mutex.lock();
++uniqueSymbol;
m_mutex.unlock();
AppSearch *appsearch = new AppSearch(searchResult, keyword, uniqueSymbol);
m_pool.start(appsearch);
}
QList<SearchPluginIface::Actioninfo> AppSearchPlugin::getActioninfo(int type)
{
switch (type) {
case 0:
return m_actionInfo_installed;
break;
case 1:
return m_actionInfo_not_installed;
break;
default:
return QList<SearchPluginIface::Actioninfo>();
break;
}
}
void AppSearchPlugin::openAction(int actionkey, QString key, int type)
{
switch (type) {
case 0:
switch (actionkey) {
case 0:
if(!launch(key)) {
qWarning() << "Fail to launch:" << key;
}
break;
case 1:
if(!addDesktopShortcut(key)) {
qWarning() << "Fail to add Desktop Shortcut:" << key;
}
break;
case 2:
if(!addPanelShortcut(key)) {
qWarning() << "Fail to add Panel Shortcut:" << key;
}
break;
default:
break;
}
break;
case 1:
if(!installAppAction(key)) {
qWarning() << "Fail to install:" << key;
}
break;
default:
break;
}
}
bool AppSearchPlugin::launch(const QString &path)
{
GDesktopAppInfo * desktopAppInfo = g_desktop_app_info_new_from_filename(path.toLocal8Bit().data());
bool res = static_cast<bool>(g_app_info_launch(G_APP_INFO(desktopAppInfo), nullptr, nullptr, nullptr));
g_object_unref(desktopAppInfo);
return res;
}
bool AppSearchPlugin::addPanelShortcut(const QString& path) {
QDBusInterface iface("com.ukui.panel.desktop",
"/",
"com.ukui.panel.desktop",
QDBusConnection::sessionBus());
if(iface.isValid()) {
QDBusReply<bool> isExist = iface.call("CheckIfExist", path);
if(isExist) {
qWarning() << "qDebug: Add shortcut to panel failed, because it is already existed!";
return false;
}
QDBusReply<QVariant> ret = iface.call("AddToTaskbar", path);
qDebug() << "qDebug: Add shortcut to panel successed!";
return true;
}
return false;
}
bool AppSearchPlugin::addDesktopShortcut(const QString& path) {
QString dirpath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
QFileInfo fileInfo(path);
QString desktopfn = fileInfo.fileName();
QFile file(path);
QString newName = QString(dirpath + "/" + desktopfn);
bool ret = file.copy(QString(dirpath + "/" + desktopfn));
if(ret) {
QProcess process;
process.startDetached(QString("chmod a+x %1").arg(newName));
return true;
}
return false;
}
bool AppSearchPlugin::installAppAction(const QString & name) {
QDBusInterface * interface = new QDBusInterface("com.kylin.softwarecenter",
"/com/kylin/softwarecenter",
"com.kylin.utiliface",
QDBusConnection::sessionBus());
if(interface->isValid()) {
//软件商店已打开,直接跳转
interface->call("show_search_result", name);
bool reply = QDBusReply<bool>(interface->call(QString("show_search_result"), name));
return reply;
} else {
//软件商店未打开,打开软件商店下载此软件
qDebug() << "Softwarecenter has not been launched, now launch it." << name;
QProcess process;
return process.startDetached(QString("kylin-software-center -find %1").arg(name));
}
}
AppSearch::AppSearch(DataQueue<SearchPluginIface::ResultInfo> *searchResult, const QString &keyword, size_t uniqueSymbol)
{
this->setAutoDelete(true);
m_search_result = searchResult;
m_keyword = keyword;
m_uniqueSymbol = uniqueSymbol;
}
AppSearch::~AppSearch()
{
}
void AppSearch::run()
{
//These weird code is mean to be compatible with the old version UI.
AppMatch::getAppMatch()->startMatchApp(m_keyword, m_installed_apps, m_not_installed_apps);
QMapIterator<NameString, QStringList> i(m_installed_apps);
while (i.hasNext()) {
i.next();
SearchPluginIface::ResultInfo ri;
if(!QIcon::fromTheme(i.value().at(1)).isNull()) {
ri.icon = QIcon::fromTheme(i.value().at(1));
}else {
ri.icon = QIcon(":/res/icons/desktop.png");
}
ri.name = i.key().app_name;
ri.actionKey = i.value().at(0);
ri.type = 0; //0 means installed apps.
if (m_uniqueSymbol == AppSearchPlugin::uniqueSymbol) {
m_search_result->enqueue(ri);
} else {
break;
}
}
QMapIterator<NameString, QStringList> in(m_not_installed_apps);
while (in.hasNext()) {
in.next();
SearchPluginIface::ResultInfo ri;
if(!QIcon(in.value().at(1)).isNull()) {
ri.icon = QIcon(in.value().at(1));
}else {
ri.icon = QIcon(":/res/icons/desktop.png");
}
ri.name = in.key().app_name;
SearchPluginIface::DescriptionInfo di;
di.key = QString(tr("Application Description:"));
di.value = in.value().at(3);
ri.description.append(di);
ri.actionKey = in.value().at(2);
ri.type = 1; //1 means not installed apps.
if (m_uniqueSymbol == AppSearchPlugin::uniqueSymbol) {
m_search_result->enqueue(ri);
} else {
break;
}
}
}

View File

@ -0,0 +1,55 @@
#ifndef APPSEARCHPLUGIN_H
#define APPSEARCHPLUGIN_H
#include <QObject>
#include "search-plugin-iface.h"
#include "app-match.h"
#include "libsearch_global.h"
namespace Zeeker {
class LIBSEARCH_EXPORT AppSearchPlugin : public QObject, public SearchPluginIface
{
friend class AppSearch;
Q_OBJECT
public:
AppSearchPlugin(QObject *parent = nullptr);
PluginType pluginType() {return PluginType::SearchPlugin;}
const QString name();
const QString description();
const QIcon icon() {return QIcon::fromTheme("appsearch");}
void setEnable(bool enable) {m_enable = enable;}
bool isEnable() {return m_enable;}
QString getPluginName();
void KeywordSearch(QString keyword,DataQueue<ResultInfo> *searchResult);
QList<SearchPluginIface::Actioninfo> getActioninfo(int type);
void openAction(int actionkey, QString key, int type);
private:
bool launch(const QString &path);
bool addPanelShortcut(const QString &path);
bool addDesktopShortcut(const QString &path);
bool installAppAction(const QString &name);
bool m_enable = true;
QList<SearchPluginIface::Actioninfo> m_actionInfo_installed;
QList<SearchPluginIface::Actioninfo> m_actionInfo_not_installed;
QThreadPool m_pool;
static size_t uniqueSymbol;
static QMutex m_mutex;
};
class AppSearch : public QObject, public QRunnable {
Q_OBJECT
public:
AppSearch(DataQueue<SearchPluginIface::ResultInfo> *searchResult, const QString& keyword, size_t uniqueSymbol);
~AppSearch();
protected:
void run() override;
private:
DataQueue<SearchPluginIface::ResultInfo> *m_search_result = nullptr;
size_t m_uniqueSymbol;
QString m_keyword;
QMap<NameString, QStringList> m_installed_apps;
QMap<NameString, QStringList> m_not_installed_apps;
};
}
#endif // APPSEARCHPLUGIN_H

View File

@ -2,6 +2,8 @@ INCLUDEPATH += $$PWD
HEADERS += \
$$PWD/app-match.h \
$$PWD/app-search-plugin.h
SOURCES += \
$$PWD/app-match.cpp \
$$PWD/app-search-plugin.cpp

View File

@ -49,7 +49,7 @@ QList<SearchPluginIface::Actioninfo> FileSearchPlugin::getActioninfo(int type)
return m_actionInfo;
}
void FileSearchPlugin::openAction(int actionkey, QString key)
void FileSearchPlugin::openAction(int actionkey, QString key, int type)
{
//TODO add some return message here.
switch (actionkey) {
@ -112,7 +112,7 @@ QList<SearchPluginIface::Actioninfo> DirSearchPlugin::getActioninfo(int type)
return m_actionInfo;
}
void DirSearchPlugin::openAction(int actionkey, QString key)
void DirSearchPlugin::openAction(int actionkey, QString key, int type)
{
//TODO add some return message here.
switch (actionkey) {
@ -173,7 +173,7 @@ QList<SearchPluginIface::Actioninfo> FileContengSearchPlugin::getActioninfo(int
return m_actionInfo;
}
void FileContengSearchPlugin::openAction(int actionkey, QString key)
void FileContengSearchPlugin::openAction(int actionkey, QString key, int type)
{
//TODO add some return message here.
switch (actionkey) {

View File

@ -24,7 +24,7 @@ public:
void KeywordSearch(QString keyword,DataQueue<ResultInfo> *searchResult);
QList<SearchPluginIface::Actioninfo> getActioninfo(int type);
void openAction(int actionkey, QString key);
void openAction(int actionkey, QString key, int type = 0);
private:
bool m_enable = true;
@ -47,7 +47,7 @@ public:
void KeywordSearch(QString keyword,DataQueue<ResultInfo> *searchResult);
QList<SearchPluginIface::Actioninfo> getActioninfo(int type);
void openAction(int actionkey, QString key);
void openAction(int actionkey, QString key, int type = 0);
private:
bool m_enable = true;
@ -70,7 +70,7 @@ public:
void KeywordSearch(QString keyword,DataQueue<ResultInfo> *searchResult);
QList<SearchPluginIface::Actioninfo> getActioninfo(int type);
void openAction(int actionkey, QString key);
void openAction(int actionkey, QString key, int type = 0);
private:
bool m_enable = true;

View File

@ -40,7 +40,7 @@ public:
virtual QString getPluginName() = 0;
virtual void KeywordSearch(QString keyword,DataQueue<ResultInfo> *searchResult) = 0;
virtual QList<Actioninfo> getActioninfo(int type) = 0;
virtual void openAction(int actionkey, QString key) = 0;
virtual void openAction(int actionkey, QString key, int type) = 0;
};
}

View File

@ -1,6 +1,7 @@
#include <QDebug>
#include "search-plugin-manager.h"
#include "file-search-plugin.h"
#include "app-search-plugin.h"-
using namespace Zeeker;
@ -10,6 +11,7 @@ SearchPluginManager::SearchPluginManager(QObject *parent)
registerPlugin(new FileSearchPlugin(this));
registerPlugin(new DirSearchPlugin(this));
registerPlugin(new FileContengSearchPlugin(this));
registerPlugin(new AppSearchPlugin(this));
}
bool SearchPluginManager::registerPlugin(Zeeker::SearchPluginIface *plugin)

View File

@ -1,6 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="zh_CN">
<context>
<name>Zeeker::AppSearch</name>
<message>
<location filename="../../libsearch/appsearch/app-search-plugin.cpp" line="196"/>
<source>Application Description:</source>
<translation></translation>
</message>
</context>
<context>
<name>Zeeker::AppSearchPlugin</name>
<message>
<location filename="../../libsearch/appsearch/app-search-plugin.cpp" line="8"/>
<source>Open</source>
<translation></translation>
</message>
<message>
<location filename="../../libsearch/appsearch/app-search-plugin.cpp" line="9"/>
<source>Add Shortcut to Desktop</source>
<translation></translation>
</message>
<message>
<location filename="../../libsearch/appsearch/app-search-plugin.cpp" line="10"/>
<source>Add Shortcut to Panel</source>
<translation></translation>
</message>
<message>
<location filename="../../libsearch/appsearch/app-search-plugin.cpp" line="11"/>
<source>Install</source>
<translation></translation>
</message>
<message>
<location filename="../../libsearch/appsearch/app-search-plugin.cpp" line="21"/>
<location filename="../../libsearch/appsearch/app-search-plugin.cpp" line="26"/>
<location filename="../../libsearch/appsearch/app-search-plugin.cpp" line="31"/>
<source>Applications Search</source>
<translation></translation>
</message>
<message>
<source>Application Description:</source>
<translation type="vanished"></translation>
</message>
</context>
<context>
<name>Zeeker::DirSearchPlugin</name>
<message>