2023-07-17 14:49:35 +08:00
|
|
|
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
|
|
|
#ifndef GLOBAL_H
|
|
|
|
#define GLOBAL_H
|
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
#include <QMap>
|
|
|
|
#include <QDBusUnixFileDescriptor>
|
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <optional>
|
|
|
|
#include <QDBusError>
|
|
|
|
#include <QMetaType>
|
|
|
|
#include <QMetaClassInfo>
|
2023-07-24 14:12:59 +08:00
|
|
|
#include <QLocale>
|
2023-08-10 14:32:09 +08:00
|
|
|
#include <QDir>
|
2023-07-24 14:12:59 +08:00
|
|
|
#include <QRegularExpression>
|
|
|
|
#include <QDBusObjectPath>
|
|
|
|
#include <unistd.h>
|
2023-08-15 14:43:34 +08:00
|
|
|
#include <QUuid>
|
2023-08-21 18:38:27 +08:00
|
|
|
#include <QLoggingCategory>
|
2023-08-21 16:02:26 +08:00
|
|
|
#include <sys/stat.h>
|
2023-08-08 15:10:32 +08:00
|
|
|
#include "constant.h"
|
2023-08-07 14:25:22 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2023-08-21 18:38:27 +08:00
|
|
|
Q_DECLARE_LOGGING_CATEGORY(DDEAMProf)
|
|
|
|
|
2023-07-17 14:49:35 +08:00
|
|
|
using IconMap = QMap<QString, QMap<uint, QMap<QString, QDBusUnixFileDescriptor>>>;
|
2023-08-18 10:01:52 +08:00
|
|
|
using ObjectMap = QMap<QDBusObjectPath, QStringList>;
|
2023-07-17 14:49:35 +08:00
|
|
|
|
2023-08-07 14:25:22 +08:00
|
|
|
inline QString getApplicationLauncherBinary()
|
|
|
|
{
|
2023-08-11 17:46:46 +08:00
|
|
|
static const QString bin = []() {
|
2023-08-11 14:53:05 +08:00
|
|
|
auto value = qgetenv("DEEPIN_APPLICATION_MANAGER_APP_LAUNCH_HELPER_BIN");
|
|
|
|
if (value.isEmpty()) {
|
2023-08-11 17:46:46 +08:00
|
|
|
return QString::fromLocal8Bit(ApplicationLaunchHelperBinary);
|
2023-08-11 14:53:05 +08:00
|
|
|
}
|
2023-08-07 14:25:22 +08:00
|
|
|
qWarning() << "Using app launch helper defined in environment variable DEEPIN_APPLICATION_MANAGER_APP_LAUNCH_HELPER_BIN.";
|
2023-08-11 17:46:46 +08:00
|
|
|
return QString::fromLocal8Bit(value);
|
2023-08-11 14:53:05 +08:00
|
|
|
}();
|
|
|
|
return bin;
|
2023-08-07 14:25:22 +08:00
|
|
|
}
|
2023-07-24 14:12:59 +08:00
|
|
|
|
|
|
|
enum class DBusType { Session = QDBusConnection::SessionBus, System = QDBusConnection::SystemBus, Custom };
|
2023-07-17 14:49:35 +08:00
|
|
|
|
2023-08-10 14:32:09 +08:00
|
|
|
template <typename T>
|
|
|
|
using remove_cvr_t = std::remove_reference_t<std::remove_cv_t<T>>;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void applyIteratively(QList<QDir> &&dirs, T &&func)
|
|
|
|
{
|
|
|
|
static_assert(std::is_invocable_v<T, const QFileInfo &>, "apply function should only accept one QFileInfo");
|
|
|
|
static_assert(std::is_same_v<decltype(func(QFileInfo{})), bool>,
|
|
|
|
"apply function should return a boolean to indicate when should return");
|
|
|
|
QList<QDir> dirList{std::move(dirs)};
|
|
|
|
|
|
|
|
while (!dirList.isEmpty()) {
|
|
|
|
const auto dir = dirList.takeFirst();
|
|
|
|
|
|
|
|
if (!dir.exists()) {
|
|
|
|
qWarning() << "apply function to an non-existent directory:" << dir.absolutePath() << ", skip.";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &infoList =
|
|
|
|
dir.entryInfoList({"*.desktop"},
|
|
|
|
QDir::Readable | QDir::AllDirs | QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot,
|
|
|
|
QDir::Name | QDir::DirsLast);
|
|
|
|
|
|
|
|
for (const auto &info : infoList) {
|
|
|
|
if (info.isFile() and func(info)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.isDir()) {
|
|
|
|
dirList.append(info.absoluteFilePath());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 14:49:35 +08:00
|
|
|
class ApplicationManager1DBus
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ApplicationManager1DBus(const ApplicationManager1DBus &) = delete;
|
|
|
|
ApplicationManager1DBus(ApplicationManager1DBus &&) = delete;
|
|
|
|
ApplicationManager1DBus &operator=(const ApplicationManager1DBus &) = delete;
|
2023-07-21 14:47:40 +08:00
|
|
|
ApplicationManager1DBus &operator=(ApplicationManager1DBus &&) = delete;
|
2023-08-10 14:32:09 +08:00
|
|
|
[[nodiscard]] const QString &globalDestBusAddress() const { return m_destBusAddress; }
|
|
|
|
[[nodiscard]] const QString &globalServerBusAddress() const { return m_serverBusAddress; }
|
2023-08-08 15:10:32 +08:00
|
|
|
void initGlobalServerBus(DBusType type, const QString &busAddress = "")
|
2023-07-17 14:49:35 +08:00
|
|
|
{
|
2023-07-24 14:12:59 +08:00
|
|
|
if (m_initFlag) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-08 15:10:32 +08:00
|
|
|
m_serverBusAddress = busAddress;
|
|
|
|
m_serverType = type;
|
2023-07-24 14:12:59 +08:00
|
|
|
m_initFlag = true;
|
|
|
|
}
|
|
|
|
|
2023-08-08 15:10:32 +08:00
|
|
|
QDBusConnection &globalServerBus()
|
2023-07-24 14:12:59 +08:00
|
|
|
{
|
2023-08-08 15:10:32 +08:00
|
|
|
if (m_serverConnection.has_value()) {
|
|
|
|
return m_serverConnection.value();
|
2023-07-24 14:12:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_initFlag) {
|
2023-08-21 16:02:26 +08:00
|
|
|
qFatal("invoke init at first.");
|
2023-07-17 14:49:35 +08:00
|
|
|
}
|
2023-07-24 14:12:59 +08:00
|
|
|
|
2023-08-08 15:10:32 +08:00
|
|
|
switch (m_serverType) {
|
2023-07-24 14:12:59 +08:00
|
|
|
case DBusType::Session:
|
|
|
|
[[fallthrough]];
|
|
|
|
case DBusType::System: {
|
2023-08-08 15:10:32 +08:00
|
|
|
m_serverConnection.emplace(QDBusConnection::connectToBus(static_cast<QDBusConnection::BusType>(m_serverType),
|
|
|
|
ApplicationManagerServerDBusName));
|
|
|
|
if (!m_serverConnection->isConnected()) {
|
2023-08-21 16:02:26 +08:00
|
|
|
qFatal("%s", m_serverConnection->lastError().message().toLocal8Bit().data());
|
2023-07-24 14:12:59 +08:00
|
|
|
}
|
2023-08-08 15:10:32 +08:00
|
|
|
return m_serverConnection.value();
|
2023-07-24 14:12:59 +08:00
|
|
|
}
|
|
|
|
case DBusType::Custom: {
|
2023-08-08 15:10:32 +08:00
|
|
|
if (m_serverBusAddress.isEmpty()) {
|
2023-08-21 16:02:26 +08:00
|
|
|
qFatal("connect to custom dbus must init this object by custom dbus address");
|
2023-07-24 14:12:59 +08:00
|
|
|
}
|
2023-08-08 15:10:32 +08:00
|
|
|
m_serverConnection.emplace(QDBusConnection::connectToBus(m_serverBusAddress, ApplicationManagerServerDBusName));
|
|
|
|
if (!m_serverConnection->isConnected()) {
|
2023-08-21 16:02:26 +08:00
|
|
|
qFatal("%s", m_serverConnection->lastError().message().toLocal8Bit().data());
|
2023-07-24 14:12:59 +08:00
|
|
|
}
|
2023-08-08 15:10:32 +08:00
|
|
|
return m_serverConnection.value();
|
2023-07-24 14:12:59 +08:00
|
|
|
}
|
|
|
|
}
|
2023-08-08 15:10:32 +08:00
|
|
|
|
2023-07-24 14:12:59 +08:00
|
|
|
Q_UNREACHABLE();
|
2023-07-17 14:49:35 +08:00
|
|
|
}
|
|
|
|
static ApplicationManager1DBus &instance()
|
|
|
|
{
|
|
|
|
static ApplicationManager1DBus dbus;
|
|
|
|
return dbus;
|
|
|
|
}
|
|
|
|
|
2023-08-08 15:10:32 +08:00
|
|
|
QDBusConnection &globalDestBus()
|
|
|
|
{
|
|
|
|
if (!m_destConnection) {
|
2023-08-21 16:02:26 +08:00
|
|
|
qFatal("please set which bus should application manager to use to invoke other D-Bus service's method.");
|
2023-08-08 15:10:32 +08:00
|
|
|
}
|
|
|
|
return m_destConnection.value();
|
|
|
|
}
|
|
|
|
|
2023-08-15 14:43:34 +08:00
|
|
|
void setDestBus(const QString &destAddress = "")
|
2023-08-08 15:10:32 +08:00
|
|
|
{
|
|
|
|
if (m_destConnection) {
|
|
|
|
m_destConnection->disconnectFromBus(ApplicationManagerDestDBusName);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_destBusAddress = destAddress;
|
|
|
|
|
|
|
|
if (m_destBusAddress.isEmpty()) {
|
|
|
|
m_destConnection.emplace(
|
|
|
|
QDBusConnection::connectToBus(QDBusConnection::BusType::SessionBus, ApplicationManagerDestDBusName));
|
|
|
|
if (!m_destConnection->isConnected()) {
|
2023-08-21 16:02:26 +08:00
|
|
|
qFatal("%s", m_destConnection->lastError().message().toLocal8Bit().data());
|
2023-08-08 15:10:32 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-10 14:32:09 +08:00
|
|
|
if (m_destBusAddress.isEmpty()) {
|
2023-08-21 16:02:26 +08:00
|
|
|
qFatal("connect to custom dbus must init this object by custom dbus address");
|
2023-08-10 14:32:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
m_destConnection.emplace(QDBusConnection::connectToBus(m_destBusAddress, ApplicationManagerDestDBusName));
|
|
|
|
if (!m_destConnection->isConnected()) {
|
2023-08-21 16:02:26 +08:00
|
|
|
qFatal("%s", m_destConnection->lastError().message().toLocal8Bit().data());
|
2023-08-10 14:32:09 +08:00
|
|
|
}
|
2023-08-08 15:10:32 +08:00
|
|
|
}
|
|
|
|
|
2023-07-17 14:49:35 +08:00
|
|
|
private:
|
|
|
|
ApplicationManager1DBus() = default;
|
|
|
|
~ApplicationManager1DBus() = default;
|
2023-08-10 14:32:09 +08:00
|
|
|
bool m_initFlag{false};
|
|
|
|
DBusType m_serverType{};
|
2023-08-08 15:10:32 +08:00
|
|
|
QString m_serverBusAddress;
|
|
|
|
QString m_destBusAddress;
|
|
|
|
std::optional<QDBusConnection> m_destConnection{std::nullopt};
|
|
|
|
std::optional<QDBusConnection> m_serverConnection{std::nullopt};
|
2023-07-17 14:49:35 +08:00
|
|
|
};
|
|
|
|
|
2023-07-24 14:12:59 +08:00
|
|
|
bool registerObjectToDBus(QObject *o, const QString &path, const QString &interface);
|
|
|
|
void unregisterObjectFromDBus(const QString &path);
|
2023-07-17 14:49:35 +08:00
|
|
|
|
2023-08-18 10:01:52 +08:00
|
|
|
inline QString getDBusInterface(const QMetaType &meta)
|
2023-07-17 14:49:35 +08:00
|
|
|
{
|
2023-08-18 10:01:52 +08:00
|
|
|
const auto *infoObject = meta.metaObject();
|
2023-07-17 14:49:35 +08:00
|
|
|
if (auto infoIndex = infoObject->indexOfClassInfo("D-Bus Interface"); infoIndex != -1) {
|
|
|
|
return infoObject->classInfo(infoIndex).value();
|
|
|
|
}
|
2023-07-24 14:12:59 +08:00
|
|
|
qWarning() << "no interface found.";
|
2023-07-17 14:49:35 +08:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-08-18 10:01:52 +08:00
|
|
|
inline QStringList getInterfacesListFromObject(QObject *o)
|
|
|
|
{
|
|
|
|
auto childs = o->children();
|
|
|
|
QStringList interfaces;
|
|
|
|
std::for_each(childs.cbegin(), childs.cend(), [&interfaces](QObject *app) {
|
|
|
|
if (app->inherits("QDBusAbstractAdaptor")) {
|
|
|
|
interfaces.emplace_back(getDBusInterface(app->metaObject()->metaType()));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return interfaces;
|
|
|
|
}
|
|
|
|
|
2023-07-24 14:12:59 +08:00
|
|
|
inline uid_t getCurrentUID()
|
|
|
|
{
|
|
|
|
return getuid(); // current use linux getuid
|
|
|
|
}
|
|
|
|
|
|
|
|
inline QLocale getUserLocale()
|
|
|
|
{
|
|
|
|
return QLocale::system(); // current use env
|
|
|
|
}
|
|
|
|
|
|
|
|
inline QString escapeToObjectPath(const QString &str)
|
|
|
|
{
|
|
|
|
if (str.isEmpty()) {
|
|
|
|
return "_";
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ret = str;
|
|
|
|
QRegularExpression re{R"([^a-zA-Z0-9])"};
|
|
|
|
auto matcher = re.globalMatch(ret);
|
|
|
|
while (matcher.hasNext()) {
|
|
|
|
auto replaceList = matcher.next().capturedTexts();
|
|
|
|
replaceList.removeDuplicates();
|
|
|
|
for (const auto &c : replaceList) {
|
|
|
|
auto hexStr = QString::number(static_cast<uint>(c.front().toLatin1()), 16);
|
|
|
|
ret.replace(c, QString{R"(_%1)"}.arg(hexStr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline QString unescapeFromObjectPath(const QString &str)
|
|
|
|
{
|
|
|
|
auto ret = str;
|
|
|
|
for (qsizetype i = 0; i < str.size(); ++i) {
|
|
|
|
if (str[i] == '_' and i + 2 < str.size()) {
|
|
|
|
auto hexStr = str.sliced(i + 1, 2);
|
2023-08-11 10:12:46 +08:00
|
|
|
ret.replace(QString{"_%1"}.arg(hexStr), QChar::fromLatin1(hexStr.toUInt(nullptr, 16)));
|
2023-07-24 14:12:59 +08:00
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-08-11 10:12:46 +08:00
|
|
|
inline QString escapeApplicationId(const QString &id)
|
|
|
|
{
|
|
|
|
if (id.isEmpty()) {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ret = id;
|
|
|
|
QRegularExpression re{R"([^a-zA-Z0-9])"};
|
|
|
|
auto matcher = re.globalMatch(ret);
|
|
|
|
while (matcher.hasNext()) {
|
|
|
|
auto replaceList = matcher.next().capturedTexts();
|
|
|
|
replaceList.removeDuplicates();
|
|
|
|
for (const auto &c : replaceList) {
|
|
|
|
auto hexStr = QString::number(static_cast<uint>(c.front().toLatin1()), 16);
|
|
|
|
ret.replace(c, QString{R"(\x%1)"}.arg(hexStr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline QString unescapeApplicationId(const QString &id)
|
|
|
|
{
|
|
|
|
auto ret = id;
|
|
|
|
for (qsizetype i = 0; i < id.size(); ++i) {
|
|
|
|
if (id[i] == '\\' and i + 3 < id.size()) {
|
|
|
|
auto hexStr = id.sliced(i + 2, 2);
|
|
|
|
ret.replace(QString{R"(\x%1)"}.arg(hexStr), QChar::fromLatin1(hexStr.toUInt(nullptr, 16)));
|
|
|
|
i += 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-08-10 14:32:09 +08:00
|
|
|
inline QString getRelativePathFromAppId(const QString &id)
|
|
|
|
{
|
|
|
|
QString path;
|
|
|
|
auto components = id.split('-', Qt::SkipEmptyParts);
|
|
|
|
for (qsizetype i = 0; i < components.size() - 1; ++i) {
|
|
|
|
path += QString{"/%1"}.arg(components[i]);
|
|
|
|
}
|
|
|
|
path += QString{R"(-%1.desktop)"}.arg(components.last());
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2023-08-11 17:46:46 +08:00
|
|
|
inline QStringList getXDGDataDirs()
|
|
|
|
{
|
|
|
|
const static auto XDGDataDirs = []() {
|
2023-08-14 11:56:47 +08:00
|
|
|
auto XDGDataDirs = QString::fromLocal8Bit(qgetenv("XDG_DATA_DIRS")).split(':', Qt::SkipEmptyParts);
|
2023-08-11 17:46:46 +08:00
|
|
|
|
2023-08-14 11:56:47 +08:00
|
|
|
if (XDGDataDirs.isEmpty()) {
|
|
|
|
XDGDataDirs.append("/usr/local/share");
|
|
|
|
XDGDataDirs.append("/usr/share");
|
2023-08-11 17:46:46 +08:00
|
|
|
}
|
|
|
|
|
2023-08-14 11:56:47 +08:00
|
|
|
auto XDGDataHome = QString::fromLocal8Bit(qgetenv("XDG_DATA_HOME"));
|
|
|
|
if (XDGDataHome.isEmpty()) {
|
|
|
|
XDGDataHome = QString{qgetenv("HOME")} + QDir::separator() + ".local" + QDir::separator() + "share";
|
|
|
|
}
|
|
|
|
|
|
|
|
XDGDataDirs.push_front(XDGDataHome);
|
|
|
|
|
|
|
|
std::for_each(XDGDataDirs.begin(), XDGDataDirs.end(), [](QString &str) {
|
2023-08-11 17:46:46 +08:00
|
|
|
if (!str.endsWith(QDir::separator())) {
|
|
|
|
str.append(QDir::separator());
|
|
|
|
}
|
|
|
|
str.append("applications");
|
|
|
|
});
|
|
|
|
|
2023-08-14 11:56:47 +08:00
|
|
|
return XDGDataDirs;
|
2023-08-11 17:46:46 +08:00
|
|
|
}();
|
|
|
|
|
|
|
|
return XDGDataDirs;
|
|
|
|
}
|
|
|
|
|
2023-08-15 14:43:34 +08:00
|
|
|
inline QPair<QString, QString> processUnitName(const QString &unitName)
|
|
|
|
{
|
|
|
|
QString instanceId;
|
|
|
|
QString applicationId;
|
|
|
|
|
|
|
|
if (unitName.endsWith(".service")) {
|
|
|
|
auto lastDotIndex = unitName.lastIndexOf('.');
|
|
|
|
auto app = unitName.sliced(0, lastDotIndex); // remove suffix
|
|
|
|
|
|
|
|
if (app.contains('@')) {
|
|
|
|
auto atIndex = app.indexOf('@');
|
|
|
|
instanceId = app.sliced(atIndex + 1);
|
|
|
|
app.remove(atIndex, instanceId.length() + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
applicationId = app.split('-').last(); // drop launcher if it exists.
|
|
|
|
} else if (unitName.endsWith(".scope")) {
|
|
|
|
auto lastDotIndex = unitName.lastIndexOf('.');
|
|
|
|
auto app = unitName.sliced(0, lastDotIndex);
|
|
|
|
|
|
|
|
auto components = app.split('-');
|
|
|
|
instanceId = components.takeLast();
|
|
|
|
applicationId = components.takeLast();
|
|
|
|
} else {
|
|
|
|
qDebug() << "it's not service or scope:" << unitName << "ignore.";
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instanceId.isEmpty()) {
|
|
|
|
instanceId = QUuid::createUuid().toString(QUuid::Id128);
|
|
|
|
}
|
|
|
|
|
|
|
|
return qMakePair(unescapeApplicationId(applicationId), std::move(instanceId));
|
|
|
|
}
|
|
|
|
|
2023-08-18 17:22:38 +08:00
|
|
|
template <typename T>
|
|
|
|
ObjectMap dumpDBusObject(const QMap<QDBusObjectPath, QSharedPointer<T>> &map)
|
|
|
|
{
|
|
|
|
ObjectMap objs;
|
|
|
|
|
|
|
|
for (const auto &[key, value] : map.asKeyValueRange()) {
|
|
|
|
auto interfaces = getInterfacesListFromObject(value.data());
|
|
|
|
objs.insert(key, interfaces);
|
|
|
|
}
|
|
|
|
|
|
|
|
return objs;
|
|
|
|
}
|
|
|
|
|
2023-08-21 16:02:26 +08:00
|
|
|
inline std::size_t getFileModifiedTime(QFile &file)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
QFileInfo info{file};
|
|
|
|
|
|
|
|
if (!file.isOpen()) {
|
|
|
|
if (auto ret = file.open(QFile::ExistingOnly | QFile::ReadOnly | QFile::Text); !ret) {
|
|
|
|
qWarning() << "open file" << info.absoluteFilePath() << "failed.";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto ret = stat(info.absoluteFilePath().toLocal8Bit().data(), &buf); ret == -1) {
|
|
|
|
qWarning() << "get file" << info.absoluteFilePath() << "state failed:" << std::strerror(errno);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr std::size_t secToNano = 1e9;
|
|
|
|
return buf.st_mtim.tv_sec * secToNano + buf.st_mtim.tv_nsec;
|
|
|
|
}
|
|
|
|
|
2023-07-17 14:49:35 +08:00
|
|
|
#endif
|