📄 qdbusconnection.cpp
字号:
/******************************************************************************** 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 <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"class QDBusConnectionManager{public: QDBusConnectionManager() {} ~QDBusConnectionManager(); void bindToApplication(); 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);private: mutable QMutex mutex; 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{ QMutexLocker locker(&mutex); return connectionHash.value(name, 0);}void QDBusConnectionManager::removeConnection(const QString &name){ QMutexLocker locker(&mutex); QDBusConnectionPrivate *d = 0; d = connectionHash.take(name); if (d && !d->ref.deref()) { delete d; } else if (d) { d->closeConnection(); qWarning("QDBusConnection: closed connection %s" "is still referred to by other QDBusConnection objects", name.toLocal8Bit().constData()); }}QDBusConnectionManager::~QDBusConnectionManager(){ for (QHash<QString, QDBusConnectionPrivate *>::const_iterator it = connectionHash.constBegin(); it != connectionHash.constEnd(); ++it) { QDBusConnectionPrivate *d = it.value(); if (!d->ref.deref()) delete d; else d->closeConnection(); } connectionHash.clear();}void QDBusConnectionManager::bindToApplication(){ QMutexLocker locker(&mutex); for (QHash<QString, QDBusConnectionPrivate *>::const_iterator it = connectionHash.constBegin(); it != connectionHash.constEnd(); ++it) { (*it)->bindToApplication(); }}QDBUS_EXPORT void qDBusBindToApplication();void qDBusBindToApplication(){ _q_manager()->bindToApplication();}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 ExportAllSignal 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 \warning It is currently not possible to export signals from objects. If you pass the ExportScriptableSignals, ExportNonScriptableSignals, or ExportAllSignal, the registerObject() function will print a warning. \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 { 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()) delete d;}/*! 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()) delete old; 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)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -