refact: improve method of launcher helper append variant value
Signed-off-by: ComixHe <heyuming@deepin.org>
This commit is contained in:
parent
94db47a015
commit
3a9973c772
@ -1,7 +1,10 @@
|
|||||||
find_package(PkgConfig REQUIRED)
|
find_package(PkgConfig REQUIRED)
|
||||||
pkg_check_modules(SYSTEMD REQUIRED IMPORTED_TARGET libsystemd)
|
pkg_check_modules(SYSTEMD REQUIRED IMPORTED_TARGET libsystemd)
|
||||||
|
|
||||||
add_executable(${APP_LAUNCH_HELPER_BIN} main.cpp)
|
file(GLOB APP_LAUNCH_HELPER_SOURCE ${CMAKE_CURRENT_LIST_DIR}/*.cpp)
|
||||||
|
|
||||||
|
add_executable(${APP_LAUNCH_HELPER_BIN} ${APP_LAUNCH_HELPER_SOURCE})
|
||||||
|
|
||||||
target_link_libraries(${APP_LAUNCH_HELPER_BIN} PRIVATE
|
target_link_libraries(${APP_LAUNCH_HELPER_BIN} PRIVATE
|
||||||
PkgConfig::SYSTEMD
|
PkgConfig::SYSTEMD
|
||||||
)
|
)
|
||||||
|
@ -3,35 +3,21 @@
|
|||||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||||
|
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
#include <systemd/sd-bus.h>
|
#include <unordered_map>
|
||||||
#include <systemd/sd-journal.h>
|
|
||||||
#include <string_view>
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <deque>
|
#include <deque>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include "constant.h"
|
#include "constant.h"
|
||||||
|
#include "types.h"
|
||||||
|
#include "variantValue.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
enum class ExitCode { SystemdError = -3, InvalidInput = -2, InternalError = -1, Done = 0, Waiting = 1 };
|
|
||||||
|
|
||||||
struct JobRemoveResult
|
|
||||||
{
|
|
||||||
std::string_view id;
|
|
||||||
int removedFlag{0};
|
|
||||||
ExitCode result{ExitCode::Waiting};
|
|
||||||
};
|
|
||||||
|
|
||||||
using msg_ptr = sd_bus_message *;
|
|
||||||
using bus_ptr = sd_bus *;
|
|
||||||
|
|
||||||
ExitCode fromString(const std::string &str)
|
ExitCode fromString(const std::string &str)
|
||||||
{
|
{
|
||||||
if (str == "done") {
|
if (str == "done") {
|
||||||
@ -153,6 +139,45 @@ int processExecStart(msg_ptr &msg, const std::deque<std::string_view> &execArgs)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DBusValueType getPropType(std::string_view key)
|
||||||
|
{
|
||||||
|
static std::unordered_map<std::string_view, DBusValueType> map{{"Environment", DBusValueType::ArrayOfString}};
|
||||||
|
|
||||||
|
if (auto it = map.find(key); it != map.cend()) {
|
||||||
|
return it->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
return DBusValueType::Default;
|
||||||
|
}
|
||||||
|
|
||||||
|
int appendPropValue(msg_ptr &msg, DBusValueType type, std::string_view value)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
auto handler = creatValueHandler(msg, type);
|
||||||
|
if (handler == nullptr) {
|
||||||
|
sd_journal_perror("unknown type of property's variant.");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret = handler->openVariant(); ret < 0) {
|
||||||
|
sd_journal_perror("open property's variant value failed.");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret = handler->appendValue(std::string{value}); ret < 0) {
|
||||||
|
sd_journal_perror("append property's variant value failed.");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret = handler->closeVariant(); ret < 0) {
|
||||||
|
sd_journal_perror("close property's variant value failed.");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int processKVPair(msg_ptr &msg, const std::map<std::string_view, std::string_view> &props)
|
int processKVPair(msg_ptr &msg, const std::map<std::string_view, std::string_view> &props)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
@ -160,9 +185,23 @@ int processKVPair(msg_ptr &msg, const std::map<std::string_view, std::string_vie
|
|||||||
for (auto [key, value] : props) {
|
for (auto [key, value] : props) {
|
||||||
std::string keyStr{key};
|
std::string keyStr{key};
|
||||||
std::string valueStr{value};
|
std::string valueStr{value};
|
||||||
ret = sd_bus_message_append(msg, "(sv)", keyStr.data(), "s", valueStr.data());
|
if (ret = sd_bus_message_open_container(msg, SD_BUS_TYPE_STRUCT, "sv"); ret < 0) {
|
||||||
|
sd_journal_perror("open struct of properties failed.");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
if (ret < 0) {
|
if (ret = sd_bus_message_append(msg, "s", keyStr.data()); ret < 0) {
|
||||||
|
sd_journal_perror("append key of property failed.");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret = appendPropValue(msg, getPropType(key), valueStr); ret < 0) {
|
||||||
|
sd_journal_perror("append value of property failed.");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret = sd_bus_message_close_container(msg); ret < 0) {
|
||||||
|
sd_journal_perror("close struct of properties failed.");
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
27
apps/app-launch-helper/src/types.h
Normal file
27
apps/app-launch-helper/src/types.h
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||||
|
|
||||||
|
#ifndef TYPES_H
|
||||||
|
#define TYPES_H
|
||||||
|
|
||||||
|
#include <systemd/sd-bus.h>
|
||||||
|
#include <systemd/sd-journal.h>
|
||||||
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
enum class ExitCode { SystemdError = -3, InvalidInput = -2, InternalError = -1, Done = 0, Waiting = 1 };
|
||||||
|
|
||||||
|
enum class DBusValueType { Default, ArrayOfString };
|
||||||
|
|
||||||
|
using msg_ptr = sd_bus_message *;
|
||||||
|
using bus_ptr = sd_bus *;
|
||||||
|
|
||||||
|
struct JobRemoveResult
|
||||||
|
{
|
||||||
|
std::string_view id;
|
||||||
|
int removedFlag{0};
|
||||||
|
ExitCode result{ExitCode::Waiting};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
50
apps/app-launch-helper/src/variantValue.h
Normal file
50
apps/app-launch-helper/src/variantValue.h
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||||
|
#include "types.h"
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
class VariantValue
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit VariantValue(msg_ptr &msg)
|
||||||
|
: m_msg(msg)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
virtual ~VariantValue() = default;
|
||||||
|
VariantValue(const VariantValue &) = delete;
|
||||||
|
VariantValue(VariantValue &&) = delete;
|
||||||
|
VariantValue &operator=(const VariantValue &) = delete;
|
||||||
|
VariantValue &operator=(VariantValue &&) = delete;
|
||||||
|
|
||||||
|
virtual int openVariant() noexcept = 0;
|
||||||
|
virtual int closeVariant() noexcept = 0;
|
||||||
|
virtual int appendValue(std::string &&value) noexcept = 0;
|
||||||
|
|
||||||
|
msg_ptr &msgRef() noexcept { return m_msg; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
msg_ptr &m_msg;
|
||||||
|
};
|
||||||
|
|
||||||
|
class StringValue : public VariantValue
|
||||||
|
{
|
||||||
|
using VariantValue::VariantValue;
|
||||||
|
|
||||||
|
public:
|
||||||
|
int openVariant() noexcept override;
|
||||||
|
int closeVariant() noexcept override;
|
||||||
|
int appendValue(std::string &&value) noexcept override;
|
||||||
|
};
|
||||||
|
|
||||||
|
class ASValue : public VariantValue
|
||||||
|
{
|
||||||
|
using VariantValue::VariantValue;
|
||||||
|
|
||||||
|
public:
|
||||||
|
int openVariant() noexcept override;
|
||||||
|
int closeVariant() noexcept override;
|
||||||
|
int appendValue(std::string &&value) noexcept override;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::unique_ptr<VariantValue> creatValueHandler(msg_ptr &msg, DBusValueType type);
|
62
apps/app-launch-helper/src/variantvalue.cpp
Normal file
62
apps/app-launch-helper/src/variantvalue.cpp
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||||
|
|
||||||
|
#include "variantValue.h"
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
std::unique_ptr<VariantValue> creatValueHandler(msg_ptr &msg, DBusValueType type)
|
||||||
|
{
|
||||||
|
switch (type) {
|
||||||
|
case DBusValueType::Default:
|
||||||
|
return std::make_unique<StringValue>(msg);
|
||||||
|
case DBusValueType::ArrayOfString:
|
||||||
|
return std::make_unique<ASValue>(msg);
|
||||||
|
default:
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int StringValue::openVariant() noexcept
|
||||||
|
{
|
||||||
|
return sd_bus_message_open_container(msgRef(), SD_BUS_TYPE_VARIANT, "s");
|
||||||
|
}
|
||||||
|
|
||||||
|
int StringValue::closeVariant() noexcept
|
||||||
|
{
|
||||||
|
return sd_bus_message_close_container(msgRef());
|
||||||
|
}
|
||||||
|
|
||||||
|
int StringValue::appendValue(std::string &&value) noexcept
|
||||||
|
{
|
||||||
|
return sd_bus_message_append(msgRef(), "s", value.data());
|
||||||
|
}
|
||||||
|
|
||||||
|
int ASValue::openVariant() noexcept
|
||||||
|
{
|
||||||
|
return sd_bus_message_open_container(msgRef(), SD_BUS_TYPE_VARIANT, "as");
|
||||||
|
}
|
||||||
|
|
||||||
|
int ASValue::closeVariant() noexcept
|
||||||
|
{
|
||||||
|
return sd_bus_message_close_container(msgRef());
|
||||||
|
}
|
||||||
|
|
||||||
|
int ASValue::appendValue(std::string &&value) noexcept
|
||||||
|
{
|
||||||
|
std::string envs{std::move(value)};
|
||||||
|
std::istringstream stream{envs};
|
||||||
|
int ret{0};
|
||||||
|
|
||||||
|
if (ret = sd_bus_message_open_container(msgRef(), SD_BUS_TYPE_ARRAY, "s"); ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (std::string line; std::getline(stream, line, ';');) {
|
||||||
|
if (ret = sd_bus_message_append(msgRef(), "s", line.data()); ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sd_bus_message_close_container(msgRef());
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user