feat: add method SendToDesktop/RemoveFromDesktop, Property isOnDesktop

Signed-off-by: ComixHe <heyuming@deepin.org>
This commit is contained in:
ComixHe 2023-09-01 15:48:29 +08:00 committed by Comix
parent 40babe8aae
commit ca24d2d908
3 changed files with 82 additions and 0 deletions

View File

@ -92,5 +92,16 @@
`env` option will not take effect at all."
/>
</method>
<property name="isOnDesktop" type="b" access="read"/>
<method name="SendToDesktop">
<arg type="b" name="success" direction="out"/>
</method>
<method name="RemoveFromDesktop">
<arg type="b" name="success" direction="out"/>
</method>
</interface>
</node>

View File

@ -13,6 +13,7 @@
#include <QUrl>
#include <QRegularExpression>
#include <QProcess>
#include <QStandardPaths>
#include <algorithm>
#include <new>
@ -186,6 +187,71 @@ QDBusObjectPath ApplicationService::Launch(const QString &action, const QStringL
std::move(res));
}
bool ApplicationService::SendToDesktop() const noexcept
{
if (isOnDesktop()) {
return true;
}
auto dir = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
if (dir.isEmpty()) {
qDebug() << "no desktop directory found.";
return false;
}
auto desktopFile = QDir{dir}.filePath(m_desktopSource.desktopId() + ".desktop");
auto success = m_desktopSource.sourceFileRef().link(desktopFile);
if (!success) {
qDebug() << "create link failed:" << m_desktopSource.sourceFileRef().errorString() << "path:" << desktopFile;
}
return success;
}
bool ApplicationService::RemoveFromDesktop() const noexcept
{
if (!isOnDesktop()) {
return true;
}
auto dir = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
if (dir.isEmpty()) {
qDebug() << "no desktop directory found.";
return false;
}
QFile desktopFile{QDir{dir}.filePath(m_desktopSource.desktopId() + ".desktop")};
auto success = desktopFile.remove();
if (!success) {
qDebug() << "remove desktop file failed:" << desktopFile.errorString();
}
return success;
}
bool ApplicationService::isOnDesktop() const noexcept
{
auto dir = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
if (dir.isEmpty()) {
qDebug() << "no desktop directory found.";
return false;
}
QFileInfo info{QDir{dir}.filePath(m_desktopSource.desktopId() + ".desktop")};
if (!info.exists()) {
return false;
}
if (!info.isSymbolicLink()) {
return false;
}
return info.symLinkTarget() == m_desktopSource.sourcePath();
}
QStringList ApplicationService::actions() const noexcept
{
auto val = findEntryValue(DesktopFileEntryKey, "Actions", EntryValueType::String);

View File

@ -69,6 +69,9 @@ public:
Q_PROPERTY(qulonglong installedTime READ installedTime)
[[nodiscard]] qulonglong installedTime() const noexcept;
Q_PROPERTY(bool isOnDesktop READ isOnDesktop)
[[nodiscard]] bool isOnDesktop() const noexcept;
[[nodiscard]] QDBusObjectPath findInstance(const QString &instanceId) const;
[[nodiscard]] const QString &getLauncher() const noexcept { return m_launcher; }
@ -89,6 +92,8 @@ public:
public Q_SLOTS:
QDBusObjectPath Launch(const QString &action, const QStringList &fields, const QVariantMap &options);
[[nodiscard]] ObjectMap GetManagedObjects() const;
[[nodiscard]] bool SendToDesktop() const noexcept;
[[nodiscard]] bool RemoveFromDesktop() const noexcept;
Q_SIGNALS:
void InterfacesAdded(const QDBusObjectPath &object_path, const ObjectInterfaceMap &interfaces);