diff options
Diffstat (limited to 'src/dbus/qdbusmarshaller.cpp')
-rw-r--r-- | src/dbus/qdbusmarshaller.cpp | 154 |
1 files changed, 55 insertions, 99 deletions
diff --git a/src/dbus/qdbusmarshaller.cpp b/src/dbus/qdbusmarshaller.cpp index 8e0b3e4598..b2ed2586fb 100644 --- a/src/dbus/qdbusmarshaller.cpp +++ b/src/dbus/qdbusmarshaller.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtDBus module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qdbusargument_p.h" #include "qdbusconnection.h" @@ -46,6 +10,8 @@ QT_BEGIN_NAMESPACE +using namespace Qt::StringLiterals; + static void qIterAppend(DBusMessageIter *it, QByteArray *ba, int type, const void *arg) { if (ba) @@ -59,6 +25,16 @@ QDBusMarshaller::~QDBusMarshaller() close(); } +void QDBusMarshaller::unregisteredTypeError(QMetaType id) +{ + const char *name = id.name(); + qWarning("QDBusMarshaller: type '%s' (%d) is not registered with D-Bus. " + "Use qDBusRegisterMetaType to register it", + name ? name : "", id.id()); + error("Unregistered type %1 passed in arguments"_L1 + .arg(QLatin1StringView(id.name()))); +} + inline QString QDBusMarshaller::currentSignature() { if (message) @@ -133,7 +109,7 @@ inline void QDBusMarshaller::append(const QDBusObjectPath &arg) { QByteArray data = arg.path().toUtf8(); if (!ba && data.isEmpty()) { - error(QLatin1String("Invalid object path passed in arguments")); + error("Invalid object path passed in arguments"_L1); } else { const char *cdata = data.constData(); if (!skipSignature) @@ -145,7 +121,7 @@ inline void QDBusMarshaller::append(const QDBusSignature &arg) { QByteArray data = arg.signature().toUtf8(); if (!ba && data.isEmpty()) { - error(QLatin1String("Invalid signature passed in arguments")); + error("Invalid signature passed in arguments"_L1); } else { const char *cdata = data.constData(); if (!skipSignature) @@ -157,7 +133,7 @@ inline void QDBusMarshaller::append(const QDBusUnixFileDescriptor &arg) { int fd = arg.fileDescriptor(); if (!ba && fd == -1) { - error(QLatin1String("Invalid file descriptor passed in arguments")); + error("Invalid file descriptor passed in arguments"_L1); } else { if (!skipSignature) qIterAppend(&iterator, ba, DBUS_TYPE_UNIX_FD, &fd); @@ -176,7 +152,7 @@ inline void QDBusMarshaller::append(const QByteArray &arg) DBusMessageIter subiterator; q_dbus_message_iter_open_container(&iterator, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING, &subiterator); - q_dbus_message_iter_append_fixed_array(&subiterator, DBUS_TYPE_BYTE, &cdata, arg.length()); + q_dbus_message_iter_append_fixed_array(&subiterator, DBUS_TYPE_BYTE, &cdata, arg.size()); q_dbus_message_iter_close_container(&iterator, &subiterator); } @@ -189,15 +165,15 @@ inline bool QDBusMarshaller::append(const QDBusVariant &arg) } const QVariant &value = arg.variant(); - int id = value.userType(); - if (id == QVariant::Invalid) { + QMetaType id = value.metaType(); + if (!id.isValid()) { qWarning("QDBusMarshaller: cannot add a null QDBusVariant"); - error(QLatin1String("Variant containing QVariant::Invalid passed in arguments")); + error("Invalid QVariant passed in arguments"_L1); return false; } QByteArray tmpSignature; - const char *signature = 0; + const char *signature = nullptr; if (id == QDBusMetaTypeId::argument()) { // take the signature from the QDBusArgument object we're marshalling tmpSignature = @@ -208,11 +184,7 @@ inline bool QDBusMarshaller::append(const QDBusVariant &arg) signature = QDBusMetaType::typeToSignature(id); } if (!signature) { - qWarning("QDBusMarshaller: type `%s' (%d) is not registered with D-BUS. " - "Use qDBusRegisterMetaType to register it", - QMetaType::typeName(id), id); - error(QLatin1String("Unregistered type %1 passed in arguments") - .arg(QLatin1String(QMetaType::typeName(id)))); + unregisteredTypeError(id); return false; } @@ -234,60 +206,48 @@ inline void QDBusMarshaller::append(const QStringList &arg) QDBusMarshaller sub(capabilities); open(sub, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING_AS_STRING); - QStringList::ConstIterator it = arg.constBegin(); - QStringList::ConstIterator end = arg.constEnd(); - for ( ; it != end; ++it) - sub.append(*it); + for (const QString &s : arg) + sub.append(s); // don't call sub.close(): it auto-closes } inline QDBusMarshaller *QDBusMarshaller::beginStructure() { - return beginCommon(DBUS_TYPE_STRUCT, 0); + return beginCommon(DBUS_TYPE_STRUCT, nullptr); } -inline QDBusMarshaller *QDBusMarshaller::beginArray(int id) +inline QDBusMarshaller *QDBusMarshaller::beginArray(QMetaType id) { - const char *signature = QDBusMetaType::typeToSignature( QVariant::Type(id) ); + const char *signature = QDBusMetaType::typeToSignature(id); if (!signature) { - qWarning("QDBusMarshaller: type `%s' (%d) is not registered with D-BUS. " - "Use qDBusRegisterMetaType to register it", - QMetaType::typeName(id), id); - error(QLatin1String("Unregistered type %1 passed in arguments") - .arg(QLatin1String(QMetaType::typeName(id)))); + unregisteredTypeError(id); return this; } return beginCommon(DBUS_TYPE_ARRAY, signature); } -inline QDBusMarshaller *QDBusMarshaller::beginMap(int kid, int vid) +inline QDBusMarshaller *QDBusMarshaller::beginMap(QMetaType kid, QMetaType vid) { - const char *ksignature = QDBusMetaType::typeToSignature( QVariant::Type(kid) ); + const char *ksignature = QDBusMetaType::typeToSignature(kid); if (!ksignature) { - qWarning("QDBusMarshaller: type `%s' (%d) is not registered with D-BUS. " - "Use qDBusRegisterMetaType to register it", - QMetaType::typeName(kid), kid); - error(QLatin1String("Unregistered type %1 passed in arguments") - .arg(QLatin1String(QMetaType::typeName(kid)))); + unregisteredTypeError(kid); return this; } if (ksignature[1] != 0 || !QDBusUtil::isValidBasicType(*ksignature)) { - qWarning("QDBusMarshaller: type '%s' (%d) cannot be used as the key type in a D-BUS map.", - QMetaType::typeName(kid), kid); - error(QLatin1String("Type %1 passed in arguments cannot be used as a key in a map") - .arg(QLatin1String(QMetaType::typeName(kid)))); +QT_WARNING_PUSH +QT_WARNING_DISABLE_GCC("-Wformat-overflow") + qWarning("QDBusMarshaller: type '%s' (%d) cannot be used as the key type in a D-Bus map.", + kid.name(), kid.id()); +QT_WARNING_POP + error("Type %1 passed in arguments cannot be used as a key in a map"_L1 + .arg(QLatin1StringView(kid.name()))); return this; } - const char *vsignature = QDBusMetaType::typeToSignature( QVariant::Type(vid) ); + const char *vsignature = QDBusMetaType::typeToSignature(vid); if (!vsignature) { - const char *typeName = QMetaType::typeName(vid); - qWarning("QDBusMarshaller: type `%s' (%d) is not registered with D-BUS. " - "Use qDBusRegisterMetaType to register it", - typeName, vid); - error(QLatin1String("Unregistered type %1 passed in arguments") - .arg(QLatin1String(typeName))); + unregisteredTypeError(vid); return this; } @@ -301,7 +261,7 @@ inline QDBusMarshaller *QDBusMarshaller::beginMap(int kid, int vid) inline QDBusMarshaller *QDBusMarshaller::beginMapEntry() { - return beginCommon(DBUS_TYPE_DICT_ENTRY, 0); + return beginCommon(DBUS_TYPE_DICT_ENTRY, nullptr); } void QDBusMarshaller::open(QDBusMarshaller &sub, int code, const char *signature) @@ -383,10 +343,10 @@ void QDBusMarshaller::error(const QString &msg) bool QDBusMarshaller::appendVariantInternal(const QVariant &arg) { - int id = arg.userType(); - if (id == QVariant::Invalid) { + QMetaType id = arg.metaType(); + if (!id.isValid()) { qWarning("QDBusMarshaller: cannot add an invalid QVariant"); - error(QLatin1String("Variant containing QVariant::Invalid passed in arguments")); + error("Invalid QVariant passed in arguments"_L1); return false; } @@ -400,7 +360,7 @@ bool QDBusMarshaller::appendVariantInternal(const QVariant &arg) QDBusDemarshaller demarshaller(capabilities); demarshaller.message = q_dbus_message_ref(d->message); - if (d->direction == Demarshalling) { + if (d->direction == Direction::Demarshalling) { // it's demarshalling; just copy demarshaller.iterator = static_cast<QDBusDemarshaller *>(d)->iterator; } else { @@ -412,13 +372,9 @@ bool QDBusMarshaller::appendVariantInternal(const QVariant &arg) return appendCrossMarshalling(&demarshaller); } - const char *signature = QDBusMetaType::typeToSignature( QVariant::Type(id) ); + const char *signature = QDBusMetaType::typeToSignature(id); if (!signature) { - qWarning("QDBusMarshaller: type `%s' (%d) is not registered with D-BUS. " - "Use qDBusRegisterMetaType to register it", - QMetaType::typeName(id), id); - error(QLatin1String("Unregistered type %1 passed in arguments") - .arg(QLatin1String(QMetaType::typeName(id)))); + unregisteredTypeError(id); return false; } @@ -485,12 +441,12 @@ bool QDBusMarshaller::appendVariantInternal(const QVariant &arg) case DBUS_TYPE_ARRAY: // could be many things // find out what kind of array it is - switch (arg.type()) { - case QVariant::StringList: + switch (arg.metaType().id()) { + case QMetaType::QStringList: append( arg.toStringList() ); return true; - case QVariant::ByteArray: + case QMetaType::QByteArray: append( arg.toByteArray() ); return true; @@ -516,7 +472,7 @@ bool QDBusMarshaller::appendVariantInternal(const QVariant &arg) Q_FALLTHROUGH(); default: - qWarning("QDBusMarshaller::appendVariantInternal: Found unknown D-BUS type '%s'", + qWarning("QDBusMarshaller::appendVariantInternal: Found unknown D-Bus type '%s'", signature); return false; } @@ -528,7 +484,7 @@ bool QDBusMarshaller::appendRegisteredType(const QVariant &arg) { ref.ref(); // reference up QDBusArgument self(QDBusArgumentPrivate::create(this)); - return QDBusMetaType::marshall(self, arg.userType(), arg.constData()); + return QDBusMetaType::marshall(self, arg.metaType(), arg.constData()); } bool QDBusMarshaller::appendCrossMarshalling(QDBusDemarshaller *demarshaller) @@ -536,7 +492,7 @@ bool QDBusMarshaller::appendCrossMarshalling(QDBusDemarshaller *demarshaller) int code = q_dbus_message_iter_get_arg_type(&demarshaller->iterator); if (QDBusUtil::isValidBasicType(code)) { // easy: just append - // do exactly like the D-BUS docs suggest + // do exactly like the D-Bus docs suggest // (see apidocs for q_dbus_message_iter_get_basic) qlonglong value; @@ -572,7 +528,7 @@ bool QDBusMarshaller::appendCrossMarshalling(QDBusDemarshaller *demarshaller) QDBusMarshaller mrecursed(capabilities); // create on the stack makes it autoclose QByteArray subSignature; - const char *sig = 0; + const char *sig = nullptr; if (code == DBUS_TYPE_VARIANT || code == DBUS_TYPE_ARRAY) { subSignature = drecursed->currentSignature().toLatin1(); if (!subSignature.isEmpty()) |