diff options
| author | Thiago Macieira <thiago@kde.org> | 2006-04-29 12:49:21 +0000 | 
|---|---|---|
| committer | Thiago Macieira <thiago@kde.org> | 2006-04-29 12:49:21 +0000 | 
| commit | 3ad7ee6334a8b69794e64386657976a4df094c57 (patch) | |
| tree | e1bead28446f8d58b9d6688a6df54d53dc70e55e /qt/examples | |
| parent | 7cabddaa4237b7e0ce1c6b9781d115b9831e77f2 (diff) | |
        * qt/examples/dbus.cpp: Enhance error messages and use
        QDBusInterfacePtr.
Diffstat (limited to 'qt/examples')
| -rw-r--r-- | qt/examples/dbus.cpp | 63 | 
1 files changed, 41 insertions, 22 deletions
diff --git a/qt/examples/dbus.cpp b/qt/examples/dbus.cpp index 0227c477..32fcc3fa 100644 --- a/qt/examples/dbus.cpp +++ b/qt/examples/dbus.cpp @@ -33,8 +33,15 @@ QDBusConnection *connection;  void listObjects(const QString &service, const QString &path)  { -    QDBusInterface *iface = connection->findInterface(service, path.isEmpty() ? "/" : path, -                                                     "org.freedesktop.DBus.Introspectable"); +    QDBusInterfacePtr iface(*connection, service, path.isEmpty() ? "/" : path, +                   "org.freedesktop.DBus.Introspectable"); +    if (!iface->isValid()) { +        QDBusError err(iface->lastError()); +        fprintf(stderr, "Cannot introspect object %s at %s:\n%s (%s)\n", +                qPrintable(path.isEmpty() ? "/" : path), qPrintable(service), +                qPrintable(err.name()), qPrintable(err.message())); +        exit(1); +    }      QDBusReply<QString> xml = iface->call("Introspect");      if (xml.isError()) @@ -52,13 +59,18 @@ void listObjects(const QString &service, const QString &path)          }          child = child.nextSiblingElement();      } - -    delete iface;  }  void listInterface(const QString &service, const QString &path, const QString &interface)  { -    QDBusInterface *iface = connection->findInterface(service, path, interface); +    QDBusInterfacePtr iface(*connection, service, path, interface); +    if (!iface->isValid()) { +        QDBusError err(iface->lastError()); +        fprintf(stderr, "Interface '%s' not available in object %s at %s:\n%s (%s)\n", +                qPrintable(interface), qPrintable(path), qPrintable(service), +                qPrintable(err.name()), qPrintable(err.message())); +        exit(1); +    }      const QMetaObject *mo = iface->metaObject();      // properties @@ -101,13 +113,18 @@ void listInterface(const QString &service, const QString &path, const QString &i          }          printf(")\n");      } -    delete iface;  }  void listAllInterfaces(const QString &service, const QString &path)  { -    QDBusInterface *iface = connection->findInterface(service, path, -                                                     "org.freedesktop.DBus.Introspectable"); +    QDBusInterfacePtr iface(*connection, service, path, "org.freedesktop.DBus.Introspectable"); +    if (!iface->isValid()) { +        QDBusError err(iface->lastError()); +        fprintf(stderr, "Cannot introspect object %s at %s:\n%s (%s)\n", +                qPrintable(path), qPrintable(service), +                qPrintable(err.name()), qPrintable(err.message())); +        exit(1); +    }      QDBusReply<QString> xml = iface->call("Introspect");      if (xml.isError()) @@ -123,8 +140,6 @@ void listAllInterfaces(const QString &service, const QString &path)          }          child = child.nextSiblingElement();      } - -    delete iface;  }  QStringList readList(int &argc, const char *const *&argv) @@ -142,12 +157,12 @@ QStringList readList(int &argc, const char *const *&argv)  void placeCall(const QString &service, const QString &path, const QString &interface,                 const QString &member, int argc, const char *const *argv)  { -    QDBusInterface *iface; -    iface = connection->findInterface(service, path, interface); - -    if (!iface) { -        fprintf(stderr, "Interface '%s' not available in object %s at %s\n", -                qPrintable(interface), qPrintable(path), qPrintable(service)); +    QDBusInterfacePtr iface(*connection, service, path, interface); +    if (!iface->isValid()) { +        QDBusError err(iface->lastError()); +        fprintf(stderr, "Interface '%s' not available in object %s at %s:\n%s (%s)\n", +                qPrintable(interface), qPrintable(path), qPrintable(service), +                qPrintable(err.name()), qPrintable(err.message()));          exit(1);      } @@ -155,7 +170,7 @@ void placeCall(const QString &service, const QString &path, const QString &inter      QByteArray match = member.toLatin1();      match += '('; -    int midx; +    int midx = -1;      for (int i = mo->methodOffset(); i < mo->methodCount(); ++i) {          QMetaMethod mm = mo->method(i);          QByteArray signature = mm.signature(); @@ -172,7 +187,7 @@ void placeCall(const QString &service, const QString &path, const QString &inter          exit(1);      } -    QMetaMethod mm = iface->metaObject()->method(midx); +    QMetaMethod mm = mo->method(midx);      QList<QByteArray> types = mm.parameterTypes();      QVariantList params; @@ -221,12 +236,16 @@ void placeCall(const QString &service, const QString &path, const QString &inter      }      foreach (QVariant v, reply) { -        if (v.userType() == qMetaTypeId<QVariant>()) -            v = qvariant_cast<QVariant>(v); -        printf("%s\n", qPrintable(v.toString())); +        if (v.userType() == QVariant::StringList) { +            foreach (QString s, v.toStringList()) +                printf("%s\n", qPrintable(s)); +        } else { +            if (v.userType() == qMetaTypeId<QVariant>()) +                v = qvariant_cast<QVariant>(v); +            printf("%s\n", qPrintable(v.toString())); +        }      } -    delete iface;      exit(0);  }  | 
