qcopchannel_qws.cpp

来自「QT 开发环境里面一个很重要的文件」· C++ 代码 · 共 603 行 · 第 1/2 页

CPP
603
字号
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module 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 "qcopchannel_qws.h"#ifndef QT_NO_COP#include "qwsdisplay_qws.h"#include "qwscommand_qws_p.h"#include "qwindowsystem_qws.h"#include "qwindowsystem_p.h"#include "qlist.h"#include "qmap.h"#include "qdatastream.h"#include "qpointer.h"#include "qmutex.h"#include "qdebug.h"typedef QMap<QString, QList<QWSClient*> > QCopServerMap;static QCopServerMap *qcopServerMap = 0;class QCopServerRegexp{public:    QCopServerRegexp( const QString& channel, QWSClient *client );    QCopServerRegexp( const QCopServerRegexp& other );    QString channel;    QWSClient *client;    QRegExp regexp;};QCopServerRegexp::QCopServerRegexp( const QString& channel, QWSClient *client ){    this->channel = channel;    this->client = client;    this->regexp = QRegExp( channel, Qt::CaseSensitive, QRegExp::Wildcard );}QCopServerRegexp::QCopServerRegexp( const QCopServerRegexp& other ){    channel = other.channel;    client = other.client;    regexp = other.regexp;}typedef QList<QCopServerRegexp> QCopServerRegexpList;static QCopServerRegexpList *qcopServerRegexpList = 0;typedef QMap<QString, QList< QPointer<QCopChannel> > > QCopClientMap;static QCopClientMap *qcopClientMap = 0;Q_GLOBAL_STATIC(QMutex, qcopClientMapMutex)// Determine if a channel name contains wildcard characters.static bool containsWildcards( const QString& channel ){    return channel.contains(QLatin1Char('*'));}class QCopChannelPrivate{public:    QString channel;};/*!    \class QCopChannel    \ingroup qws    \brief The QCopChannel class provides communication capabilities    between clients.    QCOP is a many-to-many communication protocol for transferring    messages on various channels. A channel is identified by a name,    and anyone who wants to can listen to it. The QCOP protocol allows    clients to communicate both within the same address space and    between different processes, but it is currently only available    for \l {Qtopia Core} (on X11 and Windows we are exploring the use    of existing standards such as DCOP and COM).    Typically, QCopChannel is either used to send messages to a    channel using the provided static functions, or to listen to the    traffic on a channel by deriving from the class to take advantage    of the provided functionality for receiving messages.    QCopChannel provides a couple of static functions which are usable    without an object: The send() function, which sends the given    message and data on the specified channel, and the isRegistered()    function which queries the server for the existence of the given    channel.    In addition, the QCopChannel class provides the channel() function    which returns the name of the object's channel, the virtual    receive() function which allows subclasses to process data    received from their channel, and the received() signal which is    emitted with the given message and data when a QCopChannel    subclass receives a message from its channel.    \sa QWSClient, {Running Qtopia Core Applications}*//*!    Constructs a QCop channel with the given \a parent, and registers it    with the server using the given \a channel name.    \sa isRegistered(), channel()*/QCopChannel::QCopChannel(const QString& channel, QObject *parent) :    QObject(parent){    init(channel);}#ifdef QT3_SUPPORT/*!    Use the two argument overload, and call setObjectName() to \a name    the instance, instead.*/QCopChannel::QCopChannel(const QString& channel, QObject *parent, const char *name) :    QObject(parent){    setObjectName(QString::fromAscii(name));    init(channel);}#endifvoid QCopChannel::init(const QString& channel){    d = new QCopChannelPrivate;    d->channel = channel;    if (!qt_fbdpy) {        qFatal("QCopChannel: Must construct a QApplication "                "before QCopChannel");        return;    }    {	QMutexLocker locker(qcopClientMapMutex());	if (!qcopClientMap)	    qcopClientMap = new QCopClientMap;	// do we need a new channel list ?	QCopClientMap::Iterator it = qcopClientMap->find(channel);	if (it != qcopClientMap->end()) {	    it.value().append(this);	    return;	}	it = qcopClientMap->insert(channel, QList< QPointer<QCopChannel> >());	it.value().append(QPointer<QCopChannel>(this));    }    // inform server about this channel    qt_fbdpy->registerChannel(channel);}/*!  \internal  Resend all channel registrations  */void QCopChannel::reregisterAll(){    if(qcopClientMap)        for(QCopClientMap::Iterator iter = qcopClientMap->begin();            iter != qcopClientMap->end();            ++iter)            qt_fbdpy->registerChannel(iter.key());}/*!    Destroys the client's end of the channel and notifies the server    that the client has closed its connection. The server will keep    the channel open until the last registered client detaches.    \sa QCopChannel()*/QCopChannel::~QCopChannel(){    QMutexLocker locker(qcopClientMapMutex());    QCopClientMap::Iterator it = qcopClientMap->find(d->channel);    Q_ASSERT(it != qcopClientMap->end());    it.value().removeAll(this);    // still any clients connected locally ?    if (it.value().isEmpty()) {        QByteArray data;        QDataStream s(&data, QIODevice::WriteOnly);        s << d->channel;        if (qt_fbdpy)            send(QLatin1String(""), QLatin1String("detach()"), data);        qcopClientMap->remove(d->channel);    }    delete d;}/*!    Returns the name of the channel.    \sa QCopChannel()*/QString QCopChannel::channel() const{    return d->channel;}/*!    \fn void QCopChannel::receive(const QString& message, const QByteArray &data)    This virtual function allows subclasses of QCopChannel to process    the given \a message and \a data received from their channel. The default    implementation emits the received() signal.    Note that the format of the given \a data has to be well defined    in order to extract the information it contains. In addition, it    is recommended to use the DCOP convention. This is not a    requirement, but you must ensure that the sender and receiver    agree on the argument types.    Example:    \code        void MyClass::receive(const QString &message, const QByteArray &data)        {            QDataStream in(data);            if (message == "execute(QString,QString)") {                QString cmd;                QString arg;                in >> cmd >> arg;                ...            } else if (message == "delete(QString)") {                QString fileName;                in >> fileName;                ...            } else {                ...            }        }    \endcode    This example assumes that the \c message is a DCOP-style function    signature and the \c data contains the function's arguments.    \sa send() */void QCopChannel::receive(const QString& msg, const QByteArray &data){    emit received(msg, data);}/*!    \fn void QCopChannel::received(const QString& message, const QByteArray &data)    This signal is emitted with the given \a message and \a data whenever the    receive() function gets incoming data.    \sa receive()*//*!    Queries the server for the existence of the given \a channel. Returns true    if the channel is registered; otherwise returns false.    \sa channel(), QCopChannel()*/bool QCopChannel::isRegistered(const QString&  channel){    QByteArray data;    QDataStream s(&data, QIODevice::WriteOnly);    s << channel;    if (!send(QLatin1String(""), QLatin1String("isRegistered()"), data))        return false;    QWSQCopMessageEvent *e = qt_fbdpy->waitForQCopResponse();

⌨️ 快捷键说明

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