Skip to content

Commit

Permalink
sync: from linuxdeepin/dtkcore
Browse files Browse the repository at this point in the history
Synchronize source files from linuxdeepin/dtkcore.

Source-pull-request: linuxdeepin/dtkcore#389
  • Loading branch information
deepin-ci-robot committed Oct 26, 2023
1 parent 402d493 commit 4b59a19
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 5 deletions.
12 changes: 12 additions & 0 deletions docs/global/dconfigfile.zh_CN.dox
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ dtk_add_config_meta_files(
"name": "map",
"permissions": "readwrite",
"visibility": "public"
},
"publicConfig": {
"value": true,
"serial": 0,
"flags": ["user-public"],
"name": "public configure",
"name[zh_CN]": "我是公开的配置",
"description": "I am public configure",
"permissions": "readwrite",
"visibility": "private"
}
}
}
Expand Down Expand Up @@ -188,6 +198,8 @@ sudo ./dconfigfile-example
@brief 存在此标记时,将表明则此配置项不可被覆盖(详见下述 override 机制)。反之,不存在此标记时表明此配置项允许被覆盖,对于此类配置项,如若其有界面设置入口,则当此项不可写时,应当隐藏或禁用界面的设置入口.
@var Dtk::Core::DConfigFile::Flag Dtk::Core::DConfigFile::Global
@brief 当读写此类配置时,将忽略用户身份,无论程序使用哪个用户身份执行,读操作都将获取到同样的数据,写操作将对所有用户都生效。但是,如果对应的配置存储目录不存在或无权限写入,则忽略此标志
@var Dtk::Core::DConfigFile::Flag Dtk::Core::DConfigFile::UserPublic
@brief 该类配置项允许被其他用户访问


@enum Dtk::Core::DConfigFile::Permissions
Expand Down
3 changes: 2 additions & 1 deletion include/global/dconfigfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class LIBDTKCORESHARED_EXPORT DConfigFile : public DObject{
public:
enum Flag {
NoOverride = 1 << 0,
Global = 1 << 1
Global = 1 << 1,
UserPublic = 1 << 2
};
Q_DECLARE_FLAGS(Flags, Flag)

Expand Down
2 changes: 2 additions & 0 deletions include/global/dsgapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class LIBDTKCORESHARED_EXPORT DSGApplication
public:
static QByteArray id();
static QByteArray getId(qint64 pid);

static QByteArray escape2ObjectPath(const QByteArray &appid);
};

DCORE_END_NAMESPACE
Expand Down
2 changes: 2 additions & 0 deletions src/dconfigfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ class Q_DECL_HIDDEN DConfigInfo {
flags |= DConfigFile::NoOverride;
} else if (flag == QLatin1String("global")) {
flags |= DConfigFile::Global;
} else if (flag == QLatin1String("user-public")) {
flags |= DConfigFile::UserPublic;
}
}

Expand Down
66 changes: 62 additions & 4 deletions src/dsgapplication.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2022-2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "dsgapplication.h"

#include <sys/syscall.h>
#include <unistd.h>

#include <QByteArray>
#include <QCoreApplication>
#include <QScopeGuard>
#include <QDebug>
#include <QDBusUnixFileDescriptor>
#include <QDBusReply>
#include <QRegularExpression>
#include <QLoggingCategory>

#include <DDBusInterface>

#ifdef QT_DEBUG
Q_LOGGING_CATEGORY(dsgApp, "dtk.core.dsg")
#else
Q_LOGGING_CATEGORY(dsgApp, "dtk.core.dsg", QtInfoMsg)
#endif

DCORE_BEGIN_NAMESPACE

Expand Down Expand Up @@ -34,10 +51,51 @@ QByteArray DSGApplication::id()
return result;
}

QByteArray DSGApplication::getId(qint64)
QByteArray DSGApplication::getId(qint64 pid)
{
// TODO(zccrs): Call the org.desktopspec.ApplicationManager DBus service
return nullptr;
int pidfd = syscall(SYS_pidfd_open, pid, 0);
if (pidfd < 0) {
qCWarning(dsgApp) << "pidfd open failed:" << strerror(errno);
return QByteArray();
}

QScopeGuard guard([pidfd](){
close(pidfd);
});

DDBusInterface infc("org.desktopspec.ApplicationManager1",
"/org/desktopspec/ApplicationManager1",
"org.desktopspec.ApplicationManager1");

QDBusReply<QString> reply = infc.call("Identify", QVariant::fromValue(QDBusUnixFileDescriptor(pidfd)));

if (!reply.isValid()) {
qCWarning(dsgApp) << "Identify from AM failed." << reply.error().message();
return QByteArray();
}


return reply.value().toLatin1();
}

QByteArray DSGApplication::escape2ObjectPath(const QByteArray &appid)
{
if (appid.isEmpty())
return "_";

QString ret = appid;
static 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.toLatin1();
}

DCORE_END_NAMESPACE
10 changes: 10 additions & 0 deletions tests/data/dconf-example.meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@
"name": "array value type",
"permissions": "readwrite",
"visibility": "public"
},
"publicConfig": {
"value": true,
"serial": 0,
"flags": ["user-public"],
"name": "public configure",
"name[zh_CN]": "我是公开的配置",
"description": "I am public configure",
"permissions": "readwrite",
"visibility": "private"
}
}
}
11 changes: 11 additions & 0 deletions tests/ut_dconfigfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,14 @@ TEST_F(ut_DConfigFile, setSubpath) {
ASSERT_FALSE(config.load(LocalPrefix));
}
}

TEST_F(ut_DConfigFile, userPublic) {

FileCopyGuard guard(":/data/dconf-example.meta.json", QString("%1/%2.json").arg(metaPath, FILE_NAME));
{
DConfigFile config(APP_ID, FILE_NAME);
ASSERT_TRUE(config.load(LocalPrefix));
ASSERT_TRUE(config.meta()->flags("publicConfig").testFlag(DConfigFile::UserPublic));
ASSERT_FALSE(config.meta()->flags("canExit").testFlag(DConfigFile::UserPublic));
}
}

0 comments on commit 4b59a19

Please sign in to comment.