1 | // SPDX-License-Identifier: LGPL-2.0-or-later |
2 | // SPDX-FileCopyrightText: 2023 Harald Sitter <sitter@kde.org> |
3 | |
4 | #pragma once |
5 | |
6 | #include <cctype> |
7 | #include <locale> |
8 | |
9 | #include <QString> |
10 | |
11 | inline QString kconfigDBusSanitizePath(QString path) |
12 | { |
13 | for (auto &character : path) { |
14 | if ((std::isalnum(c: character.toLatin1(), loc: std::locale::classic()) == 0) && character != QLatin1Char('_') && character != QLatin1Char('/')) { |
15 | character = QLatin1Char('_'); |
16 | } |
17 | } |
18 | // KConfig notifying or watching on / makes no sense |
19 | Q_ASSERT_X(path.length() > 1, Q_FUNC_INFO, qUtf8Printable(path)); |
20 | // As per spec the path must start with a slash |
21 | Q_ASSERT_X(path.at(0) == QLatin1Char('/'), Q_FUNC_INFO, qUtf8Printable(path)); |
22 | // ...but not end with a slash |
23 | Q_ASSERT_X(path.at(path.size() - 1) != QLatin1Char('/'), Q_FUNC_INFO, qUtf8Printable(path)); |
24 | // ...it also must not contain multiple slashes in sequence |
25 | Q_ASSERT_X(!path.contains(QLatin1String("//" )), Q_FUNC_INFO, qUtf8Printable(path)); |
26 | return path; |
27 | } |
28 | |