diff options
Diffstat (limited to 'qt/qdbustype.cpp')
-rw-r--r-- | qt/qdbustype.cpp | 214 |
1 files changed, 92 insertions, 122 deletions
diff --git a/qt/qdbustype.cpp b/qt/qdbustype.cpp index 036bbe13..97c9e15e 100644 --- a/qt/qdbustype.cpp +++ b/qt/qdbustype.cpp @@ -92,79 +92,6 @@ public: virtual QString addElementsToStruct(const QStringList& subTypes) ; }; -#if 0 -/* - * Parse the signature and return the max length that is valid - */ -static int parse(const char* signature) -{ - if (!signature || !*signature) - return 0; // not valid - - switch (signature[0]) { - case DBUS_TYPE_BOOLEAN: - case DBUS_TYPE_BYTE: - case DBUS_TYPE_INT16: - case DBUS_TYPE_INT32: - case DBUS_TYPE_UINT16: - case DBUS_TYPE_UINT32: - case DBUS_TYPE_INT64: - case DBUS_TYPE_UINT64: - case DBUS_TYPE_DOUBLE: - case DBUS_TYPE_STRING: - case DBUS_TYPE_OBJECT_PATH: - case DBUS_TYPE_SIGNATURE: - case DBUS_TYPE_VARIANT: - return 1; - - case DBUS_TYPE_ARRAY: { - // check if it's a dict-entry array - if (signature[1] == DBUS_DICT_ENTRY_BEGIN_CHAR) { - // the first type must be ok and primitive (length 1) - char c[2] = { signature[2], 0 }; - if (parse(c) != 1) - return 0; // not valid - - // the rest must be a valid type too - int len = parse(signature + 3); - if (len == 0) - return 0; // not valid - - // check the closing brace - if (signature[len + 3] != DBUS_DICT_ENTRY_END_CHAR) - return 0; // not valid - - // it's valid - return len + 4; - } - - // it's not a dict-entry, so it's ok as long as the internal type is ok too - int len = parse(signature + 1); - return len ? len + 1 : 0; - } - - case DBUS_STRUCT_BEGIN_CHAR: { - // check that each entry is valid - int i = 1; - while (i) { - if (i > 1 && signature[i] == DBUS_STRUCT_END_CHAR) - break; // this is valid - - int len = parse(signature + i); - if (len) - i += len; - else - i = 0; - } - return i; - } - - default: - return 0; // not valid - } -} -#endif - static QString findInMap(char type, const QDBusPrettyTypeBase::Entry* map) { for ( ; map->signature; ++map) @@ -197,10 +124,10 @@ inline QString QDBusPrettyTypeBase::toString(const QDBusType& type) case DBUS_TYPE_DICT_ENTRY: { Q_ASSERT_X(subTypes.size() == 2, "QDBusType::toString", "maps must have exactly two elements"); - + QString key = findInMap( subTypes.at(0).dbusType(), map ); QString value = toString( subTypes.at(1) ); - + Q_ASSERT(!key.isNull()); return addElementsToMap( key, value ); @@ -264,14 +191,14 @@ QString QDBusConventionalNames::addElementsToArray(const QString& subType) const QDBusPrettyTypeBase::Entry* QDBusQtNames::entryMap() { static QDBusPrettyTypeBase::Entry translation[] = { - { "quint8", DBUS_TYPE_BYTE }, + { "uchar", DBUS_TYPE_BYTE }, { "bool", DBUS_TYPE_BOOLEAN }, - { "qint16", DBUS_TYPE_INT16 }, - { "quint16", DBUS_TYPE_UINT16 }, - { "qint32", DBUS_TYPE_INT32 }, - { "quint32", DBUS_TYPE_UINT32 }, - { "qint64", DBUS_TYPE_INT64 }, - { "quint64", DBUS_TYPE_UINT64 }, + { "short", DBUS_TYPE_INT16 }, + { "ushort", DBUS_TYPE_UINT16 }, + { "int", DBUS_TYPE_INT32 }, + { "uint", DBUS_TYPE_UINT32 }, + { "qlonglong", DBUS_TYPE_INT64 }, + { "qulonglong", DBUS_TYPE_UINT64 }, { "double", DBUS_TYPE_DOUBLE }, { "QString", DBUS_TYPE_STRING }, { "QString", DBUS_TYPE_OBJECT_PATH }, @@ -283,8 +210,8 @@ const QDBusPrettyTypeBase::Entry* QDBusQtNames::entryMap() static inline QString templateArg(const QString& input) { - if (input.endsWith('>')) - return input + ' '; + if (input.endsWith(QLatin1Char('>'))) + return input + QLatin1Char(' '); return input; } @@ -292,11 +219,14 @@ QString QDBusQtNames::addElementsToStruct(const QStringList& subTypes) { Q_UNUSED(subTypes); - return QLatin1String("QList"); // CHANGEME in the future + return QLatin1String("QVariantList"); // CHANGEME in the future } QString QDBusQtNames::addElementsToMap(const QString& key, const QString& value) { + if (key == QLatin1String("QString") && value == QLatin1String("QDBusVariant")) + return QLatin1String("QVariantMap"); + return QString( QLatin1String("QMap<%1, %2>") ) .arg(key) .arg( templateArg(value) ); @@ -304,10 +234,13 @@ QString QDBusQtNames::addElementsToMap(const QString& key, const QString& value) QString QDBusQtNames::addElementsToArray(const QString& subType) { - if (subType == QLatin1String("quint8")) + if (subType == QLatin1String("uchar")) // special case return QLatin1String("QByteArray"); - + else if (subType == QLatin1String("QString")) + // special case + return QLatin1String("QStringList"); + return QString( QLatin1String("QList<%1>") ) .arg( templateArg(subType) ); } @@ -323,16 +256,19 @@ QString QDBusQVariantNames::addElementsToMap(const QString& key, const QString& { Q_UNUSED(key); Q_UNUSED(value); - + return QLatin1String("QVariantMap"); } QString QDBusQVariantNames::addElementsToArray(const QString& subType) { - if (subType == QLatin1String("quint8")) + if (subType == QLatin1String("uchar")) // special case return QLatin1String("QByteArray"); - + else if (subType == QLatin1String("QString")) + // special case + return QLatin1String("QStringList"); + return QLatin1String("QVariantList"); } @@ -354,15 +290,42 @@ public: /*! \class QDBusType - - Represents one single DBus type. + \brief Represents one single D-Bus type. + + D-Bus provides a set of primitive types that map to normal, C++ types and to QString, as well as + the possibility to extend the set with the so-called "container" types. The available types are + as follows: + + - Primitive (or basic): integers of 16, 32 and 64 bits, both signed and unsigned; byte (8 bits); + double-precision floating point and Unicode strings + - Arrays: a homogeneous, ordered list of zero or more entries + - Maps: an unordered list of (key, value) pairs, where key must be a primitive type and value + can be any D-Bus type + - Structs: an ordered list of a fixed number of entries of any type + - Variants: a "wildcard" container that can assume the value of any other type, including + structs and arrays + + Any type can be placed inside an array (including other arrays), but only entries of the same + type can be placed inside the same array. The analogous type for D-Bus arrays are the Qt + #QList template classes. + + Structs have a fixed number of entries and each entry has a fixed type. They are analogous to C + and C++ structs (hence the name). + + Maps or dictionaries are analogous to the Qt #QMap template class, with the additional + restriction that the key type must be a primitive one. D-Bus implements maps by using arrays of + a special type (a "dictionary entry"), so inspecting a QDBusType of a Map will reveal that it is + an array (see isArray()). + + Variants contain exactly one entry, but the type can vary freely. It is analogous to the Qt + class #QVariant, but the QtDBus implementation uses #QDBusVariant to represent D-Bus Variants. */ /*! \enum QDBusType::StringFormat This enum is used in QDBusType::toString to determine which type of formatting - to apply to the DBus types: + to apply to the D-Bus types: \value ConventionalNames Use the DBus conventional names, such as STRING, BOOLEAN or ARRAY of BYTE. @@ -380,8 +343,6 @@ QDBusType::QDBusType() /*! Constructs the type based on the given DBus type. - - \param type the type */ QDBusType::QDBusType(int type) { @@ -392,7 +353,6 @@ QDBusType::QDBusType(int type) /*! Constructs the type based on the given QVariant type. - \param type the type \sa QVariant::Type */ QDBusType::QDBusType(QVariant::Type type) @@ -430,7 +390,7 @@ QDBusType::QDBusType(QVariant::Type type) /*! Parses the given DBus signature and constructs the type it represents. - \param signature the signature to parse. It must represent one single type, but can + \param signature the signature to parse. It must represent one single type, but can be a container type. */ QDBusType::QDBusType(const char* signature) @@ -447,8 +407,8 @@ QDBusType::QDBusType(const char* signature) /*! Parses the given DBus signature and constructs the type it represents. - - \param signature the signature to parse. It must represent one single type, but can + + \param str the signature to parse. It must represent one single type, but can a container type. */ QDBusType::QDBusType(const QString& str) @@ -458,8 +418,8 @@ QDBusType::QDBusType(const QString& str) /*! Parses the given DBus signature and constructs the type it represents. - - \param signature the signature to parse. It must represent one single type, but can + + \param str the signature to parse. It must represent one single type, but can a container type. */ QDBusType::QDBusType(const QByteArray& str) @@ -468,6 +428,7 @@ QDBusType::QDBusType(const QByteArray& str) } /*! + \internal Creates a QDBusType object based on the current element pointed to by \a iter. \param iter the iterator. Can be pointing to container types. @@ -479,7 +440,7 @@ QDBusType::QDBusType(DBusSignatureIter* iter) // we have to recurse if ( d->code == DBUS_TYPE_VARIANT ) return; // no we don't. dbus_type_is_container lies to us - + // we have to recurse DBusSignatureIter subiter; dbus_signature_iter_recurse(iter, &subiter); @@ -545,11 +506,11 @@ QByteArray QDBusType::dbusSignature() const QByteArray retval; switch (d->code) { // can only be array, map or struct - + case DBUS_TYPE_ARRAY: Q_ASSERT_X(d->subTypes.size() == 1, "QDBusType::dbusSignature", "more than one element in array"); - + retval += DBUS_TYPE_ARRAY; retval += d->subTypes.at(0).dbusSignature(); break; @@ -563,7 +524,7 @@ QByteArray QDBusType::dbusSignature() const Q_ASSERT(key != DBUS_TYPE_INVALID); Q_ASSERT(!value.isEmpty()); - + retval.reserve(value.length() + 3); retval = DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING; retval += key; @@ -593,7 +554,7 @@ QVariant::Type QDBusType::qvariantType() const { if (d && d->qvariantType != QVariant::Invalid) return d->qvariantType; - + // check the special array cases: if (isArray()) { QDBusType t = arrayElement(); @@ -619,7 +580,7 @@ bool QDBusType::isValid() const /*! Returns true if this type is a basic one. - + \sa dbus_type_is_basic */ bool QDBusType::isBasic() const @@ -629,7 +590,7 @@ bool QDBusType::isBasic() const /*! Returns true if this type is a container. - + \sa dbus_type_is_container */ bool QDBusType::isContainer() const @@ -639,7 +600,7 @@ bool QDBusType::isContainer() const /*! Returns the subtypes of this type, if this is a container. - + \sa isContainer */ QDBusTypeList QDBusType::subTypes() const @@ -728,7 +689,7 @@ QString QDBusType::toString(StringFormat sf) const switch (sf) { case ConventionalNames: return QDBusConventionalNames().toString(*this); - + case QtNames: return QDBusQtNames().toString(*this); @@ -778,7 +739,7 @@ QVariant::Type QDBusType::qvariantType(const char* signature) case DBUS_TYPE_INT16: case DBUS_TYPE_INT32: return QVariant::Int; - + case DBUS_TYPE_BYTE: case DBUS_TYPE_UINT16: case DBUS_TYPE_UINT32: @@ -803,7 +764,7 @@ QVariant::Type QDBusType::qvariantType(const char* signature) case DBUS_TYPE_VARIANT: return QVariant::UserType; // must set user-type too - + case DBUS_TYPE_ARRAY: // special case // check if it's a string list if (qvariantType(signature + 1) == QVariant::String) @@ -872,7 +833,7 @@ int QDBusType::dbusType(QVariant::Type t) case QVariant::Map: // internal type information has been lost return DBUS_TYPE_DICT_ENTRY; - + case QVariant::List: case QVariant::StringList: case QVariant::ByteArray: @@ -896,7 +857,7 @@ int QDBusType::dbusType(QVariant::Type t) Converts the QVariant::Type to a DBus type signature. \param t the type to convert -*/ +*/ const char* QDBusType::dbusSignature(QVariant::Type t) { switch (t) @@ -946,11 +907,11 @@ const char* QDBusType::dbusSignature(QVariant::Type t) case QVariant::StringList: return DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING; // as - + case QVariant::ByteArray: return DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING; // ay - + case QVariant::List: // not a string list // internal list data has been lost @@ -967,6 +928,14 @@ const char* QDBusType::dbusSignature(QVariant::Type t) } /*! + \enum QDBusType::VariantListMode + Defines how the guessFromVariant() function will behave when the QVariant is of type + QVariant::List. + + \todo Improve the algorithm +*/ + +/*! Guesses the DBus type from the given variant. */ QDBusType QDBusType::guessFromVariant(const QVariant& variant, VariantListMode mode) @@ -1014,7 +983,7 @@ QDBusType QDBusType::guessFromVariant(const QVariant& variant, VariantListMode m t.d->subTypes << guessFromVariant(v, mode); return t; - } + } } else if (variant.type() == QVariant::Map) { // investigate deeper @@ -1061,25 +1030,25 @@ QDBusType QDBusType::guessFromVariant(const QVariant& variant, VariantListMode m } else return QDBusType(variant.type()); -} +} /*! \class QDBusTypeList \brief A list of DBus types. - + Represents zero or more DBus types in sequence, such as those used in argument lists or in subtypes of structs and maps. */ /*! \fn QDBusTypeList::QDBusTypeList() - + Default constructor. */ /*! \fn QDBusTypeList::QDBusTypeList(const QDBusTypeList& other) - + Copy constructor. \param other the list to copy */ @@ -1099,7 +1068,7 @@ QDBusTypeList::QDBusTypeList(const char* signature) { if (!signature || !*signature) return; // empty - + // validate it first if ( !dbus_signature_validate(signature, 0) ) return; @@ -1114,6 +1083,7 @@ QDBusTypeList::QDBusTypeList(const char* signature) } /*! + \internal Constructs a type list by parsing the elements on this iterator level. \param iter the iterator containing the elements on this level |