style(appmanager): 去除glib相关接口

This commit is contained in:
hewenfei 2024-01-18 11:58:26 +08:00
parent bca2a7cf6d
commit 588b7a4fcd
3 changed files with 27 additions and 45 deletions

View File

@ -29,7 +29,7 @@ find_package(PkgConfig REQUIRED)
set(UKUI_MENU_EXTERNAL_LIBS "") set(UKUI_MENU_EXTERNAL_LIBS "")
# glib-2.0 gio-unix-2.0 gsettings-qt x11 kysdk-waylandhelper # glib-2.0 gio-unix-2.0 gsettings-qt x11 kysdk-waylandhelper
set(UKUI_MENU_PC_PKGS glib-2.0 gsettings-qt x11 kysdk-waylandhelper ukui-search kysdk-datacollect) set(UKUI_MENU_PC_PKGS gsettings-qt x11 kysdk-waylandhelper ukui-search kysdk-datacollect)
foreach(external_lib IN ITEMS ${UKUI_MENU_PC_PKGS}) foreach(external_lib IN ITEMS ${UKUI_MENU_PC_PKGS})
pkg_check_modules(${external_lib} REQUIRED ${external_lib}) pkg_check_modules(${external_lib} REQUIRED ${external_lib})

View File

@ -15,9 +15,6 @@
* 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 <gio-unix-2.0/gio/gdesktopappinfo.h>
#include "app-manager.h" #include "app-manager.h"
#include "basic-app-model.h" #include "basic-app-model.h"
@ -26,6 +23,7 @@
#include <QDBusConnection> #include <QDBusConnection>
#include <QDBusMessage> #include <QDBusMessage>
#include <QProcess> #include <QProcess>
#include <QSettings>
#define KYLIN_APP_MANAGER_NAME "com.kylin.ProcessManager" #define KYLIN_APP_MANAGER_NAME "com.kylin.ProcessManager"
#define KYLIN_APP_MANAGER_PATH "/com/kylin/ProcessManager/AppLauncher" #define KYLIN_APP_MANAGER_PATH "/com/kylin/ProcessManager/AppLauncher"
@ -43,36 +41,31 @@ AppManager::AppManager(QObject *parent) : QObject(parent)
} }
void AppManager::launchApp(const QString &desktopFilePath) void AppManager::launchApp(const QString &appid)
{ {
if (desktopFilePath.isEmpty()) { if (appid.isEmpty()) {
return; return;
} }
Q_EMIT request(UkuiMenuApplication::Hide); Q_EMIT request(UkuiMenuApplication::Hide);
QDBusMessage message = QDBusMessage::createMethodCall(KYLIN_APP_MANAGER_NAME, KYLIN_APP_MANAGER_PATH, KYLIN_APP_MANAGER_INTERFACE, "LaunchApp"); QDBusMessage message = QDBusMessage::createMethodCall(KYLIN_APP_MANAGER_NAME, KYLIN_APP_MANAGER_PATH, KYLIN_APP_MANAGER_INTERFACE, "LaunchApp");
message << desktopFilePath; message << appid;
auto watcher = new QDBusPendingCallWatcher(QDBusPendingCall(QDBusConnection::sessionBus().asyncCall(message)), this); auto watcher = new QDBusPendingCallWatcher(QDBusPendingCall(QDBusConnection::sessionBus().asyncCall(message)), this);
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, [desktopFilePath] (QDBusPendingCallWatcher *self) { QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, [appid] (QDBusPendingCallWatcher *self) {
if (self->isError()) { if (self->isError()) {
// QString cmd = parseDesktopFile(desktopFilePath); QStringList list = parseDesktopFile(appid);
// if (!cmd.isEmpty()) { if (!list.isEmpty()) {
// QProcess::startDetached(cmd, QStringList()); QString cmd = list.takeFirst();
// } QProcess::startDetached(cmd, list);
GDesktopAppInfo *desktopAppInfo = g_desktop_app_info_new_from_filename(desktopFilePath.toLocal8Bit().data()); qDebug() << "launch with QProcess:" << cmd << list;
if (desktopAppInfo == nullptr) {
return;
} }
bool launched = g_app_info_launch(G_APP_INFO(desktopAppInfo), nullptr, nullptr, nullptr);
g_object_unref(desktopAppInfo);
qDebug() << "launch with GDesktopAppInfo:" << launched;
} }
self->deleteLater(); self->deleteLater();
}); });
BasicAppModel::instance()->databaseInterface()->updateApLaunchedState(desktopFilePath, true); BasicAppModel::instance()->databaseInterface()->updateApLaunchedState(appid, true);
} }
void AppManager::launchBinaryApp(const QString &app, const QString &args) void AppManager::launchBinaryApp(const QString &app, const QString &args)
@ -93,13 +86,13 @@ void AppManager::launchBinaryApp(const QString &app, const QString &args)
}); });
} }
void AppManager::launchAppWithArguments(const QString &desktopFile, const QStringList &args, const QString &applicationName) void AppManager::launchAppWithArguments(const QString &appid, const QStringList &args, const QString &applicationName)
{ {
QDBusMessage message = QDBusMessage::createMethodCall(KYLIN_APP_MANAGER_NAME, QDBusMessage message = QDBusMessage::createMethodCall(KYLIN_APP_MANAGER_NAME,
KYLIN_APP_MANAGER_PATH, KYLIN_APP_MANAGER_PATH,
KYLIN_APP_MANAGER_INTERFACE, KYLIN_APP_MANAGER_INTERFACE,
"LaunchAppWithArguments"); "LaunchAppWithArguments");
message << desktopFile << args; message << appid << args;
auto watcher = new QDBusPendingCallWatcher(QDBusConnection::sessionBus().asyncCall(message), this); auto watcher = new QDBusPendingCallWatcher(QDBusConnection::sessionBus().asyncCall(message), this);
connect(watcher, &QDBusPendingCallWatcher::finished, this, [applicationName, args] (QDBusPendingCallWatcher *self) { connect(watcher, &QDBusPendingCallWatcher::finished, this, [applicationName, args] (QDBusPendingCallWatcher *self) {
@ -119,41 +112,30 @@ void AppManager::changeFavoriteState(const QString &appid, bool isFavorite)
BasicAppModel::instance()->databaseInterface()->fixAppToFavorite(appid, isFavorite ? 1 : 0); BasicAppModel::instance()->databaseInterface()->fixAppToFavorite(appid, isFavorite ? 1 : 0);
} }
QString AppManager::parseDesktopFile(const QString &desktopFilePath) QStringList AppManager::parseDesktopFile(const QString &appid)
{ {
// TODO: try QSettings? // TODO: try QSettings?
GKeyFile *keyFile = g_key_file_new(); QSettings file(appid, QSettings::IniFormat);
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); file.beginGroup(QStringLiteral("Desktop Entry"));
if (!exec) { QStringList stringList = file.value(QStringLiteral("Exec")).toString().split(" ");
g_key_file_free(keyFile); file.endGroup();
return "";
}
QStringList stringList = QString(exec).split(" ");
g_free(exec);
g_key_file_free(keyFile);
if (stringList.isEmpty()) { if (stringList.isEmpty()) {
return ""; return {};
} }
QString program = stringList.first(); QStringList res;
stringList.removeFirst(); res << stringList.takeFirst();
// TODO: freedesktop group // TODO: freedesktop group
for (const auto &str : stringList) { for (const auto &str : stringList) {
if (!str.startsWith("%")) { if (!str.startsWith("%")) {
program.append(" " + str); res.append(str);
} }
} }
return program; return res;
} }
} // UkuiMenu } // UkuiMenu

View File

@ -33,14 +33,14 @@ public:
AppManager(const AppManager &obj) = delete; AppManager(const AppManager &obj) = delete;
AppManager &operator = (const AppManager &obj) = delete; AppManager &operator = (const AppManager &obj) = delete;
Q_INVOKABLE void launchApp(const QString &desktopFilePath); Q_INVOKABLE void launchApp(const QString &appid);
Q_INVOKABLE void launchBinaryApp(const QString &app, const QString &args = QString()); Q_INVOKABLE void launchBinaryApp(const QString &app, const QString &args = QString());
Q_INVOKABLE void launchAppWithArguments(const QString &desktopFile, const QStringList &args, const QString &applicationName); Q_INVOKABLE void launchAppWithArguments(const QString &appid, const QStringList &args, const QString &applicationName);
Q_INVOKABLE void changeFavoriteState(const QString &appid, bool isFavorite); Q_INVOKABLE void changeFavoriteState(const QString &appid, bool isFavorite);
private: private:
explicit AppManager(QObject *parent = nullptr); explicit AppManager(QObject *parent = nullptr);
static QString parseDesktopFile(const QString &desktopFilePath); static QStringList parseDesktopFile(const QString &desktopFilePath);
Q_SIGNALS: Q_SIGNALS:
void request(UkuiMenuApplication::Command command); void request(UkuiMenuApplication::Command command);