feat: add objectManager interface
refact the way to get DBus interface from Qt Meta System Signed-off-by: ComixHe <heyuming@deepin.org>
This commit is contained in:
@ -13,7 +13,7 @@
|
||||
#include <memory>
|
||||
#include <QMap>
|
||||
#include "dbus/jobmanager1service.h"
|
||||
#include "dbus/applicationservice.h"
|
||||
#include "dbus/APPobjectmanager1adaptor.h"
|
||||
#include "dbus/applicationadaptor.h"
|
||||
#include "identifier.h"
|
||||
|
||||
@ -39,13 +39,25 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_applicationList.constFind(application->m_applicationPath) != m_applicationList.cend()) {
|
||||
auto info = qInfo();
|
||||
info << "this application already exists.";
|
||||
if (application->m_isPersistence) {
|
||||
info << "desktop source:" << application->m_desktopSource.m_file.filePath();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
auto *ptr = application.data();
|
||||
new ApplicationAdaptor{ptr};
|
||||
|
||||
if (!registerObjectToDBus(ptr, application->m_applicationPath.path(), getDBusInterface<ApplicationAdaptor>())) {
|
||||
if (!registerObjectToDBus(
|
||||
ptr, application->m_applicationPath.path(), getDBusInterface(QMetaType::fromType<ApplicationAdaptor>()))) {
|
||||
return false;
|
||||
}
|
||||
m_applicationList.insert(application->m_applicationPath, application);
|
||||
emit InterfacesAdded(application->m_applicationPath, getInterfacesListFromObject(ptr));
|
||||
|
||||
return true;
|
||||
}
|
||||
void removeOneApplication(const QDBusObjectPath &application);
|
||||
@ -58,6 +70,11 @@ public:
|
||||
public Q_SLOTS:
|
||||
QString Identify(const QDBusUnixFileDescriptor &pidfd, QDBusObjectPath &application, QDBusObjectPath &application_instance);
|
||||
void UpdateApplicationInfo(const QStringList &appIdList);
|
||||
[[nodiscard]] ObjectMap GetManagedObjects() const;
|
||||
|
||||
Q_SIGNALS:
|
||||
void InterfacesAdded(const QDBusObjectPath &object_path, const QStringList &interfaces);
|
||||
void InterfacesRemoved(const QDBusObjectPath &object_path, const QStringList &interfaces);
|
||||
|
||||
private:
|
||||
std::unique_ptr<Identifier> m_identifier;
|
||||
@ -65,4 +82,58 @@ private:
|
||||
QMap<QDBusObjectPath, QSharedPointer<ApplicationService>> m_applicationList;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
QSharedPointer<ApplicationService> makeApplication(T &&source, ApplicationManager1Service *parent)
|
||||
{
|
||||
static_assert(std::is_same_v<T, DesktopFile> or std::is_same_v<T, QString>, "param type must be QString or DesktopFile.");
|
||||
QString objectPath;
|
||||
QTextStream sourceStream;
|
||||
QFile sourceFile;
|
||||
QSharedPointer<ApplicationService> app{nullptr};
|
||||
|
||||
if constexpr (std::is_same_v<T, DesktopFile>) {
|
||||
DesktopFile in{std::forward<T>(source)};
|
||||
objectPath = QString{DDEApplicationManager1ObjectPath} + "/" + escapeToObjectPath(in.desktopId());
|
||||
sourceFile.setFileName(in.filePath());
|
||||
|
||||
if (!sourceFile.open(QFile::ExistingOnly | QFile::ReadOnly | QFile::Text)) {
|
||||
qCritical() << "desktop file can't open:" << in.filePath() << sourceFile.errorString();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
app.reset(new ApplicationService{std::move(in)});
|
||||
sourceStream.setDevice(&sourceFile);
|
||||
} else if (std::is_same_v<T, QString>) {
|
||||
QString in{std::forward<T>(source)};
|
||||
objectPath = QString{DDEApplicationManager1ObjectPath} + "/" + QUuid::createUuid().toString(QUuid::Id128);
|
||||
|
||||
app.reset(new ApplicationService{std::move(in)});
|
||||
sourceStream.setString(&in, QTextStream::ReadOnly | QTextStream::Text);
|
||||
}
|
||||
|
||||
std::unique_ptr<DesktopEntry> entry{std::make_unique<DesktopEntry>()};
|
||||
auto error = entry->parse(sourceStream);
|
||||
|
||||
if (error != DesktopErrorCode::NoError) {
|
||||
if (error != DesktopErrorCode::EntryKeyInvalid) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
if (auto val = entry->value(DesktopFileEntryKey, "Hidden"); val.has_value()) {
|
||||
bool ok{false};
|
||||
if (auto hidden = val.value().toBoolean(ok); ok and hidden) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
app->m_parent = parent;
|
||||
app->m_entry.reset(entry.release());
|
||||
app->m_applicationPath = QDBusObjectPath{std::move(objectPath)};
|
||||
|
||||
// TODO: icon lookup
|
||||
new APPObjectManagerAdaptor{app.data()};
|
||||
return app;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user