diff options
Diffstat (limited to 'src/dbus/qdbusinternalfilters.cpp')
-rw-r--r-- | src/dbus/qdbusinternalfilters.cpp | 193 |
1 files changed, 76 insertions, 117 deletions
diff --git a/src/dbus/qdbusinternalfilters.cpp b/src/dbus/qdbusinternalfilters.cpp index edee4fc1e5..721564ed3c 100644 --- a/src/dbus/qdbusinternalfilters.cpp +++ b/src/dbus/qdbusinternalfilters.cpp @@ -1,42 +1,6 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Copyright (C) 2016 Intel Corporation. -** 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. +// Copyright (C) 2016 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qdbusconnection_p.h" @@ -63,9 +27,11 @@ QT_BEGIN_NAMESPACE +using namespace Qt::StringLiterals; + // defined in qdbusxmlgenerator.cpp -extern QString qDBusGenerateMetaObjectXml(QString interface, const QMetaObject *mo, - const QMetaObject *base, int flags); +extern Q_DBUS_EXPORT QString qDBusGenerateMetaObjectXml(QString interface, const QMetaObject *mo, + const QMetaObject *base, int flags); static const char introspectableInterfaceXml[] = " <interface name=\"org.freedesktop.DBus.Introspectable\">\n" @@ -107,16 +73,13 @@ static const char peerInterfaceXml[] = " </method>\n" " </interface>\n"; -static QString generateSubObjectXml(QObject *object) +static QString generateSubObjectXml(const QObject *object) { QString retval; - const QObjectList &objs = object->children(); - QObjectList::ConstIterator it = objs.constBegin(); - QObjectList::ConstIterator end = objs.constEnd(); - for ( ; it != end; ++it) { - QString name = (*it)->objectName(); + for (const QObject *child : object->children()) { + QString name = child->objectName(); if (!name.isEmpty() && QDBusUtil::isValidPartOfObjectPath(name)) - retval += QLatin1String(" <node name=\"") + name + QLatin1String("\"/>\n"); + retval += " <node name=\""_L1 + name + "\"/>\n"_L1; } return retval; } @@ -127,8 +90,8 @@ QString qDBusIntrospectObject(const QDBusConnectionPrivate::ObjectTreeNode &node { // object may be null - QString xml_data(QLatin1String(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE)); - xml_data += QLatin1String("<node>\n"); + QString xml_data(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE ""_L1); + xml_data += "<node>\n"_L1; if (node.obj) { Q_ASSERT_X(QThread::currentThread() == node.obj->thread(), @@ -150,20 +113,22 @@ QString qDBusIntrospectObject(const QDBusConnectionPrivate::ObjectTreeNode &node (connector = qDBusFindAdaptorConnector(node.obj))) { // trasverse every adaptor in this object - QDBusAdaptorConnector::AdaptorMap::ConstIterator it = connector->adaptors.constBegin(); - QDBusAdaptorConnector::AdaptorMap::ConstIterator end = connector->adaptors.constEnd(); - for ( ; it != end; ++it) { + for (const QDBusAdaptorConnector::AdaptorData &adaptorData : + std::as_const(connector->adaptors)) { // add the interface: - QString ifaceXml = QDBusAbstractAdaptorPrivate::retrieveIntrospectionXml(it->adaptor); + QString ifaceXml = + QDBusAbstractAdaptorPrivate::retrieveIntrospectionXml(adaptorData.adaptor); if (ifaceXml.isEmpty()) { // add the interface's contents: - ifaceXml += qDBusGenerateMetaObjectXml(QString::fromLatin1(it->interface), - it->adaptor->metaObject(), - &QDBusAbstractAdaptor::staticMetaObject, - QDBusConnection::ExportScriptableContents - | QDBusConnection::ExportNonScriptableContents); - - QDBusAbstractAdaptorPrivate::saveIntrospectionXml(it->adaptor, ifaceXml); + ifaceXml += qDBusGenerateMetaObjectXml( + QString::fromLatin1(adaptorData.interface), + adaptorData.adaptor->metaObject(), + &QDBusAbstractAdaptor::staticMetaObject, + QDBusConnection::ExportScriptableContents + | QDBusConnection::ExportNonScriptableContents); + + QDBusAbstractAdaptorPrivate::saveIntrospectionXml(adaptorData.adaptor, + ifaceXml); } xml_data += ifaceXml; @@ -175,26 +140,23 @@ QString qDBusIntrospectObject(const QDBusConnectionPrivate::ObjectTreeNode &node xml_data += node.treeNode->introspect(path); } - xml_data += QLatin1String( propertiesInterfaceXml ); + xml_data += QLatin1StringView(propertiesInterfaceXml); } - xml_data += QLatin1String( introspectableInterfaceXml ); - xml_data += QLatin1String( peerInterfaceXml ); + xml_data += QLatin1StringView(introspectableInterfaceXml); + xml_data += QLatin1StringView(peerInterfaceXml); if (node.flags & QDBusConnection::ExportChildObjects) { xml_data += generateSubObjectXml(node.obj); } else { // generate from the object tree - QDBusConnectionPrivate::ObjectTreeNode::DataList::ConstIterator it = - node.children.constBegin(); - QDBusConnectionPrivate::ObjectTreeNode::DataList::ConstIterator end = - node.children.constEnd(); - for ( ; it != end; ++it) - if (it->obj || !it->children.isEmpty()) - xml_data += QLatin1String(" <node name=\"") + it->name + QLatin1String("\"/>\n"); + for (const QDBusConnectionPrivate::ObjectTreeNode &node : node.children) { + if (node.obj || !node.children.isEmpty()) + xml_data += " <node name=\""_L1 + node.name + "\"/>\n"_L1; + } } - xml_data += QLatin1String("</node>\n"); + xml_data += "</node>\n"_L1; return xml_data; } @@ -203,7 +165,7 @@ QString qDBusIntrospectObject(const QDBusConnectionPrivate::ObjectTreeNode &node static inline QDBusMessage interfaceNotFoundError(const QDBusMessage &msg, const QString &interface_name) { return msg.createErrorReply(QDBusError::UnknownInterface, - QLatin1String("Interface %1 was not found in object %2") + "Interface %1 was not found in object %2"_L1 .arg(interface_name, msg.path())); } @@ -211,17 +173,17 @@ static inline QDBusMessage propertyNotFoundError(const QDBusMessage &msg, const QString &interface_name, const QByteArray &property_name) { return msg.createErrorReply(QDBusError::UnknownProperty, - QLatin1String("Property %1%2%3 was not found in object %4") + "Property %1%2%3 was not found in object %4"_L1 .arg(interface_name, - QLatin1String(interface_name.isEmpty() ? "" : "."), - QLatin1String(property_name), + interface_name.isEmpty() ? ""_L1 : "."_L1, + QLatin1StringView(property_name), msg.path())); } QDBusMessage qDBusPropertyGet(const QDBusConnectionPrivate::ObjectTreeNode &node, const QDBusMessage &msg) { - Q_ASSERT(msg.arguments().count() == 2); + Q_ASSERT(msg.arguments().size() == 2); Q_ASSERT_X(!node.obj || QThread::currentThread() == node.obj->thread(), "QDBusConnection: internal threading error", "function called for an object that is in another thread!!"); @@ -229,7 +191,7 @@ QDBusMessage qDBusPropertyGet(const QDBusConnectionPrivate::ObjectTreeNode &node QString interface_name = msg.arguments().at(0).toString(); QByteArray property_name = msg.arguments().at(1).toString().toUtf8(); - QDBusAdaptorConnector *connector; + const QDBusAdaptorConnector *connector; QVariant value; bool interfaceFound = false; if (node.flags & QDBusConnection::ExportAdaptors && @@ -238,12 +200,11 @@ QDBusMessage qDBusPropertyGet(const QDBusConnectionPrivate::ObjectTreeNode &node // find the class that implements interface_name or try until we've found the property // in case of an empty interface if (interface_name.isEmpty()) { - for (QDBusAdaptorConnector::AdaptorMap::ConstIterator it = connector->adaptors.constBegin(), - end = connector->adaptors.constEnd(); it != end; ++it) { - const QMetaObject *mo = it->adaptor->metaObject(); + for (const QDBusAdaptorConnector::AdaptorData &adaptorData : connector->adaptors) { + const QMetaObject *mo = adaptorData.adaptor->metaObject(); int pidx = mo->indexOfProperty(property_name); if (pidx != -1) { - value = mo->property(pidx).read(it->adaptor); + value = mo->property(pidx).read(adaptorData.adaptor); break; } } @@ -251,7 +212,7 @@ QDBusMessage qDBusPropertyGet(const QDBusConnectionPrivate::ObjectTreeNode &node QDBusAdaptorConnector::AdaptorMap::ConstIterator it; it = std::lower_bound(connector->adaptors.constBegin(), connector->adaptors.constEnd(), interface_name); - if (it != connector->adaptors.constEnd() && interface_name == QLatin1String(it->interface)) { + if (it != connector->adaptors.constEnd() && interface_name == QLatin1StringView(it->interface)) { interfaceFound = true; value = it->adaptor->property(property_name); } @@ -302,16 +263,16 @@ static QDBusMessage propertyWriteReply(const QDBusMessage &msg, const QString &i return propertyNotFoundError(msg, interface_name, property_name); case PropertyTypeMismatch: return msg.createErrorReply(QDBusError::InvalidArgs, - QLatin1String("Invalid arguments for writing to property %1%2%3") + "Invalid arguments for writing to property %1%2%3"_L1 .arg(interface_name, - QLatin1String(interface_name.isEmpty() ? "" : "."), - QLatin1String(property_name))); + interface_name.isEmpty() ? ""_L1 : "."_L1, + QLatin1StringView(property_name))); case PropertyReadOnly: return msg.createErrorReply(QDBusError::PropertyReadOnly, - QLatin1String("Property %1%2%3 is read-only") + "Property %1%2%3 is read-only"_L1 .arg(interface_name, - QLatin1String(interface_name.isEmpty() ? "" : "."), - QLatin1String(property_name))); + interface_name.isEmpty() ? ""_L1 : "."_L1, + QLatin1StringView(property_name))); case PropertyWriteFailed: return msg.createErrorReply(QDBusError::InternalError, QString::fromLatin1("Internal error")); @@ -348,39 +309,38 @@ static int writeProperty(QObject *obj, const QByteArray &property_name, QVariant // we found our property // do we have the right type? - int id = mp.userType(); - if (!id){ + QMetaType id = mp.metaType(); + if (!id.isValid()){ // type not registered or invalid / void? qWarning("QDBusConnection: Unable to handle unregistered datatype '%s' for property '%s::%s'", mp.typeName(), mo->className(), property_name.constData()); return PropertyWriteFailed; } - if (id != QMetaType::QVariant && value.userType() == QDBusMetaTypeId::argument()) { + if (id.id() != QMetaType::QVariant && value.metaType() == QDBusMetaTypeId::argument()) { // we have to demarshall before writing - void *null = 0; - QVariant other(id, null); - if (!QDBusMetaType::demarshall(qvariant_cast<QDBusArgument>(value), id, other.data())) { - qWarning("QDBusConnection: type `%s' (%d) is not registered with QtDBus. " + QVariant other{QMetaType(id)}; + if (!QDBusMetaType::demarshall(qvariant_cast<QDBusArgument>(value), other.metaType(), other.data())) { + qWarning("QDBusConnection: type '%s' (%d) is not registered with QtDBus. " "Use qDBusRegisterMetaType to register it", - mp.typeName(), id); + mp.typeName(), id.id()); return PropertyWriteFailed; } - value = other; + value = std::move(other); } - if (mp.userType() == qMetaTypeId<QDBusVariant>()) + if (mp.metaType() == QMetaType::fromType<QDBusVariant>()) value = QVariant::fromValue(QDBusVariant(value)); // the property type here should match - return mp.write(obj, value) ? PropertyWriteSuccess : PropertyWriteFailed; + return mp.write(obj, std::move(value)) ? PropertyWriteSuccess : PropertyWriteFailed; } QDBusMessage qDBusPropertySet(const QDBusConnectionPrivate::ObjectTreeNode &node, const QDBusMessage &msg) { - Q_ASSERT(msg.arguments().count() == 3); + Q_ASSERT(msg.arguments().size() == 3); Q_ASSERT_X(!node.obj || QThread::currentThread() == node.obj->thread(), "QDBusConnection: internal threading error", "function called for an object that is in another thread!!"); @@ -396,9 +356,9 @@ QDBusMessage qDBusPropertySet(const QDBusConnectionPrivate::ObjectTreeNode &node // find the class that implements interface_name or try until we've found the property // in case of an empty interface if (interface_name.isEmpty()) { - for (QDBusAdaptorConnector::AdaptorMap::ConstIterator it = connector->adaptors.constBegin(), - end = connector->adaptors.constEnd(); it != end; ++it) { - int status = writeProperty(it->adaptor, property_name, value); + for (const QDBusAdaptorConnector::AdaptorData &adaptorData : + std::as_const(connector->adaptors)) { + int status = writeProperty(adaptorData.adaptor, property_name, value); if (status == PropertyNotFound) continue; return propertyWriteReply(msg, interface_name, property_name, status); @@ -407,7 +367,7 @@ QDBusMessage qDBusPropertySet(const QDBusConnectionPrivate::ObjectTreeNode &node QDBusAdaptorConnector::AdaptorMap::ConstIterator it; it = std::lower_bound(connector->adaptors.constBegin(), connector->adaptors.constEnd(), interface_name); - if (it != connector->adaptors.cend() && interface_name == QLatin1String(it->interface)) { + if (it != connector->adaptors.cend() && interface_name == QLatin1StringView(it->interface)) { return propertyWriteReply(msg, interface_name, property_name, writeProperty(it->adaptor, property_name, value)); } @@ -436,10 +396,8 @@ QDBusMessage qDBusPropertySet(const QDBusConnectionPrivate::ObjectTreeNode &node // unite two QVariantMaps, but don't generate duplicate keys static QVariantMap &operator+=(QVariantMap &lhs, const QVariantMap &rhs) { - QVariantMap::ConstIterator it = rhs.constBegin(), - end = rhs.constEnd(); - for ( ; it != end; ++it) - lhs.insert(it.key(), it.value()); + for (const auto &[key, value] : rhs.asKeyValueRange()) + lhs.insert(key, value); return lhs; } @@ -457,10 +415,10 @@ static QVariantMap readAllProperties(QObject *object, int flags) continue; // is it a registered property? - int typeId = mp.userType(); - if (!typeId) + QMetaType type = mp.metaType(); + if (!type.isValid()) continue; - const char *signature = QDBusMetaType::typeToSignature(typeId); + const char *signature = QDBusMetaType::typeToSignature(type); if (!signature) continue; @@ -480,7 +438,7 @@ static QVariantMap readAllProperties(QObject *object, int flags) QDBusMessage qDBusPropertyGetAll(const QDBusConnectionPrivate::ObjectTreeNode &node, const QDBusMessage &msg) { - Q_ASSERT(msg.arguments().count() == 1); + Q_ASSERT(msg.arguments().size() == 1); Q_ASSERT_X(!node.obj || QThread::currentThread() == node.obj->thread(), "QDBusConnection: internal threading error", "function called for an object that is in another thread!!"); @@ -496,16 +454,17 @@ QDBusMessage qDBusPropertyGetAll(const QDBusConnectionPrivate::ObjectTreeNode &n if (interface_name.isEmpty()) { // iterate over all interfaces - for (QDBusAdaptorConnector::AdaptorMap::ConstIterator it = connector->adaptors.constBegin(), - end = connector->adaptors.constEnd(); it != end; ++it) { - result += readAllProperties(it->adaptor, QDBusConnection::ExportAllProperties); + for (const QDBusAdaptorConnector::AdaptorData &adaptorData : + std::as_const(connector->adaptors)) { + result += readAllProperties(adaptorData.adaptor, + QDBusConnection::ExportAllProperties); } } else { // find the class that implements interface_name QDBusAdaptorConnector::AdaptorMap::ConstIterator it; it = std::lower_bound(connector->adaptors.constBegin(), connector->adaptors.constEnd(), interface_name); - if (it != connector->adaptors.constEnd() && interface_name == QLatin1String(it->interface)) { + if (it != connector->adaptors.constEnd() && interface_name == QLatin1StringView(it->interface)) { interfaceFound = true; result = readAllProperties(it->adaptor, QDBusConnection::ExportAllProperties); } |