Qt 4.8
Namespaces | Functions | Variables
qdbusutil.cpp File Reference
#include "qdbusutil_p.h"
#include "qdbus_symbols_p.h"
#include <QtCore/qstringlist.h>
#include "qdbusargument.h"
#include "qdbusunixfiledescriptor.h"

Go to the source code of this file.

Namespaces

 QDBusUtil
 The QDBusUtil namespace contains a few functions that are of general use when dealing with D-Bus strings.
 

Functions

static bool argToString (const QDBusArgument &arg, QString &out)
 
QString QDBusUtil::argumentToString (const QVariant &arg)
 Dumps the contents of a QtDBus argument from arg into a string. More...
 
static bool isBasicType (int c)
 
static bool isFixedType (int c)
 
bool QDBusUtil::isValidBasicType (int c)
 Returns true if c is a valid, basic D-Bus type. More...
 
bool QDBusUtil::isValidBusName (const QString &busName)
 Returns true if busName is a valid bus name. More...
 
static bool isValidCharacter (const QChar &c)
 
static bool isValidCharacterNoDash (const QChar &c)
 
bool QDBusUtil::isValidErrorName (const QString &errorName)
 Returns true if errorName is a valid error name. More...
 
bool QDBusUtil::isValidFixedType (int c)
 Returns true if c is a valid, fixed D-Bus type. More...
 
bool QDBusUtil::isValidInterfaceName (const QString &ifaceName)
 Returns true if this is ifaceName is a valid interface name. More...
 
bool QDBusUtil::isValidMemberName (const QString &memberName)
 Returns true if memberName is a valid member name. More...
 
static bool isValidNumber (const QChar &c)
 
bool QDBusUtil::isValidObjectPath (const QString &path)
 Returns true if path is valid object path. More...
 
bool QDBusUtil::isValidPartOfObjectPath (const QString &part)
 
bool QDBusUtil::isValidSignature (const QString &signature)
 Returns true if signature is a valid D-Bus type signature for one or more types. More...
 
bool QDBusUtil::isValidSingleSignature (const QString &signature)
 Returns true if signature is a valid D-Bus type signature for exactly one full type. More...
 
bool QDBusUtil::isValidUniqueConnectionName (const QString &connName)
 Returns true if connName is a valid unique connection name. More...
 
static const char * validateSingleType (const char *signature)
 
static bool variantToString (const QVariant &arg, QString &out)
 

Variables

static const char basicTypes [] = "sogybnqiuxtdh"
 
static const char fixedTypes [] = "ybnqiuxtdh"
 
static const char oneLetterTypes [] = "vsogybnqiuxtdh"
 

Function Documentation

◆ argToString()

bool argToString ( const QDBusArgument arg,
QString out 
)
static

Definition at line 162 of file qdbusutil.cpp.

Referenced by isValidNumber(), and variantToString().

163 {
164  QString busSig = busArg.currentSignature();
165  bool doIterate = false;
166  QDBusArgument::ElementType elementType = busArg.currentType();
167 
168  if (elementType != QDBusArgument::BasicType && elementType != QDBusArgument::VariantType
169  && elementType != QDBusArgument::MapEntryType)
170  out += QLatin1String("[Argument: ") + busSig + QLatin1Char(' ');
171 
172  switch (elementType) {
175  if (!variantToString(busArg.asVariant(), out))
176  return false;
177  break;
179  busArg.beginStructure();
180  doIterate = true;
181  break;
183  busArg.beginArray();
184  out += QLatin1Char('{');
185  doIterate = true;
186  break;
188  busArg.beginMap();
189  out += QLatin1Char('{');
190  doIterate = true;
191  break;
193  busArg.beginMapEntry();
194  if (!variantToString(busArg.asVariant(), out))
195  return false;
196  out += QLatin1String(" = ");
197  if (!argToString(busArg, out))
198  return false;
199  busArg.endMapEntry();
200  break;
202  default:
203  out += QLatin1String("<ERROR - Unknown Type>");
204  return false;
205  }
206  if (doIterate && !busArg.atEnd()) {
207  while (!busArg.atEnd()) {
208  if (!argToString(busArg, out))
209  return false;
210  out += QLatin1String(", ");
211  }
212  out.chop(2);
213  }
214  switch (elementType) {
219  // nothing to do
220  break;
222  busArg.endStructure();
223  break;
225  out += QLatin1Char('}');
226  busArg.endArray();
227  break;
229  out += QLatin1Char('}');
230  busArg.endMap();
231  break;
232  }
233 
234  if (elementType != QDBusArgument::BasicType && elementType != QDBusArgument::VariantType
235  && elementType != QDBusArgument::MapEntryType)
236  out += QLatin1Char(']');
237 
238  return true;
239 }
static bool argToString(const QDBusArgument &arg, QString &out)
Definition: qdbusutil.cpp:162
void chop(int n)
Removes n characters from the end of the string.
Definition: qstring.cpp:4623
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
static bool variantToString(const QVariant &arg, QString &out)
Definition: qdbusutil.cpp:81
ElementType
This enum describes the type of element held by the argument.
Definition: qdbusargument.h:72
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ isBasicType()

