qdbus.cpp
来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 446 行 · 第 1/2 页
CPP
446 行
/******************************************************************************** Copyright (C) 2006-2007 Trolltech ASA. All rights reserved.**** This file is part of the tools applications of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include <stdio.h>#include <stdlib.h>#include <QtCore/QCoreApplication>#include <QtCore/QStringList>#include <QtCore/qmetaobject.h>#include <QtXml/QDomDocument>#include <QtXml/QDomElement>#include <QtDBus/QtDBus>#include <private/qdbusutil_p.h>static QDBusConnection connection(QLatin1String(""));static void printArg(const QVariant &v){ if (v.userType() == QVariant::StringList) { foreach (QString s, v.toStringList()) printf("%s\n", qPrintable(s)); } else if (v.userType() == QVariant::List) { foreach (const QVariant &var, v.toList()) printArg(var); } else if (v.userType() == QVariant::Map) { const QVariantMap map = v.toMap(); QVariantMap::ConstIterator it = map.constBegin(); for ( ; it != map.constEnd(); ++it) { printf("%s: ", qPrintable(it.key())); printArg(it.value()); } } else if (v.userType() == qMetaTypeId<QDBusVariant>()) { printArg(qvariant_cast<QDBusVariant>(v).variant()); } else if (v.userType() == qMetaTypeId<QDBusArgument>()) { QDBusArgument arg = qvariant_cast<QDBusArgument>(v); if (arg.currentSignature() == QLatin1String("av")) printArg(qdbus_cast<QVariantList>(arg)); else if (arg.currentSignature() == QLatin1String("a{sv}")) printArg(qdbus_cast<QVariantMap>(arg)); else printf("qdbus: I don't know how to display an argument of type '%s'", qPrintable(arg.currentSignature())); } else { printf("%s\n", qPrintable(v.toString())); }}static void listObjects(const QString &service, const QString &path){ QDBusInterface iface(service, path.isEmpty() ? QLatin1String("/") : path, QLatin1String("org.freedesktop.DBus.Introspectable"), connection); if (!iface.isValid()) { QDBusError err(iface.lastError()); fprintf(stderr, "Cannot introspect object %s at %s:\n%s (%s)\n", qPrintable(path.isEmpty() ? QString(QLatin1String("/")) : path), qPrintable(service), qPrintable(err.name()), qPrintable(err.message())); exit(1); } QDBusReply<QString> xml = iface.call(QLatin1String("Introspect")); if (!xml.isValid()) return; // silently QDomDocument doc; doc.setContent(xml); QDomElement node = doc.documentElement(); QDomElement child = node.firstChildElement(); while (!child.isNull()) { if (child.tagName() == QLatin1String("node")) { QString sub = path + QLatin1Char('/') + child.attribute(QLatin1String("name")); printf("%s\n", qPrintable(sub)); listObjects(service, sub); } child = child.nextSiblingElement(); }}static void listInterface(const QString &service, const QString &path, const QString &interface){ QDBusInterface iface(service, path, interface, connection); 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 for (int i = mo->propertyOffset(); i < mo->propertyCount(); ++i) { QMetaProperty mp = mo->property(i); printf("property "); if (mp.isReadable() && mp.isWritable()) printf("readwrite"); else if (mp.isReadable()) printf("read"); else printf("write"); printf(" %s %s.%s\n", mp.typeName(), qPrintable(interface), mp.name()); } // methods (signals and slots) for (int i = mo->methodOffset(); i < mo->methodCount(); ++i) { QMetaMethod mm = mo->method(i); QByteArray signature = mm.signature(); signature.truncate(signature.indexOf('(')); printf("%s %s%s%s %s.%s(", mm.methodType() == QMetaMethod::Signal ? "signal" : "method", mm.tag(), *mm.tag() ? " " : "", *mm.typeName() ? mm.typeName() : "void", qPrintable(interface), signature.constData()); QList<QByteArray> types = mm.parameterTypes(); QList<QByteArray> names = mm.parameterNames(); bool first = true; for (int i = 0; i < types.count(); ++i) { printf("%s%s", first ? "" : ", ", types.at(i).constData()); if (!names.at(i).isEmpty()) printf(" %s", names.at(i).constData()); first = false; } printf(")\n"); }}static void listAllInterfaces(const QString &service, const QString &path){ QDBusInterface iface(service, path, QLatin1String("org.freedesktop.DBus.Introspectable"), connection); 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(QLatin1String("Introspect")); if (!xml.isValid()) return; // silently QDomDocument doc; doc.setContent(xml); QDomElement node = doc.documentElement(); QDomElement child = node.firstChildElement(); while (!child.isNull()) { if (child.tagName() == QLatin1String("interface")) { QString ifaceName = child.attribute(QLatin1String("name")); if (QDBusUtil::isValidInterfaceName(ifaceName)) listInterface(service, path, ifaceName); else { qWarning("Invalid D-BUS interface name '%s' found while parsing introspection", qPrintable(ifaceName)); } } child = child.nextSiblingElement(); }}static QStringList readList(QStringList &args){ args.takeFirst(); QStringList retval; while (!args.isEmpty() && args.at(0) != QLatin1String(")")) retval += args.takeFirst(); if (args.value(0) == QLatin1String(")")) args.takeFirst(); return retval;}static void placeCall(const QString &service, const QString &path, const QString &interface, const QString &member, QStringList args){ QDBusInterface iface(service, path, interface, connection); 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); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?