@@ -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}
0 commit comments