feat: emit signal when AutoStart Changed

Signed-off-by: ComixHe <heyuming@deepin.org>
This commit is contained in:
ComixHe 2023-09-08 10:58:41 +08:00 committed by deepin-bot[bot]
parent ac71f99bc6
commit dc96c21c7d
7 changed files with 118 additions and 6 deletions

View File

@ -41,6 +41,7 @@ constexpr auto JobInterface = "org.desktopspec.JobManager1.Job";
constexpr auto ApplicationManagerInterface = "org.desktopspec.ApplicationManager1"; constexpr auto ApplicationManagerInterface = "org.desktopspec.ApplicationManager1";
constexpr auto InstanceInterface = "org.desktopspec.ApplicationManager1.Instance"; constexpr auto InstanceInterface = "org.desktopspec.ApplicationManager1.Instance";
constexpr auto ApplicationInterface = "org.desktopspec.ApplicationManager1.Application"; constexpr auto ApplicationInterface = "org.desktopspec.ApplicationManager1.Application";
constexpr auto PropertiesInterface = u8"org.freedesktop.DBus.Properties";
constexpr auto systemdOption = u8"systemd"; constexpr auto systemdOption = u8"systemd";
constexpr auto splitOption = u8"split"; constexpr auto splitOption = u8"split";

View File

@ -6,6 +6,7 @@
#include "applicationservice.h" #include "applicationservice.h"
#include "dbus/AMobjectmanager1adaptor.h" #include "dbus/AMobjectmanager1adaptor.h"
#include "systemdsignaldispatcher.h" #include "systemdsignaldispatcher.h"
#include "propertiesForwarder.h"
#include <QFile> #include <QFile>
#include <QDBusMessage> #include <QDBusMessage>
#include <unistd.h> #include <unistd.h>
@ -81,6 +82,10 @@ ApplicationManager1Service::ApplicationManager1Service(std::unique_ptr<Identifie
} }
scanAutoStart(); scanAutoStart();
if (auto *ptr = new (std::nothrow) PropertiesForwarder{DDEApplicationManager1ObjectPath, this}; ptr == nullptr) {
qCritical() << "new PropertiesForwarder of Application Manager failed.";
}
} }
void ApplicationManager1Service::addInstanceToApplication(const QString &unitName, const QDBusObjectPath &systemdUnitPath) void ApplicationManager1Service::addInstanceToApplication(const QString &unitName, const QDBusObjectPath &systemdUnitPath)
@ -240,6 +245,7 @@ bool ApplicationManager1Service::addApplication(DesktopFile desktopFileSource) n
return false; return false;
} }
m_applicationList.insert(application->applicationPath(), application); m_applicationList.insert(application->applicationPath(), application);
emit listChanged();
emit InterfacesAdded(application->applicationPath(), getChildInterfacesAndPropertiesFromObject(ptr)); emit InterfacesAdded(application->applicationPath(), getChildInterfacesAndPropertiesFromObject(ptr));
return true; return true;
@ -251,6 +257,7 @@ void ApplicationManager1Service::removeOneApplication(const QDBusObjectPath &app
emit InterfacesRemoved(application, getChildInterfacesFromObject(it->data())); emit InterfacesRemoved(application, getChildInterfacesFromObject(it->data()));
unregisterObjectFromDBus(application.path()); unregisterObjectFromDBus(application.path());
m_applicationList.remove(application); m_applicationList.remove(application);
emit listChanged();
} }
} }

View File

@ -29,7 +29,7 @@ public:
ApplicationManager1Service &operator=(const ApplicationManager1Service &) = delete; ApplicationManager1Service &operator=(const ApplicationManager1Service &) = delete;
ApplicationManager1Service &operator=(ApplicationManager1Service &&) = delete; ApplicationManager1Service &operator=(ApplicationManager1Service &&) = delete;
Q_PROPERTY(QList<QDBusObjectPath> List READ list) Q_PROPERTY(QList<QDBusObjectPath> List READ list NOTIFY listChanged)
[[nodiscard]] QList<QDBusObjectPath> list() const; [[nodiscard]] QList<QDBusObjectPath> list() const;
bool addApplication(DesktopFile desktopFileSource) noexcept; bool addApplication(DesktopFile desktopFileSource) noexcept;
@ -48,6 +48,7 @@ public Q_SLOTS:
Q_SIGNALS: Q_SIGNALS:
void InterfacesAdded(const QDBusObjectPath &object_path, const ObjectInterfaceMap &interfaces); void InterfacesAdded(const QDBusObjectPath &object_path, const ObjectInterfaceMap &interfaces);
void InterfacesRemoved(const QDBusObjectPath &object_path, const QStringList &interfaces); void InterfacesRemoved(const QDBusObjectPath &object_path, const QStringList &interfaces);
void listChanged();
private: private:
std::unique_ptr<Identifier> m_identifier; std::unique_ptr<Identifier> m_identifier;

View File