static bool isBasicType ( int  c)
static

Definition at line 246 of file qdbusutil.cpp.

Referenced by QDBusUtil::isValidBasicType(), and validateSingleType().

247 {
248  return c != DBUS_TYPE_INVALID && strchr(basicTypes, c) != NULL;
249 }
unsigned char c[8]
Definition: qnumeric_p.h:62
static const char basicTypes[]
Definition: qdbusutil.cpp:243

◆ isFixedType()

static bool isFixedType ( int  c)
static

Definition at line 251 of file qdbusutil.cpp.

Referenced by QDBusUtil::isValidFixedType().

252 {
253  return c != DBUS_TYPE_INVALID && strchr(fixedTypes, c) != NULL;
254 }
unsigned char c[8]
Definition: qnumeric_p.h:62
static const char fixedTypes[]
Definition: qdbusutil.cpp:244

◆ isValidCharacter()

static bool isValidCharacter ( const QChar c)
inlinestatic

Definition at line 64 of file qdbusutil.cpp.

Referenced by QDBusUtil::isValidBusName(), and QDBusUtil::isValidUniqueConnectionName().

65 {
66  register ushort u = c.unicode();
67  return (u >= 'a' && u <= 'z')
68  || (u >= 'A' && u <= 'Z')
69  || (u >= '0' && u <= '9')
70  || (u == '_') || (u == '-');
71 }
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
quint16 u
unsigned short ushort
Definition: qglobal.h:995

◆ isValidCharacterNoDash()

static bool isValidCharacterNoDash ( const QChar c)
inlinestatic

Definition at line 55 of file qdbusutil.cpp.

Referenced by QDBusUtil::isValidMemberName(), and QDBusUtil::isValidPartOfObjectPath().

56 {
57  register ushort u = c.unicode();
58  return (u >= 'a' && u <= 'z')
59  || (u >= 'A' && u <= 'Z')
60  || (u >= '0' && u <= '9')
61  || (u == '_');
62 }
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
quint16 u
unsigned short ushort
Definition: qglobal.h:995

◆ isValidNumber()

static bool isValidNumber ( const QChar c)
inlinestatic

Definition at line 73 of file qdbusutil.cpp.

Referenced by QDBusUtil::isValidBusName(), and QDBusUtil::isValidMemberName().

74 {
75  register ushort u = c.unicode();
76  return (u >= '0' && u <= '9');
77 }
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
quint16 u
unsigned short ushort
Definition: qglobal.h:995

◆ validateSingleType()

static const char* validateSingleType ( const char *  signature)
static

Definition at line 258 of file qdbusutil.cpp.

Referenced by QDBusUtil::isValidSignature(), and QDBusUtil::isValidSingleSignature().

