2023-07-10 10:18:33 +08:00
|
|
|
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
#include "applicationmanager1service.h"
|
2023-07-24 14:12:59 +08:00
|
|
|
#include "applicationmanager1adaptor.h"
|
|
|
|
#include <QFile>
|
|
|
|
#include <unistd.h>
|
2023-07-10 10:18:33 +08:00
|
|
|
|
2023-07-17 14:49:35 +08:00
|
|
|
ApplicationManager1Service::~ApplicationManager1Service() = default;
|
|
|
|
|
2023-07-24 14:12:59 +08:00
|
|
|
ApplicationManager1Service::ApplicationManager1Service(std::unique_ptr<Identifier> ptr, QDBusConnection &connection)
|
|
|
|
: m_identifier(std::move(ptr))
|
|
|
|
{
|
|
|
|
if (!connection.registerService(DDEApplicationManager1ServiceName)) {
|
|
|
|
qFatal() << connection.lastError();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!registerObjectToDBus(new ApplicationManager1Adaptor{this},
|
|
|
|
DDEApplicationManager1ObjectPath,
|
|
|
|
getDBusInterface<ApplicationManager1Adaptor>())) {
|
|
|
|
std::terminate();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_jobManager.reset(new JobManager1Service(this));
|
|
|
|
}
|
2023-07-10 10:18:33 +08:00
|
|
|
|
2023-07-21 14:47:40 +08:00
|
|
|
QList<QDBusObjectPath> ApplicationManager1Service::list() const
|
|
|
|
{
|
|
|
|
return m_applicationList.keys();
|
|
|
|
}
|
2023-07-10 10:18:33 +08:00
|
|
|
|
2023-07-17 14:49:35 +08:00
|
|
|
bool ApplicationManager1Service::removeOneApplication(const QDBusObjectPath &application)
|
2023-07-10 10:18:33 +08:00
|
|
|
{
|
2023-07-17 14:49:35 +08:00
|
|
|
return m_applicationList.remove(application) != 0;
|
2023-07-10 10:18:33 +08:00
|
|
|
}
|
|
|
|
|
2023-07-21 14:47:40 +08:00
|
|
|
void ApplicationManager1Service::removeAllApplication()
|
|
|
|
{
|
|
|
|
m_applicationList.clear();
|
|
|
|
}
|
2023-07-17 14:49:35 +08:00
|
|
|
|
2023-07-24 14:12:59 +08:00
|
|
|
QDBusObjectPath ApplicationManager1Service::Application(const QString &id) const
|
2023-07-10 10:18:33 +08:00
|
|
|
{
|
2023-07-24 14:12:59 +08:00
|
|
|
auto ret = std::find_if(m_applicationList.cbegin(), m_applicationList.cend(), [&id](const auto &app) {
|
|
|
|
if (app->id() == id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (ret != m_applicationList.cend()) {
|
|
|
|
return ret.key();
|
|
|
|
}
|
2023-07-10 10:18:33 +08:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ApplicationManager1Service::Identify(const QDBusUnixFileDescriptor &pidfd,
|
|
|
|
QDBusObjectPath &application,
|
|
|
|
QDBusObjectPath &application_instance)
|
|
|
|
{
|
2023-07-24 14:12:59 +08:00
|
|
|
if (!pidfd.isValid()) {
|
|
|
|
qWarning() << "pidfd isn't a valid unix file descriptor";
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_ASSERT_X(static_cast<bool>(m_identifier), "Identify", "Broken Identifier.");
|
|
|
|
|
|
|
|
QString fdFilePath = QString{"/proc/self/fdinfo/%1"}.arg(pidfd.fileDescriptor());
|
|
|
|
QFile fdFile{fdFilePath};
|
|
|
|
if (!fdFile.open(QFile::ExistingOnly | QFile::ReadOnly | QFile::Text)) {
|
|
|
|
qWarning() << "open " << fdFilePath << "failed: " << fdFile.errorString();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
auto content = fdFile.readAll();
|
|
|
|
QTextStream stream{content};
|
|
|
|
QString appPid;
|
|
|
|
while (!stream.atEnd()) {
|
|
|
|
auto line = stream.readLine();
|
|
|
|
if (line.startsWith("Pid")) {
|
|
|
|
appPid = line.split(":").last().trimmed();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (appPid.isEmpty()) {
|
|
|
|
qWarning() << "can't find pid which corresponding with the instance of this application.";
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
bool ok;
|
|
|
|
auto pid = appPid.toUInt(&ok);
|
|
|
|
if (!ok) {
|
|
|
|
qWarning() << "AppId is failed to convert to uint.";
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto ret = m_identifier->Identify(pid);
|
|
|
|
|
|
|
|
auto app = std::find_if(m_applicationList.cbegin(), m_applicationList.cend(), [&ret](const auto &appPtr) {
|
|
|
|
return appPtr->id() == ret.ApplicationId;
|
|
|
|
});
|
|
|
|
if (app == m_applicationList.cend()) {
|
|
|
|
qWarning() << "can't find application:" << ret.ApplicationId;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
application = m_applicationList.key(*app);
|
|
|
|
application_instance = (*app)->findInstance(ret.InstanceId);
|
|
|
|
return ret.ApplicationId;
|
2023-07-10 10:18:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QDBusObjectPath ApplicationManager1Service::Launch(const QString &id,
|
2023-07-17 14:49:35 +08:00
|
|
|
const QString &actions,
|
2023-07-10 10:18:33 +08:00
|
|
|
const QStringList &fields,
|
|
|
|
const QVariantMap &options)
|
|
|
|
{
|
2023-07-24 14:12:59 +08:00
|
|
|
auto app = Application(id);
|
|
|
|
if (app.path().isEmpty()) {
|
|
|
|
qWarning() << "No such application.";
|
|
|
|
return {};
|
2023-07-17 14:49:35 +08:00
|
|
|
}
|
2023-07-24 14:12:59 +08:00
|
|
|
const auto &value = m_applicationList.value(app);
|
|
|
|
return value->Launch(actions, fields, options);
|
2023-07-10 10:18:33 +08:00
|
|
|
}
|
2023-07-24 14:12:59 +08:00
|
|
|
|
|
|
|
void ApplicationManager1Service::UpdateApplicationInfo(const QStringList &update_path) {}
|