| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qcopchannel_qws.h"
|
|---|
| 43 |
|
|---|
| 44 | #ifndef QT_NO_COP
|
|---|
| 45 |
|
|---|
| 46 | #include "qwsdisplay_qws.h"
|
|---|
| 47 | #include "qwscommand_qws_p.h"
|
|---|
| 48 | #include "qwindowsystem_qws.h"
|
|---|
| 49 | #include "qwindowsystem_p.h"
|
|---|
| 50 | #include "qlist.h"
|
|---|
| 51 | #include "qmap.h"
|
|---|
| 52 | #include "qdatastream.h"
|
|---|
| 53 | #include "qpointer.h"
|
|---|
| 54 | #include "qmutex.h"
|
|---|
| 55 |
|
|---|
| 56 | #include "qdebug.h"
|
|---|
| 57 |
|
|---|
| 58 | QT_BEGIN_NAMESPACE
|
|---|
| 59 |
|
|---|
| 60 | typedef QMap<QString, QList<QWSClient*> > QCopServerMap;
|
|---|
| 61 | static QCopServerMap *qcopServerMap = 0;
|
|---|
| 62 |
|
|---|
| 63 | class QCopServerRegexp
|
|---|
| 64 | {
|
|---|
| 65 | public:
|
|---|
| 66 | QCopServerRegexp( const QString& channel, QWSClient *client );
|
|---|
| 67 | QCopServerRegexp( const QCopServerRegexp& other );
|
|---|
| 68 |
|
|---|
| 69 | QString channel;
|
|---|
| 70 | QWSClient *client;
|
|---|
| 71 | QRegExp regexp;
|
|---|
| 72 | };
|
|---|
| 73 |
|
|---|
| 74 | QCopServerRegexp::QCopServerRegexp( const QString& channel, QWSClient *client )
|
|---|
| 75 | {
|
|---|
| 76 | this->channel = channel;
|
|---|
| 77 | this->client = client;
|
|---|
| 78 | this->regexp = QRegExp( channel, Qt::CaseSensitive, QRegExp::Wildcard );
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | QCopServerRegexp::QCopServerRegexp( const QCopServerRegexp& other )
|
|---|
| 82 | {
|
|---|
| 83 | channel = other.channel;
|
|---|
| 84 | client = other.client;
|
|---|
| 85 | regexp = other.regexp;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | typedef QList<QCopServerRegexp> QCopServerRegexpList;
|
|---|
| 89 | static QCopServerRegexpList *qcopServerRegexpList = 0;
|
|---|
| 90 |
|
|---|
| 91 | typedef QMap<QString, QList< QPointer<QCopChannel> > > QCopClientMap;
|
|---|
| 92 | static QCopClientMap *qcopClientMap = 0;
|
|---|
| 93 |
|
|---|
| 94 | Q_GLOBAL_STATIC(QMutex, qcopClientMapMutex)
|
|---|
| 95 |
|
|---|
| 96 | // Determine if a channel name contains wildcard characters.
|
|---|
| 97 | static bool containsWildcards( const QString& channel )
|
|---|
| 98 | {
|
|---|
| 99 | return channel.contains(QLatin1Char('*'));
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | class QCopChannelPrivate
|
|---|
| 103 | {
|
|---|
| 104 | public:
|
|---|
| 105 | QString channel;
|
|---|
| 106 | };
|
|---|
| 107 |
|
|---|
| 108 | /*!
|
|---|
| 109 | \class QCopChannel
|
|---|
| 110 | \ingroup qws
|
|---|
| 111 |
|
|---|
| 112 | \brief The QCopChannel class provides communication capabilities
|
|---|
| 113 | between clients in \l{Qt for Embedded Linux}.
|
|---|
| 114 |
|
|---|
| 115 | Note that this class is only available in \l{Qt for Embedded Linux}.
|
|---|
| 116 |
|
|---|
| 117 | The Qt COmmunication Protocol (QCOP) is a many-to-many protocol
|
|---|
| 118 | for transferring messages across registered channels. A channel is
|
|---|
| 119 | registered by name, and anyone who wants to can listen to the
|
|---|
| 120 | channel as well as send messages through it. The QCOP protocol
|
|---|
| 121 | allows clients to communicate both within the same address space
|
|---|
| 122 | and between different processes.
|
|---|
| 123 |
|
|---|
| 124 | To send messages to a given channel, QCopChannel provides the
|
|---|
| 125 | static send() function. Using this function alone, the messages
|
|---|
| 126 | are queued until Qt re-enters the event loop. To immediately flush
|
|---|
| 127 | all queued messages to the registered listeners, call the static
|
|---|
| 128 | flush() function.
|
|---|
| 129 |
|
|---|
| 130 | To listen to the traffic on a given channel, you typically
|
|---|
| 131 | instantiate a QCopChannel object for the given channel and connect
|
|---|
| 132 | to its received() signal that is emitted whenever there is
|
|---|
| 133 | incoming data. Use the static isRegistered() function to query
|
|---|
| 134 | the server for the existence of a given channel. QCopChannel
|
|---|
| 135 | provides the channel() function returning the name of this
|
|---|
| 136 | QCopChannel object's channel.
|
|---|
| 137 |
|
|---|
| 138 | In additon, QCopChannel provides the virtual receive() function
|
|---|
| 139 | that can be reimplemented to filter the incoming messages and
|
|---|
| 140 | data. The default implementation simply emits the received()
|
|---|
| 141 | signal.
|
|---|
| 142 |
|
|---|
| 143 | \sa QWSServer, QWSClient, {Qt for Embedded Linux Architecture}
|
|---|
| 144 | */
|
|---|
| 145 |
|
|---|
| 146 | /*!
|
|---|
| 147 | Constructs a QCopChannel object for the specified \a channel, with
|
|---|
| 148 | the given \a parent. Once created, the channel is registered by
|
|---|
| 149 | the server.
|
|---|
| 150 |
|
|---|
| 151 | \sa isRegistered(), channel()
|
|---|
| 152 | */
|
|---|
| 153 |
|
|---|
| 154 | QCopChannel::QCopChannel(const QString& channel, QObject *parent) :
|
|---|
| 155 | QObject(parent)
|
|---|
| 156 | {
|
|---|
| 157 | init(channel);
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | #ifdef QT3_SUPPORT
|
|---|
| 161 | /*!
|
|---|
| 162 | Use the two argument overload instead, and call the
|
|---|
| 163 | QObject::setObjectName() function to \a name the instance.
|
|---|
| 164 | */
|
|---|
| 165 | QCopChannel::QCopChannel(const QString& channel, QObject *parent, const char *name) :
|
|---|
| 166 | QObject(parent)
|
|---|
| 167 | {
|
|---|
| 168 | setObjectName(QString::fromAscii(name));
|
|---|
| 169 | init(channel);
|
|---|
| 170 | }
|
|---|
| 171 | #endif
|
|---|
| 172 |
|
|---|
| 173 | void QCopChannel::init(const QString& channel)
|
|---|
| 174 | {
|
|---|
| 175 | d = new QCopChannelPrivate;
|
|---|
| 176 | d->channel = channel;
|
|---|
| 177 |
|
|---|
| 178 | if (!qt_fbdpy) {
|
|---|
| 179 | qFatal("QCopChannel: Must construct a QApplication "
|
|---|
| 180 | "before QCopChannel");
|
|---|
| 181 | return;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | {
|
|---|
| 185 | QMutexLocker locker(qcopClientMapMutex());
|
|---|
| 186 |
|
|---|
| 187 | if (!qcopClientMap)
|
|---|
| 188 | qcopClientMap = new QCopClientMap;
|
|---|
| 189 |
|
|---|
| 190 | // do we need a new channel list ?
|
|---|
| 191 | QCopClientMap::Iterator it = qcopClientMap->find(channel);
|
|---|
| 192 | if (it != qcopClientMap->end()) {
|
|---|
| 193 | it.value().append(this);
|
|---|
| 194 | return;
|
|---|
|
|---|