void QCopChannel::init(const QString& channel)
{
    d = new QCopChannelPrivate;
    d->channel = channel;

    if (!qApp) {
        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
    if ( !clientConnection )
	clientConnection = new QCopX11Client();
    clientConnection->registerChannel(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()) {
        if (clientConnection)
            clientConnection->detachChannel(d->channel);
        qcopClientMap->remove(d->channel);
    }

    delete d;
}
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;
}