File indexing completed on 2024-04-28 03:53:14

0001 // SPDX-License-Identifier: LGPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2023 Harald Sitter <sitter@kde.org>
0003 
0004 #pragma once
0005 
0006 #include <cctype>
0007 #include <locale>
0008 
0009 #include <QString>
0010 
0011 inline QString kconfigDBusSanitizePath(QString path)
0012 {
0013     for (auto &character : path) {
0014         if ((std::isalnum(character.toLatin1(), std::locale::classic()) == 0) && character != QLatin1Char('_') && character != QLatin1Char('/')) {
0015             character = QLatin1Char('_');
0016         }
0017     }
0018     // KConfig notifying or watching on / makes no sense
0019     Q_ASSERT_X(path.length() > 1, Q_FUNC_INFO, qUtf8Printable(path));
0020     // As per spec the path must start with a slash
0021     Q_ASSERT_X(path.at(0) == QLatin1Char('/'), Q_FUNC_INFO, qUtf8Printable(path));
0022     // ...but not end with a slash
0023     Q_ASSERT_X(path.at(path.size() - 1) != QLatin1Char('/'), Q_FUNC_INFO, qUtf8Printable(path));
0024     // ...it also must not contain multiple slashes in sequence
0025     Q_ASSERT_X(!path.contains(QLatin1String("//")), Q_FUNC_INFO, qUtf8Printable(path));
0026     return path;
0027 }