Qt 4.8
Classes | Functions
main.cpp File Reference
#include "main.h"
#include <QDebug>
#include <QMetaMethod>
#include <QScriptExtensionPlugin>

Go to the source code of this file.

Classes

class  QtDBusScriptPlugin
 

Functions

static QScriptValue do_dbus_call (QScriptContext *context, QScriptEngine *engine)
 
static QScriptValue messageToScriptValue (QScriptEngine *engine, const QDBusMessage &message)
 
QScriptValue qDBusErrorToScriptValue (QScriptEngine *engine, const QDBusError &error)
 
template<typename T >
void qDBusReplyFromScriptValue (const QScriptValue &, QDBusReply< T > &)
 
template<typename T >
QScriptValue qDBusReplyToScriptValue (QScriptEngine *eng, const QDBusReply< T > &reply)
 
template<>
QScriptValue qDBusReplyToScriptValue (QScriptEngine *eng, const QDBusReply< QStringList > &reply)
 
static void scriptValueToMessage (const QScriptValue &value, QDBusMessage &message)
 
void scriptValueToQDBusError (const QScriptValue &value, QDBusError &error)
 
static QScriptValue setupDBusInterface (QScriptEngine *engine, QDBusAbstractInterface *iface)
 

Function Documentation

◆ do_dbus_call()

static QScriptValue do_dbus_call ( QScriptContext context,
QScriptEngine engine 
)
static

Definition at line 53 of file main.cpp.

Referenced by setupDBusInterface().

