-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add dde-am tool to launch application
add dde-am tool.
- Loading branch information
1 parent
b504363
commit d5f4315
Showing
12 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_subdirectory(src) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
set(BIN_NAME dde-am) | ||
|
||
add_executable(${BIN_NAME} main.cpp | ||
launcher.h launcher.cpp | ||
) | ||
|
||
target_link_libraries(${BIN_NAME} PRIVATE | ||
dde_am_static | ||
Dtk6::Core | ||
) | ||
|
||
target_include_directories(${BIN_NAME} PRIVATE | ||
${PROJECT_SOURCE_DIR}/src | ||
${PROJECT_BINARY_DIR}/src | ||
) | ||
|
||
install(TARGETS ${BIN_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. | ||
// | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
|
||
#include "launcher.h" | ||
#include "global.h" | ||
|
||
#include <QDBusConnection> | ||
#include <QDBusMetaType> | ||
#include <DConfig> | ||
|
||
DCORE_USE_NAMESPACE | ||
|
||
namespace { | ||
void registerComplexDbusType() | ||
{ | ||
qRegisterMetaType<ObjectInterfaceMap>(); | ||
qDBusRegisterMetaType<ObjectInterfaceMap>(); | ||
qRegisterMetaType<ObjectMap>(); | ||
qDBusRegisterMetaType<ObjectMap>(); | ||
qDBusRegisterMetaType<QStringMap>(); | ||
qRegisterMetaType<QStringMap>(); | ||
qRegisterMetaType<PropMap>(); | ||
qDBusRegisterMetaType<PropMap>(); | ||
qDBusRegisterMetaType<QDBusObjectPath>(); | ||
} | ||
|
||
template<class T> | ||
DExpected<T> parseDBusField(const QVariantMap &map, const QString &key) | ||
{ | ||
if (!map.contains(key)) | ||
return DUnexpected{emplace_tag::USE_EMPLACE, -1, QString("%1 doesn't exist.").arg(key)}; | ||
const auto value = map.value(key); | ||
return DExpected<T>{qdbus_cast<T>(value)}; | ||
} | ||
|
||
ObjectMap getManagedObjects() | ||
{ | ||
static ObjectMap objects; | ||
static bool initialized = false; | ||
if (initialized) | ||
return objects; | ||
|
||
initialized = true; | ||
|
||
registerComplexDbusType(); | ||
|
||
auto con = QDBusConnection::sessionBus(); | ||
auto msg = QDBusMessage::createMethodCall( | ||
DDEApplicationManager1ServiceName, DDEApplicationManager1ObjectPath, ObjectManagerInterface, "GetManagedObjects"); | ||
|
||
auto reply = con.call(msg); | ||
|
||
if (reply.type() != QDBusMessage::ReplyMessage) { | ||
qFatal() << "Failed to fetch application infos" << reply.errorMessage(); | ||
} | ||
const auto &arguments = reply.arguments(); | ||
Q_ASSERT_X(!arguments.isEmpty(), "", "Incorrect reply argument for GetManagedObjects call."); | ||
|
||
objects = qdbus_cast<ObjectMap>(arguments.first()); | ||
return objects; | ||
} | ||
} // namespace | ||
|
||
Dtk::Core::DExpected<QStringList> Launcher::appIds() | ||
{ | ||
QStringList appIds; | ||
const auto objects = getManagedObjects(); | ||
for (auto iter = objects.cbegin(); iter != objects.cend(); ++iter) { | ||
const auto &objPath = iter.key().path(); | ||
const ObjectInterfaceMap &objs = iter.value(); | ||
const QVariantMap appInfo = objs.value("org.desktopspec.ApplicationManager1.Application"); | ||
if (appInfo.isEmpty()) | ||
continue; | ||
if (auto value = parseDBusField<QString>(appInfo, u8"ID")) { | ||
appIds.append(value.value()); | ||
} else { | ||
qFatal() << "Failed to parse application ID"; | ||
} | ||
} | ||
return appIds; | ||
} | ||
|
||
void Launcher::setPath(const QString &path) | ||
{ | ||
m_path = path; | ||
} | ||
|
||
void Launcher::setAction(const QString &action) | ||
{ | ||
m_action = action; | ||
} | ||
|
||
Dtk::Core::DExpected<void> Launcher::run() | ||
{ | ||
if (auto value = launch(); !value) | ||
return value; | ||
|
||
updateLaunchedTimes(); | ||
return {}; | ||
} | ||
|
||
Dtk::Core::DExpected<void> Launcher::launch() | ||
{ | ||
auto con = QDBusConnection::sessionBus(); | ||
auto msg = QDBusMessage::createMethodCall( | ||
DDEApplicationManager1ServiceName, m_path, ApplicationInterface, "Launch"); | ||
const QList<QVariant> arguments { | ||
m_action, | ||
QStringList{}, | ||
QVariantMap{} | ||
}; | ||
msg.setArguments(arguments); | ||
auto reply = con.call(msg); | ||
|
||
if (reply.type() != QDBusMessage::ReplyMessage) { | ||
return DUnexpected{emplace_tag::USE_EMPLACE, | ||
static_cast<int>(reply.type()), | ||
QString("Failed to launch: %1, error: %2").arg(appId(), reply.errorMessage())}; | ||
} | ||
return {}; | ||
} | ||
|
||
void Launcher::updateLaunchedTimes() | ||
{ | ||
std::unique_ptr<DConfig> config(DConfig::create(ApplicationServiceID, ApplicationManager1ToolsConfig)); | ||
if (!config->isValid()) | ||
return; | ||
|
||
const QString AppsLaunchedTimes(u8"appsLaunchedTimes"); | ||
QVariantMap launchedTimes = config->value(AppsLaunchedTimes).toMap(); | ||
const auto appKey = appId(); | ||
if (appKey.isEmpty()) | ||
return; | ||
launchedTimes[appKey] = launchedTimes[appKey].toLongLong() + 1; | ||
config->setValue(AppsLaunchedTimes, launchedTimes); | ||
} | ||
|
||
QString Launcher::appId() const | ||
{ | ||
if (m_path.isEmpty()) return {}; | ||
const auto startIndex = QString(DDEApplicationManager1ObjectPath).size(); | ||
auto endIndex = m_path.indexOf("/", startIndex + 1); | ||
const auto id = endIndex <= -1 ? m_path.mid(startIndex + 1) : | ||
m_path.sliced(startIndex + 1, endIndex - (startIndex + 1)); | ||
return unescapeFromObjectPath(id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. | ||
// | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
|
||
#pragma once | ||
|
||
#include <QString> | ||
#include <DExpected> | ||
|
||
class Launcher { | ||
public: | ||
void setPath(const QString &path); | ||
void setAction(const QString &action); | ||
Dtk::Core::DExpected<void> run(); | ||
|
||
static Dtk::Core::DExpected<QStringList> appIds(); | ||
private: | ||
Dtk::Core::DExpected<void> launch(); | ||
void updateLaunchedTimes(); | ||
QString appId() const; | ||
|
||
QString m_path; | ||
QString m_action; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. | ||
// | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
|
||
#include <QCoreApplication> | ||
#include <QCommandLineOption> | ||
#include <QCommandLineParser> | ||
|
||
#include "launcher.h" | ||
#include "global.h" | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
QCoreApplication app{argc, argv}; | ||
|
||
QCommandLineParser parser; | ||
parser.addHelpOption(); | ||
parser.addVersionOption(); | ||
parser.process(app); | ||
|
||
auto arguments = parser.positionalArguments(); | ||
if (arguments.size() < 1) | ||
parser.showHelp(); | ||
|
||
const auto pos1 = arguments.takeFirst(); | ||
if (pos1 == u8"list") { | ||
const auto apps = Launcher::appIds(); | ||
if (!apps) { | ||
qWarning() << apps.error(); | ||
} | ||
for (const auto &item :apps.value()) { | ||
qDebug() << qPrintable(item); | ||
} | ||
return 0; | ||
} | ||
|
||
std::unique_ptr<Launcher> launcher = std::make_unique<Launcher>(); | ||
QString appPath = pos1.startsWith("/") ? pos1 : | ||
QString("%1/%2").arg(DDEApplicationManager1ObjectPath, escapeToObjectPath(pos1)); | ||
launcher->setPath(appPath); | ||
if (arguments.size() >= 1) { | ||
const auto action = arguments.takeFirst(); | ||
launcher->setAction(action); | ||
} | ||
|
||
QMetaObject::invokeMethod(&app, [launcher = std::move(launcher)]() { | ||
auto ret = launcher->run(); | ||
if (!ret) { | ||
qWarning() << ret.error(); | ||
qApp->exit(ret.error().getErrorCode()); | ||
} | ||
qApp->exit(); | ||
}, Qt::QueuedConnection); | ||
|
||
return app.exec(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
misc/dsg/configs/applicationmanager1/org.deepin.applicationmanager1.tools.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"magic": "dsg.config.meta", | ||
"version": "1.0", | ||
"contents": { | ||
"appsLaunchedTimes": { | ||
"value": {}, | ||
"serial": 0, | ||
"flags": [], | ||
"name": "launched times of all apps", | ||
"name[zh_CN]": "所有应用的启动次数", | ||
"description": "launched times of all apps", | ||
"permissions": "readwrite", | ||
"visibility": "public" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters