qdbusintegrator.cpp

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

CPP
1,726
字号
/******************************************************************************** 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 <qcoreapplication.h>#include <qdebug.h>#include <qmetaobject.h>#include <qobject.h>#include <qsocketnotifier.h>#include <qstringlist.h>#include <qtimer.h>#include <qthread.h>#include "qdbusargument.h"#include "qdbusconnection_p.h"#include "qdbusinterface_p.h"#include "qdbusmessage.h"#include "qdbusmetatype.h"#include "qdbusmetatype_p.h"#include "qdbusabstractadaptor.h"#include "qdbusabstractadaptor_p.h"#include "qdbusutil_p.h"#include "qdbusmessage_p.h"#include "qdbuscontext_p.h"#include "qdbusintegrator_p.h"#include "qdbusthreaddebug_p.h"static bool isDebugging;#define qDBusDebug              if (!::isDebugging); else qDebugstatic inline QDebug operator<<(QDebug dbg, const QThread *th){    dbg.nospace() << "QThread(ptr=" << (void*)th;    if (th && !th->objectName().isEmpty())        dbg.nospace() << ", name=" << th->objectName();    dbg.nospace() << ")";    return dbg.space();}#if QDBUS_THREAD_DEBUGstatic inline QDebug operator<<(QDebug dbg, const QDBusConnectionPrivate *conn){    dbg.nospace() << "QDBusConnection("                  << "ptr=" << (void*)conn                  << ", name=" << conn->name                  << ", baseService=" << conn->baseService                  << ", thread=";    if (conn->thread() == QThread::currentThread())        dbg.nospace() << "same thread";    else        dbg.nospace() << conn->thread();    dbg.nospace() << ")";    return dbg.space();}Q_AUTOTEST_EXPORT void qdbusDefaultThreadDebug(int action, int condition, QDBusConnectionPrivate *conn){    qDBusDebug() << QThread::currentThread()                 << "QtDBus threading action" << action                 << (condition == QDBusLockerBase::BeforeLock ? "before lock" :                     condition == QDBusLockerBase::AfterLock ? "after lock" :                     condition == QDBusLockerBase::BeforeUnlock ? "before unlock" :                     condition == QDBusLockerBase::AfterUnlock ? "after unlock" :                     condition == QDBusLockerBase::BeforePost ? "before event posting" :                     condition == QDBusLockerBase::AfterPost ? "after event posting" :                     condition == QDBusLockerBase::BeforeDeliver ? "before event delivery" :                     condition == QDBusLockerBase::AfterDeliver ? "after event delivery" :                     condition == QDBusLockerBase::BeforeAcquire ? "before acquire" :                     condition == QDBusLockerBase::AfterAcquire ? "after acquire" :                     condition == QDBusLockerBase::BeforeRelease ? "before release" :                     condition == QDBusLockerBase::AfterRelease ? "after release" :                     "condition unknown")                 << "in connection" << conn;}Q_AUTOTEST_EXPORT qdbusThreadDebugFunc qdbusThreadDebug = qdbusDefaultThreadDebug;#endiftypedef void (*QDBusSpyHook)(const QDBusMessage&);typedef QVarLengthArray<QDBusSpyHook, 4> QDBusSpyHookList;Q_GLOBAL_STATIC(QDBusSpyHookList, qDBusSpyHookList)static bool qDBusRealAddTimeout(QDBusConnectionPrivate *d, DBusTimeout *timeout, int ms);static dbus_bool_t qDBusAddTimeout(DBusTimeout *timeout, void *data){    Q_ASSERT(timeout);    Q_ASSERT(data);  //  qDebug("addTimeout %d", dbus_timeout_get_interval(timeout));    QDBusConnectionPrivate *d = static_cast<QDBusConnectionPrivate *>(data);    if (!dbus_timeout_get_enabled(timeout))        return true;    if (QCoreApplication::instance() && QThread::currentThread() == d->thread()) {        // correct thread        return qDBusRealAddTimeout(d, timeout, dbus_timeout_get_interval(timeout));    } else {        // wrong thread: sync back        QDBusConnectionCallbackEvent *ev = new QDBusConnectionCallbackEvent;        ev->subtype = QDBusConnectionCallbackEvent::AddTimeout;        ev->timeout = timeout;        ev->extra = dbus_timeout_get_interval(timeout);        d->postEventToThread(AddTimeoutAction, d, ev);        return true;    }}static bool qDBusRealAddTimeout(QDBusConnectionPrivate *d, DBusTimeout *timeout, int ms){    int timerId = d->startTimer(ms);    if (!timerId)        return false;    d->timeouts[timerId] = timeout;    return true;}static void qDBusRealRemoveTimeout(QDBusConnectionPrivate *d, DBusTimeout *timeout);static void qDBusRemoveTimeout(DBusTimeout *timeout, void *data){    Q_ASSERT(timeout);    Q_ASSERT(data);  //  qDebug("removeTimeout");    QDBusConnectionPrivate *d = static_cast<QDBusConnectionPrivate *>(data);    if (QCoreApplication::instance() && QThread::currentThread() == d->thread()) {        qDBusRealRemoveTimeout(d, timeout);    } else {        QDBusConnectionCallbackEvent *ev = new QDBusConnectionCallbackEvent;        ev->subtype = QDBusConnectionCallbackEvent::RemoveTimeout;        ev->timeout = timeout;        d->postEventToThread(RemoveTimeoutAction, d, ev);    }}static void qDBusRealRemoveTimeout(QDBusConnectionPrivate *d, DBusTimeout *timeout){    QDBusConnectionPrivate::TimeoutHash::iterator it = d->timeouts.begin();    while (it != d->timeouts.end()) {        if (it.value() == timeout) {            d->killTimer(it.key());            it = d->timeouts.erase(it);        } else {            ++it;        }    }}static void qDBusToggleTimeout(DBusTimeout *timeout, void *data){    Q_ASSERT(timeout);    Q_ASSERT(data);    //qDebug("ToggleTimeout");    qDBusRemoveTimeout(timeout, data);    qDBusAddTimeout(timeout, data);}static bool qDBusRealAddWatch(QDBusConnectionPrivate *d, DBusWatch *watch, int flags, int fd);static dbus_bool_t qDBusAddWatch(DBusWatch *watch, void *data){    Q_ASSERT(watch);    Q_ASSERT(data);    QDBusConnectionPrivate *d = static_cast<QDBusConnectionPrivate *>(data);    int flags = dbus_watch_get_flags(watch);    int fd = dbus_watch_get_fd(watch);    if (QCoreApplication::instance() && QThread::currentThread() == d->thread()) {        return qDBusRealAddWatch(d, watch, flags, fd);    } else {        QDBusConnectionCallbackEvent *ev = new QDBusConnectionCallbackEvent;        ev->subtype = QDBusConnectionCallbackEvent::AddWatch;        ev->watch = watch;        ev->fd = fd;        ev->extra = flags;        d->postEventToThread(AddWatchAction, d, ev);        return true;    }}static bool qDBusRealAddWatch(QDBusConnectionPrivate *d, DBusWatch *watch, int flags, int fd){    QDBusConnectionPrivate::Watcher watcher;    if (flags & DBUS_WATCH_READABLE) {        //qDebug("addReadWatch %d", fd);        watcher.watch = watch;        if (QCoreApplication::instance()) {            watcher.read = new QSocketNotifier(fd, QSocketNotifier::Read, d);            watcher.read->setEnabled(dbus_watch_get_enabled(watch));            d->connect(watcher.read, SIGNAL(activated(int)), SLOT(socketRead(int)));        }    }    if (flags & DBUS_WATCH_WRITABLE) {        //qDebug("addWriteWatch %d", fd);        watcher.watch = watch;        if (QCoreApplication::instance()) {            watcher.write = new QSocketNotifier(fd, QSocketNotifier::Write, d);            watcher.write->setEnabled(dbus_watch_get_enabled(watch));            d->connect(watcher.write, SIGNAL(activated(int)), SLOT(socketWrite(int)));        }    }    d->watchers.insertMulti(fd, watcher);    return true;}static void qDBusRealRemoveWatch(QDBusConnectionPrivate *d, DBusWatch *watch, int fd);static void qDBusRemoveWatch(DBusWatch *watch, void *data){    Q_ASSERT(watch);    Q_ASSERT(data);    //qDebug("remove watch");    QDBusConnectionPrivate *d = static_cast<QDBusConnectionPrivate *>(data);    int fd = dbus_watch_get_fd(watch);    if (QCoreApplication::instance() && QThread::currentThread() == d->thread()) {        qDBusRealRemoveWatch(d, watch, fd);    } else {        QDBusConnectionCallbackEvent *ev = new QDBusConnectionCallbackEvent;        ev->subtype = QDBusConnectionCallbackEvent::RemoveWatch;        ev->watch = watch;        ev->fd = fd;        d->postEventToThread(RemoveWatchAction, d, ev);    }}static void qDBusRealRemoveWatch(QDBusConnectionPrivate *d, DBusWatch *watch, int fd){    QDBusConnectionPrivate::WatcherHash::iterator i = d->watchers.find(fd);    while (i != d->watchers.end() && i.key() == fd) {        if (i.value().watch == watch) {            delete i.value().read;            delete i.value().write;            d->watchers.erase(i);            return;        }        ++i;    }}static void qDBusRealToggleWatch(QDBusConnectionPrivate *d, DBusWatch *watch, int fd);static void qDBusToggleWatch(DBusWatch *watch, void *data){    Q_ASSERT(watch);    Q_ASSERT(data);    QDBusConnectionPrivate *d = static_cast<QDBusConnectionPrivate *>(data);    int fd = dbus_watch_get_fd(watch);    if (QCoreApplication::instance() && QThread::currentThread() == d->thread()) {        qDBusRealToggleWatch(d, watch, fd);    } else {        QDBusConnectionCallbackEvent *ev = new QDBusConnectionCallbackEvent;        ev->subtype = QDBusConnectionCallbackEvent::ToggleWatch;        ev->watch = watch;        ev->fd = fd;        d->postEventToThread(ToggleWatchAction, d, ev);    }}static void qDBusRealToggleWatch(QDBusConnectionPrivate *d, DBusWatch *watch, int fd){    QDBusConnectionPrivate::WatcherHash::iterator i = d->watchers.find(fd);    while (i != d->watchers.end() && i.key() == fd) {        if (i.value().watch == watch) {            bool enabled = dbus_watch_get_enabled(watch);            int flags = dbus_watch_get_flags(watch);            //qDebug("toggle watch %d to %d (write: %d, read: %d)", dbus_watch_get_fd(watch), enabled, flags & DBUS_WATCH_WRITABLE, flags & DBUS_WATCH_READABLE);            if (flags & DBUS_WATCH_READABLE && i.value().read)                i.value().read->setEnabled(enabled);            if (flags & DBUS_WATCH_WRITABLE && i.value().write)                i.value().write->setEnabled(enabled);            return;        }        ++i;    }}static void qDBusNewConnection(DBusServer *server, DBusConnection *c, void *data){    Q_ASSERT(data); Q_ASSERT(server); Q_ASSERT(c);    Q_UNUSED(data); Q_UNUSED(server); Q_UNUSED(c);    qDebug("SERVER: GOT A NEW CONNECTION"); // TODO}static QByteArray buildMatchRule(const QString &service, const QString & /*owner*/,                                 const QString &objectPath, const QString &interface,                                 const QString &member, const QString & /*signature*/){    QString result = QLatin1String("type='signal',");    QString keyValue = QLatin1String("%1='%2',");    if (!service.isEmpty())        result += keyValue.arg(QLatin1String("sender"), service);    if (!objectPath.isEmpty())        result += keyValue.arg(QLatin1String("path"), objectPath);    if (!interface.isEmpty())        result += keyValue.arg(QLatin1String("interface"), interface);    if (!member.isEmpty())        result += keyValue.arg(QLatin1String("member"), member);    result.chop(1);             // remove ending comma

⌨️ 快捷键说明

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