| 1 |
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include "ddbusinterface.h" |
| 6 |
#include "ddbusinterface_p.h" |
| 7 |
|
| 8 |
#include <QMetaObject> |
| 9 |
#include <qmetaobject.h> |
| 10 |
#include <QDBusConnection> |
| 11 |
#include <QDBusInterface> |
| 12 |
#include <QDBusMetaType> |
| 13 |
#include <QDBusPendingReply> |
| 14 |
#include <QDebug> |
| 15 |
|
| 16 |
DCORE_BEGIN_NAMESPACE |
| 17 |
|
| 18 |
static const QString &FreedesktopService = QStringLiteral("org.freedesktop.DBus"); |
| 19 |
static const QString &FreedesktopPath = QStringLiteral("/org/freedesktop/DBus"); |
| 20 |
static const QString &FreedesktopInterface = QStringLiteral("org.freedesktop.DBus"); |
| 21 |
static const QString &NameOwnerChanged = QStringLiteral("NameOwnerChanged"); |
| 22 |
|
| 23 |
static const QString &PropertiesInterface = QStringLiteral("org.freedesktop.DBus.Properties"); |
| 24 |
static const QString &PropertiesChanged = QStringLiteral("PropertiesChanged"); |
| 25 |
static const char *PropertyName = "propname"; |
| 26 |
|
| 27 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 28 |
#define PropType(metaProperty) metaProperty.metaType() |
| 29 |
#else |
| 30 |
#define PropType(metaProperty) metaProperty.userType() |
| 31 |
#endif |
| 32 |
|
| 33 |
static QVariant demarshall(const QMetaProperty &metaProperty, const QVariant &value) |
| 34 |
{ |
| 35 |
// if the value is the same with parent one, return value |
| 36 |
if (value.userType() == metaProperty.userType()) |
| 37 |
return value; |
| 38 |
|
| 39 |
// unwrap the value with parent one |
| 40 |
QVariant result = QVariant(PropType(metaProperty), nullptr); |
| 41 |
|
| 42 |
|
| 43 |
if (value.userType() == qMetaTypeId<QDBusArgument>()) { |
| 44 |
QDBusArgument dbusArg = value.value<QDBusArgument>(); |
| 45 |
QDBusMetaType::demarshall(dbusArg, PropType(metaProperty), result.data()); |
| 46 |
} |
| 47 |
|
| 48 |
return result; |
| 49 |
} |
| 50 |
|
| 51 |
DDBusInterfacePrivate::DDBusInterfacePrivate(DDBusInterface *interface, QObject *parent) |
| 52 |
: QObject(interface) |
| 53 |
, m_parent(parent) |
| 54 |
, m_serviceValid(false) |
| 55 |
, q_ptr(interface) |
| 56 |
{ |
| 57 |
QDBusMessage message = QDBusMessage::createMethodCall(FreedesktopService, FreedesktopPath, FreedesktopInterface, "NameHasOwner"); |
| 58 |
message << interface->service(); |
| 59 |
interface->connection().callWithCallback(message, this, SLOT(onDBusNameHasOwner(bool))); |
| 60 |
|
| 61 |
interface->connection().connect(interface->service(), |
| 62 |
interface->path(), |
| 63 |
PropertiesInterface, |
| 64 |
PropertiesChanged, |
| 65 |
{interface->interface()}, |
| 66 |
QString(), |
| 67 |
this, |
| 68 |
SLOT(onPropertiesChanged(QString, QVariantMap, QStringList))); |
| 69 |
} |
| 70 |
|
| 71 |
void DDBusInterfacePrivate::updateProp(const char *propName, const QVariant &value) |
| 72 |
{ |
| 73 |
if (!m_parent) |
| 74 |
return; |
| 75 |
|
| 76 |
const QMetaObject *metaObj = m_parent->metaObject(); |
| 77 |
const char *typeName(value.typeName()); |
| 78 |
void *data = const_cast<void *>(value.data()); |
| 79 |
int propertyIndex = metaObj->indexOfProperty(propName); |
| 80 |
QVariant result = value; |
| 81 |
|
| 82 |
// TODO: it now cannot convert right, Like QMap |
| 83 |
// if there is property, then try to convert with property |
| 84 |
if (propertyIndex != -1) { |
| 85 |
QMetaProperty metaProperty = metaObj->property(propertyIndex); |
| 86 |
result = demarshall(metaProperty, value); |
| 87 |
data = const_cast<void *>(result.data()); |
| 88 |
typeName = result.typeName(); |
| 89 |
} else if (value.canConvert<QDBusArgument>()) { |
| 90 |
auto dbusType = qvariant_cast<QDBusArgument>(value); |
| 91 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 92 |
auto dbusMetaType = QDBusMetaType::signatureToMetaType(dbusType.currentSignature().toUtf8()); |
| 93 |
typeName = dbusMetaType.name(); |
| 94 |
|
| 95 |
void *dbusData = dbusMetaType.create(); |
| 96 |
QDBusMetaType::demarshall(dbusType, dbusMetaType, dbusData); |
| 97 |
data = dbusData; |
| 98 |
QObject dbusDataDeleter; |
| 99 |
QObject::connect(&dbusDataDeleter, &QObject::destroyed, m_parent, [dbusData, dbusMetaType]() { |
| 100 |
dbusMetaType.destroy(dbusData); |
| 101 |
}, Qt::QueuedConnection); |
| 102 |
#else |
| 103 |
auto dbusMetaType = QDBusMetaType::signatureToType(dbusType.currentSignature().toUtf8()); |
| 104 |
typeName = QMetaType::typeName(dbusMetaType); |
| 105 |
|
| 106 |
void *dbusData = QMetaType::create(dbusMetaType); |
| 107 |
QDBusMetaType::demarshall(dbusType, dbusMetaType, dbusData); |
| 108 |
data = dbusData; |
| 109 |
// release dbus data of `QMetaType::create`. |
| 110 |
QObject dbusDataDeleter; |
| 111 |
QObject::connect(&dbusDataDeleter, &QObject::destroyed, m_parent, [dbusData, dbusMetaType]() { |
| 112 |
QMetaType::destroy(dbusMetaType, dbusData); |
| 113 |
}, Qt::QueuedConnection); |
| 114 |
#endif |
| 115 |
} |
| 116 |
QByteArray baSignal = QStringLiteral("%1Changed(%2)").arg(propName).arg(typeName).toLatin1(); |
| 117 |
int i = metaObj->indexOfSignal(baSignal.data()); |
| 118 |
if (i != -1) { |
| 119 |
auto method = metaObj->method(i); |
| 120 |
if (method.parameterCount() == 1) { |
| 121 |
method.invoke(m_parent, Qt::DirectConnection, QGenericArgument(method.parameterTypes().first(), data)); |
| 122 |
} else { |
| 123 |
method.invoke(m_parent, Qt::DirectConnection); |
| 124 |
} |
| 125 |
} else { |
| 126 |
qDebug() << "It's not exist the property:[" << propName << "] for parent:" << m_parent << ", interface:" << q_ptr->interface() << ", and It's changed value is:" << value; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
void DDBusInterfacePrivate::initDBusConnection() |
| 131 |
{ |
| 132 |
if (!m_parent) |
| 133 |
return; |
| 134 |
|
| 135 |
Q_Q(DDBusInterface); |
| 136 |
QDBusConnection connection = q->connection(); |
| 137 |
QStringList signalList; |
| 138 |
QDBusInterface inter(q->service(), q->path(), q->interface(), connection); |
| 139 |
const QMetaObject *meta = inter.metaObject(); |
| 140 |
for (int i = meta->methodOffset(); i < meta->methodCount(); ++i) { |
| 141 |
const QMetaMethod &method = meta->method(i); |
| 142 |
if (method.methodType() == QMetaMethod::Signal) { |
| 143 |
signalList << method.methodSignature(); |
| 144 |
} |
| 145 |
} |
| 146 |
const QMetaObject *parentMeta = m_parent->metaObject(); |
| 147 |
for (const QString &signal : signalList) { |
| 148 |
int i = parentMeta->indexOfSignal(QMetaObject::normalizedSignature(signal.toLatin1())); |
| 149 |
if (i != -1) { |
| 150 |
const QMetaMethod &parentMethod = parentMeta->method(i); |
| 151 |
connection.connect(q->service(), |
| 152 |
q->path(), |
| 153 |
q->interface(), |
| 154 |
parentMethod.name(), |
| 155 |
m_parent, |
| 156 |
QT_STRINGIFY(QSIGNAL_CODE) + parentMethod.methodSignature()); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
void DDBusInterfacePrivate::onPropertiesChanged(const QString &interfaceName, |
| 162 |
const QVariantMap &changedProperties, |
| 163 |
const QStringList &invalidatedProperties) |
| 164 |
{ |
| 165 |
Q_UNUSED(interfaceName) |
| 166 |
Q_UNUSED(invalidatedProperties) |
| 167 |
for (QVariantMap::const_iterator it = changedProperties.cbegin(); it != changedProperties.cend(); ++it) |
| 168 |
updateProp((it.key() + m_suffix).toLatin1(), it.value()); |
| 169 |
} |
| 170 |
|
| 171 |
void DDBusInterfacePrivate::onAsyncPropertyFinished(QDBusPendingCallWatcher *w) |
| 172 |
{ |
| 173 |
QDBusPendingReply<QVariant> reply = *w; |
| 174 |
if (!reply.isError()) { |
| 175 |
updateProp(w->property(PropertyName).toString().toLatin1(), reply.value()); |
| 176 |
} |
| 177 |
w->deleteLater(); |
| 178 |
} |
| 179 |
|
| 180 |
void DDBusInterfacePrivate::setServiceValid(bool valid) |
| 181 |
{ |
| 182 |
if (m_serviceValid != valid) { |
| 183 |
Q_Q(DDBusInterface); |
| 184 |
m_serviceValid = valid; |
| 185 |
Q_EMIT q->serviceValidChanged(m_serviceValid); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
void DDBusInterfacePrivate::onDBusNameHasOwner(bool valid) |
| 190 |
{ |
| 191 |
Q_Q(DDBusInterface); |
| 192 |
setServiceValid(valid); |
| 193 |
if (valid) |
| 194 |
initDBusConnection(); |
| 195 |
else |
| 196 |
q->connection().connect(FreedesktopService, |
| 197 |
FreedesktopPath, |
| 198 |
FreedesktopInterface, |
| 199 |
NameOwnerChanged, |
| 200 |
this, |
| 201 |
SLOT(onDBusNameOwnerChanged(QString, QString, QString))); |
| 202 |
} |
| 203 |
|
| 204 |
void DDBusInterfacePrivate::onDBusNameOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner) |
| 205 |
{ |
| 206 |
Q_Q(DDBusInterface); |
| 207 |
if (name == q->service() && oldOwner.isEmpty()) { |
| 208 |
initDBusConnection(); |
| 209 |
q->connection().disconnect(FreedesktopService, |
| 210 |
FreedesktopPath, |
| 211 |
FreedesktopInterface, |
| 212 |
NameOwnerChanged, |
| 213 |
this, |
| 214 |
SLOT(onDBusNameOwnerChanged(QString, QString, QString))); |
| 215 |
setServiceValid(true); |
| 216 |
} else if (name == q->service() && newOwner.isEmpty()) |
| 217 |
setServiceValid(false); |
| 218 |
} |
| 219 |
|
| 220 |
////////////////////////////////////////////////////////// |
| 221 |
// class DDBusInterface |
| 222 |
////////////////////////////////////////////////////////// |
| 223 |
|
| 224 |
DDBusInterface::DDBusInterface(const QString &service, const QString &path, const QString &interface, const QDBusConnection &connection, QObject *parent) |
| 225 |
: QDBusAbstractInterface(service, path, interface.toLatin1(), connection, parent) |
| 226 |
, d_ptr(new DDBusInterfacePrivate(this, parent)) |
| 227 |
{ |
| 228 |
} |
| 229 |
|
| 230 |
DDBusInterface::~DDBusInterface() {} |
| 231 |
|
| 232 |
bool DDBusInterface::serviceValid() const |
| 233 |
{ |
| 234 |
Q_D(const DDBusInterface); |
| 235 |
return d->m_serviceValid; |
| 236 |
} |
| 237 |
|
| 238 |
QString DDBusInterface::suffix() const |
| 239 |
{ |
| 240 |
Q_D(const DDBusInterface); |
| 241 |
return d->m_suffix; |
| 242 |
} |
| 243 |
|
| 244 |
void DDBusInterface::setSuffix(const QString &suffix) |
| 245 |
{ |
| 246 |
Q_D(DDBusInterface); |
| 247 |
d->m_suffix = suffix; |
| 248 |
} |
| 249 |
|
| 250 |
inline QString originalPropname(const char *propname, QString suffix) |
| 251 |
{ |
| 252 |
QString propStr(propname); |
| 253 |
return propStr.left(propStr.length() - suffix.length()); |
| 254 |
} |
| 255 |
|
| 256 |
QVariant DDBusInterface::property(const char *propName) |
| 257 |
{ |
| 258 |
Q_D(DDBusInterface); |
| 259 |
|
| 260 |
QDBusMessage msg = QDBusMessage::createMethodCall(service(), path(), PropertiesInterface, QStringLiteral("Get")); |
| 261 |
msg << interface() << originalPropname(propName, d->m_suffix); |
| 262 |
QDBusPendingReply<QVariant> prop = connection().asyncCall(msg); |
| 263 |
if (prop.value().isValid()) { |
| 264 |
// if there is no parent, return value |
| 265 |
if (!parent()) { |
| 266 |
qWarning() << "you use it without parent, and if the value is not valid, you may get nothing"; |
| 267 |
return prop.value(); |
| 268 |
} |
| 269 |
auto metaObject = parent()->metaObject(); |
| 270 |
QVariant propresult = prop.value(); |
| 271 |
int i = metaObject->indexOfProperty(propName); |
| 272 |
if (i != -1) { |
| 273 |
QMetaProperty metaProperty = metaObject->property(i); |
| 274 |
// try to use property in parent to unwrap the value |
| 275 |
propresult = demarshall(metaProperty, propresult); |
| 276 |
} |
| 277 |
return propresult; |
| 278 |
} |
| 279 |
|
| 280 |
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(prop, this); |
| 281 |
watcher->setProperty(PropertyName, propName); |
| 282 |
connect(watcher, &QDBusPendingCallWatcher::finished, d, &DDBusInterfacePrivate::onAsyncPropertyFinished); |
| 283 |
|
| 284 |
return QVariant(); |
| 285 |
} |
| 286 |
|
| 287 |
void DDBusInterface::setProperty(const char *propName, const QVariant &value) |
| 288 |
{ |
| 289 |
Q_D(const DDBusInterface); |
| 290 |
QDBusMessage msg = QDBusMessage::createMethodCall(service(), path(), PropertiesInterface, QStringLiteral("Set")); |
| 291 |
msg << interface() << originalPropname(propName, d->m_suffix) << QVariant::fromValue(QDBusVariant(value)); |
| 292 |
|
| 293 |
QDBusPendingReply<void> reply = connection().asyncCall(msg); |
| 294 |
reply.waitForFinished(); |
| 295 |
if (!reply.isValid()) { |
| 296 |
qWarning() << reply.error().message(); |
| 297 |
} |
| 298 |
} |
| 299 |
DCORE_END_NAMESPACE |