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";
|
|
|
|
|
|
|
|
enum class ParseError {
|
|
|
|
NoError,
|
|
|
|
NotFound,
|
|
|
|
MismatchedFile,
|
|
|
|
InvalidLocation,
|
|
|
|
OpenFailed,
|
|
|
|
GroupHeaderInvalid,
|
|
|
|
EntryKeyInvalid
|
|
|
|
};
|
|
|
|
|
|
|
|
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-19 17:56:45 +08:00
|
|
|
[[nodiscard]] ParseError parse(QTextStream& stream) noexcept;
|
|
|
|
[[nodiscard]] QMap<QString, Value> group(const QString &key) 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;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DesktopFile
|
|
|
|
{
|
2023-07-19 17:56:45 +08:00
|
|
|
DesktopFile(const DesktopFile &) = default;
|
|
|
|
DesktopFile(DesktopFile &&) = default;
|
|
|
|
DesktopFile &operator=(const DesktopFile &) = default;
|
|
|
|
DesktopFile &operator=(DesktopFile &&) = default;
|
|
|
|
~DesktopFile() = default;
|
2023-07-12 15:58:41 +08:00
|
|
|
|
2023-07-19 17:56:45 +08:00
|
|
|
const QString &filePath() const { return m_filePath; }
|
|
|
|
const QString &desktopId() const { return m_desktopId; }
|
|
|
|
|
2023-07-12 15:58:41 +08:00
|
|
|
static std::optional<DesktopFile> searchDesktopFile(const QString &desktopFilePath, ParseError& err) noexcept;
|
|
|
|
|
|
|
|
private:
|
2023-07-19 17:56:45 +08:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
QDebug operator<<(QDebug debug, const DesktopEntry::Value& v);
|
2023-07-19 17:56:45 +08:00
|
|
|
|
|
|
|
QDebug operator<<(QDebug debug, const ParseError &v);
|