/* SPDX-FileCopyrightText: 2012 Martin Gräßlin SPDX-FileCopyrightText: 2024 Fushan Wen SPDX-License-Identifier: LGPL-2.1-or-later */ #include "dbusdecoder.h" #include #include #include #include #include #include "dbustype.h" namespace Decoder { QVariant dbusToVariant(const QVariant &variant) { if (variant.metaType() == QMetaType::fromType()) { const QDBusArgument argument = variant.value(); switch (argument.currentType()) { case QDBusArgument::BasicType: case QDBusArgument::MapEntryType: return dbusToVariant(argument.asVariant()); case QDBusArgument::VariantType: return dbusToVariant(argument.asVariant().value().variant()); case QDBusArgument::ArrayType: { QVariantList array; argument.beginArray(); while (!argument.atEnd()) { const QVariant value = argument.asVariant(); array.append(dbusToVariant(value)); } argument.endArray(); return array; } case QDBusArgument::StructureType: { QVariantList structure; argument.beginStructure(); while (!argument.atEnd()) { const QVariant value = argument.asVariant(); structure.append(dbusToVariant(value)); } argument.endStructure(); return structure; } case QDBusArgument::MapType: { QVariantMap map; argument.beginMap(); while (!argument.atEnd()) { argument.beginMapEntry(); const QVariant key = argument.asVariant(); const QVariant value = argument.asVariant(); argument.endMapEntry(); map.insert(key.toString(), dbusToVariant(value)); } argument.endMap(); return map; } default: return variant; } } else if (variant.metaType() == QMetaType::fromType()) { return Plasma::DBus::OBJECTPATH(get(variant).path()); } else if (variant.metaType() == QMetaType::fromType()) { return Plasma::DBus::SIGNATURE(get(variant).signature()); } else if (variant.metaType() == QMetaType::fromType()) { return dbusToVariant(get(variant).variant()); } else { switch (variant.typeId()) { case QMetaType::Bool: return Plasma::DBus::BOOL(get(variant)); case QMetaType::Short: return Plasma::DBus::INT16(get(variant)); case QMetaType::Int: return Plasma::DBus::INT32(get(variant)); case QMetaType::LongLong: return Plasma::DBus::INT64(get(variant)); case QMetaType::UShort: return Plasma::DBus::UINT16(get(variant)); case QMetaType::UInt: return Plasma::DBus::UINT32(get(variant)); case QMetaType::ULongLong: return Plasma::DBus::UINT64(get(variant)); case QMetaType::Double: return Plasma::DBus::DOUBLE(get(variant)); case QMetaType::UChar: return Plasma::DBus::BYTE(get(variant)); case QMetaType::QString: return Plasma::DBus::STRING(get(variant)); case QMetaType::QByteArray: { const QByteArray &bytes = get(variant); return QVariant::fromValue(QList{bytes.cbegin(), bytes.cend()}); } } } return variant; } QVariantList decode(const QDBusMessage &message) { QVariantList arguments = message.arguments(); for (QVariant &arg : arguments) { arg = dbusToVariant(arg); } return arguments; } }