Skip to content

Commit

Permalink
refact: updating launchedTimes from DConfig
Browse files Browse the repository at this point in the history
Launching app by `dde-am` tools instead of calling dbus directly.
Updating launchedTimes from DConfig.
  • Loading branch information
18202781743 committed Mar 29, 2024
1 parent dc020c7 commit 2abdf44
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
61 changes: 50 additions & 11 deletions src/ddeintegration/appmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "AppManager1Application.h"
#include "AppManager1ApplicationObjectManager.h"

#include <DConfig>
#include <DExpected>
DCORE_USE_NAMESPACE

Expand Down Expand Up @@ -64,6 +65,7 @@ static AppMgr::AppItem *parseDBus2AppItem(const ObjectInterfaceMap &source)
if (auto value = parseDBusField<QString>(appInfo, u8"ID")) {
item = new AppMgr::AppItem();
item->id = value.value() + ".desktop";
item->appId = value.value();
}

if (!item) {
Expand Down Expand Up @@ -96,10 +98,6 @@ static AppMgr::AppItem *parseDBus2AppItem(const ObjectInterfaceMap &source)
item->lastLaunchedTime = value.value();
}

if (auto value = parseDBusField<qint64>(appInfo, u8"LaunchedTimes")) {
item->launchedTimes = value.value();
}

return item;
}

Expand Down Expand Up @@ -153,8 +151,18 @@ bool AppMgr::launchApp(const QString &desktopId)
AppManager1Application * amAppIface = createAM1AppIface(desktopId);
if (!amAppIface) return false;

amAppIface->Launch(QString(), QStringList{}, QVariantMap());

const auto path = amAppIface->path();
QProcess process;
process.setProcessChannelMode(QProcess::MergedChannels);
process.start("dde-am", {"--by-user", path});
if (!process.waitForFinished()) {
qWarning() << "Failed to launch the desktopId:" << desktopId << process.errorString();
return false;
} else if (process.exitCode() != 0) {
qWarning() << "Failed to launch the desktopId:" << desktopId << process.readAll();
return false;
}
qDebug() << "Launch the desktopId" << desktopId;
return true;
}

Expand Down Expand Up @@ -294,11 +302,25 @@ void AppMgr::watchingAppItemPropertyChanged(const QString &key, AppMgr::AppItem
appItem->lastLaunchedTime = value;
Q_EMIT itemDataChanged(appItem->id);
});
connect(amAppIface, &AppManager1Application::LaunchedTimesChanged, this, [this, appItem](const qint64 & value) {
qDebug() << "LaunchedTimesChanged by AM, desktopId" << appItem->id;
appItem->launchedTimes = value;
Q_EMIT itemDataChanged(appItem->id);
});
}

void AppMgr::updateAppsLaunchedTimes(const QVariantMap &appsLaunchedTimes)
{
// need to update times for removed and updated.
const auto &appItems = m_appItems.values();
for (const auto item : std::as_const(appItems)) {
auto iter = appsLaunchedTimes.find(item->appId);
qint64 times = 0;
if (iter != appsLaunchedTimes.cend())
times = iter->toLongLong();

// including reset and increase times.
if (item->launchedTimes != times) {
qDebug() << "LaunchedTimesChanged by DConfig, desktopId" << item->id;
item->launchedTimes = times;
Q_EMIT itemDataChanged(item->id);
}
}
}

void AppMgr::initObjectManager()
Expand Down Expand Up @@ -327,6 +349,23 @@ void AppMgr::initObjectManager()
});

fetchAppItems();

DConfig *config = DConfig::create("org.deepin.dde.application-manager", "org.deepin.dde.am", "", this);
if (!config->isValid()) {
qWarning() << "DConfig is invalid when getting launched times.";
} else {
static const QString AppsLaunchedTimes(u8"appsLaunchedTimes");
const auto &value = config->value(AppsLaunchedTimes).toMap();
updateAppsLaunchedTimes(value);
QObject::connect(config, &DConfig::valueChanged, this, [this, config](const QString &key) {
if (key != AppsLaunchedTimes)
return;

qDebug() << "appsLaunchedTimes of DConfig Changed.";
const auto &value = config->value(AppsLaunchedTimes).toMap();
updateAppsLaunchedTimes(value);
});
}
}

void AppMgr::fetchAppItems()
Expand Down
6 changes: 6 additions & 0 deletions src/ddeintegration/appmgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
#include <QMap>
#include <QObject>
#include <QPointer>
#include <dtkcore_global.h>

DCORE_BEGIN_NAMESPACE
class DConfig;
DCORE_END_NAMESPACE
class __AppManager1Application;
class __AppManager1ApplicationObjectManager;
class AppMgr : public QObject
Expand All @@ -27,6 +31,7 @@ class AppMgr : public QObject
qint64 installedTime = 0;
qint64 lastLaunchedTime = 0;
qint64 launchedTimes = 0;
QString appId;
};

static AppMgr *instance();
Expand Down Expand Up @@ -54,6 +59,7 @@ class AppMgr : public QObject
void watchingAppItemAdded(const QString &key, AppMgr::AppItem *appItem);
void watchingAppItemRemoved(const QString &key);
void watchingAppItemPropertyChanged(const QString &key, AppMgr::AppItem *appItem);
void updateAppsLaunchedTimes(const QVariantMap &appsLaunchedTimes);

private:
__AppManager1ApplicationObjectManager *m_objectManager;
Expand Down

0 comments on commit 2abdf44

Please sign in to comment.