⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qdbusmessage.cpp

📁 QT 开发环境里面一个很重要的文件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************** Copyright (C) 1992-2006 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://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** 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 "qdbusmessage.h"#include <qdebug.h>#include <qstringlist.h>#include <dbus/dbus.h>#include "qdbusargument_p.h"#include "qdbuserror.h"#include "qdbusmessage_p.h"#include "qdbusmetatype.h"static inline const char *data(const QByteArray &arr){    return arr.isEmpty() ? 0 : arr.constData();}QDBusMessagePrivate::QDBusMessagePrivate()    : msg(0), reply(0), type(DBUS_MESSAGE_TYPE_INVALID),      timeout(-1), ref(1), delayedReply(false), localMessage(false){}QDBusMessagePrivate::~QDBusMessagePrivate(){    if (msg)        dbus_message_unref(msg);    if (reply)        dbus_message_unref(reply);}/*!    \internal    Creates a QDBusMessage that represents the same error as the QDBusError object.*/QDBusMessage QDBusMessagePrivate::fromError(const QDBusError &error){    QDBusMessage message;    message.d_ptr->type = DBUS_MESSAGE_TYPE_ERROR;    message.d_ptr->name = error.name();    message << error.message();    return message;}/*!    \internal    Constructs a DBusMessage object from this object. The returned value must be de-referenced    with dbus_message_unref.*/DBusMessage *QDBusMessagePrivate::toDBusMessage(const QDBusMessage &message){    DBusMessage *msg = 0;    const QDBusMessagePrivate *d_ptr = message.d_ptr;    switch (d_ptr->type) {    case DBUS_MESSAGE_TYPE_INVALID:        //qDebug() << "QDBusMessagePrivate::toDBusMessage" <<  "message is invalid";        break;    case DBUS_MESSAGE_TYPE_METHOD_CALL:        msg = dbus_message_new_method_call(data(d_ptr->service.toUtf8()), data(d_ptr->path.toUtf8()),                                           data(d_ptr->interface.toUtf8()), data(d_ptr->name.toUtf8()));        break;    case DBUS_MESSAGE_TYPE_METHOD_RETURN:        if (d_ptr->reply)            msg = dbus_message_new_method_return(d_ptr->reply);        else            qDebug() << "QDBusMessagePrivate::toDBusMessage" << "reply is invalid";        break;    case DBUS_MESSAGE_TYPE_ERROR:        msg = dbus_message_new_error(d_ptr->reply, data(d_ptr->name.toUtf8()), data(d_ptr->message.toUtf8()));        break;    case DBUS_MESSAGE_TYPE_SIGNAL:        msg = dbus_message_new_signal(data(d_ptr->path.toUtf8()), data(d_ptr->interface.toUtf8()),                                      data(d_ptr->name.toUtf8()));        break;    default:        Q_ASSERT(false);        break;    }#if 0    DBusError err;    dbus_error_init(&err);    if (dbus_error_is_set(&err)) {        QDBusError qe(&err);        qDebug() << "QDBusMessagePrivate::toDBusMessage" << qe;    }#endif    if (!msg)        return 0;    QDBusMarshaller marshaller;    QVariantList::ConstIterator it =  d_ptr->arguments.constBegin();    QVariantList::ConstIterator cend = d_ptr->arguments.constEnd();    dbus_message_iter_init_append(msg, &marshaller.iterator);    for ( ; it != cend; ++it)        marshaller.appendVariantInternal(*it);    // check if everything is ok    if (marshaller.ok)        return msg;    // not ok;    dbus_message_unref(msg);    Q_ASSERT(false);    return 0;}/*struct DBusMessage{    DBusAtomic refcount;    DBusHeader header;    DBusString body;    char byte_order;    unsigned int locked : 1;DBUS_DISABLE_CHECKS    unsigned int in_cache : 1;#endif    DBusList *size_counters;    long size_counter_delta;    dbus_uint32_t changed_stamp : CHANGED_STAMP_BITS;    DBusDataSlotList slot_list;#ifndef DBUS_DISABLE_CHECKS    int generation;#endif};*//*!    \internal    Constructs a QDBusMessage by parsing the given DBusMessage object.*/QDBusMessage QDBusMessagePrivate::fromDBusMessage(DBusMessage *dmsg){    QDBusMessage message;    if (!dmsg)        return message;    message.d_ptr->type = dbus_message_get_type(dmsg);    message.d_ptr->path = QString::fromUtf8(dbus_message_get_path(dmsg));    message.d_ptr->interface = QString::fromUtf8(dbus_message_get_interface(dmsg));    message.d_ptr->name = message.d_ptr->type == DBUS_MESSAGE_TYPE_ERROR ?                      QString::fromUtf8(dbus_message_get_error_name(dmsg)) :                      QString::fromUtf8(dbus_message_get_member(dmsg));    message.d_ptr->service = QString::fromUtf8(dbus_message_get_sender(dmsg));    message.d_ptr->signature = QString::fromUtf8(dbus_message_get_signature(dmsg));    message.d_ptr->msg = dbus_message_ref(dmsg);    QDBusDemarshaller demarshaller;    demarshaller.message = dbus_message_ref(dmsg);    if (dbus_message_iter_init(demarshaller.message, &demarshaller.iterator))        while (!demarshaller.atEnd())            message << demarshaller.toVariantInternal();    return message;}QDBusMessage QDBusMessagePrivate::updateSignature(const QDBusMessage &message, DBusMessage *dmsg){    QDBusMessage messageWithSignature = message; // no signature    QString signature = QString::fromUtf8(dbus_message_get_signature(dmsg));    messageWithSignature.d_ptr->signature = signature;    return messageWithSignature;}void QDBusMessagePrivate::setLocal(const QDBusMessage *message, bool local){    Q_ASSERT(message);    message->d_ptr->localMessage = local;}bool QDBusMessagePrivate::isLocal(const QDBusMessage &message){    return message.d_ptr->localMessage;}void QDBusMessagePrivate::setArguments(const QDBusMessage *message, const QList<QVariant> &arguments){    Q_ASSERT(message);    message->d_ptr->arguments = arguments;}void QDBusMessagePrivate::setType(const QDBusMessage *message, QDBusMessage::MessageType type){    Q_ASSERT(message);    message->d_ptr->type = type;}/*!    \class QDBusMessage    \inmodule QtDBus    \since 4.2    \brief The QDBusMessage class represents one message sent or    received over the D-Bus bus.    This object can represent any of the four different types of    messages (MessageType) that can occur on the bus:    \list      \o Method calls      \o Method return values      \o Signal emissions      \o Error codes    \endlist    Objects of this type are created with the static createError(),    createMethodCall() and createSignal() functions. Use the    QDBusConnection::send() function to send the messages.*//*!    \enum QDBusMessage::MessageType    The possible message types:    \value MethodCallMessage    a message representing an outgoing or incoming method call    \value SignalMessage        a message representing an outgoing or incoming signal emission    \value ReplyMessage         a message representing the return values of a method call    \value ErrorMessage         a message representing an error condition in response to a method call    \value InvalidMessage       an invalid message: this is never set on messages received from D-Bus*//*!    Constructs a new DBus message with the given \a path, \a interface    and \a name, representing a signal emission.    A DBus signal is emitted from one application and is received by    all applications that are listening for that signal from that    interface.    The QDBusMessage object that is returned can be sent using the    QDBusConnection::send() function.*/QDBusMessage QDBusMessage::createSignal(const QString &path, const QString &interface,                                        const QString &name){    QDBusMessage message;    message.d_ptr->type = DBUS_MESSAGE_TYPE_SIGNAL;    message.d_ptr->path = path;    message.d_ptr->interface = interface;    message.d_ptr->name = name;    return message;}/*!    Constructs a new DBus message representing a method call.    A method call always informs its destination address    (\a service, \a path, \a interface and \a method).    The DBus bus allows calling a method on a given remote object without specifying the    destination interface, if the method name is unique. However, if two interfaces on the    remote object export the same method name, the result is undefined (one of the two may be    called or an error may be returned).    When using DBus in a peer-to-peer context (i.e., not on a bus), the \a service parameter is    optional.    The QDBusObject and QDBusInterface classes provide a simpler abstraction to synchronous    method calling.    This function returns a QDBusMessage object that can be sent with    QDBusConnection::call().*/QDBusMessage QDBusMessage::createMethodCall(const QString &service, const QString &path,                                            const QString &interface, const QString &method){    QDBusMessage message;    message.d_ptr->type = DBUS_MESSAGE_TYPE_METHOD_CALL;    message.d_ptr->service = service;    message.d_ptr->path = path;    message.d_ptr->interface = interface;    message.d_ptr->name = method;    return message;}/*!    Constructs a new DBus message representing an error,    with the given \a name and \a msg.*/QDBusMessage QDBusMessage::createError(const QString &name, const QString &msg){    QDBusMessage error;    error.d_ptr->type = DBUS_MESSAGE_TYPE_ERROR;    error.d_ptr->name = name;    error.d_ptr->message = msg;    return error;}/*!    \fn QDBusMessage QDBusMessage::createError(const QDBusError &error)    Constructs a new DBus message representing the given \a error.*//*!    \fn QDBusMessage QDBusMessage::createReply(const QList<QVariant> &arguments) const    Constructs a new DBus message representing a reply, with the given    \a arguments.*/QDBusMessage QDBusMessage::createReply(const QVariantList &arguments) const{    QDBusMessage reply;    reply.setArguments(arguments);    reply.d_ptr->type = DBUS_MESSAGE_TYPE_METHOD_RETURN;    if (d_ptr->msg)        reply.d_ptr->reply = dbus_message_ref(d_ptr->msg);    else        d_ptr->localMessage = true;    Q_ASSERT(d_ptr->msg || d_ptr->localMessage);    //qDebug() << "QDBusMessagePrivate::createReply" << "message has no dbus message";    return reply;}/*!    Constructs a new DBus message representing an error reply message,    with the given \a name and \a msg.*/QDBusMessage QDBusMessage::createErrorReply(const QString name, const QString &msg) const{    QDBusMessage reply = QDBusMessage::createError(name, msg);    if (d_ptr->msg)        reply.d_ptr->reply = dbus_message_ref(d_ptr->msg);    else        d_ptr->localMessage = true;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -