📄 qcopchannel_qws.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 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://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 "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 in Qtopia Core. Note that this class is only available in \l {Qtopia Core}. 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 as well as send messages to it. The QCOP protocol allows clients to communicate both within the same address space and between different processes. To send messages to a given channel, QCopChannel provides the static send() function. Using this function alone, the messages are queued until Qt re-enters the event loop. To immediately flush all queued messages to the registered listeners, call the static flush() function. To listen to the traffic on a given channel, you typically instantiate a QCopChannel object for the given channel and connect to its received() signal that is emitted whenever there is incoming data. Use the static isRegistered() function to query the server for the existence of a given channel. QCopChannel provides the channel() function returning the name of this QCopChannel object's channel. In additon, QCopChannel provides the virtual receive() function that can be reimplemented to filter the incoming messages and data. The default implementation simply emits the received() signal. \sa QWSServer, QWSClient, {Qtopia Core Architecture}*//*! Constructs a QCopChannel object for the specified \a channel, with the given \a parent. Once created, the channel is registered by the server. \sa isRegistered(), channel()*/QCopChannel::QCopChannel(const QString& channel, QObject *parent) : QObject(parent){ init(channel);}#ifdef QT3_SUPPORT/*! Use the two argument overload instead, and call the QObject::setObjectName() function to \a name the instance.*/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 this QCopChannel object. The server is notified that this particular listener has closed its connection. The server will keep the channel open until the last registered listener detaches. \sa isRegistered(), channel()*/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 this object's channel. \sa isRegistered()*/QString QCopChannel::channel() const{ return d->channel;}/*! \fn void QCopChannel::receive(const QString& message, const QByteArray &data) Processes the incoming \a message and \a data. This function is called by the server when this object's channel receives new messages. Note that the default implementation simply emits the received() signal; reimplement this function to process the incoming \a message and \a data. 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. For 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 The above code assumes that the \c message is a DCOP-style function signature and the \c data contains the function's arguments. \sa send(), channel(), received() */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 whenever this object's channel receives new messages (i.e., it is emitted by the receive() function), passing the incoming \a message and \a data as parameters. \sa receive(), channel()*//*! Queries the server for the existence of the given \a channel. Returns true if the channel is registered; otherwise returns false.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -