refact: ApplicationService ctor and some member functions

Signed-off-by: ComixHe <heyuming@deepin.org>
This commit is contained in:
ComixHe
2023-08-20 18:25:59 +08:00
committed by Comix
parent 0e7d84f61d
commit 920594d6e5
10 changed files with 229 additions and 204 deletions

View File

@ -12,6 +12,7 @@
#include <QStringView>
#include <QVariant>
#include <iostream>
#include <chrono>
auto DesktopEntry::parserGroupHeader(const QString &str) noexcept
{
@ -23,6 +24,8 @@ auto DesktopEntry::parserGroupHeader(const QString &str) noexcept
return it;
}
void functest(QMap<QString, QString> p) {}
DesktopErrorCode DesktopEntry::parseEntry(const QString &str, decltype(m_entryMap)::iterator &currentGroup) noexcept
{
if (str.startsWith("#")) {
@ -87,6 +90,13 @@ DesktopErrorCode DesktopEntry::parseEntry(const QString &str, decltype(m_entryMa
return DesktopErrorCode::NoError;
}
std::optional<DesktopFile> DesktopFile::createTemporaryDesktopFile(QString content) noexcept
{
auto now = std::chrono::high_resolution_clock::now().time_since_epoch();
auto nano = std::chrono::duration_cast<std::chrono::nanoseconds>(now);
return DesktopFile{false, std::move(content), "", static_cast<size_t>(nano.count())};
}
std::optional<DesktopFile> DesktopFile::searchDesktopFileByPath(const QString &desktopFile, DesktopErrorCode &err) noexcept
{
constexpr decltype(auto) desktopSuffix = ".desktop";
@ -132,9 +142,9 @@ std::optional<DesktopFile> DesktopFile::searchDesktopFileByPath(const QString &d
}
err = DesktopErrorCode::NoError;
constexpr std::size_t nanoToSec = 1e9;
constexpr std::size_t secToNano = 1e9;
return DesktopFile{std::move(path), std::move(id), buf.st_mtim.tv_sec * nanoToSec + buf.st_mtim.tv_nsec};
return DesktopFile{true, std::move(path), std::move(id), buf.st_mtim.tv_sec * secToNano + buf.st_mtim.tv_nsec};
}
std::optional<DesktopFile> DesktopFile::searchDesktopFileById(const QString &appId, DesktopErrorCode &err) noexcept
@ -168,15 +178,20 @@ bool DesktopFile::modified(std::size_t time) const noexcept
return time != m_mtime;
}
DesktopErrorCode DesktopEntry::parse(const DesktopFile &appId) noexcept
DesktopErrorCode DesktopEntry::parse(const DesktopFile &file) noexcept
{
auto file = QFile(appId.filePath());
if (!file.open(QFile::ExistingOnly | QFile::ReadOnly | QFile::Text)) {
qWarning() << appId.filePath() << "can't open.";
return DesktopErrorCode::OpenFailed;
QTextStream in;
if (file.persistence()) {
auto sourceFile = QFile(file.fileSource());
if (!sourceFile.open(QFile::ExistingOnly | QFile::ReadOnly | QFile::Text)) {
qWarning() << file.fileSource() << "can't open.";
return DesktopErrorCode::OpenFailed;
}
in.setDevice(&sourceFile);
} else {
in.setString(const_cast<QString *>(&file.fileSource()));
}
QTextStream in{&file};
return parse(in);
}