qdbusconnection.cpp

来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 963 行 · 第 1/3 页

CPP
963
字号
/******************************************************************************** Copyright (C) 1992-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 <qdebug.h>#include <qcoreapplication.h>#include <qstringlist.h>#include "qdbusconnection.h"#include "qdbusconnectioninterface.h"#include "qdbuserror.h"#include "qdbusmessage.h"#include "qdbusmessage_p.h"#include "qdbusconnection_p.h"#include "qdbusinterface_p.h"#include "qdbusutil_p.h"#include "qdbusthreaddebug_p.h"class QDBusConnectionManager{public:    QDBusConnectionManager() {}    ~QDBusConnectionManager();    QDBusConnectionPrivate *connection(const QString &name) const;    void removeConnection(const QString &name);    void setConnection(const QString &name, QDBusConnectionPrivate *c);    QDBusConnectionPrivate *sender() const;    void setSender(const QDBusConnectionPrivate *s);    mutable QMutex mutex;private:    QHash<QString, QDBusConnectionPrivate *> connectionHash;    mutable QMutex senderMutex;    QString senderName; // internal; will probably change};Q_GLOBAL_STATIC(QDBusConnectionManager, _q_manager)QDBusConnectionPrivate *QDBusConnectionManager::sender() const{    QMutexLocker locker(&senderMutex);    return connection(senderName);}void QDBusConnectionManager::setSender(const QDBusConnectionPrivate *s){    QMutexLocker locker(&senderMutex);    senderName = (s ? s->name : QString());}QDBusConnectionPrivate *QDBusConnectionManager::connection(const QString &name) const{    return connectionHash.value(name, 0);}void QDBusConnectionManager::removeConnection(const QString &name){    QDBusConnectionPrivate *d = 0;    d = connectionHash.take(name);    if (d && !d->ref.deref())        d->deleteYourself();    // Static objects may be keeping the connection open.    // However, it is harmless to have outstanding references to a connection that is    // closing as long as those references will be soon dropped without being used.    // ### Output a warning if connections are being used after they have been removed.}QDBusConnectionManager::~QDBusConnectionManager(){    for (QHash<QString, QDBusConnectionPrivate *>::const_iterator it = connectionHash.constBegin();         it != connectionHash.constEnd(); ++it) {        QDBusConnectionPrivate *d = it.value();        if (!d->ref.deref())            d->deleteYourself();        else            d->closeConnection();    }    connectionHash.clear();}QDBUS_EXPORT void qDBusBindToApplication();void qDBusBindToApplication(){}void QDBusConnectionManager::setConnection(const QString &name, QDBusConnectionPrivate *c){    connectionHash[name] = c;    c->name = name;}/*!    \fn QDBusConnection &QDBusConnection::sessionBus()    \relates QDBusConnection    Returns a QDBusConnection object opened with the session bus. The object reference returned    by this function is valid until the QCoreApplication's destructor is run, when the    connection will be closed and the object, deleted.*//*!    \fn QDBusConnection &QDBusConnection::systemBus()    \relates QDBusConnection    Returns a QDBusConnection object opened with the system bus. The object reference returned    by this function is valid until the QCoreApplication's destructor is run, when the    connection will be closed and the object, deleted.*//*!    \class QDBusConnection    \inmodule QtDBus    \since 4.2    \brief The QDBusConnection class represents a connection to the D-Bus bus daemon.    This class is the initial point in a D-Bus session. Using it, you    can get access to remote objects, interfaces; connect remote    signals to your object's slots; register objects, etc.    D-Bus connections are created using the connectToBus() function,    which opens a connection to the server daemon and does the initial    handshaking, associating that connection with a name. Further    attempts to connect using the same name will return the same    connection.    The connection is then torn down using the disconnectFromBus()    function.    As a convenience for the two most common connection types, the    sessionBus() and systemBus() functions return open connections to    the session server daemon and the system server daemon,    respectively. Those connections are opened when first used and are    closed when the QCoreApplication destructor is run.    D-Bus also supports peer-to-peer connections, without the need for    a bus server daemon. Using this facility, two applications can    talk to each other and exchange messages. This can be achieved by    passing an address to connectToBus() function, which was opened by    another D-Bus application using QDBusServer.*//*!    \enum QDBusConnection::BusType    Specifies the type of the bus connection. The valid bus types are:    \value SessionBus           the session bus, associated with the running desktop session    \value SystemBus            the system bus, used to communicate with system-wide processes    \value ActivationBus        the activation bus, the "alias" for the bus that started the                                service    On the Session Bus, one can find other applications by the same user that are sharing the same    desktop session (hence the name). On the System Bus, however, processes shared for the whole    system are usually found.*//*!    \enum QDBusConnection::RegisterOption    Specifies the options for registering objects with the connection. The possible values are:    \value ExportAdaptors                       export the contents of adaptors found in this object    \value ExportScriptableSlots                export this object's scriptable slots    \value ExportScriptableSignals              export this object's scriptable signals    \value ExportScriptableProperties           export this object's scriptable properties    \value ExportScriptableContents             shorthand form for ExportScriptableSlots |                                                ExportScriptableSignals |                                                ExportScriptableProperties    \value ExportNonScriptableSlots             export this object's non-scriptable slots    \value ExportNonScriptableSignals           export this object's non-scriptable signals    \value ExportNonScriptableProperties        export this object's non-scriptable properties    \value ExportNonScriptableContents          shorthand form for ExportNonScriptableSlots |                                                ExportNonScriptableSignals |                                                ExportNonScriptableProperties    \value ExportAllSlots                       export all of this object's slots    \value ExportAllSignals                     export all of this object's signals    \value ExportAllProperties                  export all of this object's properties    \value ExportAllContents                    export all of this object's contents    \value ExportChildObjects                   export this object's child objects    \sa registerObject(), QDBusAbstractAdaptor, {usingadaptors.html}{Using adaptors}*//*!    \enum QDBusConnection::UnregisterMode    The mode for unregistering an object path:    \value UnregisterNode       unregister this node only: do not unregister child objects    \value UnregisterTree       unregister this node and all its sub-tree    Note, however, if this object was registered with the ExportChildObjects option, UnregisterNode    will unregister the child objects too.*//*!    Creates a QDBusConnection object attached to the connection with name \a name.    This does not open the connection. You have to call connectToBus() to open it.*/QDBusConnection::QDBusConnection(const QString &name){    if (name.isEmpty()) {        d = 0;    } else {        QMutexLocker locker(&_q_manager()->mutex);        d = _q_manager()->connection(name);        if (d)            d->ref.ref();    }}/*!    Creates a copy of the \a other connection.*/QDBusConnection::QDBusConnection(const QDBusConnection &other){    d = other.d;    if (d)        d->ref.ref();}/*!  \internal   Creates a connection object with the given \a dd as private object.*/QDBusConnection::QDBusConnection(QDBusConnectionPrivate *dd){    d = dd;    if (d)        d->ref.ref();}/*!    Disposes of this object. This does not close the connection: you    have to call disconnectFromBus() to do that.*/QDBusConnection::~QDBusConnection(){    if (d && !d->ref.deref())        d->deleteYourself();}/*!    Creates a copy of the connection \a other in this object. Note    that the connection this object referenced before the copy, is not    spontaneously disconnected.    \sa disconnectFromBus()*/QDBusConnection &QDBusConnection::operator=(const QDBusConnection &other){    if (other.d)        other.d->ref.ref();    QDBusConnectionPrivate *old = static_cast<QDBusConnectionPrivate *>(            q_atomic_set_ptr(&d, other.d));    if (old && !old->ref.deref())        old->deleteYourself();    return *this;}/*!    Opens a connection of type \a type to one of the known busses and    associate with it the connection name \a name. Returns a    QDBusConnection object associated with that connection.*/QDBusConnection QDBusConnection::connectToBus(BusType type, const QString &name){//    Q_ASSERT_X(QCoreApplication::instance(), "QDBusConnection::addConnection",//               "Cannot create connection without a Q[Core]Application instance");    QMutexLocker locker(&_q_manager()->mutex);    QDBusConnectionPrivate *d = _q_manager()->connection(name);    if (d || name.isEmpty())        return QDBusConnection(d);    d = new QDBusConnectionPrivate;    DBusConnection *c = 0;    QDBusErrorInternal error;    switch (type) {

⌨️ 快捷键说明

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