在不存在appmanager时,手动解析dekstopfile,直接执行应用

This commit is contained in:
hewenfei 2023-02-27 11:06:07 +08:00
parent 37992ca49d
commit c0b2e23229
2 changed files with 63 additions and 8 deletions

View File

@ -15,12 +15,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
* *
*/ */
#include <gio/gio.h>
#include "app-manager.h" #include "app-manager.h"
#include <QDebug> #include <QDebug>
#include <QDBusReply> #include <QDBusReply>
#include <QDBusInterface> #include <QDBusInterface>
#include <QProcess>
#define KYLIN_APP_MANAGER_NAME "com.kylin.AppManager" #define KYLIN_APP_MANAGER_NAME "com.kylin.AppManager"
#define KYLIN_APP_MANAGER_PATH "/com/kylin/AppManager" #define KYLIN_APP_MANAGER_PATH "/com/kylin/AppManager"
@ -44,15 +46,66 @@ AppManager::AppManager(QObject *parent) : QObject(parent)
} }
} }
bool AppManager::launchApp(const QString &desktopFile) bool AppManager::launchApp(const QString &desktopFilePath)
{ {
if (m_appManagerDbusInterface != nullptr) { if (launchAppWithDBus(desktopFilePath)) {
QDBusReply<bool> status = m_appManagerDbusInterface->call("LaunchApp", desktopFile); return true;
return status; }
} else {
qWarning() <<"LaunchApp is failed,return false"; QString cmd = parseDesktopFile(desktopFilePath);
// qDebug() << "launchApp:" << desktopFilePath << cmd;
if (cmd.isEmpty()) {
return false; return false;
} }
return QProcess::startDetached(cmd);
}
bool AppManager::launchAppWithDBus(const QString &desktopFilePath)
{
if (!m_appManagerDbusInterface) {
return false;
}
QDBusReply<bool> status = m_appManagerDbusInterface->call("LaunchApp", desktopFilePath);
return status.value();
}
QString AppManager::parseDesktopFile(const QString &desktopFilePath)
{
// TODO: try QSettings?
GKeyFile *keyFile = g_key_file_new();
gboolean res = g_key_file_load_from_file(keyFile, desktopFilePath.toUtf8(), G_KEY_FILE_NONE, nullptr);
if (!res) {
return "";
}
gchar *exec = g_key_file_get_value(keyFile, "Desktop Entry", "Exec", nullptr);
if (!exec) {
g_key_file_free(keyFile);
return "";
}
QStringList stringList = QString(exec).split(" ");
g_free(exec);
g_key_file_free(keyFile);
if (stringList.isEmpty()) {
return "";
}
QString program = stringList.first();
stringList.removeFirst();
// TODO: freedesktop group
for (const auto &str : stringList) {
if (!str.startsWith("%")) {
program.append(" " + str);
}
}
return program;
} }
} // UkuiMenu } // UkuiMenu

View File

@ -32,10 +32,12 @@ public:
AppManager(const AppManager &obj) = delete; AppManager(const AppManager &obj) = delete;
AppManager &operator = (const AppManager &obj) = delete; AppManager &operator = (const AppManager &obj) = delete;
bool launchApp(const QString &desktopFile); bool launchApp(const QString &desktopFilePath);
private: private:
explicit AppManager(QObject *parent = nullptr); explicit AppManager(QObject *parent = nullptr);
bool launchAppWithDBus(const QString &desktopFilePath);
static QString parseDesktopFile(const QString &desktopFilePath);
private: private:
QDBusInterface *m_appManagerDbusInterface = nullptr; QDBusInterface *m_appManagerDbusInterface = nullptr;