| 1 |
// SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include "dexportedinterface.h" |
| 6 |
#include "base/private/dobject_p.h" |
| 7 |
|
| 8 |
#include <QHash> |
| 9 |
#include <QPair> |
| 10 |
#include <QVector> |
| 11 |
#include <QVariant> |
| 12 |
#include <QDBusConnection> |
| 13 |
#include <QDBusVariant> |
| 14 |
#include <QDBusContext> |
| 15 |
#include <QDBusMessage> |
| 16 |
|
| 17 |
DCORE_BEGIN_NAMESPACE |
| 18 |
namespace DUtil { |
| 19 |
|
| 20 |
class DExportedInterfacePrivate; |
| 21 |
class DExportedInterfaceDBusInterface : public QObject, protected QDBusContext |
| 22 |
{ |
| 23 |
Q_OBJECT |
| 24 |
Q_CLASSINFO("D-Bus Interface", "com.deepin.ExportedInterface") |
| 25 |
|
| 26 |
public: |
| 27 |
DExportedInterfaceDBusInterface(DExportedInterfacePrivate *priv); |
| 28 |
|
| 29 |
public Q_SLOTS: |
| 30 |
QStringList list(); |
| 31 |
QString help(const QString &action); |
| 32 |
QDBusVariant invoke(QString action, QString parameters); |
| 33 |
|
| 34 |
private: |
| 35 |
DExportedInterfacePrivate *p; |
| 36 |
}; |
| 37 |
|
| 38 |
class DExportedInterfacePrivate : public DObjectPrivate |
| 39 |
{ |
| 40 |
public: |
| 41 |
DExportedInterfacePrivate(DExportedInterface *q); |
| 42 |
|
| 43 |
private: |
| 44 |
QStringList actionHelp(QString action, int indent); |
| 45 |
|
| 46 |
QHash<QString, QPair<std::function<QVariant(QString)>, QString>> actions; |
| 47 |
QScopedPointer<DExportedInterfaceDBusInterface> dbusif; |
| 48 |
D_DECLARE_PUBLIC(DExportedInterface) |
| 49 |
|
| 50 |
friend class DExportedInterfaceDBusInterface; |
| 51 |
}; |
| 52 |
|
| 53 |
DExportedInterface::DExportedInterface(QObject *parent) |
| 54 |
: QObject(parent), |
| 55 |
DObject(*new DExportedInterfacePrivate(this)) |
| 56 |
{ |
| 57 |
D_D(DExportedInterface); |
| 58 |
QDBusConnection::sessionBus().registerObject("/", d->dbusif.data(), QDBusConnection::RegisterOption::ExportAllSlots); |
| 59 |
} |
| 60 |
|
| 61 |
DExportedInterface::~DExportedInterface() |
| 62 |
{ |
| 63 |
QDBusConnection::sessionBus().unregisterObject("/"); |
| 64 |
} |
| 65 |
|
| 66 |
void DExportedInterface::registerAction(const QString &action, const QString &description, const std::function<QVariant (QString)> handler) |
| 67 |
{ |
| 68 |
D_D(DExportedInterface); |
| 69 |
d->actions[action] = {handler, description}; |
| 70 |
} |
| 71 |
|
| 72 |
QVariant DExportedInterface::invoke(const QString &action, const QString ¶meters) const |
| 73 |
{ |
| 74 |
D_DC(DExportedInterface); |
| 75 |
if (auto func = d->actions.value(action).first) { |
| 76 |
return func(parameters); |
| 77 |
} |
| 78 |
return QVariant(); |
| 79 |
} |
| 80 |
|
| 81 |
DExportedInterfacePrivate::DExportedInterfacePrivate(DExportedInterface *q) |
| 82 |
: DObjectPrivate(q) |
| 83 |
, dbusif(new DExportedInterfaceDBusInterface(this)) |
| 84 |
{} |
| 85 |
|
| 86 |
QStringList DExportedInterfacePrivate::actionHelp(QString action, int indent) |
| 87 |
{ |
| 88 |
QStringList ret; |
| 89 |
if (actions.contains(action)) { |
| 90 |
ret << QString(indent * 2, ' ') + QString("%1: %2").arg(action).arg(actions[action].second); |
| 91 |
} |
| 92 |
return ret; |
| 93 |
} |
| 94 |
|
| 95 |
DExportedInterfaceDBusInterface::DExportedInterfaceDBusInterface(DExportedInterfacePrivate *priv) |
| 96 |
: QObject(nullptr) |
| 97 |
, p(priv) |
| 98 |
{} |
| 99 |
|
| 100 |
QStringList DExportedInterfaceDBusInterface::list() |
| 101 |
{ |
| 102 |
return p->actions.keys(); |
| 103 |
} |
| 104 |
|
| 105 |
QString DExportedInterfaceDBusInterface::help(const QString &action) |
| 106 |
{ |
| 107 |
if (action.length()) { |
| 108 |
return p->actionHelp(action, 0).join('\n'); |
| 109 |
} else { |
| 110 |
QString ret = "Available actions:"; |
| 111 |
QStringList actions = p->actions.keys(); |
| 112 |
actions.sort(); |
| 113 |
for (auto action : actions) { |
| 114 |
ret += QString("\n\n") + p->actionHelp(action, 1).join('\n'); |
| 115 |
} |
| 116 |
return ret; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
QDBusVariant DExportedInterfaceDBusInterface::invoke(QString action, QString parameters) |
| 121 |
{ |
| 122 |
QDBusVariant ret; |
| 123 |
if (!p->actions.contains(action)) { |
| 124 |
sendErrorReply(QDBusError::ErrorType::InvalidArgs, QString("Action \"%1\" is not registered").arg(action)); |
| 125 |
} else { |
| 126 |
ret.setVariant(p->q_func()->invoke(action, parameters)); |
| 127 |
} |
| 128 |
return ret; |
| 129 |
} |
| 130 |
|
| 131 |
} |
| 132 |
DCORE_END_NAMESPACE |
| 133 |
|
| 134 |
#include "dexportedinterface.moc" |