Skip to content

Commit 18eaf24

Browse files
ComixHedeepin-bot[bot]
authored andcommitted
refactor(util): move inline function to library codebase
Signed-off-by: ComixHe <heyuming@deepin.org>
1 parent 12ebd23 commit 18eaf24

3 files changed

Lines changed: 112 additions & 91 deletions

File tree

include/util/dutil.h

Lines changed: 5 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -54,99 +54,13 @@ void SecureErase(T &obj)
5454
}
5555
}
5656

57-
inline QString escapeToObjectPath(const QByteArray &str) noexcept
58-
{
59-
if (str.isEmpty()) {
60-
return QStringLiteral("_");
61-
}
62-
63-
QString ret;
64-
ret.reserve(str.size() * 3);
65-
66-
for (qsizetype i = 0; i < str.size(); ++i) {
67-
auto byte = static_cast<unsigned char>(str.at(i));
68-
if (std::isalnum(byte) != 0 || byte == '/') {
69-
ret.append(QChar::fromLatin1(byte));
70-
} else {
71-
// TODO: a valid dbus object path component only allows "[A-Z][a-z][0-9]_"
72-
// for compatibility with existing applications, we escape all unicode to avoid breakage
73-
// but we should consider to drop this compatibility hack in the future.
74-
ret.append(u'_');
75-
ret.append(QString::number(byte, 16).rightJustified(2, u'0').toLower());
76-
}
77-
}
78-
79-
ret.shrink_to_fit();
80-
return ret;
81-
}
57+
QString escapeToObjectPath(const QByteArray &str) noexcept;
8258

83-
inline QString escapeToObjectPath(const QString &str) noexcept
84-
{
85-
return escapeToObjectPath(str.toUtf8());
86-
}
87-
88-
inline QString unescapeFromObjectPath(const QString &str) noexcept
89-
{
90-
QByteArray ret;
91-
ret.reserve(str.length());
59+
QString escapeToObjectPath(const QString &str) noexcept;
9260

93-
for (qsizetype i = 0; i < str.length();) {
94-
if (i <= str.length() - 3 && str.at(i) == u'_') {
95-
bool ok{false};
96-
auto byte = static_cast<unsigned char>(str.mid(i + 1, 2).toUShort(&ok, 16));
97-
if (ok) {
98-
ret.append(static_cast<char>(byte));
99-
i += 3;
100-
continue;
101-
}
102-
}
61+
QString unescapeFromObjectPath(const QString &str) noexcept;
10362

104-
ret.append(str.at(i).toLatin1());
105-
++i;
106-
}
107-
108-
return QString::fromUtf8(ret);
109-
}
63+
QString getAppIdFromAbsolutePath(const QString &path) noexcept;
11064

111-
inline QString getAppIdFromAbsolutePath(const QString &path)
112-
{
113-
static QString desktopSuffix{u8".desktop"};
114-
const auto &appDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
115-
if (!path.endsWith(desktopSuffix) ||
116-
!std::any_of(appDirs.cbegin(), appDirs.constEnd(), [&path](const QString &dir) { return path.startsWith(dir); })) {
117-
return {};
118-
}
119-
120-
auto tmp = path.chopped(desktopSuffix.size());
121-
auto components = tmp.split(QDir::separator(), Qt::SkipEmptyParts);
122-
auto location = std::find(components.cbegin(), components.cend(), "applications");
123-
if (location == components.cend()) {
124-
return {};
125-
}
126-
127-
auto appId = QStringList{location + 1, components.cend()}.join('-');
128-
return appId;
129-
}
130-
131-
inline QStringList getAbsolutePathFromAppId(const QString &appId)
132-
{
133-
auto components = appId.split('-', Qt::SkipEmptyParts);
134-
auto appDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
135-
136-
QStringList ret;
137-
for (const auto &dirPath : appDirs) {
138-
auto currentDir = dirPath;
139-
for (auto it = components.cbegin(); it != components.cend(); ++it) {
140-
auto currentName = QStringList{it, components.cend()}.join('-') + QString{".desktop"};
141-
QDir dir{currentDir};
142-
if (dir.exists(currentName)) {
143-
ret.append(dir.filePath(currentName));
144-
}
145-
146-
currentDir.append(QDir::separator() + *it);
147-
}
148-
}
149-
150-
return ret;
151-
}
65+
QStringList getAbsolutePathFromAppId(const QString &appId) noexcept;
15266
}

src/util/dutil.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
2+
//
3+
// SPDX-License-Identifier: LGPL-3.0-or-later
4+
5+
#include "dutil.h"
6+
7+
namespace DUtil {
8+
9+
QString escapeToObjectPath(const QByteArray &str) noexcept
10+
{
11+
if (str.isEmpty()) {
12+
return QStringLiteral("_");
13+
}
14+
15+
QString ret;
16+
ret.reserve(str.size() * 3);
17+
18+
for (qsizetype i = 0; i < str.size(); ++i) {
19+
auto byte = static_cast<unsigned char>(str.at(i));
20+
if (std::isalnum(byte) != 0 || byte == '/') {
21+
ret.append(QChar::fromLatin1(byte));
22+
} else {
23+
// TODO: a valid dbus object path component only allows "[A-Z][a-z][0-9]_"
24+
// for compatibility with existing applications, we escape all unicode to avoid breakage
25+
// but we should consider to drop this compatibility hack in the future.
26+
ret.append(u'_');
27+
ret.append(QString::number(byte, 16).rightJustified(2, u'0').toLower());
28+
}
29+
}
30+
31+
ret.shrink_to_fit();
32+
return ret;
33+
}
34+
35+
QString escapeToObjectPath(const QString &str) noexcept
36+
{
37+
return escapeToObjectPath(str.toUtf8());
38+
}
39+
40+
QString unescapeFromObjectPath(const QString &str) noexcept
41+
{
42+
QByteArray ret;
43+
ret.reserve(str.length());
44+
45+
for (qsizetype i = 0; i < str.length();) {
46+
if (i <= str.length() - 3 && str.at(i) == u'_') {
47+
bool ok{false};
48+
auto byte = static_cast<unsigned char>(str.mid(i + 1, 2).toUShort(&ok, 16));
49+
if (ok) {
50+
ret.append(static_cast<char>(byte));
51+
i += 3;
52+
continue;
53+
}
54+
}
55+
56+
ret.append(str.at(i).toLatin1());
57+
++i;
58+
}
59+
60+
return QString::fromUtf8(ret);
61+
}
62+
63+
QString getAppIdFromAbsolutePath(const QString &path) noexcept
64+
{
65+
static QString desktopSuffix{u8".desktop"};
66+
const auto &appDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
67+
if (!path.endsWith(desktopSuffix) ||
68+
!std::any_of(appDirs.cbegin(), appDirs.constEnd(), [&path](const QString &dir) { return path.startsWith(dir); })) {
69+
return {};
70+
}
71+
72+
auto tmp = path.chopped(desktopSuffix.size());
73+
auto components = tmp.split(QDir::separator(), Qt::SkipEmptyParts);
74+
auto location = std::find(components.cbegin(), components.cend(), "applications");
75+
if (location == components.cend()) {
76+
return {};
77+
}
78+
79+
auto appId = QStringList{location + 1, components.cend()}.join('-');
80+
return appId;
81+
}
82+
83+
QStringList getAbsolutePathFromAppId(const QString &appId) noexcept
84+
{
85+
auto components = appId.split('-', Qt::SkipEmptyParts);
86+
auto appDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
87+
88+
QStringList ret;
89+
for (const auto &dirPath : appDirs) {
90+
auto currentDir = dirPath;
91+
for (auto it = components.cbegin(); it != components.cend(); ++it) {
92+
auto currentName = QStringList{it, components.cend()}.join('-') + QString{".desktop"};
93+
QDir dir{currentDir};
94+
if (dir.exists(currentName)) {
95+
ret.append(dir.filePath(currentName));
96+
}
97+
98+
currentDir.append(QDir::separator() + *it);
99+
}
100+
}
101+
102+
return ret;
103+
}
104+
105+
}

src/util/util.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ if(LINUX)
1717
${CMAKE_CURRENT_LIST_DIR}/ddbusextendedabstractinterface.cpp
1818
${CMAKE_CURRENT_LIST_DIR}/ddbusextendedpendingcallwatcher.cpp
1919
${CMAKE_CURRENT_LIST_DIR}/dtextencoding.cpp
20+
${CMAKE_CURRENT_LIST_DIR}/dutil.cpp
2021
)
2122
else()
2223
set(UTILS_SOURCES
@@ -37,6 +38,7 @@ else()
3738
${CMAKE_CURRENT_LIST_DIR}/ddbusextendedabstractinterface.cpp
3839
${CMAKE_CURRENT_LIST_DIR}/ddbusextendedpendingcallwatcher.cpp
3940
${CMAKE_CURRENT_LIST_DIR}/dtextencoding.cpp
41+
${CMAKE_CURRENT_LIST_DIR}/dutil.cpp
4042
)
4143
endif()
4244
file(GLOB UTILS_HEADERS

0 commit comments

Comments
 (0)