259 {
260  register char c = *signature;
261  if (c == DBUS_TYPE_INVALID)
262  return 0;
263 
264  // is it one of the one-letter types?
265  if (strchr(oneLetterTypes, c) != NULL)
266  return signature + 1;
267 
268  // is it an array?
269  if (c == DBUS_TYPE_ARRAY) {
270  // then it's valid if the next type is valid
271  // or if it's a dict-entry
272  c = *++signature;
273  if (c == DBUS_DICT_ENTRY_BEGIN_CHAR) {
274  // beginning of a dictionary entry
275  // a dictionary entry has a key which is of basic types
276  // and a free value
277  c = *++signature;
278  if (!isBasicType(c))
279  return 0;
280  signature = validateSingleType(signature + 1);
281  return signature && *signature == DBUS_DICT_ENTRY_END_CHAR ? signature + 1 : 0;
282  }
283 
284  return validateSingleType(signature);
285  }
286 
287  if (c == DBUS_STRUCT_BEGIN_CHAR) {
288  // beginning of a struct
289  ++signature;
290  while (true) {
291  signature = validateSingleType(signature);
292  if (!signature)
293  return 0;
294  if (*signature == DBUS_STRUCT_END_CHAR)
295  return signature + 1;
296  }
297  }
298 
299  // invalid/unknown type
300  return 0;
301 }
unsigned char c[8]
Definition: qnumeric_p.h:62
static const char * validateSingleType(const char *signature)
Definition: qdbusutil.cpp:258
static bool isBasicType(int c)
Definition: qdbusutil.cpp:246
static const char oneLetterTypes[]
Definition: qdbusutil.cpp:242

◆ variantToString()

static bool variantToString ( const QVariant arg,
QString out 
)
static

Definition at line 81 of file qdbusutil.cpp.

Referenced by argToString(), and QDBusUtil::argumentToString().