54 {
55  int firstArgument = 0;
56  QString functionName = context->callee().property(QLatin1String("functionName")).toString();
57  if (functionName.isEmpty()) {
58  functionName = context->argument(0).toString();
59  ++firstArgument;
60  }
61 
62  QScriptValue thisObject = context->thisObject();
64  if (!iface)
65  return QScriptValue();
66 
68  iface->path(),
69  iface->interface(),
70  functionName);
71 
72  QList<QVariant> args;
73  for (int i = firstArgument; i < context->argumentCount(); ++i) {
74  args.append(context->argument(i).toVariant());
75  }
76  msg.setArguments(args);
77 
78  msg = iface->connection().call(msg);
79 
80  QScriptValue returnValue = engine->nullValue();
81  args = msg.arguments();
82  if (args.count() != 1)
83  return returnValue;
84 
85  QVariant variant = args.first();
86  if (variant.type() == QVariant::UserType
87  && variant.userType() == qMetaTypeId<QDBusObjectPath>()) {
89 
90  QDBusInterface *returnedIface = new QDBusInterface(iface->service(),
91  path.path(),
92  /*interface*/QString(),
93  iface->connection(),
94  engine);
95  returnValue = setupDBusInterface(engine, returnedIface);
96  } else {
97  returnValue = engine->newVariant(variant);
98  }
99 
100  return returnValue;
101 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QScriptValue property(const QString &name, const ResolveFlags &mode=ResolvePrototype) const
Returns the value of this QScriptValue&#39;s property with the given name, using the given mode to resolv...
QString path() const
Returns the object path that this interface is associated with.
QScriptValue callee() const
Returns the callee.
QDBusConnection connection() const
Returns the connection this interface is assocated with.
QString toString() const
Returns the string value of this QScriptValue, as defined in ECMA-262 section 9.8, "ToString".
QDBusMessage call(const QDBusMessage &message, QDBus::CallMode mode=QDBus::Block, int timeout=-1) const
Sends the message over this connection and blocks, waiting for a reply, for at most timeout milliseco...
The QDBusInterface class is a proxy for interfaces on remote objects.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QObject * toQObject() const
If this QScriptValue is a QObject, returns the QObject pointer that the QScriptValue represents; othe...
QString interface() const
Returns the name of this interface.
The QString class provides a Unicode character string.
Definition: qstring.h:83
T * qobject_cast(QObject *object)
Definition: qobject.h:375
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
The QDBusAbstractInterface class is the base class for all D-Bus interfaces in the QtDBus binding...
QList< QVariant > arguments() const
Returns the list of arguments that are going to be sent or were received from D-Bus.
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QVariant toVariant() const
Returns the QVariant value of this QScriptValue, if it can be converted to a QVariant; otherwise retu...
QString path() const
Returns this object path.
int argumentCount() const
Returns the number of arguments passed to the function in this invocation.
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
static QDBusMessage createMethodCall(const QString &destination, const QString &path, const QString &interface, const QString &method)
Constructs a new DBus message representing a method call.
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
The QDBusObjectPath class enables the programmer to identify the OBJECT_PATH type provided by the D-B...
QString service() const
Returns the name of the service this interface is associated with.
The QDBusMessage class represents one message sent or received over the D-Bus bus.
Definition: qdbusmessage.h:59
void setArguments(const QList< QVariant > &arguments)
Sets the arguments that are going to be sent over D-Bus to arguments.
static QScriptValue setupDBusInterface(QScriptEngine *engine, QDBusAbstractInterface *iface)
Definition: main.cpp:103
const char * variant
QScriptValue thisObject() const
Returns the `this&#39; object associated with this QScriptContext.
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
QScriptValue argument(int index) const
Returns the function argument at the given index.
QScriptValue newVariant(const QVariant &value)
Creates a QtScript object holding the given variant value.
QScriptValue nullValue()
Returns a QScriptValue of the primitive type Null.
T qvariant_cast(const QVariant &value)
Returns the given value converted to the template type T.
Definition: qvariant.h:571

◆ messageToScriptValue()

static QScriptValue messageToScriptValue ( QScriptEngine engine,
const QDBusMessage message 
)
static

Definition at line 241 of file main.cpp.

Referenced by QScriptDBusMessageConstructor::createErrorReply(), QScriptDBusMessageConstructor::createReply(), and QtDBusScriptPlugin::initialize().

242 {
243  QScriptValue v = engine->newVariant(QVariant::fromValue(message));
244  v.setProperty(QLatin1String("service"), QScriptValue(engine, message.service()), QScriptValue::ReadOnly);
245  v.setProperty(QLatin1String("path"), QScriptValue(engine, message.path()), QScriptValue::ReadOnly);
246  v.setProperty(QLatin1String("interface"), QScriptValue(engine, message.interface()), QScriptValue::ReadOnly);
247  v.setProperty(QLatin1String("member"), QScriptValue(engine, message.member()), QScriptValue::ReadOnly);
248  v.setProperty(QLatin1String("type"), QScriptValue(engine, message.type()), QScriptValue::ReadOnly);
249  v.setProperty(QLatin1String("signature"), QScriptValue(engine, message.signature()), QScriptValue::ReadOnly);
250  v.setProperty(QLatin1String("isReplyRequired"), QScriptValue(engine, message.isReplyRequired()), QScriptValue::ReadOnly);
251 
252  v.setProperty(QLatin1String("delayedReply"), QScriptValue(engine, message.isDelayedReply()));
253  QScriptValue argValue = engine->newArray();
254  const QList<QVariant> args = message.arguments();
255  for (int i = 0; i < args.count(); ++i)
256  argValue.setProperty(QScriptValue(engine, i).toString(),
257  engine->newVariant(args.at(i)));
258 
259  v.setProperty(QLatin1String("arguments"), argValue);
260 
261  return v;
262 }
QString service() const
Returns the name of the service or the bus address of the remote method call.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
QString signature() const
Returns the signature of the signal that was received or for the output arguments of a method call...
static QString toString(Register *reg, int type, bool *ok=0)
QList< QVariant > arguments() const
Returns the list of arguments that are going to be sent or were received from D-Bus.
QString path() const
Returns the path of the object that this message is being sent to (in the case of a method call) or b...
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
bool isReplyRequired() const
Returns the flag that indicates if this message should see a reply or not.
QString member() const
Returns the name of the signal that was emitted or the name of the method that was called...
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
bool isDelayedReply() const
Returns the delayed reply flag, as set by setDelayedReply().
QString interface() const
Returns the interface of the method being called (in the case of a method call) or of the signal bein...
void setProperty(const QString &name, const QScriptValue &value, const PropertyFlags &flags=KeepExistingFlags)
Sets the value of this QScriptValue&#39;s property with the given name to the given value.
MessageType type() const
Returns the message type.
QScriptValue newArray(uint length=0)
Creates a QtScript object of class Array with the given length.
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
QScriptValue newVariant(const QVariant &value)
Creates a QtScript object holding the given variant value.

◆ qDBusErrorToScriptValue()

QScriptValue qDBusErrorToScriptValue ( QScriptEngine engine,
const QDBusError error 
)

Definition at line 328 of file main.cpp.

Referenced by QtDBusScriptPlugin::initialize().

329 {
330  QScriptValue v = engine->newObject();
331  v.setProperty(QLatin1String("type"), QScriptValue(engine, error.type()), QScriptValue::ReadOnly);
332  v.setProperty(QLatin1String("name"), QScriptValue(engine, error.name()), QScriptValue::ReadOnly);
333  v.setProperty(QLatin1String("message"), QScriptValue(engine, error.message()), QScriptValue::ReadOnly);
334  v.setProperty(QLatin1String("isValid"), QScriptValue(engine, error.isValid()), QScriptValue::ReadOnly);
335  return v;
336 }
bool isValid() const
Returns true if this is a valid error condition (i.e., if there was an error), otherwise false...
Definition: qdbuserror.cpp:356
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
ErrorType type() const
Returns this error&#39;s ErrorType.
Definition: qdbuserror.cpp:323
QScriptValue newObject()
Creates a QtScript object of class Object.
QString name() const
Returns this error&#39;s name.
Definition: qdbuserror.cpp:335
void setProperty(const QString &name, const QScriptValue &value, const PropertyFlags &flags=KeepExistingFlags)
Sets the value of this QScriptValue&#39;s property with the given name to the given value.
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
QString message() const
Returns the message that the callee associated with this error.
Definition: qdbuserror.cpp:346

◆ qDBusReplyFromScriptValue()

template<typename T >
void qDBusReplyFromScriptValue ( const QScriptValue ,
QDBusReply< T > &   
)

Definition at line 323 of file main.cpp.

Referenced by QtDBusScriptPlugin::initialize().

324 {
325  // never called
326 }

◆ qDBusReplyToScriptValue() [1/2]

template<typename T >
QScriptValue qDBusReplyToScriptValue ( QScriptEngine eng,
const QDBusReply< T > &  reply 
)

Definition at line 307 of file main.cpp.

Referenced by QtDBusScriptPlugin::initialize().

308 {
309  return QScriptValue(eng, reply.value());
310 }
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
Type value() const
Returns the remote function&#39;s calls return value.
Definition: qdbusreply.h:118

◆ qDBusReplyToScriptValue() [2/2]

template<>
QScriptValue qDBusReplyToScriptValue ( QScriptEngine eng,
const QDBusReply< QStringList > &  reply 
)

Definition at line 313 of file main.cpp.

314 {
315  QScriptValue v = eng->newArray();
316  const QStringList &lst = reply.value();
317  for (int i = 0; i < lst.count(); ++i)
318  v.setProperty(i, QScriptValue(eng, lst.at(i)));
319  return v;
320 }
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
void setProperty(const QString &name, const QScriptValue &value, const PropertyFlags &flags=KeepExistingFlags)
Sets the value of this QScriptValue&#39;s property with the given name to the given value.
QScriptValue newArray(uint length=0)
Creates a QtScript object of class Array with the given length.
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
Type value() const
Returns the remote function&#39;s calls return value.
Definition: qdbusreply.h:118

◆ scriptValueToMessage()

static void scriptValueToMessage ( const QScriptValue value,
QDBusMessage message 
)
static

Definition at line 264 of file main.cpp.

Referenced by QScriptDBusMessageConstructor::createErrorReply(), QScriptDBusMessageConstructor::createReply(), and QtDBusScriptPlugin::initialize().

265 {
266  QVariant v = value.toVariant();
267  message = qvariant_cast<QDBusMessage>(v);
268  message.setDelayedReply(value.property(QLatin1String("delayedReply")).toBoolean());
269 
270  QList<QVariant> args;
271  quint32 len = value.property(QLatin1String("length")).toUInt32();
272  for (quint32 i = 0; i < len; ++i) {
273  QScriptValue item = value.property(i);
274  args.append(item.toVariant());
275  }
276  message.setArguments(args);
277 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QScriptValue property(const QString &name, const ResolveFlags &mode=ResolvePrototype) const
Returns the value of this QScriptValue&#39;s property with the given name, using the given mode to resolv...
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void setDelayedReply(bool enable) const
Sets whether the message will be replied later (if enable is true) or if an automatic reply should be...
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
QVariant toVariant() const
Returns the QVariant value of this QScriptValue, if it can be converted to a QVariant; otherwise retu...
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
quint32 toUInt32() const
Returns the unsigned 32-bit integer value of this QScriptValue, using the conversion rules described ...
bool toBoolean() const
Use toBool() instead.
unsigned int quint32
Definition: qglobal.h:938
The QDBusMessage class represents one message sent or received over the D-Bus bus.
Definition: qdbusmessage.h:59
void setArguments(const QList< QVariant > &arguments)
Sets the arguments that are going to be sent over D-Bus to arguments.
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
T qvariant_cast(const QVariant &value)
Returns the given value converted to the template type T.
Definition: qvariant.h:571

◆ scriptValueToQDBusError()

void scriptValueToQDBusError ( const QScriptValue value,
QDBusError error 
)

Definition at line 338 of file main.cpp.

Referenced by QtDBusScriptPlugin::initialize().

339 {
340  Q_UNUSED(value)
341  Q_UNUSED(error)
342  // never called
343 }
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ setupDBusInterface()

static QScriptValue setupDBusInterface ( QScriptEngine engine,
QDBusAbstractInterface iface 
)
static

Definition at line 103 of file main.cpp.

Referenced by do_dbus_call(), QScriptDBusInterfaceConstructor::qscript_call(), and QScriptDBusConnection::QScriptDBusConnection().

104 {
105  QScriptValue v = engine->newQObject(iface);
106 
107  if (!qobject_cast<QDBusConnectionInterface *>(iface)) {
108  const QMetaObject *mo = iface->metaObject();
109  for (int i = 0; i < mo->methodCount(); ++i) {
110  const QMetaMethod method = mo->method(i);
111  const QByteArray signature = method.signature();
112  //qDebug() << "signature" << signature;
113  int parenIndex = signature.indexOf('(');
114  if (parenIndex == -1)
115  continue;
116  const QByteArray name = signature.left(parenIndex);
117  if (name.isEmpty())
118  continue;
119 
120  // don't try to override properties
121  if (mo->indexOfProperty(name) != -1)
122  continue;
123 
124  QScriptValue callWrapper = engine->newFunction(do_dbus_call);
125  const QString nameString = QString::fromAscii(name);
126  callWrapper.setProperty(QLatin1String("functionName"), QScriptValue(engine, nameString));
127  v.setProperty(nameString, callWrapper);
128  }
129  }
130 
131  v.setProperty(QLatin1String("service"), QScriptValue(engine, iface->service()), QScriptValue::ReadOnly);
132  v.setProperty(QLatin1String("path"), QScriptValue(engine, iface->path()), QScriptValue::ReadOnly);
133  v.setProperty(QLatin1String("interface"), QScriptValue(engine, iface->interface()), QScriptValue::ReadOnly);
134  v.setProperty(QLatin1String("isValid"), QScriptValue(engine, iface->isValid()), QScriptValue::ReadOnly);
135  v.setProperty(QLatin1String("connection"), engine->newQObject(new QScriptDBusConnection(iface->connection(), engine)), QScriptValue::ReadOnly);
136 
137  return v;
138 }
QScriptValue newFunction(FunctionSignature signature, int length=0)
Creates a QScriptValue that wraps a native (C++) function.
The QMetaObject class contains meta-information about Qt objects.
Definition: qobjectdefs.h:304
QString path() const
Returns the object path that this interface is associated with.
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
bool isValid() const
Returns true if this is a valid reference to a remote object.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QDBusConnection connection() const
Returns the connection this interface is assocated with.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QString interface() const
Returns the name of this interface.
The QString class provides a Unicode character string.
Definition: qstring.h:83
int indexOfProperty(const char *name) const
Finds property name and returns its index; otherwise returns -1.
const char * name
QByteArray left(int len) const
Returns a byte array that contains the leftmost len bytes of this byte array.
int indexOf(char c, int from=0) const
Returns the index position of the first occurrence of the character ch in the byte array...
QScriptValue newQObject(QObject *object, ValueOwnership ownership=QtOwnership, const QObjectWrapOptions &options=0)
Creates a QtScript object that wraps the given QObject object, using the given ownership.
void setProperty(const QString &name, const QScriptValue &value, const PropertyFlags &flags=KeepExistingFlags)
Sets the value of this QScriptValue&#39;s property with the given name to the given value.
QString service() const
Returns the name of the service this interface is associated with.
QMetaMethod method(int index) const
Returns the meta-data for the method with the given index.
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
const char * signature() const
Returns the signature of this method (e.g., setValue(double)).
static QScriptValue do_dbus_call(QScriptContext *context, QScriptEngine *engine)
Definition: main.cpp:53
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
int methodCount() const
Returns the number of methods known to the meta-object system in this class, including the number of ...
The QMetaMethod class provides meta-data about a member function.
Definition: qmetaobject.h:56
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.