@ -6,6 +6,7 @@
#include "APPobjectmanager1adaptor.h" #include "APPobjectmanager1adaptor.h"
#include "applicationchecker.h" #include "applicationchecker.h"
#include "applicationmanager1service.h" #include "applicationmanager1service.h"
#include "propertiesForwarder.h"
#include "dbus/instanceadaptor.h" #include "dbus/instanceadaptor.h"
#include "launchoptions.h" #include "launchoptions.h"
#include <QUuid> #include <QUuid>
@ -84,6 +85,11 @@ QSharedPointer<ApplicationService> ApplicationService::createApplicationService(
return nullptr; return nullptr;
} }
if (auto *ptr = new (std::nothrow) PropertiesForwarder{app->m_applicationPath.path(), app.data()}; ptr == nullptr) {
qCritical() << "new PropertiesForwarder of Application failed.";
return nullptr;
}
return app; return app;
} }
@ -402,12 +408,14 @@ bool ApplicationService::autostartCheck(const QString &linkPath) noexcept
{ {
QFileInfo info{linkPath}; QFileInfo info{linkPath};
if (!info.exists() or !info.isSymbolicLink()) { if (info.exists()) {
if (info.isSymbolicLink()) {
return true;
}
qWarning() << "same name desktop file exists:" << linkPath << "but this may not created by AM."; qWarning() << "same name desktop file exists:" << linkPath << "but this may not created by AM.";
return false;
} }
return true; return false;
} }
bool ApplicationService::isAutoStart() const noexcept bool ApplicationService::isAutoStart() const noexcept
@ -437,6 +445,8 @@ void ApplicationService::setAutoStart(bool autostart) noexcept
} }
} }
} }
emit autostartChanged();
} }
QList<QDBusObjectPath> ApplicationService::instances() const noexcept QList<QDBusObjectPath> ApplicationService::instances() const noexcept

View File

@ -58,8 +58,7 @@ public:
// FIXME: // FIXME:
// This property should implement with fuse guarded $XDG_CONFIG_HOME/autostart/. // This property should implement with fuse guarded $XDG_CONFIG_HOME/autostart/.
// Current implementation has some problems, // Current implementation has some problems,
// such as it will not emit changed signal. Q_PROPERTY(bool AutoStart READ isAutoStart WRITE setAutoStart NOTIFY autostartChanged)
Q_PROPERTY(bool AutoStart READ isAutoStart WRITE setAutoStart)
[[nodiscard]] bool isAutoStart() const noexcept; [[nodiscard]] bool isAutoStart() const noexcept;
void setAutoStart(bool autostart) noexcept; void setAutoStart(bool autostart) noexcept;
@ -107,6 +106,7 @@ public Q_SLOTS:
Q_SIGNALS: Q_SIGNALS:
void InterfacesAdded(const QDBusObjectPath &object_path, const ObjectInterfaceMap &interfaces); void InterfacesAdded(const QDBusObjectPath &object_path, const ObjectInterfaceMap &interfaces);
void InterfacesRemoved(const QDBusObjectPath &object_path, const QStringList &interfaces); void InterfacesRemoved(const QDBusObjectPath &object_path, const QStringList &interfaces);
void autostartChanged();
private: private:
friend class ApplicationManager1Service; friend class ApplicationManager1Service;

View File

@ -0,0 +1,71 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "propertiesForwarder.h"
#include "global.h"
#include <QMetaProperty>
#include <QDBusMessage>
#include <QDBusAbstractAdaptor>
PropertiesForwarder::PropertiesForwarder(QString path, QObject *parent)
: QObject(parent)
, m_path(std::move(path))
{
const auto *mo = parent->metaObject();
if (mo == nullptr) {
qCritical() << "relay propertiesChanged failed.";
return;
}
for (auto i = mo->propertyOffset(); i < mo->propertyCount(); ++i) {
auto prop = mo->property(i);
if (!prop.hasNotifySignal()) {
continue;
}
auto signal = prop.notifySignal();
auto slot = metaObject()->method(metaObject()->indexOfSlot("PropertyChanged()"));
QObject::connect(parent, signal, this, slot);
}
}
void PropertiesForwarder::PropertyChanged()
{
auto *sender = QObject::sender();
auto sigIndex = QObject::senderSignalIndex();
const auto *mo = sender->metaObject();
if (mo == nullptr) {
qCritical() << "PropertiesForwarder::PropertyChanged [relay propertiesChanged failed.]";
return;
}
auto sig = mo->property(sigIndex);
const auto *propName = sig.name();
auto value = sig.read(sender);
auto childs = sender->children();
QString interface;
for (const auto &adaptor : childs) {
if (adaptor->inherits("QDBusAbstractAdaptor")) {
const auto *adaptorMo = adaptor->metaObject();
if (adaptorMo->indexOfProperty(propName) != -1) {
interface = getDBusInterface(adaptor->metaObject()->metaType());
break;
}
}
};
if (interface.isEmpty()) {
return;
}
auto msg = QDBusMessage::createSignal(m_path, interface, "PropertiesChanged");
msg << QString{ApplicationInterface};
msg << QVariantMap{{QString{propName}, value}};
msg << QStringList{};
ApplicationManager1DBus::instance().globalServerBus().send(msg);
}

22
src/propertiesForwarder.h Normal file
View File

@ -0,0 +1,22 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#ifndef PROPERTIESFORWARDER_H
#define PROPERTIESFORWARDER_H
#include <QObject>
class PropertiesForwarder : public QObject
{
Q_OBJECT
public:
explicit PropertiesForwarder(QString path, QObject *parent);
public Q_SLOTS:
void PropertyChanged();
private:
QString m_path;
};
#endif