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>
|
2023-08-10 14:32:09 +08:00
|
|
|
#include <sys/stat.h>
|
2023-07-12 15:58:41 +08:00
|
|
|
|
|
|
|
constexpr static auto defaultKeyStr = "default";
|
|
|
|
|
2023-08-11 17:46:46 +08:00
|
|
|
enum class DesktopErrorCode {
|
|
|
|
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;
|
|
|
|
|
2023-08-10 14:32:09 +08:00
|
|
|
[[nodiscard]] const QString &filePath() const { return m_filePath; }
|
|
|
|
[[nodiscard]] const QString &desktopId() const { return m_desktopId; }
|
2023-07-24 14:12:59 +08:00
|
|
|
|
2023-08-11 17:46:46 +08:00
|
|
|
static std::optional<DesktopFile> searchDesktopFileById(const QString &appId, DesktopErrorCode &err) noexcept;
|
|
|
|
static std::optional<DesktopFile> searchDesktopFileByPath(const QString &desktopFilePath, DesktopErrorCode &err) noexcept;
|
2023-08-10 14:32:09 +08:00
|
|
|
[[nodiscard]] bool modified(std::size_t time) const noexcept;
|
2023-07-24 14:12:59 +08:00
|
|
|
|
|
|
|
private:
|
2023-08-10 14:32:09 +08:00
|
|
|
DesktopFile(QString &&path, QString &&fileId, std::size_t mtime)
|
|
|
|
: m_mtime(mtime)
|
|
|
|
, m_filePath(std::move(path))
|
2023-07-24 14:12:59 +08:00
|
|
|
, m_desktopId(std::move(fileId))
|
|
|
|
{
|
|
|
|
}
|
2023-08-10 14:32:09 +08:00
|
|
|
|
|
|
|
std::size_t m_mtime;
|
2023-07-24 14:12:59 +08:00
|
|
|
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-08-10 14:32:09 +08:00
|
|
|
[[nodiscard]] static QString unescape(const QString &str) noexcept;
|
2023-07-12 15:58:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
DesktopEntry() = default;
|
2023-08-10 14:32:09 +08:00
|
|
|
DesktopEntry(const DesktopEntry &) = default;
|
|
|
|
DesktopEntry(DesktopEntry &&) = default;
|
|
|
|
DesktopEntry &operator=(const DesktopEntry &) = default;
|
|
|
|
DesktopEntry &operator=(DesktopEntry &&) = default;
|
|
|
|
|
2023-07-12 15:58:41 +08:00
|
|
|
~DesktopEntry() = default;
|
2023-08-11 17:46:46 +08:00
|
|
|
[[nodiscard]] DesktopErrorCode parse(const DesktopFile &file) noexcept;
|
|
|
|
[[nodiscard]] DesktopErrorCode 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;
|
2023-08-11 17:46:46 +08:00
|
|
|
static DesktopErrorCode parseEntry(const QString &str, decltype(m_entryMap)::iterator ¤tGroup) noexcept;
|
2023-07-12 15:58:41 +08:00
|
|
|
};
|
|
|
|
|
2023-07-21 14:47:40 +08:00
|
|
|
QDebug operator<<(QDebug debug, const DesktopEntry::Value &v);
|
2023-07-19 17:56:45 +08:00
|
|
|
|
2023-08-11 17:46:46 +08:00
|
|
|
QDebug operator<<(QDebug debug, const DesktopErrorCode &v);
|