2023-07-12 15:58:41 +08:00
|
|
|
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
#include <QMap>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QLocale>
|
|
|
|
#include <QTextStream>
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
constexpr static auto defaultKeyStr = "default";
|
|
|
|
|
2023-07-21 14:47:40 +08:00
|
|
|
enum class ParseError { NoError, NotFound, MismatchedFile, InvalidLocation, OpenFailed, GroupHeaderInvalid, EntryKeyInvalid };
|
2023-07-12 15:58:41 +08:00
|
|
|
|
2023-07-24 14:12:59 +08:00
|
|
|
struct DesktopFile
|
|
|
|
{
|
|
|
|
DesktopFile(const DesktopFile &) = default;
|
|
|
|
DesktopFile(DesktopFile &&) = default;
|
|
|
|
DesktopFile &operator=(const DesktopFile &) = default;
|
|
|
|
DesktopFile &operator=(DesktopFile &&) = default;
|
|
|
|
~DesktopFile() = default;
|
|
|
|
|
|
|
|
const QString &filePath() const { return m_filePath; }
|
|
|
|
const QString &desktopId() const { return m_desktopId; }
|
|
|
|
|
|
|
|
static std::optional<DesktopFile> searchDesktopFile(const QString &desktopFilePath, ParseError &err) noexcept;
|
|
|
|
|
|
|
|
private:
|
|
|
|
DesktopFile(QString &&path, QString &&fileId)
|
|
|
|
: m_filePath(std::move(path))
|
|
|
|
, m_desktopId(std::move(fileId))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
QString m_filePath{""};
|
|
|
|
QString m_desktopId{""};
|
|
|
|
};
|
|
|
|
|
2023-07-12 15:58:41 +08:00
|
|
|
class DesktopEntry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
class Value : public QMap<QString, QString>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using QMap<QString, QString>::QMap;
|
|
|
|
QString toString(bool &ok) const noexcept;
|
|
|
|
bool toBoolean(bool &ok) const noexcept;
|
|
|
|
QString toIconString(bool &ok) const noexcept;
|
|
|
|
float toNumeric(bool &ok) const noexcept;
|
|
|
|
QString toLocaleString(const QLocale &locale, bool &ok) const noexcept;
|
|
|
|
friend QDebug operator<<(QDebug debug, const DesktopEntry::Value &v);
|
|
|
|
|
|
|
|
private:
|
2023-07-19 17:56:45 +08:00
|
|
|
[[nodiscard]] QString unescape(const QString &str) const noexcept;
|
2023-07-12 15:58:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
DesktopEntry() = default;
|
|
|
|
~DesktopEntry() = default;
|
2023-07-24 14:12:59 +08:00
|
|
|
[[nodiscard]] ParseError parse(const DesktopFile &file) noexcept;
|
2023-07-21 14:47:40 +08:00
|
|
|
[[nodiscard]] ParseError parse(QTextStream &stream) noexcept;
|
2023-07-24 14:12:59 +08:00
|
|
|
[[nodiscard]] std::optional<QMap<QString, Value>> group(const QString &key) const noexcept;
|
|
|
|
[[nodiscard]] std::optional<Value> value(const QString &key, const QString &valueKey) const noexcept;
|
2023-07-12 15:58:41 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
QMap<QString, QMap<QString, Value>> m_entryMap;
|
|
|
|
auto parserGroupHeader(const QString &str) noexcept;
|
|
|
|
ParseError parseEntry(const QString &str, decltype(m_entryMap)::iterator ¤tGroup) noexcept;
|
|
|
|
};
|
|
|
|
|
2023-07-21 14:47:40 +08:00
|
|
|
QDebug operator<<(QDebug debug, const DesktopEntry::Value &v);
|
2023-07-19 17:56:45 +08:00
|
|
|
|
|
|
|
QDebug operator<<(QDebug debug, const ParseError &v);
|