refact: method Identify and CURD method of storage

Signed-off-by: ComixHe <heyuming@deepin.org>
This commit is contained in:
ComixHe
2023-09-14 17:16:10 +08:00
committed by Comix
parent ebb5f613c4
commit 6448481cfc
9 changed files with 162 additions and 94 deletions

View File

@ -124,16 +124,7 @@ void ApplicationManager1Service::addInstanceToApplication(const QString &unitNam
return;
}
if (sender() != nullptr) { // activate by signal
auto timestamp = QDateTime::currentMSecsSinceEpoch();
if (auto ptr = m_storage.lock(); ptr) {
ptr->updateApplicationValue((*appIt)->m_desktopSource.desktopId(),
ApplicationPropertiesGroup,
LastLaunchedTime,
QVariant::fromValue(timestamp));
}
}
(*appIt)->updateAfterLaunch(sender() != nullptr); // activate by signal
const auto &applicationPath = (*appIt)->applicationPath().path();
@ -278,17 +269,6 @@ bool ApplicationManager1Service::addApplication(DesktopFile desktopFileSource) n
}
m_applicationList.insert(application->applicationPath(), application);
if (auto storagePtr = m_storage.lock(); storagePtr) {
auto appId = ptr->id();
auto value = storagePtr->readApplicationValue(appId, ApplicationPropertiesGroup, LastLaunchedTime);
if (value.isNull()) {
storagePtr->createApplicationValue(
appId, ApplicationPropertiesGroup, LastLaunchedTime, QVariant::fromValue<qint64>(0));
} else {
ptr->m_lastLaunch = value.toInt();
}
}
emit listChanged();
emit InterfacesAdded(application->applicationPath(), getChildInterfacesAndPropertiesFromObject(ptr));
@ -300,7 +280,7 @@ void ApplicationManager1Service::removeOneApplication(const QDBusObjectPath &app
if (auto it = m_applicationList.find(application); it != m_applicationList.cend()) {
emit InterfacesRemoved(application, getChildInterfacesFromObject(it->data()));
if (auto ptr = m_storage.lock(); ptr) {
ptr->deleteApplicationValue((*it)->id());
ptr->deleteApplication((*it)->id());
}
unregisterObjectFromDBus(application.path());
m_applicationList.remove(application);
@ -317,7 +297,8 @@ void ApplicationManager1Service::removeAllApplication() noexcept
}
QString ApplicationManager1Service::Identify(const QDBusUnixFileDescriptor &pidfd,
ObjectMap &application_instance_info) const noexcept
QDBusObjectPath &instance,
ObjectInterfaceMap &application_instance_info) const noexcept
{
if (!pidfd.isValid()) {
qWarning() << "pidfd isn't a valid unix file descriptor";
@ -369,17 +350,16 @@ QString ApplicationManager1Service::Identify(const QDBusUnixFileDescriptor &pidf
return {};
}
auto instance = (*app)->findInstance(ret.InstanceId);
auto instancePath = (*app)->findInstance(ret.InstanceId);
if (auto path = instance.path(); path.isEmpty()) {
if (auto path = instancePath.path(); path.isEmpty()) {
qWarning() << "can't find instance:" << path;
return {};
}
instance = instancePath;
auto instanceObj = (*app)->m_Instances.constFind(instance);
auto map = getChildInterfacesAndPropertiesFromObject(instanceObj->get());
application_instance_info.insert(instance, map);
application_instance_info = getChildInterfacesAndPropertiesFromObject(instanceObj->get());
return ret.ApplicationId;
}
@ -407,6 +387,7 @@ void ApplicationManager1Service::updateApplication(const QSharedPointer<Applicat
if (destApp->m_entry != newEntry) {
destApp->resetEntry(newEntry);
destApp->m_desktopSource = std::move(desktopFile);
destApp->detachAllInstance();
}
}

View File

@ -44,7 +44,9 @@ public:
JobManager1Service &jobManager() noexcept { return *m_jobManager; }
public Q_SLOTS:
QString Identify(const QDBusUnixFileDescriptor &pidfd, ObjectMap &application_instance_info) const noexcept;
QString Identify(const QDBusUnixFileDescriptor &pidfd,
QDBusObjectPath &instance,
ObjectInterfaceMap &application_instance_info) const noexcept;
void ReloadApplications();
[[nodiscard]] ObjectMap GetManagedObjects() const;

View File

@ -29,14 +29,28 @@ ApplicationService::ApplicationService(DesktopFile source,
, m_storage(std::move(storage))
, m_desktopSource(std::move(source))
{
auto storagePtr = m_storage.lock();
if (!storagePtr) {
m_lastLaunch = -1;
return;
}
auto appId = id();
auto value = storagePtr->readApplicationValue(appId, ApplicationPropertiesGroup, LastLaunchedTime);
if (value.isNull()) {
if (!storagePtr->createApplicationValue(
appId, ApplicationPropertiesGroup, LastLaunchedTime, QVariant::fromValue<qint64>(0))) {
m_lastLaunch = -1;
}
return;
}
m_lastLaunch = value.toInt();
}
ApplicationService::~ApplicationService()
{
for (auto &instance : m_Instances.values()) {
orphanedInstances->append(instance);
instance->m_orphaned = true;
}
detachAllInstance();
}
QSharedPointer<ApplicationService> ApplicationService::createApplicationService(
@ -93,12 +107,6 @@ QSharedPointer<ApplicationService> ApplicationService::createApplicationService(
return nullptr;
}
auto ptr = app->m_storage.lock();
if (!ptr) {
qWarning() << "runtime storage doesn't exists.";
return app;
}
return app;
}
@ -501,6 +509,16 @@ void ApplicationService::removeAllInstance() noexcept
}
}
void ApplicationService::detachAllInstance() noexcept
{
for (auto &instance : m_Instances.values()) {
orphanedInstances->append(instance);
instance->m_orphaned = true;
}
m_Instances.clear();
}
QDBusObjectPath ApplicationService::findInstance(const QString &instanceId) const
{
for (auto it = m_Instances.constKeyValueBegin(); it != m_Instances.constKeyValueEnd(); ++it) {
@ -715,6 +733,20 @@ QVariant ApplicationService::findEntryValue(const QString &group,
return ret;
}
void ApplicationService::updateAfterLaunch(bool isLaunch) noexcept
{
if (!isLaunch) {
return;
}
auto timestamp = QDateTime::currentMSecsSinceEpoch();
if (auto ptr = m_storage.lock(); ptr) {
ptr->updateApplicationValue(
m_desktopSource.desktopId(), ApplicationPropertiesGroup, ::LastLaunchedTime, QVariant::fromValue(timestamp));
}
}
QString getDeepinWineScaleFactor(const QString &appId) noexcept
{
qCritical() << "Don't using env to control the window scale factor, this function"

View File

@ -100,6 +100,7 @@ public:
return m_Instances;
}
void resetEntry(DesktopEntry *newEntry) noexcept;
void detachAllInstance() noexcept;
public Q_SLOTS:
QDBusObjectPath Launch(const QString &action, const QStringList &fields, const QVariantMap &options);
@ -131,6 +132,7 @@ private:
const QString &valueKey,
EntryValueType type,
const QLocale &locale = getUserLocale()) const noexcept;
void updateAfterLaunch(bool isLaunch) noexcept;
static bool shouldBeShown(const std::unique_ptr<DesktopEntry> &entry) noexcept;
static bool autostartCheck(const QString &linkPath) noexcept;
};