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 "desktopentry.h"
|
2023-08-10 14:32:09 +08:00
|
|
|
#include "global.h"
|
2023-09-20 18:29:42 +08:00
|
|
|
#include "desktopfileparser.h"
|
2023-07-12 15:58:41 +08:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDir>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <QRegularExpression>
|
|
|
|
#include <QDirIterator>
|
|
|
|
#include <QStringView>
|
|
|
|
#include <QVariant>
|
|
|
|
#include <iostream>
|
2023-08-20 18:25:59 +08:00
|
|
|
#include <chrono>
|
2023-08-23 17:20:47 +08:00
|
|
|
#include <cstdio>
|
2023-07-12 15:58:41 +08:00
|
|
|
|
2023-08-25 16:04:55 +08:00
|
|
|
QString DesktopFile::sourcePath() const noexcept
|
|
|
|
{
|
|
|
|
if (!m_fileSource) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
QFileInfo info(*m_fileSource);
|
|
|
|
return info.absoluteFilePath();
|
2023-07-12 15:58:41 +08:00
|
|
|
}
|
|
|
|
|
2023-08-25 10:47:17 +08:00
|
|
|
bool DesktopEntry::checkMainEntryValidation() const noexcept
|
|
|
|
{
|
|
|
|
auto it = m_entryMap.find(DesktopFileEntryKey);
|
|
|
|
if (it == m_entryMap.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto name = it->find("Name"); name == it->end()) {
|
|
|
|
qWarning() << "No Name.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto type = it->find("Type");
|
|
|
|
if (type == it->end()) {
|
|
|
|
qWarning() << "No Type.";
|
2023-09-06 16:49:47 +08:00
|
|
|
for (auto tmp = it->constKeyValueBegin(); tmp != it->constKeyValueEnd(); ++tmp) {
|
2023-09-11 13:01:20 +08:00
|
|
|
const auto &[k, v] = *tmp;
|
2023-08-25 10:47:17 +08:00
|
|
|
qInfo() << "keyName:" << k;
|
2023-09-11 13:01:20 +08:00
|
|
|
for (auto valIt = v.constKeyValueBegin(); valIt != v.constKeyValueEnd(); ++valIt) {
|
2023-09-06 16:49:47 +08:00
|
|
|
const auto &[key, value] = *valIt;
|
2023-08-25 10:47:17 +08:00
|
|
|
qInfo() << key << value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &typeStr = *type->find(defaultKeyStr);
|
|
|
|
|
|
|
|
if (typeStr == "Link") {
|
|
|
|
if (it->find("URL") == it->end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-21 16:02:26 +08:00
|
|
|
std::optional<DesktopFile> DesktopFile::createTemporaryDesktopFile(std::unique_ptr<QFile> temporaryFile) noexcept
|
2023-08-20 18:25:59 +08:00
|
|
|
{
|
2023-08-31 17:59:09 +08:00
|
|
|
auto [ctime, mtime, _] = getFileTimeInfo(QFileInfo{*temporaryFile});
|
2023-08-21 16:02:26 +08:00
|
|
|
if (mtime == 0) {
|
|
|
|
qWarning() << "create temporary file failed.";
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2023-08-31 16:40:29 +08:00
|
|
|
return DesktopFile{std::move(temporaryFile), "", mtime, ctime};
|
2023-08-20 18:25:59 +08:00
|
|
|
}
|
|
|
|
|
2023-08-23 17:20:47 +08:00
|
|
|
std::optional<DesktopFile> DesktopFile::createTemporaryDesktopFile(const QString &temporaryFile) noexcept
|
|
|
|
{
|
|
|
|
const static QString userTmp = QString{"/run/user/%1/"}.arg(getCurrentUID());
|
|
|
|
auto tempFile = std::make_unique<QFile>(QString{userTmp + QUuid::createUuid().toString(QUuid::Id128) + ".desktop"});
|
|
|
|
|
|
|
|
if (!tempFile->open(QFile::NewOnly | QFile::WriteOnly | QFile::Text)) {
|
|
|
|
qWarning() << "failed to create temporary desktop file:" << QFileInfo{*tempFile}.absoluteFilePath()
|
|
|
|
<< tempFile->errorString();
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto content = temporaryFile.toLocal8Bit();
|
|
|
|
auto writeByte = tempFile->write(content);
|
|
|
|
|
|
|
|
if (writeByte == -1 || writeByte != content.length()) {
|
|
|
|
qWarning() << "write to temporary file failed:" << tempFile->errorString();
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
tempFile->close();
|
|
|
|
|
|
|
|
return createTemporaryDesktopFile(std::move(tempFile));
|
|
|
|
}
|
|
|
|
|
2023-09-20 18:29:42 +08:00
|
|
|
std::optional<DesktopFile> DesktopFile::searchDesktopFileByPath(const QString &desktopFile, ParserError &err) noexcept
|
2023-07-12 15:58:41 +08:00
|
|
|
{
|
2023-08-23 17:20:47 +08:00
|
|
|
decltype(auto) desktopSuffix = ".desktop";
|
2023-08-10 14:32:09 +08:00
|
|
|
|
2023-08-14 16:30:16 +08:00
|
|
|
if (!desktopFile.endsWith(desktopSuffix)) {
|
2023-08-11 17:46:46 +08:00
|
|
|
qWarning() << "file isn't a desktop file:" << desktopFile;
|
2023-09-20 18:29:42 +08:00
|
|
|
err = ParserError::MismatchedFile;
|
2023-08-11 17:46:46 +08:00
|
|
|
return std::nullopt;
|
2023-07-12 15:58:41 +08:00
|
|
|
}
|
|
|
|
|
2023-08-11 17:46:46 +08:00
|
|
|
QFileInfo fileinfo{desktopFile};
|
|
|
|
if (!fileinfo.isAbsolute() or !fileinfo.exists()) {
|
2023-07-12 15:58:41 +08:00
|
|
|
qWarning() << "desktop file not found.";
|
2023-09-20 18:29:42 +08:00
|
|
|
err = ParserError::NotFound;
|
2023-07-12 15:58:41 +08:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
2023-08-11 17:46:46 +08:00
|
|
|
|
|
|
|
QString path{desktopFile};
|
|
|
|
QString id;
|
|
|
|
|
2023-08-23 17:20:47 +08:00
|
|
|
const auto &XDGDataDirs = getDesktopFileDirs();
|
2023-08-11 17:46:46 +08:00
|
|
|
auto idGen = std::any_of(XDGDataDirs.cbegin(), XDGDataDirs.cend(), [&desktopFile](const QString &suffixPath) {
|
|
|
|
return desktopFile.startsWith(suffixPath);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (idGen) {
|
2023-08-14 16:30:16 +08:00
|
|
|
auto tmp = path.chopped(sizeof(desktopSuffix) - 1);
|
2023-08-11 17:46:46 +08:00
|
|
|
auto components = tmp.split(QDir::separator()).toList();
|
|
|
|
auto it = std::find(components.cbegin(), components.cend(), "applications");
|
2023-07-19 17:56:45 +08:00
|
|
|
QString FileId;
|
|
|
|
++it;
|
2023-08-11 17:46:46 +08:00
|
|
|
while (it != components.cend()) {
|
2023-07-19 17:56:45 +08:00
|
|
|
FileId += (*(it++) + "-");
|
2023-08-11 17:46:46 +08:00
|
|
|
}
|
2023-07-24 14:12:59 +08:00
|
|
|
id = FileId.chopped(1); // remove extra "-""
|
2023-07-12 15:58:41 +08:00
|
|
|
}
|
2023-08-10 14:32:09 +08:00
|
|
|
|
2023-08-21 16:02:26 +08:00
|
|
|
auto filePtr = std::make_unique<QFile>(std::move(path));
|
|
|
|
|
2023-08-31 17:59:09 +08:00
|
|
|
auto [ctime, mtime, _] = getFileTimeInfo(QFileInfo{*filePtr});
|
2023-08-10 14:32:09 +08:00
|
|
|
|
2023-09-20 18:29:42 +08:00
|
|
|
err = ParserError::NoError;
|
2023-08-31 16:40:29 +08:00
|
|
|
return DesktopFile{std::move(filePtr), std::move(id), mtime, ctime};
|
2023-08-10 14:32:09 +08:00
|
|
|
}
|
|
|
|
|
2023-09-20 18:29:42 +08:00
|
|
|
std::optional<DesktopFile> DesktopFile::searchDesktopFileById(const QString &appId, ParserError &err) noexcept
|
2023-08-11 17:46:46 +08:00
|
|
|
{
|
2023-08-23 17:20:47 +08:00
|
|
|
auto XDGDataDirs = getDesktopFileDirs();
|
2023-08-14 16:30:16 +08:00
|
|
|
constexpr auto desktopSuffix = u8".desktop";
|
2023-08-11 17:46:46 +08:00
|
|
|
|
|
|
|
for (const auto &dir : XDGDataDirs) {
|
2023-08-14 16:30:16 +08:00
|
|
|
auto app = QFileInfo{dir + QDir::separator() + appId + desktopSuffix};
|
2023-08-11 17:46:46 +08:00
|
|
|
while (!app.exists()) {
|
|
|
|
auto filePath = app.absoluteFilePath();
|
|
|
|
auto hyphenIndex = filePath.indexOf('-');
|
|
|
|
if (hyphenIndex == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
filePath.replace(hyphenIndex, 1, QDir::separator());
|
|
|
|
app.setFile(filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (app.exists()) {
|
|
|
|
return searchDesktopFileByPath(app.absoluteFilePath(), err);
|
|
|
|
}
|
|
|
|
}
|
2023-08-14 16:30:16 +08:00
|
|
|
|
2023-09-20 18:29:42 +08:00
|
|
|
err = ParserError::NotFound;
|
2023-08-11 17:46:46 +08:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2023-09-11 13:01:20 +08:00
|
|
|
bool DesktopFile::modified(qint64 time) const noexcept
|
2023-08-10 14:32:09 +08:00
|
|
|
{
|
|
|
|
return time != m_mtime;
|
2023-07-12 15:58:41 +08:00
|
|
|
}
|
|
|
|
|
2023-09-20 18:29:42 +08:00
|
|
|
ParserError DesktopEntry::parse(const DesktopFile &file) noexcept
|
2023-07-24 14:12:59 +08:00
|
|
|
{
|
2023-08-21 16:02:26 +08:00
|
|
|
DesktopFileGuard guard{file};
|
|
|
|
|
|
|
|
if (!guard.try_open()) {
|
|
|
|
qWarning() << file.sourcePath() << "can't open.";
|
2023-09-20 18:29:42 +08:00
|
|
|
return ParserError::OpenFailed;
|
2023-07-24 14:12:59 +08:00
|
|
|
}
|
|
|
|
|
2023-08-21 16:02:26 +08:00
|
|
|
QTextStream stream;
|
|
|
|
stream.setDevice(file.sourceFile());
|
|
|
|
return parse(stream);
|
2023-07-24 14:12:59 +08:00
|
|
|
}
|
|
|
|
|
2023-09-20 18:29:42 +08:00
|
|
|
ParserError DesktopEntry::parse(QTextStream &stream) noexcept
|
2023-07-12 15:58:41 +08:00
|
|
|
{
|
2023-09-20 18:29:42 +08:00
|
|
|
if (m_parsed) {
|
|
|
|
return ParserError::Parsed;
|
2023-08-25 16:04:55 +08:00
|
|
|
}
|
|
|
|
|
2023-08-10 14:32:09 +08:00
|
|
|
if (stream.atEnd()) {
|
2023-09-20 18:29:42 +08:00
|
|
|
return ParserError::OpenFailed;
|
2023-08-10 14:32:09 +08:00
|
|
|
}
|
2023-07-12 15:58:41 +08:00
|
|
|
|
|
|
|
stream.setEncoding(QStringConverter::Utf8);
|
|
|
|
|
2023-09-20 18:29:42 +08:00
|
|
|
ParserError err{ParserError::NoError};
|
|
|
|
DesktopFileParser p(stream);
|
2023-08-25 16:04:55 +08:00
|
|
|
err = p.parse(m_entryMap);
|
|
|
|
m_parsed = true;
|
2023-09-20 18:29:42 +08:00
|
|
|
if (err != ParserError::NoError) {
|
2023-08-25 16:04:55 +08:00
|
|
|
return err;
|
2023-08-25 10:47:17 +08:00
|
|
|
}
|
2023-07-12 15:58:41 +08:00
|
|
|
|
2023-08-25 10:47:17 +08:00
|
|
|
if (!checkMainEntryValidation()) {
|
|
|
|
qWarning() << "invalid MainEntry, abort.";
|
2023-09-20 18:29:42 +08:00
|
|
|
err = ParserError::MissingInfo;
|
2023-07-12 15:58:41 +08:00
|
|
|
}
|
2023-08-25 10:47:17 +08:00
|
|
|
|
2023-07-12 15:58:41 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-07-24 14:12:59 +08:00
|
|
|
std::optional<QMap<QString, DesktopEntry::Value>> DesktopEntry::group(const QString &key) const noexcept
|
2023-07-12 15:58:41 +08:00
|
|
|
{
|
2023-08-10 14:32:09 +08:00
|
|
|
if (auto group = m_entryMap.find(key); group != m_entryMap.cend()) {
|
2023-07-12 15:58:41 +08:00
|
|
|
return *group;
|
2023-08-10 14:32:09 +08:00
|
|
|
}
|
2023-07-24 14:12:59 +08:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<DesktopEntry::Value> DesktopEntry::value(const QString &groupKey, const QString &valueKey) const noexcept
|
|
|
|
{
|
|
|
|
const auto &destGroup = group(groupKey);
|
|
|
|
if (!destGroup) {
|
2023-08-30 12:12:53 +08:00
|
|
|
qDebug() << "group " << groupKey << " can't be found.";
|
2023-07-24 14:12:59 +08:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = destGroup->find(valueKey);
|
|
|
|
if (it == destGroup->cend()) {
|
2023-08-30 12:12:53 +08:00
|
|
|
qDebug() << "value " << valueKey << " can't be found.";
|
2023-07-24 14:12:59 +08:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
return *it;
|
2023-07-12 15:58:41 +08:00
|
|
|
}
|
|
|
|
|
2023-08-10 14:32:09 +08:00
|
|
|
QString DesktopEntry::Value::unescape(const QString &str) noexcept
|
2023-07-12 15:58:41 +08:00
|
|
|
{
|
|
|
|
QString unescapedStr;
|
|
|
|
for (qsizetype i = 0; i < str.size(); ++i) {
|
|
|
|
auto c = str.at(i);
|
|
|
|
if (c != '\\') {
|
|
|
|
unescapedStr.append(c);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (str.at(i + 1).toLatin1()) {
|
2023-08-25 16:04:55 +08:00
|
|
|
default:
|
|
|
|
unescapedStr.append(c);
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
unescapedStr.append('\n');
|
|
|
|
++i;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
unescapedStr.append('\t');
|
|
|
|
++i;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
unescapedStr.append('\r');
|
|
|
|
++i;
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
unescapedStr.append('\\');
|
|
|
|
++i;
|
|
|
|
break;
|
|
|
|
case ';':
|
|
|
|
unescapedStr.append(';');
|
|
|
|
++i;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
unescapedStr.append(' ');
|
|
|
|
++i;
|
|
|
|
break;
|
2023-07-12 15:58:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return unescapedStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DesktopEntry::Value::toString(bool &ok) const noexcept
|
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
auto str = this->find(defaultKeyStr);
|
2023-08-25 18:21:11 +08:00
|
|
|
|
2023-08-10 14:32:09 +08:00
|
|
|
if (str == this->end()) {
|
2023-08-25 18:21:11 +08:00
|
|
|
qWarning() << "value not found.";
|
2023-07-12 15:58:41 +08:00
|
|
|
return {};
|
2023-08-10 14:32:09 +08:00
|
|
|
}
|
2023-08-25 12:05:10 +08:00
|
|
|
|
2023-07-12 15:58:41 +08:00
|
|
|
auto unescapedStr = unescape(*str);
|
2023-08-25 12:05:10 +08:00
|
|
|
if (hasNonAsciiAndControlCharacters(unescapedStr)) {
|
2023-07-12 15:58:41 +08:00
|
|
|
return {};
|
2023-08-10 14:32:09 +08:00
|
|
|
}
|
2023-07-12 15:58:41 +08:00
|
|
|
|
|
|
|
ok = true;
|
|
|
|
return unescapedStr;
|
|
|
|
}
|
|
|
|
|
2023-07-21 14:47:40 +08:00
|
|
|
QString DesktopEntry::Value::toLocaleString(const QLocale &locale, bool &ok) const noexcept
|
2023-07-12 15:58:41 +08:00
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
for (auto it = this->constKeyValueBegin(); it != this->constKeyValueEnd(); ++it) {
|
|
|
|
auto [a, b] = *it;
|
|
|
|
if (QLocale{a}.name() == locale.name()) {
|
|
|
|
ok = true;
|
|
|
|
return unescape(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toString(ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DesktopEntry::Value::toIconString(bool &ok) const noexcept
|
|
|
|
{
|
|
|
|
return toString(ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DesktopEntry::Value::toBoolean(bool &ok) const noexcept
|
|
|
|
{
|
2023-07-19 17:56:45 +08:00
|
|
|
ok = false;
|
2023-07-21 14:47:40 +08:00
|
|
|
const auto &str = (*this)[defaultKeyStr];
|
2023-07-19 17:56:45 +08:00
|
|
|
if (str == "true") {
|
|
|
|
ok = true;
|
2023-07-12 15:58:41 +08:00
|
|
|
return true;
|
2023-07-19 17:56:45 +08:00
|
|
|
}
|
|
|
|
if (str == "false") {
|
|
|
|
ok = true;
|
2023-07-12 15:58:41 +08:00
|
|
|
return false;
|
2023-07-19 17:56:45 +08:00
|
|
|
}
|
2023-07-12 15:58:41 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float DesktopEntry::Value::toNumeric(bool &ok) const noexcept
|
|
|
|
{
|
2023-07-21 14:47:40 +08:00
|
|
|
const auto &str = (*this)[defaultKeyStr];
|
2023-07-12 15:58:41 +08:00
|
|
|
QVariant v{str};
|
|
|
|
return v.toFloat(&ok);
|
|
|
|
}
|
|
|
|
|
2023-09-13 16:52:53 +08:00
|
|
|
bool operator==(const DesktopEntry &lhs, const DesktopEntry &rhs)
|
|
|
|
{
|
|
|
|
if (lhs.m_parsed != rhs.m_parsed) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lhs.m_entryMap != rhs.m_entryMap) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const DesktopEntry &lhs, const DesktopEntry &rhs)
|
|
|
|
{
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2023-09-15 14:44:27 +08:00
|
|
|
bool operator==(const DesktopFile &lhs, const DesktopFile &rhs)
|
|
|
|
{
|
|
|
|
if (lhs.m_desktopId != rhs.m_desktopId) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lhs.sourcePath() != rhs.sourcePath()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lhs.m_ctime != rhs.m_ctime or lhs.m_mtime != rhs.m_mtime) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const DesktopFile &lhs, const DesktopFile &rhs)
|
|
|
|
{
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2023-07-12 15:58:41 +08:00
|
|
|
QDebug operator<<(QDebug debug, const DesktopEntry::Value &v)
|
|
|
|
{
|
|
|
|
QDebugStateSaver saver{debug};
|
2023-07-21 14:47:40 +08:00
|
|
|
debug << static_cast<const QMap<QString, QString> &>(v);
|
2023-07-12 15:58:41 +08:00
|
|
|
return debug;
|
|
|
|
}
|