From c0b2e23229e37b968e531e660123db33f6550df1 Mon Sep 17 00:00:00 2001 From: hewenfei Date: Mon, 27 Feb 2023 11:06:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E4=B8=8D=E5=AD=98=E5=9C=A8appmanager?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E6=89=8B=E5=8A=A8=E8=A7=A3=E6=9E=90dekstopfi?= =?UTF-8?q?le=EF=BC=8C=E7=9B=B4=E6=8E=A5=E6=89=A7=E8=A1=8C=E5=BA=94?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/app-manager.cpp | 67 +++++++++++++++++++++++++++++++++++---- src/utils/app-manager.h | 4 ++- 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/utils/app-manager.cpp b/src/utils/app-manager.cpp index 8f46ce8..677a262 100644 --- a/src/utils/app-manager.cpp +++ b/src/utils/app-manager.cpp @@ -15,12 +15,14 @@ * along with this program. If not, see . * */ +#include #include "app-manager.h" #include #include #include +#include #define KYLIN_APP_MANAGER_NAME "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) { - QDBusReply status = m_appManagerDbusInterface->call("LaunchApp", desktopFile); - return status; - } else { - qWarning() <<"LaunchApp is failed,return false"; + if (launchAppWithDBus(desktopFilePath)) { + return true; + } + + QString cmd = parseDesktopFile(desktopFilePath); +// qDebug() << "launchApp:" << desktopFilePath << cmd; + if (cmd.isEmpty()) { return false; } + + return QProcess::startDetached(cmd); } -} // UkuiMenu \ No newline at end of file +bool AppManager::launchAppWithDBus(const QString &desktopFilePath) +{ + if (!m_appManagerDbusInterface) { + return false; + } + + QDBusReply 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 diff --git a/src/utils/app-manager.h b/src/utils/app-manager.h index 437bff8..78fada2 100644 --- a/src/utils/app-manager.h +++ b/src/utils/app-manager.h @@ -32,10 +32,12 @@ public: AppManager(const AppManager &obj) = delete; AppManager &operator = (const AppManager &obj) = delete; - bool launchApp(const QString &desktopFile); + bool launchApp(const QString &desktopFilePath); private: explicit AppManager(QObject *parent = nullptr); + bool launchAppWithDBus(const QString &desktopFilePath); + static QString parseDesktopFile(const QString &desktopFilePath); private: QDBusInterface *m_appManagerDbusInterface = nullptr;