From d4b0a4bf2fe0992a2d4baa842226a9e7dc728fb2 Mon Sep 17 00:00:00 2001 From: Ye ShanShan Date: Mon, 25 Mar 2024 16:17:00 +0800 Subject: [PATCH] feat: add AM applet to reset appsLaunchedTimes We connect app removed signal of AM to reset appsLaunchedTimes int `dde-am` set when app uninstalled, --- CMakeLists.txt | 1 + applets/CMakeLists.txt | 5 ++ applets/dde-am/CMakeLists.txt | 15 +++++ applets/dde-am/amapplet.cpp | 88 ++++++++++++++++++++++++++++ applets/dde-am/amapplet.h | 30 ++++++++++ applets/dde-am/package/metadata.json | 7 +++ 6 files changed, 146 insertions(+) create mode 100644 applets/CMakeLists.txt create mode 100644 applets/dde-am/CMakeLists.txt create mode 100644 applets/dde-am/amapplet.cpp create mode 100644 applets/dde-am/amapplet.h create mode 100644 applets/dde-am/package/metadata.json diff --git a/CMakeLists.txt b/CMakeLists.txt index 22146fb92..4cc8e6dce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,6 +59,7 @@ add_subdirectory(frame) add_subdirectory(shell) add_subdirectory(example) add_subdirectory(panels) +add_subdirectory(applets) configure_package_config_file( "${CMAKE_CURRENT_LIST_DIR}/misc/DDEShellConfig.cmake.in" diff --git a/applets/CMakeLists.txt b/applets/CMakeLists.txt new file mode 100644 index 000000000..1b525b759 --- /dev/null +++ b/applets/CMakeLists.txt @@ -0,0 +1,5 @@ +# SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +# +# SPDX-License-Identifier: GPL-3.0-or-later + +add_subdirectory(dde-am) diff --git a/applets/dde-am/CMakeLists.txt b/applets/dde-am/CMakeLists.txt new file mode 100644 index 000000000..6c95db425 --- /dev/null +++ b/applets/dde-am/CMakeLists.txt @@ -0,0 +1,15 @@ +# SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +# +# SPDX-License-Identifier: GPL-3.0-or-later + +add_library(dde-am SHARED + amapplet.cpp + amapplet.h +) + +target_link_libraries(dde-am PRIVATE + dde-shell-frame + Qt${QT_MAJOR_VERSION}::DBus +) + +ds_install_package(PACKAGE org.deepin.ds.dde-am TARGET dde-am) diff --git a/applets/dde-am/amapplet.cpp b/applets/dde-am/amapplet.cpp new file mode 100644 index 000000000..257d9e66d --- /dev/null +++ b/applets/dde-am/amapplet.cpp @@ -0,0 +1,88 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "amapplet.h" + +#include "pluginfactory.h" + +#include +#include +#include + +#include +#include + +DCORE_USE_NAMESPACE +DS_BEGIN_NAMESPACE +namespace am { + +AMApplet::AMApplet(QObject *parent) + : DApplet(parent) +{ +} + +AMApplet::~AMApplet() +{ +} + +bool AMApplet::load() +{ + // the signal maybe loss when AM is crashed. + auto conn = QDBusConnection::sessionBus(); + bool ret = conn.connect("org.desktopspec.ApplicationManager1", + "/org/desktopspec/ApplicationManager1", + "org.desktopspec.DBus.ObjectManager", + "InterfacesRemoved", + this, + SLOT(onInterfacesRemoved(QDBusObjectPath,QStringList)) + ); + if (!ret) { + qWarning() << "Failed to connect InterfacesReoved signal for AM" << conn.lastError().message(); + return false; + } + return DApplet::load(); +} + + +static QString unescapeAppIdFromObjectPath(const QString &path) +{ + if (path.isEmpty()) return {}; + const auto startIndex = QString("/org/desktopspec/ApplicationManager1").size(); + auto endIndex = path.indexOf("/", startIndex + 1); + const auto id = endIndex <= -1 ? path.mid(startIndex + 1) : + path.sliced(startIndex + 1, endIndex - (startIndex + 1)); + return DUtil::unescapeFromObjectPath(id); +} + +void AMApplet::onInterfacesRemoved(const QDBusObjectPath &objPath, const QStringList &interfaces) +{ + const QString &path(objPath.path()); + qDebug() << "InterfacesRemoved by AM, path:" << path; + + const QString &appId = unescapeAppIdFromObjectPath(path); + updateAppsLaunchedTimes(appId); +} + +void AMApplet::updateAppsLaunchedTimes(const QString &appId) +{ + std::unique_ptr config(DConfig::create("org.deepin.dde.application-manager", "org.deepin.dde.am")); + if (!config->isValid()) { + qWarning() << "DConfig is invalid when updating launched times."; + return; + } + + const QString AppsLaunchedTimes(u8"appsLaunchedTimes"); + auto value = config->value(AppsLaunchedTimes).toMap(); + if (auto iter = value.find(appId); iter != value.end()) { + value.remove(appId); + config->setValue(AppsLaunchedTimes, value); + qDebug() << "Reset launched times for the app:" << appId; + } +} + +D_APPLET_CLASS(AMApplet) +} +DS_END_NAMESPACE + +#include "amapplet.moc" diff --git a/applets/dde-am/amapplet.h b/applets/dde-am/amapplet.h new file mode 100644 index 000000000..a075ccf2d --- /dev/null +++ b/applets/dde-am/amapplet.h @@ -0,0 +1,30 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include "applet.h" + +#include + +DS_BEGIN_NAMESPACE +namespace am { +class AMApplet : public DApplet +{ + Q_OBJECT +public: + explicit AMApplet(QObject *parent = nullptr); + ~AMApplet(); + + virtual bool load() override; + +private slots: + void onInterfacesRemoved(const QDBusObjectPath &objPath, const QStringList &interfaces); + +private: + void updateAppsLaunchedTimes(const QString &appId); +}; + +} +DS_END_NAMESPACE diff --git a/applets/dde-am/package/metadata.json b/applets/dde-am/package/metadata.json new file mode 100644 index 000000000..e54e17ba5 --- /dev/null +++ b/applets/dde-am/package/metadata.json @@ -0,0 +1,7 @@ +{ + "Plugin": { + "Version": "1.0", + "Id": "org.deepin.ds.dde-am", + "Category": "DDE" + } +}