82 {
83  int argType = arg.userType();
84 
85  if (argType == QVariant::StringList) {
86  out += QLatin1Char('{');
87  QStringList list = arg.toStringList();
88  foreach (QString item, list)
89  out += QLatin1Char('\"') + item + QLatin1String("\", ");
90  if (!list.isEmpty())
91  out.chop(2);
92  out += QLatin1Char('}');
93  } else if (argType == QVariant::ByteArray) {
94  out += QLatin1Char('{');
95  QByteArray list = arg.toByteArray();
96  for (int i = 0; i < list.count(); ++i) {
97  out += QString::number(list.at(i));
98  out += QLatin1String(", ");
99  }
100  if (!list.isEmpty())
101  out.chop(2);
102  out += QLatin1Char('}');
103  } else if (argType == QVariant::List) {
104  out += QLatin1Char('{');
105  QList<QVariant> list = arg.toList();
106  foreach (QVariant item, list) {
107  if (!variantToString(item, out))
108  return false;
109  out += QLatin1String(", ");
110  }
111  if (!list.isEmpty())
112  out.chop(2);
113  out += QLatin1Char('}');
114  } else if (argType == QMetaType::Char || argType == QMetaType::Short || argType == QMetaType::Int
115  || argType == QMetaType::Long || argType == QMetaType::LongLong) {
116  out += QString::number(arg.toLongLong());
117  } else if (argType == QMetaType::UChar || argType == QMetaType::UShort || argType == QMetaType::UInt
118  || argType == QMetaType::ULong || argType == QMetaType::ULongLong) {
119  out += QString::number(arg.toULongLong());
120  } else if (argType == QMetaType::Double) {
121  out += QString::number(arg.toDouble());
122  } else if (argType == QMetaType::Bool) {
123  out += QLatin1String(arg.toBool() ? "true" : "false");
124  } else if (argType == qMetaTypeId<QDBusArgument>()) {
125  argToString(qvariant_cast<QDBusArgument>(arg), out);
126  } else if (argType == qMetaTypeId<QDBusObjectPath>()) {
127  const QString path = qvariant_cast<QDBusObjectPath>(arg).path();
128  out += QLatin1String("[ObjectPath: ");
129  out += path;
130  out += QLatin1Char(']');
131  } else if (argType == qMetaTypeId<QDBusSignature>()) {
132  out += QLatin1String("[Signature: ") + qvariant_cast<QDBusSignature>(arg).signature();
133  out += QLatin1Char(']');
134  } else if (argType == qMetaTypeId<QDBusUnixFileDescriptor>()) {
135  out += QLatin1String("[Unix FD: ");
136  out += QLatin1String(qvariant_cast<QDBusUnixFileDescriptor>(arg).isValid() ? "valid" : "not valid");
137  out += QLatin1Char(']');
138  } else if (argType == qMetaTypeId<QDBusVariant>()) {
139  const QVariant v = qvariant_cast<QDBusVariant>(arg).variant();
140  out += QLatin1String("[Variant");
141  int vUserType = v.userType();
142  if (vUserType != qMetaTypeId<QDBusVariant>()
143  && vUserType != qMetaTypeId<QDBusSignature>()
144  && vUserType != qMetaTypeId<QDBusObjectPath>()
145  && vUserType != qMetaTypeId<QDBusArgument>())
146  out += QLatin1Char('(') + QLatin1String(v.typeName()) + QLatin1Char(')');
147  out += QLatin1String(": ");
148  if (!variantToString(v, out))
149  return false;
150  out += QLatin1Char(']');
151  } else if (arg.canConvert(QVariant::String)) {
152  out += QLatin1Char('\"') + arg.toString() + QLatin1Char('\"');
153  } else {
154  out += QLatin1Char('[');
155  out += QLatin1String(arg.typeName());
156  out += QLatin1Char(']');
157  }
158 
159  return true;
160 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
static bool argToString(const QDBusArgument &arg, QString &out)
Definition: qdbusutil.cpp:162
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void chop(int n)
Removes n characters from the end of the string.
Definition: qstring.cpp:4623
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
The QDBusSignature class enables the programmer to identify the SIGNATURE type provided by the D-Bus ...
QList< QVariant > toList() const
Returns the variant as a QVariantList if the variant has type() List or StringList ; otherwise return...
Definition: qvariant.cpp:2751
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool toBool() const
Returns the variant as a bool if the variant has type() Bool.
Definition: qvariant.cpp:2691
The QString class provides a Unicode character string.
Definition: qstring.h:83
QByteArray toByteArray() const
Returns the variant as a QByteArray if the variant has type() ByteArray or String (converted using QS...
Definition: qvariant.cpp:2383
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
QStringList toStringList() const
Returns the variant as a QStringList if the variant has type() StringList, String ...
Definition: qvariant.cpp:2259
qlonglong toLongLong(bool *ok=0) const
Returns the variant as a long long int if the variant has type() LongLong , Bool , ByteArray , Char , Double , Int , String , UInt , or ULongLong ; otherwise returns 0.
Definition: qvariant.cpp:2659
bool canConvert(Type t) const
Returns true if the variant&#39;s type can be cast to the requested type, t.
Definition: qvariant.cpp:2886
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
qulonglong toULongLong(bool *ok=0) const
Returns the variant as as an unsigned long long int if the variant has type() ULongLong ...
Definition: qvariant.cpp:2675
static bool variantToString(const QVariant &arg, QString &out)
Definition: qdbusutil.cpp:81
const char * typeName() const
Returns the name of the type stored in the variant.
Definition: qvariant.cpp:1984
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
int count(char c) const
Returns the number of occurrences of character ch in the byte array.
The QDBusObjectPath class enables the programmer to identify the OBJECT_PATH type provided by the D-B...
double toDouble(bool *ok=0) const
Returns the variant as a double if the variant has type() Double , QMetaType::Float ...
Definition: qvariant.cpp:2710
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
const char * variant
char at(int i) const
Returns the character at index position i in the byte array.
Definition: qbytearray.h:413
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
The QDBusVariant class enables the programmer to identify the variant type provided by the D-Bus type...
T qvariant_cast(const QVariant &value)
Returns the given value converted to the template type T.
Definition: qvariant.h:571

Variable Documentation

◆ basicTypes

const char basicTypes[] = "sogybnqiuxtdh"
static

Definition at line 243 of file qdbusutil.cpp.

Referenced by isBasicType().

◆ fixedTypes

const char fixedTypes[] = "ybnqiuxtdh"
static

Definition at line 244 of file qdbusutil.cpp.

Referenced by isFixedType().

◆ oneLetterTypes

const char oneLetterTypes[] = "vsogybnqiuxtdh"
static

Definition at line 242 of file qdbusutil.cpp.

Referenced by validateSingleType().